blob: a2a99869e601ba7dbea820076c24bca4239e6082 [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 Moolenaard6e256c2011-12-14 15:32:50 +0100470static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200474static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100476static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200480static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100493static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100495static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
519static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200529static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531#ifdef FEAT_FLOAT
532static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000536static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#ifdef FEAT_FLOAT
543static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200545static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000547static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000556static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000558static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200562static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200566static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000574static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000575static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200576static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100588static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000591static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000605static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100610static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200625static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200628#ifdef FEAT_LUA
629static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000635static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200636static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000638static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000640static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000644#ifdef vim_mkdir
645static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100648#ifdef FEAT_MZSCHEME
649static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100653static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000654static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
656static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
657#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000659static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000660static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200661#ifdef FEAT_PYTHON3
662static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
663#endif
664#ifdef FEAT_PYTHON
665static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000668static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000669static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000681#ifdef FEAT_FLOAT
682static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
683#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200684static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100686static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000689static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000691static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200696static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000699static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000700static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000701static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000702static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200704static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000705static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100707#ifdef FEAT_CRYPT
708static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
709#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000710static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200711static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000713#ifdef FEAT_FLOAT
714static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200715static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000718static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000719static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#ifdef FEAT_FLOAT
723static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
725#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000726static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728#ifdef HAVE_STRFTIME
729static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
730#endif
731static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200737static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200738static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000744static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200745static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200747static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000748static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000749static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000750static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000751static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000752static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000754static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200755#ifdef FEAT_FLOAT
756static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
758#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
763static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
764#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200766static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200767static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100768static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100772static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
775static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000779static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000782static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100783static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784
Bram Moolenaar493c1782014-05-28 14:34:46 +0200785static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000786static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static int get_env_len __ARGS((char_u **arg));
788static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000789static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000790static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
791#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
792#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
793 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000794static 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 +0000795static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000796static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200797static 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 +0000798static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static typval_T *alloc_tv __ARGS((void));
800static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static void init_tv __ARGS((typval_T *varp));
802static long get_tv_number __ARGS((typval_T *varp));
803static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000804static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static char_u *get_tv_string __ARGS((typval_T *varp));
806static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000807static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100808static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
809static 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 +0000810static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
811static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
812static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000813static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
814static 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 +0000815static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200816static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
817static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200818static int var_check_func_name __ARGS((char_u *name, int new_var));
819static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200820static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000821static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
823static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
824static int eval_fname_script __ARGS((char_u *p));
825static int eval_fname_sid __ARGS((char_u *p));
826static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static ufunc_T *find_func __ARGS((char_u *name));
828static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200829static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830#ifdef FEAT_PROFILE
831static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000832static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
833static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
834static int
835# ifdef __BORLANDC__
836 _RTLENTRYF
837# endif
838 prof_total_cmp __ARGS((const void *s1, const void *s2));
839static int
840# ifdef __BORLANDC__
841 _RTLENTRYF
842# endif
843 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200845static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000847static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849static 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 +0000850static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
851static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000852static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000853static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
854static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000855static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000856static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000857static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200858static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200859static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200861
862#ifdef EBCDIC
863static int compare_func_name __ARGS((const void *s1, const void *s2));
864static void sortFunctions __ARGS(());
865#endif
866
Bram Moolenaar33570922005-01-25 22:26:29 +0000867/*
868 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000869 */
870 void
871eval_init()
872{
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 int i;
874 struct vimvar *p;
875
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200876 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
877 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200878 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000880 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881
882 for (i = 0; i < VV_LEN; ++i)
883 {
884 p = &vimvars[i];
885 STRCPY(p->vv_di.di_key, p->vv_name);
886 if (p->vv_flags & VV_RO)
887 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
888 else if (p->vv_flags & VV_RO_SBX)
889 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
890 else
891 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000892
893 /* add to v: scope dict, unless the value is not always available */
894 if (p->vv_type != VAR_UNKNOWN)
895 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000896 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000897 /* add to compat scope dict */
898 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000899 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000900 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100901 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200902 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200903 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904
905#ifdef EBCDIC
906 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100907 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200908 */
909 sortFunctions();
910#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000911}
912
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000913#if defined(EXITFREE) || defined(PROTO)
914 void
915eval_clear()
916{
917 int i;
918 struct vimvar *p;
919
920 for (i = 0; i < VV_LEN; ++i)
921 {
922 p = &vimvars[i];
923 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000924 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000925 vim_free(p->vv_str);
926 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000927 }
928 else if (p->vv_di.di_tv.v_type == VAR_LIST)
929 {
930 list_unref(p->vv_list);
931 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000932 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000933 }
934 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000935 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936 hash_clear(&compat_hashtab);
937
Bram Moolenaard9fba312005-06-26 22:34:35 +0000938 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100939# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200940 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100941# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942
943 /* global variables */
944 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000945
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000946 /* autoloaded script names */
947 ga_clear_strings(&ga_loaded);
948
Bram Moolenaarcca74132013-09-25 21:00:28 +0200949 /* Script-local variables. First clear all the variables and in a second
950 * loop free the scriptvar_T, because a variable in one script might hold
951 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200952 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200953 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200954 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200955 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200956 ga_clear(&ga_scripts);
957
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000958 /* unreferenced lists and dicts */
959 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000960
961 /* functions */
962 free_all_functions();
963 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000964}
965#endif
966
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 * Return the name of the executed function.
969 */
970 char_u *
971func_name(cookie)
972 void *cookie;
973{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000974 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975}
976
977/*
978 * Return the address holding the next breakpoint line for a funccall cookie.
979 */
980 linenr_T *
981func_breakpoint(cookie)
982 void *cookie;
983{
Bram Moolenaar33570922005-01-25 22:26:29 +0000984 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986
987/*
988 * Return the address holding the debug tick for a funccall cookie.
989 */
990 int *
991func_dbg_tick(cookie)
992 void *cookie;
993{
Bram Moolenaar33570922005-01-25 22:26:29 +0000994 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995}
996
997/*
998 * Return the nesting level for a funccall cookie.
999 */
1000 int
1001func_level(cookie)
1002 void *cookie;
1003{
Bram Moolenaar33570922005-01-25 22:26:29 +00001004 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
1007/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001008funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001010/* pointer to list of previously used funccal, still around because some
1011 * item in it is still being used. */
1012funccall_T *previous_funccal = NULL;
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014/*
1015 * Return TRUE when a function was ended by a ":return" command.
1016 */
1017 int
1018current_func_returned()
1019{
1020 return current_funccal->returned;
1021}
1022
1023
1024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 * Set an internal variable to a string value. Creates the variable if it does
1026 * not already exist.
1027 */
1028 void
1029set_internal_string_var(name, value)
1030 char_u *name;
1031 char_u *value;
1032{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001033 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 val = vim_strsave(value);
1037 if (val != NULL)
1038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001040 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001042 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001043 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045 }
1046}
1047
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001048static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001049static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050static char_u *redir_endp = NULL;
1051static char_u *redir_varname = NULL;
1052
1053/*
1054 * Start recording command output to a variable
1055 * Returns OK if successfully completed the setup. FAIL otherwise.
1056 */
1057 int
1058var_redir_start(name, append)
1059 char_u *name;
1060 int append; /* append to an existing variable */
1061{
1062 int save_emsg;
1063 int err;
1064 typval_T tv;
1065
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001066 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001067 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 {
1069 EMSG(_(e_invarg));
1070 return FAIL;
1071 }
1072
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001073 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074 redir_varname = vim_strsave(name);
1075 if (redir_varname == NULL)
1076 return FAIL;
1077
1078 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1079 if (redir_lval == NULL)
1080 {
1081 var_redir_stop();
1082 return FAIL;
1083 }
1084
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001085 /* The output is stored in growarray "redir_ga" until redirection ends. */
1086 ga_init2(&redir_ga, (int)sizeof(char), 500);
1087
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001089 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001090 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1092 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001093 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 if (redir_endp != NULL && *redir_endp != NUL)
1095 /* Trailing characters are present after the variable name */
1096 EMSG(_(e_trailing));
1097 else
1098 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001099 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 var_redir_stop();
1101 return FAIL;
1102 }
1103
1104 /* check if we can write to the variable: set it to or append an empty
1105 * string */
1106 save_emsg = did_emsg;
1107 did_emsg = FALSE;
1108 tv.v_type = VAR_STRING;
1109 tv.vval.v_string = (char_u *)"";
1110 if (append)
1111 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1112 else
1113 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001114 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001116 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 if (err)
1118 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001119 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 var_redir_stop();
1121 return FAIL;
1122 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 return OK;
1125}
1126
1127/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 * Append "value[value_len]" to the variable set by var_redir_start().
1129 * The actual appending is postponed until redirection ends, because the value
1130 * appended may in fact be the string we write to, changing it may cause freed
1131 * memory to be used:
1132 * :redir => foo
1133 * :let foo
1134 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 */
1136 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001141 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142
1143 if (redir_lval == NULL)
1144 return;
1145
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001147 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001148 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001149 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152 {
1153 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001154 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 }
1156 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158}
1159
1160/*
1161 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 */
1164 void
1165var_redir_stop()
1166{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 typval_T tv;
1168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 if (redir_lval != NULL)
1170 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 /* If there was no error: assign the text to the variable. */
1172 if (redir_endp != NULL)
1173 {
1174 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1175 tv.v_type = VAR_STRING;
1176 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001177 /* Call get_lval() again, if it's inside a Dict or List it may
1178 * have changed. */
1179 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001180 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1182 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1183 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001184 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001186 /* free the collected output */
1187 vim_free(redir_ga.ga_data);
1188 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 vim_free(redir_lval);
1191 redir_lval = NULL;
1192 }
1193 vim_free(redir_varname);
1194 redir_varname = NULL;
1195}
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197# if defined(FEAT_MBYTE) || defined(PROTO)
1198 int
1199eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1200 char_u *enc_from;
1201 char_u *enc_to;
1202 char_u *fname_from;
1203 char_u *fname_to;
1204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1208 set_vim_var_string(VV_CC_TO, enc_to, -1);
1209 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1210 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1211 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1212 err = TRUE;
1213 set_vim_var_string(VV_CC_FROM, NULL, -1);
1214 set_vim_var_string(VV_CC_TO, NULL, -1);
1215 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1216 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1217
1218 if (err)
1219 return FAIL;
1220 return OK;
1221}
1222# endif
1223
1224# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1225 int
1226eval_printexpr(fname, args)
1227 char_u *fname;
1228 char_u *args;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, fname, -1);
1233 set_vim_var_string(VV_CMDARG, args, -1);
1234 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1235 err = TRUE;
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_CMDARG, NULL, -1);
1238
1239 if (err)
1240 {
1241 mch_remove(fname);
1242 return FAIL;
1243 }
1244 return OK;
1245}
1246# endif
1247
1248# if defined(FEAT_DIFF) || defined(PROTO)
1249 void
1250eval_diff(origfile, newfile, outfile)
1251 char_u *origfile;
1252 char_u *newfile;
1253 char_u *outfile;
1254{
1255 int err = FALSE;
1256
1257 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1258 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1259 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1260 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1261 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1262 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1263 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1264}
1265
1266 void
1267eval_patch(origfile, difffile, outfile)
1268 char_u *origfile;
1269 char_u *difffile;
1270 char_u *outfile;
1271{
1272 int err;
1273
1274 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1275 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1276 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1277 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1278 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1279 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1280 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1281}
1282# endif
1283
1284/*
1285 * Top level evaluation function, returning a boolean.
1286 * Sets "error" to TRUE if there was an error.
1287 * Return TRUE or FALSE.
1288 */
1289 int
1290eval_to_bool(arg, error, nextcmd, skip)
1291 char_u *arg;
1292 int *error;
1293 char_u **nextcmd;
1294 int skip; /* only parse, don't execute */
1295{
Bram Moolenaar33570922005-01-25 22:26:29 +00001296 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 int retval = FALSE;
1298
1299 if (skip)
1300 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 else
1304 {
1305 *error = FALSE;
1306 if (!skip)
1307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001308 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
1319 * Top level evaluation function, returning a string. If "skip" is TRUE,
1320 * only parsing to "nextcmd" is done, without reporting errors. Return
1321 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1322 */
1323 char_u *
1324eval_to_string_skip(arg, nextcmd, skip)
1325 char_u *arg;
1326 char_u **nextcmd;
1327 int skip; /* only parse, don't execute */
1328{
Bram Moolenaar33570922005-01-25 22:26:29 +00001329 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 char_u *retval;
1331
1332 if (skip)
1333 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 retval = NULL;
1336 else
1337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 retval = vim_strsave(get_tv_string(&tv));
1339 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
1341 if (skip)
1342 --emsg_skip;
1343
1344 return retval;
1345}
1346
1347/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001348 * Skip over an expression at "*pp".
1349 * Return FAIL for an error, OK otherwise.
1350 */
1351 int
1352skip_expr(pp)
1353 char_u **pp;
1354{
Bram Moolenaar33570922005-01-25 22:26:29 +00001355 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001356
1357 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001359}
1360
1361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363 * When "convert" is TRUE convert a List into a sequence of lines and convert
1364 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 * Return pointer to allocated memory, or NULL for failure.
1366 */
1367 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 char_u *arg;
1370 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001375 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001376#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 retval = NULL;
1382 else
1383 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001385 {
1386 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001387 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001388 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001389 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001390 if (tv.vval.v_list->lv_len > 0)
1391 ga_append(&ga, NL);
1392 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001393 ga_append(&ga, NUL);
1394 retval = (char_u *)ga.ga_data;
1395 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001396#ifdef FEAT_FLOAT
1397 else if (convert && tv.v_type == VAR_FLOAT)
1398 {
1399 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1400 retval = vim_strsave(numbuf);
1401 }
1402#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 else
1404 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001405 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
1407
1408 return retval;
1409}
1410
1411/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001412 * Call eval_to_string() without using current local variables and using
1413 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 */
1415 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 char_u *arg;
1418 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001419 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 char_u *retval;
1422 void *save_funccalp;
1423
1424 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001425 if (use_sandbox)
1426 ++sandbox;
1427 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001428 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001429 if (use_sandbox)
1430 --sandbox;
1431 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 restore_funccal(save_funccalp);
1433 return retval;
1434}
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436/*
1437 * Top level evaluation function, returning a number.
1438 * Evaluates "expr" silently.
1439 * Returns -1 for an error.
1440 */
1441 int
1442eval_to_number(expr)
1443 char_u *expr;
1444{
Bram Moolenaar33570922005-01-25 22:26:29 +00001445 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001447 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 ++emsg_off;
1450
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 retval = -1;
1453 else
1454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001455 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458 --emsg_off;
1459
1460 return retval;
1461}
1462
Bram Moolenaara40058a2005-07-11 22:42:07 +00001463/*
1464 * Prepare v: variable "idx" to be used.
1465 * Save the current typeval in "save_tv".
1466 * When not used yet add the variable to the v: hashtable.
1467 */
1468 static void
1469prepare_vimvar(idx, save_tv)
1470 int idx;
1471 typval_T *save_tv;
1472{
1473 *save_tv = vimvars[idx].vv_tv;
1474 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1475 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1476}
1477
1478/*
1479 * Restore v: variable "idx" to typeval "save_tv".
1480 * When no longer defined, remove the variable from the v: hashtable.
1481 */
1482 static void
1483restore_vimvar(idx, save_tv)
1484 int idx;
1485 typval_T *save_tv;
1486{
1487 hashitem_T *hi;
1488
Bram Moolenaara40058a2005-07-11 22:42:07 +00001489 vimvars[idx].vv_tv = *save_tv;
1490 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1491 {
1492 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1493 if (HASHITEM_EMPTY(hi))
1494 EMSG2(_(e_intern2), "restore_vimvar()");
1495 else
1496 hash_remove(&vimvarht, hi);
1497 }
1498}
1499
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001500#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501/*
1502 * Evaluate an expression to a list with suggestions.
1503 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001504 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505 */
1506 list_T *
1507eval_spell_expr(badword, expr)
1508 char_u *badword;
1509 char_u *expr;
1510{
1511 typval_T save_val;
1512 typval_T rettv;
1513 list_T *list = NULL;
1514 char_u *p = skipwhite(expr);
1515
1516 /* Set "v:val" to the bad word. */
1517 prepare_vimvar(VV_VAL, &save_val);
1518 vimvars[VV_VAL].vv_type = VAR_STRING;
1519 vimvars[VV_VAL].vv_str = badword;
1520 if (p_verbose == 0)
1521 ++emsg_off;
1522
1523 if (eval1(&p, &rettv, TRUE) == OK)
1524 {
1525 if (rettv.v_type != VAR_LIST)
1526 clear_tv(&rettv);
1527 else
1528 list = rettv.vval.v_list;
1529 }
1530
1531 if (p_verbose == 0)
1532 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001533 restore_vimvar(VV_VAL, &save_val);
1534
1535 return list;
1536}
1537
1538/*
1539 * "list" is supposed to contain two items: a word and a number. Return the
1540 * word in "pp" and the number as the return value.
1541 * Return -1 if anything isn't right.
1542 * Used to get the good word and score from the eval_spell_expr() result.
1543 */
1544 int
1545get_spellword(list, pp)
1546 list_T *list;
1547 char_u **pp;
1548{
1549 listitem_T *li;
1550
1551 li = list->lv_first;
1552 if (li == NULL)
1553 return -1;
1554 *pp = get_tv_string(&li->li_tv);
1555
1556 li = li->li_next;
1557 if (li == NULL)
1558 return -1;
1559 return get_tv_number(&li->li_tv);
1560}
1561#endif
1562
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001563/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 * Top level evaluation function.
1565 * Returns an allocated typval_T with the result.
1566 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001567 */
1568 typval_T *
1569eval_expr(arg, nextcmd)
1570 char_u *arg;
1571 char_u **nextcmd;
1572{
1573 typval_T *tv;
1574
1575 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001576 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001577 {
1578 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001579 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 }
1581
1582 return tv;
1583}
1584
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588 * Uses argv[argc] for the function arguments. Only Number and String
1589 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001592 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001593call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 char_u *func;
1595 int argc;
1596 char_u **argv;
1597 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
Bram Moolenaar33570922005-01-25 22:26:29 +00001601 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 long n;
1603 int len;
1604 int i;
1605 int doesrange;
1606 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001609 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 for (i = 0; i < argc; i++)
1614 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001615 /* Pass a NULL or empty argument as an empty string */
1616 if (argv[i] == NULL || *argv[i] == NUL)
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_STRING;
1619 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001620 continue;
1621 }
1622
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001623 if (str_arg_only)
1624 len = 0;
1625 else
1626 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001627 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (len != 0 && len == (int)STRLEN(argv[i]))
1629 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001630 argvars[i].v_type = VAR_NUMBER;
1631 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 }
1633 else
1634 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001635 argvars[i].v_type = VAR_STRING;
1636 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
1638 }
1639
1640 if (safe)
1641 {
1642 save_funccalp = save_funccal();
1643 ++sandbox;
1644 }
1645
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1647 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (safe)
1651 {
1652 --sandbox;
1653 restore_funccal(save_funccalp);
1654 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 vim_free(argvars);
1656
1657 if (ret == FAIL)
1658 clear_tv(rettv);
1659
1660 return ret;
1661}
1662
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001663/*
1664 * Call vimL function "func" and return the result as a number.
1665 * Returns -1 when calling the function fails.
1666 * Uses argv[argc] for the function arguments.
1667 */
1668 long
1669call_func_retnr(func, argc, argv, safe)
1670 char_u *func;
1671 int argc;
1672 char_u **argv;
1673 int safe; /* use the sandbox */
1674{
1675 typval_T rettv;
1676 long retval;
1677
1678 /* All arguments are passed as strings, no conversion to number. */
1679 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1680 return -1;
1681
1682 retval = get_tv_number_chk(&rettv, NULL);
1683 clear_tv(&rettv);
1684 return retval;
1685}
1686
1687#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1688 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1689
Bram Moolenaar4f688582007-07-24 12:34:30 +00001690# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 * Call vimL function "func" and return the result as a string.
1693 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694 * Uses argv[argc] for the function arguments.
1695 */
1696 void *
1697call_func_retstr(func, argc, argv, safe)
1698 char_u *func;
1699 int argc;
1700 char_u **argv;
1701 int safe; /* use the sandbox */
1702{
1703 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001706 /* All arguments are passed as strings, no conversion to number. */
1707 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 return NULL;
1709
1710 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 return retval;
1713}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001714# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001716/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001717 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001719 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 */
1721 void *
1722call_func_retlist(func, argc, argv, safe)
1723 char_u *func;
1724 int argc;
1725 char_u **argv;
1726 int safe; /* use the sandbox */
1727{
1728 typval_T rettv;
1729
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001730 /* All arguments are passed as strings, no conversion to number. */
1731 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 return NULL;
1733
1734 if (rettv.v_type != VAR_LIST)
1735 {
1736 clear_tv(&rettv);
1737 return NULL;
1738 }
1739
1740 return rettv.vval.v_list;
1741}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#endif
1743
1744/*
1745 * Save the current function call pointer, and set it to NULL.
1746 * Used when executing autocommands and for ":source".
1747 */
1748 void *
1749save_funccal()
1750{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001751 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 current_funccal = NULL;
1754 return (void *)fc;
1755}
1756
1757 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001758restore_funccal(vfc)
1759 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001761 funccall_T *fc = (funccall_T *)vfc;
1762
1763 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764}
1765
Bram Moolenaar05159a02005-02-26 23:04:13 +00001766#if defined(FEAT_PROFILE) || defined(PROTO)
1767/*
1768 * Prepare profiling for entering a child or something else that is not
1769 * counted for the script/function itself.
1770 * Should always be called in pair with prof_child_exit().
1771 */
1772 void
1773prof_child_enter(tm)
1774 proftime_T *tm; /* place to store waittime */
1775{
1776 funccall_T *fc = current_funccal;
1777
1778 if (fc != NULL && fc->func->uf_profiling)
1779 profile_start(&fc->prof_child);
1780 script_prof_save(tm);
1781}
1782
1783/*
1784 * Take care of time spent in a child.
1785 * Should always be called after prof_child_enter().
1786 */
1787 void
1788prof_child_exit(tm)
1789 proftime_T *tm; /* where waittime was stored */
1790{
1791 funccall_T *fc = current_funccal;
1792
1793 if (fc != NULL && fc->func->uf_profiling)
1794 {
1795 profile_end(&fc->prof_child);
1796 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1797 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1798 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1799 }
1800 script_prof_restore(tm);
1801}
1802#endif
1803
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805#ifdef FEAT_FOLDING
1806/*
1807 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1808 * it in "*cp". Doesn't give error messages.
1809 */
1810 int
1811eval_foldexpr(arg, cp)
1812 char_u *arg;
1813 int *cp;
1814{
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 int retval;
1817 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001818 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1819 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 ++sandbox;
1824 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 retval = 0;
1828 else
1829 {
1830 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 if (tv.v_type == VAR_NUMBER)
1832 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001833 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 retval = 0;
1835 else
1836 {
1837 /* If the result is a string, check if there is a non-digit before
1838 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (!VIM_ISDIGIT(*s) && *s != '-')
1841 *cp = *s++;
1842 retval = atol((char *)s);
1843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001847 if (use_sandbox)
1848 --sandbox;
1849 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 return retval;
1852}
1853#endif
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 * ":let" list all variable values
1857 * ":let var1 var2" list variable values
1858 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 * ":let var += expr" assignment command.
1860 * ":let var -= expr" assignment command.
1861 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
1864 void
1865ex_let(eap)
1866 exarg_T *eap;
1867{
1868 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 int var_count = 0;
1873 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001875 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
Bram Moolenaardb552d602006-03-23 22:59:57 +00001878 argend = skip_var_list(arg, &var_count, &semicolon);
1879 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001881 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1882 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001883 expr = skipwhite(argend);
1884 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1885 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001887 /*
1888 * ":let" without "=": list variables
1889 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 if (*arg == '[')
1891 EMSG(_(e_invarg));
1892 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001894 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 list_glob_vars(&first);
1899 list_buf_vars(&first);
1900 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001901#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001903#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001904 list_script_vars(&first);
1905 list_func_vars(&first);
1906 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 eap->nextcmd = check_nextcmd(arg);
1909 }
1910 else
1911 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 op[0] = '=';
1913 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001914 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1917 op[0] = *expr; /* +=, -= or .= */
1918 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001920 else
1921 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (eap->skip)
1924 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (eap->skip)
1927 {
1928 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 --emsg_skip;
1931 }
1932 else if (i != FAIL)
1933 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938 }
1939}
1940
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941/*
1942 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1943 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 * When "nextchars" is not NULL it points to a string with characters that
1945 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1946 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 * Returns OK or FAIL;
1948 */
1949 static int
1950ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1951 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 int copy; /* copy values from "tv", don't move */
1954 int semicolon; /* from skip_var_list() */
1955 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957{
1958 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001959 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001961 listitem_T *item;
1962 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963
1964 if (*arg != '[')
1965 {
1966 /*
1967 * ":let var = expr" or ":for var in list"
1968 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 return OK;
1972 }
1973
1974 /*
1975 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1976 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001977 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 {
1979 EMSG(_(e_listreq));
1980 return FAIL;
1981 }
1982
1983 i = list_len(l);
1984 if (semicolon == 0 && var_count < i)
1985 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001986 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 return FAIL;
1988 }
1989 if (var_count - semicolon > i)
1990 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001991 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 return FAIL;
1993 }
1994
1995 item = l->lv_first;
1996 while (*arg != ']')
1997 {
1998 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001999 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 item = item->li_next;
2001 if (arg == NULL)
2002 return FAIL;
2003
2004 arg = skipwhite(arg);
2005 if (*arg == ';')
2006 {
2007 /* Put the rest of the list (may be empty) in the var after ';'.
2008 * Create a new list for this. */
2009 l = list_alloc();
2010 if (l == NULL)
2011 return FAIL;
2012 while (item != NULL)
2013 {
2014 list_append_tv(l, &item->li_tv);
2015 item = item->li_next;
2016 }
2017
2018 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002019 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 ltv.vval.v_list = l;
2021 l->lv_refcount = 1;
2022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2024 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 clear_tv(&ltv);
2026 if (arg == NULL)
2027 return FAIL;
2028 break;
2029 }
2030 else if (*arg != ',' && *arg != ']')
2031 {
2032 EMSG2(_(e_intern2), "ex_let_vars()");
2033 return FAIL;
2034 }
2035 }
2036
2037 return OK;
2038}
2039
2040/*
2041 * Skip over assignable variable "var" or list of variables "[var, var]".
2042 * Used for ":let varvar = expr" and ":for varvar in expr".
2043 * For "[var, var]" increment "*var_count" for each variable.
2044 * for "[var, var; var]" set "semicolon".
2045 * Return NULL for an error.
2046 */
2047 static char_u *
2048skip_var_list(arg, var_count, semicolon)
2049 char_u *arg;
2050 int *var_count;
2051 int *semicolon;
2052{
2053 char_u *p, *s;
2054
2055 if (*arg == '[')
2056 {
2057 /* "[var, var]": find the matching ']'. */
2058 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002059 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002060 {
2061 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2062 s = skip_var_one(p);
2063 if (s == p)
2064 {
2065 EMSG2(_(e_invarg2), p);
2066 return NULL;
2067 }
2068 ++*var_count;
2069
2070 p = skipwhite(s);
2071 if (*p == ']')
2072 break;
2073 else if (*p == ';')
2074 {
2075 if (*semicolon == 1)
2076 {
2077 EMSG(_("Double ; in list of variables"));
2078 return NULL;
2079 }
2080 *semicolon = 1;
2081 }
2082 else if (*p != ',')
2083 {
2084 EMSG2(_(e_invarg2), p);
2085 return NULL;
2086 }
2087 }
2088 return p + 1;
2089 }
2090 else
2091 return skip_var_one(arg);
2092}
2093
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002094/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002095 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002096 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002098 static char_u *
2099skip_var_one(arg)
2100 char_u *arg;
2101{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002102 if (*arg == '@' && arg[1] != NUL)
2103 return arg + 2;
2104 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2105 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002106}
2107
Bram Moolenaara7043832005-01-21 11:56:39 +00002108/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002109 * List variables for hashtab "ht" with prefix "prefix".
2110 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002114 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118{
Bram Moolenaar33570922005-01-25 22:26:29 +00002119 hashitem_T *hi;
2120 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 int todo;
2122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002123 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2125 {
2126 if (!HASHITEM_EMPTY(hi))
2127 {
2128 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002129 di = HI2DI(hi);
2130 if (empty || di->di_tv.v_type != VAR_STRING
2131 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133 }
2134 }
2135}
2136
2137/*
2138 * List global variables.
2139 */
2140 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141list_glob_vars(first)
2142 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
2147/*
2148 * List buffer variables.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_buf_vars(first)
2152 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002153{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154 char_u numbuf[NUMBUFLEN];
2155
Bram Moolenaar429fa852013-04-15 12:27:36 +02002156 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158
2159 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2161 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002162}
2163
2164/*
2165 * List window variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_win_vars(first)
2169 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002173}
2174
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002175#ifdef FEAT_WINDOWS
2176/*
2177 * List tab page variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_tab_vars(first)
2181 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002182{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002183 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002185}
2186#endif
2187
Bram Moolenaara7043832005-01-21 11:56:39 +00002188/*
2189 * List Vim variables.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_vim_vars(first)
2193 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196}
2197
2198/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002199 * List script-local variables, if there is a script.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_script_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2207 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
2211 * List function variables, if there is a function.
2212 */
2213 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_func_vars(first)
2215 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216{
2217 if (current_funccal != NULL)
2218 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220}
2221
2222/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 * List variables in "arg".
2224 */
2225 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 exarg_T *eap;
2228 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230{
2231 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 char_u *name_start;
2235 char_u *arg_subsc;
2236 char_u *tofree;
2237 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238
2239 while (!ends_excmd(*arg) && !got_int)
2240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002243 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2245 {
2246 emsg_severe = TRUE;
2247 EMSG(_(e_trailing));
2248 break;
2249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 /* get_name_len() takes care of expanding curly braces */
2254 name_start = name = arg;
2255 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2256 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 /* This is mainly to keep test 49 working: when expanding
2259 * curly braces fails overrule the exception error message. */
2260 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 emsg_severe = TRUE;
2263 EMSG2(_(e_invarg2), arg);
2264 break;
2265 }
2266 error = TRUE;
2267 }
2268 else
2269 {
2270 if (tofree != NULL)
2271 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002272 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 else
2275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 /* handle d.key, l[idx], f(expr) */
2277 arg_subsc = arg;
2278 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 'g': list_glob_vars(first); break;
2287 case 'b': list_buf_vars(first); break;
2288 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002289#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002290 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002291#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 case 'v': list_vim_vars(first); break;
2293 case 's': list_script_vars(first); break;
2294 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 default:
2296 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 }
2299 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 {
2301 char_u numbuf[NUMBUFLEN];
2302 char_u *tf;
2303 int c;
2304 char_u *s;
2305
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002306 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 c = *arg;
2308 *arg = NUL;
2309 list_one_var_a((char_u *)"",
2310 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002311 tv.v_type,
2312 s == NULL ? (char_u *)"" : s,
2313 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 *arg = c;
2315 vim_free(tf);
2316 }
2317 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
2320 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321
2322 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002324
2325 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 }
2327
2328 return arg;
2329}
2330
2331/*
2332 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2333 * Returns a pointer to the char just after the var name.
2334 * Returns NULL if there is an error.
2335 */
2336 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002339 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002341 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343{
2344 int c1;
2345 char_u *name;
2346 char_u *p;
2347 char_u *arg_end = NULL;
2348 int len;
2349 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002350 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351
2352 /*
2353 * ":let $VAR = expr": Set environment variable.
2354 */
2355 if (*arg == '$')
2356 {
2357 /* Find the end of the name. */
2358 ++arg;
2359 name = arg;
2360 len = get_env_len(&arg);
2361 if (len == 0)
2362 EMSG2(_(e_invarg2), name - 1);
2363 else
2364 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 if (op != NULL && (*op == '+' || *op == '-'))
2366 EMSG2(_(e_letwrong), op);
2367 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002370 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 {
2372 c1 = name[len];
2373 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002374 p = get_tv_string_chk(tv);
2375 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376 {
2377 int mustfree = FALSE;
2378 char_u *s = vim_getenv(name, &mustfree);
2379
2380 if (s != NULL)
2381 {
2382 p = tofree = concat_str(s, p);
2383 if (mustfree)
2384 vim_free(s);
2385 }
2386 }
2387 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 if (STRICMP(name, "HOME") == 0)
2391 init_homedir();
2392 else if (didset_vim && STRICMP(name, "VIM") == 0)
2393 didset_vim = FALSE;
2394 else if (didset_vimruntime
2395 && STRICMP(name, "VIMRUNTIME") == 0)
2396 didset_vimruntime = FALSE;
2397 arg_end = arg;
2398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401 }
2402 }
2403 }
2404
2405 /*
2406 * ":let &option = expr": Set option value.
2407 * ":let &l:option = expr": Set local option value.
2408 * ":let &g:option = expr": Set global option value.
2409 */
2410 else if (*arg == '&')
2411 {
2412 /* Find the end of the name. */
2413 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002414 if (p == NULL || (endchars != NULL
2415 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 EMSG(_(e_letunexp));
2417 else
2418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 long n;
2420 int opt_type;
2421 long numval;
2422 char_u *stringval = NULL;
2423 char_u *s;
2424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 c1 = *p;
2426 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427
2428 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 s = get_tv_string_chk(tv); /* != NULL if number or string */
2430 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 {
2432 opt_type = get_option_value(arg, &numval,
2433 &stringval, opt_flags);
2434 if ((opt_type == 1 && *op == '.')
2435 || (opt_type == 0 && *op != '.'))
2436 EMSG2(_(e_letwrong), op);
2437 else
2438 {
2439 if (opt_type == 1) /* number */
2440 {
2441 if (*op == '+')
2442 n = numval + n;
2443 else
2444 n = numval - n;
2445 }
2446 else if (opt_type == 0 && stringval != NULL) /* string */
2447 {
2448 s = concat_str(stringval, s);
2449 vim_free(stringval);
2450 stringval = s;
2451 }
2452 }
2453 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 if (s != NULL)
2455 {
2456 set_option_value(arg, n, s, opt_flags);
2457 arg_end = p;
2458 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
2462 }
2463
2464 /*
2465 * ":let @r = expr": Set register contents.
2466 */
2467 else if (*arg == '@')
2468 {
2469 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 if (op != NULL && (*op == '+' || *op == '-'))
2471 EMSG2(_(e_letwrong), op);
2472 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002473 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 EMSG(_(e_letunexp));
2475 else
2476 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002477 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 char_u *s;
2479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 p = get_tv_string_chk(tv);
2481 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002483 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 if (s != NULL)
2485 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002486 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002487 vim_free(s);
2488 }
2489 }
2490 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 arg_end = arg + 1;
2494 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002495 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 }
2497 }
2498
2499 /*
2500 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002503 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002505 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002507 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2511 EMSG(_(e_letunexp));
2512 else
2513 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 arg_end = p;
2516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 }
2520
2521 else
2522 EMSG2(_(e_invarg2), arg);
2523
2524 return arg_end;
2525}
2526
2527/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2529 */
2530 static int
2531check_changedtick(arg)
2532 char_u *arg;
2533{
2534 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2535 {
2536 EMSG2(_(e_readonlyvar), arg);
2537 return TRUE;
2538 }
2539 return FALSE;
2540}
2541
2542/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Get an lval: variable, Dict item or List item that can be assigned a value
2544 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2545 * "name.key", "name.key[expr]" etc.
2546 * Indexing only works if "name" is an existing List or Dictionary.
2547 * "name" points to the start of the name.
2548 * If "rettv" is not NULL it points to the value to be assigned.
2549 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2550 * wrong; must end in space or cmd separator.
2551 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002552 * flags:
2553 * GLV_QUIET: do not give error messages
2554 * GLV_NO_AUTOLOAD: do not use script autoloading
2555 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002557 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 */
2560 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002561get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 typval_T *rettv;
2564 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 int unlet;
2566 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002567 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002568 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 char_u *expr_start, *expr_end;
2572 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002573 dictitem_T *v;
2574 typval_T var1;
2575 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002581 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585
2586 if (skip)
2587 {
2588 /* When skipping just find the end of the name. */
2589 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002590 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 }
2592
2593 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (expr_start != NULL)
2596 {
2597 /* Don't expand the name when we already know there is an error. */
2598 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2599 && *p != '[' && *p != '.')
2600 {
2601 EMSG(_(e_trailing));
2602 return NULL;
2603 }
2604
2605 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2606 if (lp->ll_exp_name == NULL)
2607 {
2608 /* Report an invalid expression in braces, unless the
2609 * expression evaluation has been cancelled due to an
2610 * aborting error, an interrupt, or an exception. */
2611 if (!aborting() && !quiet)
2612 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002613 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 EMSG2(_(e_invarg2), name);
2615 return NULL;
2616 }
2617 }
2618 lp->ll_name = lp->ll_exp_name;
2619 }
2620 else
2621 lp->ll_name = name;
2622
2623 /* Without [idx] or .key we are done. */
2624 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2625 return p;
2626
2627 cc = *p;
2628 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002629 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (v == NULL && !quiet)
2631 EMSG2(_(e_undefvar), lp->ll_name);
2632 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 if (v == NULL)
2634 return NULL;
2635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 /*
2637 * Loop until no more [idx] or .key is following.
2638 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2643 && !(lp->ll_tv->v_type == VAR_DICT
2644 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_("E689: Can only index a List or Dictionary"));
2648 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_("E708: [:] must come last"));
2654 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 len = -1;
2658 if (*p == '.')
2659 {
2660 key = p + 1;
2661 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2662 ;
2663 if (len == 0)
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_(e_emptykey));
2667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = key + len;
2670 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 else
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (*p == ':')
2676 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677 else
2678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 empty1 = FALSE;
2680 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var1) == NULL)
2683 {
2684 /* not a number or string */
2685 clear_tv(&var1);
2686 return NULL;
2687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689
2690 /* Optionally get the second index [ :expr]. */
2691 if (*p == ':')
2692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002696 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 if (!empty1)
2698 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (rettv != NULL && (rettv->v_type != VAR_LIST
2702 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
2705 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
2710 p = skipwhite(p + 1);
2711 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 else
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 if (get_tv_string_chk(&var2) == NULL)
2723 {
2724 /* not a number or string */
2725 if (!empty1)
2726 clear_tv(&var1);
2727 clear_tv(&var2);
2728 return NULL;
2729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (!quiet)
2739 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (!empty1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746
2747 /* Skip to past ']'. */
2748 ++p;
2749 }
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
2753 if (len == -1)
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (*key == NUL)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (!quiet)
2760 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 }
2764 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002765 lp->ll_list = NULL;
2766 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002767 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002769 /* When assigning to a scope dictionary check that a function and
2770 * variable name is valid (only variable name unless it is l: or
2771 * g: dictionary). Disallow overwriting a builtin function. */
2772 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 int prevval;
2775 int wrong;
2776
2777 if (len != -1)
2778 {
2779 prevval = key[len];
2780 key[len] = NUL;
2781 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002782 else
2783 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2785 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 || !valid_varname(key);
2788 if (len != -1)
2789 key[len] = prevval;
2790 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 return NULL;
2792 }
2793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002796 /* Can't add "v:" variable. */
2797 if (lp->ll_dict == &vimvardict)
2798 {
2799 EMSG2(_(e_illvar), name);
2800 return NULL;
2801 }
2802
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002803 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002807 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 if (len == -1)
2809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 if (len == -1)
2817 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 p = NULL;
2820 break;
2821 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002822 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002823 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 return NULL;
2825
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 if (len == -1)
2827 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 }
2830 else
2831 {
2832 /*
2833 * Get the number and item for the only or first index of the List.
2834 */
2835 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 else
2838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002839 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 clear_tv(&var1);
2841 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002842 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_list = lp->ll_tv->vval.v_list;
2844 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2845 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002847 if (lp->ll_n1 < 0)
2848 {
2849 lp->ll_n1 = 0;
2850 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2851 }
2852 }
2853 if (lp->ll_li == NULL)
2854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 if (!quiet)
2858 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 }
2861
2862 /*
2863 * May need to find the item or absolute index for the second
2864 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 * When no index given: "lp->ll_empty2" is TRUE.
2866 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002870 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 }
2883
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2885 if (lp->ll_n1 < 0)
2886 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2887 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 {
2889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 return p;
2900}
2901
2902/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 */
2905 static void
2906clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908{
2909 vim_free(lp->ll_exp_name);
2910 vim_free(lp->ll_newkey);
2911}
2912
2913/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002916 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 */
2918 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002922 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925{
2926 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 listitem_T *ri;
2928 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929
2930 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 cc = *endp;
2935 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 if (op != NULL && *op != '=')
2937 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002941 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002942 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002943 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 if ((di == NULL
2946 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2947 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2948 FALSE)))
2949 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 set_var(lp->ll_name, &tv, FALSE);
2951 clear_tv(&tv);
2952 }
2953 }
2954 else
2955 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 else if (tv_check_lock(lp->ll_newkey == NULL
2960 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002961 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002962 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 else if (lp->ll_range)
2964 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002965 listitem_T *ll_li = lp->ll_li;
2966 int ll_n1 = lp->ll_n1;
2967
2968 /*
2969 * Check whether any of the list items is locked
2970 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002971 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002972 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002973 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002974 return;
2975 ri = ri->li_next;
2976 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2977 break;
2978 ll_li = ll_li->li_next;
2979 ++ll_n1;
2980 }
2981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 /*
2983 * Assign the List values to the list items.
2984 */
2985 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002986 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 if (op != NULL && *op != '=')
2988 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2989 else
2990 {
2991 clear_tv(&lp->ll_li->li_tv);
2992 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 ri = ri->li_next;
2995 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2996 break;
2997 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003000 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 ri = NULL;
3003 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 lp->ll_li = lp->ll_li->li_next;
3007 ++lp->ll_n1;
3008 }
3009 if (ri != NULL)
3010 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 else if (lp->ll_empty2
3012 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 : lp->ll_n1 != lp->ll_n2)
3014 EMSG(_("E711: List value has not enough items"));
3015 }
3016 else
3017 {
3018 /*
3019 * Assign to a List or Dictionary item.
3020 */
3021 if (lp->ll_newkey != NULL)
3022 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 if (op != NULL && *op != '=')
3024 {
3025 EMSG2(_(e_letwrong), op);
3026 return;
3027 }
3028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003030 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 if (di == NULL)
3032 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003033 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3034 {
3035 vim_free(di);
3036 return;
3037 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003038 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003039 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 else if (op != NULL && *op != '=')
3041 {
3042 tv_op(lp->ll_tv, rettv, op);
3043 return;
3044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003045 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 /*
3049 * Assign the value to the variable or list item.
3050 */
3051 if (copy)
3052 copy_tv(rettv, lp->ll_tv);
3053 else
3054 {
3055 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003056 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 }
3059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003060}
3061
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003062/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3064 * Returns OK or FAIL.
3065 */
3066 static int
3067tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003068 typval_T *tv1;
3069 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 char_u *op;
3071{
3072 long n;
3073 char_u numbuf[NUMBUFLEN];
3074 char_u *s;
3075
3076 /* Can't do anything with a Funcref or a Dict on the right. */
3077 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3078 {
3079 switch (tv1->v_type)
3080 {
3081 case VAR_DICT:
3082 case VAR_FUNC:
3083 break;
3084
3085 case VAR_LIST:
3086 if (*op != '+' || tv2->v_type != VAR_LIST)
3087 break;
3088 /* List += List */
3089 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3090 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3091 return OK;
3092
3093 case VAR_NUMBER:
3094 case VAR_STRING:
3095 if (tv2->v_type == VAR_LIST)
3096 break;
3097 if (*op == '+' || *op == '-')
3098 {
3099 /* nr += nr or nr -= nr*/
3100 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003101#ifdef FEAT_FLOAT
3102 if (tv2->v_type == VAR_FLOAT)
3103 {
3104 float_T f = n;
3105
3106 if (*op == '+')
3107 f += tv2->vval.v_float;
3108 else
3109 f -= tv2->vval.v_float;
3110 clear_tv(tv1);
3111 tv1->v_type = VAR_FLOAT;
3112 tv1->vval.v_float = f;
3113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#endif
3116 {
3117 if (*op == '+')
3118 n += get_tv_number(tv2);
3119 else
3120 n -= get_tv_number(tv2);
3121 clear_tv(tv1);
3122 tv1->v_type = VAR_NUMBER;
3123 tv1->vval.v_number = n;
3124 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003125 }
3126 else
3127 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128 if (tv2->v_type == VAR_FLOAT)
3129 break;
3130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 /* str .= str */
3132 s = get_tv_string(tv1);
3133 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3134 clear_tv(tv1);
3135 tv1->v_type = VAR_STRING;
3136 tv1->vval.v_string = s;
3137 }
3138 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139
3140#ifdef FEAT_FLOAT
3141 case VAR_FLOAT:
3142 {
3143 float_T f;
3144
3145 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3146 && tv2->v_type != VAR_NUMBER
3147 && tv2->v_type != VAR_STRING))
3148 break;
3149 if (tv2->v_type == VAR_FLOAT)
3150 f = tv2->vval.v_float;
3151 else
3152 f = get_tv_number(tv2);
3153 if (*op == '+')
3154 tv1->vval.v_float += f;
3155 else
3156 tv1->vval.v_float -= f;
3157 }
3158 return OK;
3159#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003160 }
3161 }
3162
3163 EMSG2(_(e_letwrong), op);
3164 return FAIL;
3165}
3166
3167/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168 * Add a watcher to a list.
3169 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003170 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 list_T *l;
3173 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174{
3175 lw->lw_next = l->lv_watch;
3176 l->lv_watch = lw;
3177}
3178
3179/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003180 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 * No warning when it isn't found...
3182 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003183 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003185 list_T *l;
3186 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187{
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189
3190 lwp = &l->lv_watch;
3191 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3192 {
3193 if (lw == lwrem)
3194 {
3195 *lwp = lw->lw_next;
3196 break;
3197 }
3198 lwp = &lw->lw_next;
3199 }
3200}
3201
3202/*
3203 * Just before removing an item from a list: advance watchers to the next
3204 * item.
3205 */
3206 static void
3207list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003208 list_T *l;
3209 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210{
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212
3213 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3214 if (lw->lw_item == item)
3215 lw->lw_item = item->li_next;
3216}
3217
3218/*
3219 * Evaluate the expression used in a ":for var in expr" command.
3220 * "arg" points to "var".
3221 * Set "*errp" to TRUE for an error, FALSE otherwise;
3222 * Return a pointer that holds the info. Null when there is an error.
3223 */
3224 void *
3225eval_for_line(arg, errp, nextcmdp, skip)
3226 char_u *arg;
3227 int *errp;
3228 char_u **nextcmdp;
3229 int skip;
3230{
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 typval_T tv;
3234 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 *errp = TRUE; /* default: there is an error */
3237
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 if (fi == NULL)
3240 return NULL;
3241
3242 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3243 if (expr == NULL)
3244 return fi;
3245
3246 expr = skipwhite(expr);
3247 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3248 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003249 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 return fi;
3251 }
3252
3253 if (skip)
3254 ++emsg_skip;
3255 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3256 {
3257 *errp = FALSE;
3258 if (!skip)
3259 {
3260 l = tv.vval.v_list;
3261 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 clear_tv(&tv);
3265 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 else
3267 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003268 /* No need to increment the refcount, it's already set for the
3269 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 fi->fi_list = l;
3271 list_add_watch(l, &fi->fi_lw);
3272 fi->fi_lw.lw_item = l->lv_first;
3273 }
3274 }
3275 }
3276 if (skip)
3277 --emsg_skip;
3278
3279 return fi;
3280}
3281
3282/*
3283 * Use the first item in a ":for" list. Advance to the next.
3284 * Assign the values to the variable (list). "arg" points to the first one.
3285 * Return TRUE when a valid item was found, FALSE when at end of list or
3286 * something wrong.
3287 */
3288 int
3289next_for_item(fi_void, arg)
3290 void *fi_void;
3291 char_u *arg;
3292{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003293 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296
3297 item = fi->fi_lw.lw_item;
3298 if (item == NULL)
3299 result = FALSE;
3300 else
3301 {
3302 fi->fi_lw.lw_item = item->li_next;
3303 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3304 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3305 }
3306 return result;
3307}
3308
3309/*
3310 * Free the structure used to store info used by ":for".
3311 */
3312 void
3313free_for_info(fi_void)
3314 void *fi_void;
3315{
Bram Moolenaar33570922005-01-25 22:26:29 +00003316 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003318 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003319 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321 list_unref(fi->fi_list);
3322 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 vim_free(fi);
3324}
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3327
3328 void
3329set_context_for_expression(xp, arg, cmdidx)
3330 expand_T *xp;
3331 char_u *arg;
3332 cmdidx_T cmdidx;
3333{
3334 int got_eq = FALSE;
3335 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338 if (cmdidx == CMD_let)
3339 {
3340 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003341 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 {
3343 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003344 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 {
3346 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003347 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 if (vim_iswhite(*p))
3349 break;
3350 }
3351 return;
3352 }
3353 }
3354 else
3355 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3356 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 while ((xp->xp_pattern = vim_strpbrk(arg,
3358 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3359 {
3360 c = *xp->xp_pattern;
3361 if (c == '&')
3362 {
3363 c = xp->xp_pattern[1];
3364 if (c == '&')
3365 {
3366 ++xp->xp_pattern;
3367 xp->xp_context = cmdidx != CMD_let || got_eq
3368 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3369 }
3370 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003373 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3374 xp->xp_pattern += 2;
3375
3376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378 else if (c == '$')
3379 {
3380 /* environment variable */
3381 xp->xp_context = EXPAND_ENV_VARS;
3382 }
3383 else if (c == '=')
3384 {
3385 got_eq = TRUE;
3386 xp->xp_context = EXPAND_EXPRESSION;
3387 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003388 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 && xp->xp_context == EXPAND_FUNCTIONS
3390 && vim_strchr(xp->xp_pattern, '(') == NULL)
3391 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 break;
3394 }
3395 else if (cmdidx != CMD_let || got_eq)
3396 {
3397 if (c == '"') /* string */
3398 {
3399 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3400 if (c == '\\' && xp->xp_pattern[1] != NUL)
3401 ++xp->xp_pattern;
3402 xp->xp_context = EXPAND_NOTHING;
3403 }
3404 else if (c == '\'') /* literal string */
3405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003406 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3408 /* skip */ ;
3409 xp->xp_context = EXPAND_NOTHING;
3410 }
3411 else if (c == '|')
3412 {
3413 if (xp->xp_pattern[1] == '|')
3414 {
3415 ++xp->xp_pattern;
3416 xp->xp_context = EXPAND_EXPRESSION;
3417 }
3418 else
3419 xp->xp_context = EXPAND_COMMANDS;
3420 }
3421 else
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
3424 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003425 /* Doesn't look like something valid, expand as an expression
3426 * anyway. */
3427 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 arg = xp->xp_pattern;
3429 if (*arg != NUL)
3430 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3431 /* skip */ ;
3432 }
3433 xp->xp_pattern = arg;
3434}
3435
3436#endif /* FEAT_CMDL_COMPL */
3437
3438/*
3439 * ":1,25call func(arg1, arg2)" function call.
3440 */
3441 void
3442ex_call(eap)
3443 exarg_T *eap;
3444{
3445 char_u *arg = eap->arg;
3446 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003448 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 linenr_T lnum;
3452 int doesrange;
3453 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003456 if (eap->skip)
3457 {
3458 /* trans_function_name() doesn't work well when skipping, use eval0()
3459 * instead to skip to any following command, e.g. for:
3460 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003461 ++emsg_skip;
3462 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3463 clear_tv(&rettv);
3464 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003465 return;
3466 }
3467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003469 if (fudi.fd_newkey != NULL)
3470 {
3471 /* Still need to give an error message for missing key. */
3472 EMSG2(_(e_dictkey), fudi.fd_newkey);
3473 vim_free(fudi.fd_newkey);
3474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003475 if (tofree == NULL)
3476 return;
3477
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003478 /* Increase refcount on dictionary, it could get deleted when evaluating
3479 * the arguments. */
3480 if (fudi.fd_dict != NULL)
3481 ++fudi.fd_dict->dv_refcount;
3482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003483 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003484 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003485 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar532c7802005-01-27 14:44:31 +00003487 /* Skip white space to allow ":call func ()". Not good, but required for
3488 * backward compatibility. */
3489 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003490 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 if (*startarg != '(')
3493 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003494 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 goto end;
3496 }
3497
3498 /*
3499 * When skipping, evaluate the function once, to find the end of the
3500 * arguments.
3501 * When the function takes a range, this is discovered after the first
3502 * call, and the loop is broken.
3503 */
3504 if (eap->skip)
3505 {
3506 ++emsg_skip;
3507 lnum = eap->line2; /* do it once, also with an invalid range */
3508 }
3509 else
3510 lnum = eap->line1;
3511 for ( ; lnum <= eap->line2; ++lnum)
3512 {
3513 if (!eap->skip && eap->addr_count > 0)
3514 {
3515 curwin->w_cursor.lnum = lnum;
3516 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003517#ifdef FEAT_VIRTUALEDIT
3518 curwin->w_cursor.coladd = 0;
3519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 eap->line1, eap->line2, &doesrange,
3524 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 failed = TRUE;
3527 break;
3528 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003529
3530 /* Handle a function returning a Funcref, Dictionary or List. */
3531 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3532 {
3533 failed = TRUE;
3534 break;
3535 }
3536
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (doesrange || eap->skip)
3539 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (aborting())
3546 break;
3547 }
3548 if (eap->skip)
3549 --emsg_skip;
3550
3551 if (!failed)
3552 {
3553 /* Check for trailing illegal characters and a following command. */
3554 if (!ends_excmd(*arg))
3555 {
3556 emsg_severe = TRUE;
3557 EMSG(_(e_trailing));
3558 }
3559 else
3560 eap->nextcmd = check_nextcmd(arg);
3561 }
3562
3563end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003564 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003565 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568/*
3569 * ":unlet[!] var1 ... " command.
3570 */
3571 void
3572ex_unlet(eap)
3573 exarg_T *eap;
3574{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575 ex_unletlock(eap, eap->arg, 0);
3576}
3577
3578/*
3579 * ":lockvar" and ":unlockvar" commands
3580 */
3581 void
3582ex_lockvar(eap)
3583 exarg_T *eap;
3584{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586 int deep = 2;
3587
3588 if (eap->forceit)
3589 deep = -1;
3590 else if (vim_isdigit(*arg))
3591 {
3592 deep = getdigits(&arg);
3593 arg = skipwhite(arg);
3594 }
3595
3596 ex_unletlock(eap, arg, deep);
3597}
3598
3599/*
3600 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3601 */
3602 static void
3603ex_unletlock(eap, argstart, deep)
3604 exarg_T *eap;
3605 char_u *argstart;
3606 int deep;
3607{
3608 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 do
3614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003616 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003617 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 if (lv.ll_name == NULL)
3619 error = TRUE; /* error but continue parsing */
3620 if (name_end == NULL || (!vim_iswhite(*name_end)
3621 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 if (name_end != NULL)
3624 {
3625 emsg_severe = TRUE;
3626 EMSG(_(e_trailing));
3627 }
3628 if (!(eap->skip || error))
3629 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 break;
3631 }
3632
3633 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 {
3635 if (eap->cmdidx == CMD_unlet)
3636 {
3637 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3638 error = TRUE;
3639 }
3640 else
3641 {
3642 if (do_lock_var(&lv, name_end, deep,
3643 eap->cmdidx == CMD_lockvar) == FAIL)
3644 error = TRUE;
3645 }
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 if (!eap->skip)
3649 clear_lval(&lv);
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 arg = skipwhite(name_end);
3652 } while (!ends_excmd(*arg));
3653
3654 eap->nextcmd = check_nextcmd(arg);
3655}
3656
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 int forceit;
3662{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 int ret = OK;
3664 int cc;
3665
3666 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 cc = *name_end;
3669 *name_end = NUL;
3670
3671 /* Normal name or expanded name. */
3672 if (check_changedtick(lp->ll_name))
3673 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003678 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003679 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003680 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 else if (lp->ll_range)
3684 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003686 listitem_T *ll_li = lp->ll_li;
3687 int ll_n1 = lp->ll_n1;
3688
3689 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3690 {
3691 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003693 return FAIL;
3694 ll_li = li;
3695 ++ll_n1;
3696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697
3698 /* Delete a range of List items. */
3699 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3700 {
3701 li = lp->ll_li->li_next;
3702 listitem_remove(lp->ll_list, lp->ll_li);
3703 lp->ll_li = li;
3704 ++lp->ll_n1;
3705 }
3706 }
3707 else
3708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 /* unlet a List item. */
3711 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003714 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 }
3716
3717 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003718}
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720/*
3721 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 */
3724 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 hashtab_T *ht;
3730 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003731 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003733 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaar33570922005-01-25 22:26:29 +00003735 ht = find_var_ht(name, &varname);
3736 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003738 if (ht == &globvarht)
3739 d = &globvardict;
3740 else if (current_funccal != NULL
3741 && ht == &current_funccal->l_vars.dv_hashtab)
3742 d = &current_funccal->l_vars;
3743 else
3744 {
3745 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3746 d = di->di_tv.vval.v_dict;
3747 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003748 hi = hash_find(ht, varname);
3749 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003750 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003751 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003752 if (var_check_fixed(di->di_flags, name, FALSE)
3753 || var_check_ro(di->di_flags, name, FALSE)
3754 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003755 return FAIL;
3756 delete_var(ht, hi);
3757 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760 if (forceit)
3761 return OK;
3762 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 return FAIL;
3764}
3765
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766/*
3767 * Lock or unlock variable indicated by "lp".
3768 * "deep" is the levels to go (-1 for unlimited);
3769 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3770 */
3771 static int
3772do_lock_var(lp, name_end, deep, lock)
3773 lval_T *lp;
3774 char_u *name_end;
3775 int deep;
3776 int lock;
3777{
3778 int ret = OK;
3779 int cc;
3780 dictitem_T *di;
3781
3782 if (deep == 0) /* nothing to do */
3783 return OK;
3784
3785 if (lp->ll_tv == NULL)
3786 {
3787 cc = *name_end;
3788 *name_end = NUL;
3789
3790 /* Normal name or expanded name. */
3791 if (check_changedtick(lp->ll_name))
3792 ret = FAIL;
3793 else
3794 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003795 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003796 if (di == NULL)
3797 ret = FAIL;
3798 else
3799 {
3800 if (lock)
3801 di->di_flags |= DI_FLAGS_LOCK;
3802 else
3803 di->di_flags &= ~DI_FLAGS_LOCK;
3804 item_lock(&di->di_tv, deep, lock);
3805 }
3806 }
3807 *name_end = cc;
3808 }
3809 else if (lp->ll_range)
3810 {
3811 listitem_T *li = lp->ll_li;
3812
3813 /* (un)lock a range of List items. */
3814 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3815 {
3816 item_lock(&li->li_tv, deep, lock);
3817 li = li->li_next;
3818 ++lp->ll_n1;
3819 }
3820 }
3821 else if (lp->ll_list != NULL)
3822 /* (un)lock a List item. */
3823 item_lock(&lp->ll_li->li_tv, deep, lock);
3824 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003825 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003826 item_lock(&lp->ll_di->di_tv, deep, lock);
3827
3828 return ret;
3829}
3830
3831/*
3832 * Lock or unlock an item. "deep" is nr of levels to go.
3833 */
3834 static void
3835item_lock(tv, deep, lock)
3836 typval_T *tv;
3837 int deep;
3838 int lock;
3839{
3840 static int recurse = 0;
3841 list_T *l;
3842 listitem_T *li;
3843 dict_T *d;
3844 hashitem_T *hi;
3845 int todo;
3846
3847 if (recurse >= DICT_MAXNEST)
3848 {
3849 EMSG(_("E743: variable nested too deep for (un)lock"));
3850 return;
3851 }
3852 if (deep == 0)
3853 return;
3854 ++recurse;
3855
3856 /* lock/unlock the item itself */
3857 if (lock)
3858 tv->v_lock |= VAR_LOCKED;
3859 else
3860 tv->v_lock &= ~VAR_LOCKED;
3861
3862 switch (tv->v_type)
3863 {
3864 case VAR_LIST:
3865 if ((l = tv->vval.v_list) != NULL)
3866 {
3867 if (lock)
3868 l->lv_lock |= VAR_LOCKED;
3869 else
3870 l->lv_lock &= ~VAR_LOCKED;
3871 if (deep < 0 || deep > 1)
3872 /* recursive: lock/unlock the items the List contains */
3873 for (li = l->lv_first; li != NULL; li = li->li_next)
3874 item_lock(&li->li_tv, deep - 1, lock);
3875 }
3876 break;
3877 case VAR_DICT:
3878 if ((d = tv->vval.v_dict) != NULL)
3879 {
3880 if (lock)
3881 d->dv_lock |= VAR_LOCKED;
3882 else
3883 d->dv_lock &= ~VAR_LOCKED;
3884 if (deep < 0 || deep > 1)
3885 {
3886 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003887 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003888 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3889 {
3890 if (!HASHITEM_EMPTY(hi))
3891 {
3892 --todo;
3893 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3894 }
3895 }
3896 }
3897 }
3898 }
3899 --recurse;
3900}
3901
Bram Moolenaara40058a2005-07-11 22:42:07 +00003902/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003903 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3904 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003905 */
3906 static int
3907tv_islocked(tv)
3908 typval_T *tv;
3909{
3910 return (tv->v_lock & VAR_LOCKED)
3911 || (tv->v_type == VAR_LIST
3912 && tv->vval.v_list != NULL
3913 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3914 || (tv->v_type == VAR_DICT
3915 && tv->vval.v_dict != NULL
3916 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3917}
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3920/*
3921 * Delete all "menutrans_" variables.
3922 */
3923 void
3924del_menutrans_vars()
3925{
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003927 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003930 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003931 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 {
3933 if (!HASHITEM_EMPTY(hi))
3934 {
3935 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3937 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 }
3939 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941}
3942#endif
3943
3944#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3945
3946/*
3947 * Local string buffer for the next two functions to store a variable name
3948 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3949 * get_user_var_name().
3950 */
3951
3952static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3953
3954static char_u *varnamebuf = NULL;
3955static int varnamebuflen = 0;
3956
3957/*
3958 * Function to concatenate a prefix and a variable name.
3959 */
3960 static char_u *
3961cat_prefix_varname(prefix, name)
3962 int prefix;
3963 char_u *name;
3964{
3965 int len;
3966
3967 len = (int)STRLEN(name) + 3;
3968 if (len > varnamebuflen)
3969 {
3970 vim_free(varnamebuf);
3971 len += 10; /* some additional space */
3972 varnamebuf = alloc(len);
3973 if (varnamebuf == NULL)
3974 {
3975 varnamebuflen = 0;
3976 return NULL;
3977 }
3978 varnamebuflen = len;
3979 }
3980 *varnamebuf = prefix;
3981 varnamebuf[1] = ':';
3982 STRCPY(varnamebuf + 2, name);
3983 return varnamebuf;
3984}
3985
3986/*
3987 * Function given to ExpandGeneric() to obtain the list of user defined
3988 * (global/buffer/window/built-in) variable names.
3989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 char_u *
3991get_user_var_name(xp, idx)
3992 expand_T *xp;
3993 int idx;
3994{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003995 static long_u gdone;
3996 static long_u bdone;
3997 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003998#ifdef FEAT_WINDOWS
3999 static long_u tdone;
4000#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 static int vidx;
4002 static hashitem_T *hi;
4003 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004006 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004007 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004008#ifdef FEAT_WINDOWS
4009 tdone = 0;
4010#endif
4011 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004012
4013 /* Global variables */
4014 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004017 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004018 else
4019 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004020 while (HASHITEM_EMPTY(hi))
4021 ++hi;
4022 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4023 return cat_prefix_varname('g', hi->hi_key);
4024 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004026
4027 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004028 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004032 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004033 else
4034 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004035 while (HASHITEM_EMPTY(hi))
4036 ++hi;
4037 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 return (char_u *)"b:changedtick";
4043 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004044
4045 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004046 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004049 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004051 else
4052 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 while (HASHITEM_EMPTY(hi))
4054 ++hi;
4055 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004057
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004058#ifdef FEAT_WINDOWS
4059 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004060 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004061 if (tdone < ht->ht_used)
4062 {
4063 if (tdone++ == 0)
4064 hi = ht->ht_array;
4065 else
4066 ++hi;
4067 while (HASHITEM_EMPTY(hi))
4068 ++hi;
4069 return cat_prefix_varname('t', hi->hi_key);
4070 }
4071#endif
4072
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 /* v: variables */
4074 if (vidx < VV_LEN)
4075 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 vim_free(varnamebuf);
4078 varnamebuf = NULL;
4079 varnamebuflen = 0;
4080 return NULL;
4081}
4082
4083#endif /* FEAT_CMDL_COMPL */
4084
4085/*
4086 * types for expressions.
4087 */
4088typedef enum
4089{
4090 TYPE_UNKNOWN = 0
4091 , TYPE_EQUAL /* == */
4092 , TYPE_NEQUAL /* != */
4093 , TYPE_GREATER /* > */
4094 , TYPE_GEQUAL /* >= */
4095 , TYPE_SMALLER /* < */
4096 , TYPE_SEQUAL /* <= */
4097 , TYPE_MATCH /* =~ */
4098 , TYPE_NOMATCH /* !~ */
4099} exptype_T;
4100
4101/*
4102 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4105 */
4106
4107/*
4108 * Handle zero level expression.
4109 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004110 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004111 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 * Return OK or FAIL.
4113 */
4114 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 char_u **nextcmd;
4119 int evaluate;
4120{
4121 int ret;
4122 char_u *p;
4123
4124 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (ret == FAIL || !ends_excmd(*p))
4127 {
4128 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 /*
4131 * Report the invalid expression unless the expression evaluation has
4132 * been cancelled due to an aborting error, an interrupt, or an
4133 * exception.
4134 */
4135 if (!aborting())
4136 EMSG2(_(e_invexpr2), arg);
4137 ret = FAIL;
4138 }
4139 if (nextcmd != NULL)
4140 *nextcmd = check_nextcmd(p);
4141
4142 return ret;
4143}
4144
4145/*
4146 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004147 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 *
4149 * "arg" must point to the first non-white of the expression.
4150 * "arg" is advanced to the next non-white after the recognized expression.
4151 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004152 * Note: "rettv.v_lock" is not set.
4153 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 * Return OK or FAIL.
4155 */
4156 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 int evaluate;
4161{
4162 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004163 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
4165 /*
4166 * Get the first variable.
4167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 return FAIL;
4170
4171 if ((*arg)[0] == '?')
4172 {
4173 result = FALSE;
4174 if (evaluate)
4175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176 int error = FALSE;
4177
4178 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 if (error)
4182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184
4185 /*
4186 * Get the second variable.
4187 */
4188 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 return FAIL;
4191
4192 /*
4193 * Check for the ":".
4194 */
4195 if ((*arg)[0] != ':')
4196 {
4197 EMSG(_("E109: Missing ':' after '?'"));
4198 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 return FAIL;
4201 }
4202
4203 /*
4204 * Get the third variable.
4205 */
4206 *arg = skipwhite(*arg + 1);
4207 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4208 {
4209 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 return FAIL;
4212 }
4213 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216
4217 return OK;
4218}
4219
4220/*
4221 * Handle first level expression:
4222 * expr2 || expr2 || expr2 logical OR
4223 *
4224 * "arg" must point to the first non-white of the expression.
4225 * "arg" is advanced to the next non-white after the recognized expression.
4226 *
4227 * Return OK or FAIL.
4228 */
4229 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 int evaluate;
4234{
Bram Moolenaar33570922005-01-25 22:26:29 +00004235 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 long result;
4237 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 /*
4241 * Get the first variable.
4242 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 return FAIL;
4245
4246 /*
4247 * Repeat until there is no following "||".
4248 */
4249 first = TRUE;
4250 result = FALSE;
4251 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4252 {
4253 if (evaluate && first)
4254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (error)
4259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 first = FALSE;
4261 }
4262
4263 /*
4264 * Get the second variable.
4265 */
4266 *arg = skipwhite(*arg + 2);
4267 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4268 return FAIL;
4269
4270 /*
4271 * Compute the result.
4272 */
4273 if (evaluate && !result)
4274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (error)
4279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 if (evaluate)
4282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 rettv->v_type = VAR_NUMBER;
4284 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
4288 return OK;
4289}
4290
4291/*
4292 * Handle second level expression:
4293 * expr3 && expr3 && expr3 logical AND
4294 *
4295 * "arg" must point to the first non-white of the expression.
4296 * "arg" is advanced to the next non-white after the recognized expression.
4297 *
4298 * Return OK or FAIL.
4299 */
4300 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 int evaluate;
4305{
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 long result;
4308 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 /*
4318 * Repeat until there is no following "&&".
4319 */
4320 first = TRUE;
4321 result = TRUE;
4322 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4323 {
4324 if (evaluate && first)
4325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (error)
4330 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 first = FALSE;
4332 }
4333
4334 /*
4335 * Get the second variable.
4336 */
4337 *arg = skipwhite(*arg + 2);
4338 if (eval4(arg, &var2, evaluate && result) == FAIL)
4339 return FAIL;
4340
4341 /*
4342 * Compute the result.
4343 */
4344 if (evaluate && result)
4345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (error)
4350 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 if (evaluate)
4353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 rettv->v_type = VAR_NUMBER;
4355 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
4358
4359 return OK;
4360}
4361
4362/*
4363 * Handle third level expression:
4364 * var1 == var2
4365 * var1 =~ var2
4366 * var1 != var2
4367 * var1 !~ var2
4368 * var1 > var2
4369 * var1 >= var2
4370 * var1 < var2
4371 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004372 * var1 is var2
4373 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 *
4375 * "arg" must point to the first non-white of the expression.
4376 * "arg" is advanced to the next non-white after the recognized expression.
4377 *
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 int evaluate;
4385{
Bram Moolenaar33570922005-01-25 22:26:29 +00004386 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 char_u *p;
4388 int i;
4389 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 int len = 2;
4392 long n1, n2;
4393 char_u *s1, *s2;
4394 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4395 regmatch_T regmatch;
4396 int ic;
4397 char_u *save_cpo;
4398
4399 /*
4400 * Get the first variable.
4401 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 return FAIL;
4404
4405 p = *arg;
4406 switch (p[0])
4407 {
4408 case '=': if (p[1] == '=')
4409 type = TYPE_EQUAL;
4410 else if (p[1] == '~')
4411 type = TYPE_MATCH;
4412 break;
4413 case '!': if (p[1] == '=')
4414 type = TYPE_NEQUAL;
4415 else if (p[1] == '~')
4416 type = TYPE_NOMATCH;
4417 break;
4418 case '>': if (p[1] != '=')
4419 {
4420 type = TYPE_GREATER;
4421 len = 1;
4422 }
4423 else
4424 type = TYPE_GEQUAL;
4425 break;
4426 case '<': if (p[1] != '=')
4427 {
4428 type = TYPE_SMALLER;
4429 len = 1;
4430 }
4431 else
4432 type = TYPE_SEQUAL;
4433 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 case 'i': if (p[1] == 's')
4435 {
4436 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4437 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004438 i = p[len];
4439 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 {
4441 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4442 type_is = TRUE;
4443 }
4444 }
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447
4448 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004449 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 */
4451 if (type != TYPE_UNKNOWN)
4452 {
4453 /* extra question mark appended: ignore case */
4454 if (p[len] == '?')
4455 {
4456 ic = TRUE;
4457 ++len;
4458 }
4459 /* extra '#' appended: match case */
4460 else if (p[len] == '#')
4461 {
4462 ic = FALSE;
4463 ++len;
4464 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004465 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 else
4467 ic = p_ic;
4468
4469 /*
4470 * Get the second variable.
4471 */
4472 *arg = skipwhite(p + len);
4473 if (eval5(arg, &var2, evaluate) == FAIL)
4474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FAIL;
4477 }
4478
4479 if (evaluate)
4480 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 if (type_is && rettv->v_type != var2.v_type)
4482 {
4483 /* For "is" a different type always means FALSE, for "notis"
4484 * it means TRUE. */
4485 n1 = (type == TYPE_NEQUAL);
4486 }
4487 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4488 {
4489 if (type_is)
4490 {
4491 n1 = (rettv->v_type == var2.v_type
4492 && rettv->vval.v_list == var2.vval.v_list);
4493 if (type == TYPE_NEQUAL)
4494 n1 = !n1;
4495 }
4496 else if (rettv->v_type != var2.v_type
4497 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4498 {
4499 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004500 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004501 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004502 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 clear_tv(rettv);
4504 clear_tv(&var2);
4505 return FAIL;
4506 }
4507 else
4508 {
4509 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004510 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4511 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 if (type == TYPE_NEQUAL)
4513 n1 = !n1;
4514 }
4515 }
4516
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004517 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4518 {
4519 if (type_is)
4520 {
4521 n1 = (rettv->v_type == var2.v_type
4522 && rettv->vval.v_dict == var2.vval.v_dict);
4523 if (type == TYPE_NEQUAL)
4524 n1 = !n1;
4525 }
4526 else if (rettv->v_type != var2.v_type
4527 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4528 {
4529 if (rettv->v_type != var2.v_type)
4530 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4531 else
4532 EMSG(_("E736: Invalid operation for Dictionary"));
4533 clear_tv(rettv);
4534 clear_tv(&var2);
4535 return FAIL;
4536 }
4537 else
4538 {
4539 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004540 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4541 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004542 if (type == TYPE_NEQUAL)
4543 n1 = !n1;
4544 }
4545 }
4546
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004547 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4548 {
4549 if (rettv->v_type != var2.v_type
4550 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4551 {
4552 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004553 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004555 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 clear_tv(rettv);
4557 clear_tv(&var2);
4558 return FAIL;
4559 }
4560 else
4561 {
4562 /* Compare two Funcrefs for being equal or unequal. */
4563 if (rettv->vval.v_string == NULL
4564 || var2.vval.v_string == NULL)
4565 n1 = FALSE;
4566 else
4567 n1 = STRCMP(rettv->vval.v_string,
4568 var2.vval.v_string) == 0;
4569 if (type == TYPE_NEQUAL)
4570 n1 = !n1;
4571 }
4572 }
4573
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004574#ifdef FEAT_FLOAT
4575 /*
4576 * If one of the two variables is a float, compare as a float.
4577 * When using "=~" or "!~", always compare as string.
4578 */
4579 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4580 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4581 {
4582 float_T f1, f2;
4583
4584 if (rettv->v_type == VAR_FLOAT)
4585 f1 = rettv->vval.v_float;
4586 else
4587 f1 = get_tv_number(rettv);
4588 if (var2.v_type == VAR_FLOAT)
4589 f2 = var2.vval.v_float;
4590 else
4591 f2 = get_tv_number(&var2);
4592 n1 = FALSE;
4593 switch (type)
4594 {
4595 case TYPE_EQUAL: n1 = (f1 == f2); break;
4596 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4597 case TYPE_GREATER: n1 = (f1 > f2); break;
4598 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4599 case TYPE_SMALLER: n1 = (f1 < f2); break;
4600 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4601 case TYPE_UNKNOWN:
4602 case TYPE_MATCH:
4603 case TYPE_NOMATCH: break; /* avoid gcc warning */
4604 }
4605 }
4606#endif
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /*
4609 * If one of the two variables is a number, compare as a number.
4610 * When using "=~" or "!~", always compare as string.
4611 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004612 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 n1 = get_tv_number(rettv);
4616 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 switch (type)
4618 {
4619 case TYPE_EQUAL: n1 = (n1 == n2); break;
4620 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4621 case TYPE_GREATER: n1 = (n1 > n2); break;
4622 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4623 case TYPE_SMALLER: n1 = (n1 < n2); break;
4624 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4625 case TYPE_UNKNOWN:
4626 case TYPE_MATCH:
4627 case TYPE_NOMATCH: break; /* avoid gcc warning */
4628 }
4629 }
4630 else
4631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 s1 = get_tv_string_buf(rettv, buf1);
4633 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4635 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4636 else
4637 i = 0;
4638 n1 = FALSE;
4639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (i == 0); break;
4642 case TYPE_NEQUAL: n1 = (i != 0); break;
4643 case TYPE_GREATER: n1 = (i > 0); break;
4644 case TYPE_GEQUAL: n1 = (i >= 0); break;
4645 case TYPE_SMALLER: n1 = (i < 0); break;
4646 case TYPE_SEQUAL: n1 = (i <= 0); break;
4647
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH:
4650 /* avoid 'l' flag in 'cpoptions' */
4651 save_cpo = p_cpo;
4652 p_cpo = (char_u *)"";
4653 regmatch.regprog = vim_regcomp(s2,
4654 RE_MAGIC + RE_STRING);
4655 regmatch.rm_ic = ic;
4656 if (regmatch.regprog != NULL)
4657 {
4658 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004659 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (type == TYPE_NOMATCH)
4661 n1 = !n1;
4662 }
4663 p_cpo = save_cpo;
4664 break;
4665
4666 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4667 }
4668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
4670 clear_tv(&var2);
4671 rettv->v_type = VAR_NUMBER;
4672 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 }
4675
4676 return OK;
4677}
4678
4679/*
4680 * Handle fourth level expression:
4681 * + number addition
4682 * - number subtraction
4683 * . string concatenation
4684 *
4685 * "arg" must point to the first non-white of the expression.
4686 * "arg" is advanced to the next non-white after the recognized expression.
4687 *
4688 * Return OK or FAIL.
4689 */
4690 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int evaluate;
4695{
Bram Moolenaar33570922005-01-25 22:26:29 +00004696 typval_T var2;
4697 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 int op;
4699 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004700#ifdef FEAT_FLOAT
4701 float_T f1 = 0, f2 = 0;
4702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 char_u *s1, *s2;
4704 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4705 char_u *p;
4706
4707 /*
4708 * Get the first variable.
4709 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004710 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 return FAIL;
4712
4713 /*
4714 * Repeat computing, until no '+', '-' or '.' is following.
4715 */
4716 for (;;)
4717 {
4718 op = **arg;
4719 if (op != '+' && op != '-' && op != '.')
4720 break;
4721
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 if ((op != '+' || rettv->v_type != VAR_LIST)
4723#ifdef FEAT_FLOAT
4724 && (op == '.' || rettv->v_type != VAR_FLOAT)
4725#endif
4726 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 {
4728 /* For "list + ...", an illegal use of the first operand as
4729 * a number cannot be determined before evaluating the 2nd
4730 * operand: if this is also a list, all is ok.
4731 * For "something . ...", "something - ..." or "non-list + ...",
4732 * we know that the first operand needs to be a string or number
4733 * without evaluating the 2nd operand. So check before to avoid
4734 * side effects after an error. */
4735 if (evaluate && get_tv_string_chk(rettv) == NULL)
4736 {
4737 clear_tv(rettv);
4738 return FAIL;
4739 }
4740 }
4741
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 /*
4743 * Get the second variable.
4744 */
4745 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004746 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 return FAIL;
4750 }
4751
4752 if (evaluate)
4753 {
4754 /*
4755 * Compute the result.
4756 */
4757 if (op == '.')
4758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4760 s2 = get_tv_string_buf_chk(&var2, buf2);
4761 if (s2 == NULL) /* type error ? */
4762 {
4763 clear_tv(rettv);
4764 clear_tv(&var2);
4765 return FAIL;
4766 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004767 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 clear_tv(rettv);
4769 rettv->v_type = VAR_STRING;
4770 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004772 else if (op == '+' && rettv->v_type == VAR_LIST
4773 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004774 {
4775 /* concatenate Lists */
4776 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4777 &var3) == FAIL)
4778 {
4779 clear_tv(rettv);
4780 clear_tv(&var2);
4781 return FAIL;
4782 }
4783 clear_tv(rettv);
4784 *rettv = var3;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 else
4787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 int error = FALSE;
4789
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790#ifdef FEAT_FLOAT
4791 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793 f1 = rettv->vval.v_float;
4794 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 else
4797#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004798 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004799 n1 = get_tv_number_chk(rettv, &error);
4800 if (error)
4801 {
4802 /* This can only happen for "list + non-list". For
4803 * "non-list + ..." or "something - ...", we returned
4804 * before evaluating the 2nd operand. */
4805 clear_tv(rettv);
4806 return FAIL;
4807 }
4808#ifdef FEAT_FLOAT
4809 if (var2.v_type == VAR_FLOAT)
4810 f1 = n1;
4811#endif
4812 }
4813#ifdef FEAT_FLOAT
4814 if (var2.v_type == VAR_FLOAT)
4815 {
4816 f2 = var2.vval.v_float;
4817 n2 = 0;
4818 }
4819 else
4820#endif
4821 {
4822 n2 = get_tv_number_chk(&var2, &error);
4823 if (error)
4824 {
4825 clear_tv(rettv);
4826 clear_tv(&var2);
4827 return FAIL;
4828 }
4829#ifdef FEAT_FLOAT
4830 if (rettv->v_type == VAR_FLOAT)
4831 f2 = n2;
4832#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004835
4836#ifdef FEAT_FLOAT
4837 /* If there is a float on either side the result is a float. */
4838 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4839 {
4840 if (op == '+')
4841 f1 = f1 + f2;
4842 else
4843 f1 = f1 - f2;
4844 rettv->v_type = VAR_FLOAT;
4845 rettv->vval.v_float = f1;
4846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004848#endif
4849 {
4850 if (op == '+')
4851 n1 = n1 + n2;
4852 else
4853 n1 = n1 - n2;
4854 rettv->v_type = VAR_NUMBER;
4855 rettv->vval.v_number = n1;
4856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 }
4861 return OK;
4862}
4863
4864/*
4865 * Handle fifth level expression:
4866 * * number multiplication
4867 * / number division
4868 * % number modulo
4869 *
4870 * "arg" must point to the first non-white of the expression.
4871 * "arg" is advanced to the next non-white after the recognized expression.
4872 *
4873 * Return OK or FAIL.
4874 */
4875 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004876eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004880 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
Bram Moolenaar33570922005-01-25 22:26:29 +00004882 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 int op;
4884 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#ifdef FEAT_FLOAT
4886 int use_float = FALSE;
4887 float_T f1 = 0, f2;
4888#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004889 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
4891 /*
4892 * Get the first variable.
4893 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004894 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 return FAIL;
4896
4897 /*
4898 * Repeat computing, until no '*', '/' or '%' is following.
4899 */
4900 for (;;)
4901 {
4902 op = **arg;
4903 if (op != '*' && op != '/' && op != '%')
4904 break;
4905
4906 if (evaluate)
4907 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908#ifdef FEAT_FLOAT
4909 if (rettv->v_type == VAR_FLOAT)
4910 {
4911 f1 = rettv->vval.v_float;
4912 use_float = TRUE;
4913 n1 = 0;
4914 }
4915 else
4916#endif
4917 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004919 if (error)
4920 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922 else
4923 n1 = 0;
4924
4925 /*
4926 * Get the second variable.
4927 */
4928 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004929 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return FAIL;
4931
4932 if (evaluate)
4933 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004934#ifdef FEAT_FLOAT
4935 if (var2.v_type == VAR_FLOAT)
4936 {
4937 if (!use_float)
4938 {
4939 f1 = n1;
4940 use_float = TRUE;
4941 }
4942 f2 = var2.vval.v_float;
4943 n2 = 0;
4944 }
4945 else
4946#endif
4947 {
4948 n2 = get_tv_number_chk(&var2, &error);
4949 clear_tv(&var2);
4950 if (error)
4951 return FAIL;
4952#ifdef FEAT_FLOAT
4953 if (use_float)
4954 f2 = n2;
4955#endif
4956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958 /*
4959 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962#ifdef FEAT_FLOAT
4963 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965 if (op == '*')
4966 f1 = f1 * f2;
4967 else if (op == '/')
4968 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004969# ifdef VMS
4970 /* VMS crashes on divide by zero, work around it */
4971 if (f2 == 0.0)
4972 {
4973 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004976 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004977 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004978 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979 }
4980 else
4981 f1 = f1 / f2;
4982# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 /* We rely on the floating point library to handle divide
4984 * by zero to result in "inf" and not a crash. */
4985 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004986# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004990 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 return FAIL;
4992 }
4993 rettv->v_type = VAR_FLOAT;
4994 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 if (op == '*')
5000 n1 = n1 * n2;
5001 else if (op == '/')
5002 {
5003 if (n2 == 0) /* give an error message? */
5004 {
5005 if (n1 == 0)
5006 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5007 else if (n1 < 0)
5008 n1 = -0x7fffffffL;
5009 else
5010 n1 = 0x7fffffffL;
5011 }
5012 else
5013 n1 = n1 / n2;
5014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005016 {
5017 if (n2 == 0) /* give an error message? */
5018 n1 = 0;
5019 else
5020 n1 = n1 % n2;
5021 }
5022 rettv->v_type = VAR_NUMBER;
5023 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 }
5027
5028 return OK;
5029}
5030
5031/*
5032 * Handle sixth level expression:
5033 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005034 * "string" string constant
5035 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 * &option-name option value
5037 * @r register contents
5038 * identifier variable value
5039 * function() function call
5040 * $VAR environment variable
5041 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005042 * [expr, expr] List
5043 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *
5045 * Also handle:
5046 * ! in front logical NOT
5047 * - in front unary minus
5048 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 * trailing [] subscript in String or List
5050 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 *
5052 * "arg" must point to the first non-white of the expression.
5053 * "arg" is advanced to the next non-white after the recognized expression.
5054 *
5055 * Return OK or FAIL.
5056 */
5057 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005058eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005062 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 long n;
5065 int len;
5066 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 char_u *start_leader, *end_leader;
5068 int ret = OK;
5069 char_u *alias;
5070
5071 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077 /*
5078 * Skip '!' and '-' characters. They are handled later.
5079 */
5080 start_leader = *arg;
5081 while (**arg == '!' || **arg == '-' || **arg == '+')
5082 *arg = skipwhite(*arg + 1);
5083 end_leader = *arg;
5084
5085 switch (**arg)
5086 {
5087 /*
5088 * Number constant.
5089 */
5090 case '0':
5091 case '1':
5092 case '2':
5093 case '3':
5094 case '4':
5095 case '5':
5096 case '6':
5097 case '7':
5098 case '8':
5099 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100 {
5101#ifdef FEAT_FLOAT
5102 char_u *p = skipdigits(*arg + 1);
5103 int get_float = FALSE;
5104
5105 /* We accept a float when the format matches
5106 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005107 * strict to avoid backwards compatibility problems.
5108 * Don't look for a float after the "." operator, so that
5109 * ":let vers = 1.2.3" doesn't fail. */
5110 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005112 get_float = TRUE;
5113 p = skipdigits(p + 2);
5114 if (*p == 'e' || *p == 'E')
5115 {
5116 ++p;
5117 if (*p == '-' || *p == '+')
5118 ++p;
5119 if (!vim_isdigit(*p))
5120 get_float = FALSE;
5121 else
5122 p = skipdigits(p + 1);
5123 }
5124 if (ASCII_ISALPHA(*p) || *p == '.')
5125 get_float = FALSE;
5126 }
5127 if (get_float)
5128 {
5129 float_T f;
5130
5131 *arg += string2float(*arg, &f);
5132 if (evaluate)
5133 {
5134 rettv->v_type = VAR_FLOAT;
5135 rettv->vval.v_float = f;
5136 }
5137 }
5138 else
5139#endif
5140 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005141 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005142 *arg += len;
5143 if (evaluate)
5144 {
5145 rettv->v_type = VAR_NUMBER;
5146 rettv->vval.v_number = n;
5147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151
5152 /*
5153 * String constant: "string".
5154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005155 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 break;
5157
5158 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 break;
5163
5164 /*
5165 * List: [expr, expr]
5166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169
5170 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 * Dictionary: {key: val, key: val}
5172 */
5173 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5174 break;
5175
5176 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005179 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /*
5183 * Environment variable: $VAR.
5184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /*
5189 * Register contents: @r.
5190 */
5191 case '@': ++*arg;
5192 if (evaluate)
5193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005194 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005195 rettv->vval.v_string = get_reg_contents(**arg,
5196 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198 if (**arg != NUL)
5199 ++*arg;
5200 break;
5201
5202 /*
5203 * nested expression: (expression).
5204 */
5205 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (**arg == ')')
5208 ++*arg;
5209 else if (ret == OK)
5210 {
5211 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 ret = FAIL;
5214 }
5215 break;
5216
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220
5221 if (ret == NOTDONE)
5222 {
5223 /*
5224 * Must be a variable or function name.
5225 * Can also be a curly-braces kind of name: {expr}.
5226 */
5227 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 if (alias != NULL)
5230 s = alias;
5231
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005232 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 ret = FAIL;
5234 else
5235 {
5236 if (**arg == '(') /* recursive! */
5237 {
5238 /* If "s" is the name of a variable of type VAR_FUNC
5239 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005240 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241
5242 /* Invoke the function. */
5243 ret = get_func_tv(s, len, rettv, arg,
5244 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005245 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005246
5247 /* If evaluate is FALSE rettv->v_type was not set in
5248 * get_func_tv, but it's needed in handle_subscript() to parse
5249 * what follows. So set it here. */
5250 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5251 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005252 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005253 rettv->v_type = VAR_FUNC;
5254 }
5255
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 /* Stop the expression evaluation when immediately
5257 * aborting on error, or when an interrupt occurred or
5258 * an exception was thrown but not caught. */
5259 if (aborting())
5260 {
5261 if (ret == OK)
5262 clear_tv(rettv);
5263 ret = FAIL;
5264 }
5265 }
5266 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005267 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005268 else
5269 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005271 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 }
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 *arg = skipwhite(*arg);
5275
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5277 * expr(expr). */
5278 if (ret == OK)
5279 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
5281 /*
5282 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5283 */
5284 if (ret == OK && evaluate && end_leader > start_leader)
5285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 int val = 0;
5288#ifdef FEAT_FLOAT
5289 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005291 if (rettv->v_type == VAR_FLOAT)
5292 f = rettv->vval.v_float;
5293 else
5294#endif
5295 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 clear_tv(rettv);
5299 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005301 else
5302 {
5303 while (end_leader > start_leader)
5304 {
5305 --end_leader;
5306 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005307 {
5308#ifdef FEAT_FLOAT
5309 if (rettv->v_type == VAR_FLOAT)
5310 f = !f;
5311 else
5312#endif
5313 val = !val;
5314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005316 {
5317#ifdef FEAT_FLOAT
5318 if (rettv->v_type == VAR_FLOAT)
5319 f = -f;
5320 else
5321#endif
5322 val = -val;
5323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005325#ifdef FEAT_FLOAT
5326 if (rettv->v_type == VAR_FLOAT)
5327 {
5328 clear_tv(rettv);
5329 rettv->vval.v_float = f;
5330 }
5331 else
5332#endif
5333 {
5334 clear_tv(rettv);
5335 rettv->v_type = VAR_NUMBER;
5336 rettv->vval.v_number = val;
5337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340
5341 return ret;
5342}
5343
5344/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005345 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5346 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5348 */
5349 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005350eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005354 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355{
5356 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005357 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 long len = -1;
5360 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005364 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 if (verbose)
5367 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 return FAIL;
5369 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005370#ifdef FEAT_FLOAT
5371 else if (rettv->v_type == VAR_FLOAT)
5372 {
5373 if (verbose)
5374 EMSG(_(e_float_as_string));
5375 return FAIL;
5376 }
5377#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005379 init_tv(&var1);
5380 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005381 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383 /*
5384 * dict.name
5385 */
5386 key = *arg + 1;
5387 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5388 ;
5389 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 }
5393 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 /*
5396 * something[idx]
5397 *
5398 * Get the (first) variable from inside the [].
5399 */
5400 *arg = skipwhite(*arg + 1);
5401 if (**arg == ':')
5402 empty1 = TRUE;
5403 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5404 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005405 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5406 {
5407 /* not a number or string */
5408 clear_tv(&var1);
5409 return FAIL;
5410 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411
5412 /*
5413 * Get the second variable from inside the [:].
5414 */
5415 if (**arg == ':')
5416 {
5417 range = TRUE;
5418 *arg = skipwhite(*arg + 1);
5419 if (**arg == ']')
5420 empty2 = TRUE;
5421 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005423 if (!empty1)
5424 clear_tv(&var1);
5425 return FAIL;
5426 }
5427 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5428 {
5429 /* not a number or string */
5430 if (!empty1)
5431 clear_tv(&var1);
5432 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 return FAIL;
5434 }
5435 }
5436
5437 /* Check for the ']'. */
5438 if (**arg != ']')
5439 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005440 if (verbose)
5441 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442 clear_tv(&var1);
5443 if (range)
5444 clear_tv(&var2);
5445 return FAIL;
5446 }
5447 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 }
5449
5450 if (evaluate)
5451 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452 n1 = 0;
5453 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005455 n1 = get_tv_number(&var1);
5456 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 if (range)
5459 {
5460 if (empty2)
5461 n2 = -1;
5462 else
5463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 n2 = get_tv_number(&var2);
5465 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 }
5467 }
5468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 {
5471 case VAR_NUMBER:
5472 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 len = (long)STRLEN(s);
5475 if (range)
5476 {
5477 /* The resulting variable is a substring. If the indexes
5478 * are out of range the result is empty. */
5479 if (n1 < 0)
5480 {
5481 n1 = len + n1;
5482 if (n1 < 0)
5483 n1 = 0;
5484 }
5485 if (n2 < 0)
5486 n2 = len + n2;
5487 else if (n2 >= len)
5488 n2 = len;
5489 if (n1 >= len || n2 < 0 || n1 > n2)
5490 s = NULL;
5491 else
5492 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5493 }
5494 else
5495 {
5496 /* The resulting variable is a string of a single
5497 * character. If the index is too big or negative the
5498 * result is empty. */
5499 if (n1 >= len || n1 < 0)
5500 s = NULL;
5501 else
5502 s = vim_strnsave(s + n1, 1);
5503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 clear_tv(rettv);
5505 rettv->v_type = VAR_STRING;
5506 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 break;
5508
5509 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 if (n1 < 0)
5512 n1 = len + n1;
5513 if (!empty1 && (n1 < 0 || n1 >= len))
5514 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005515 /* For a range we allow invalid values and return an empty
5516 * list. A list index out of range is an error. */
5517 if (!range)
5518 {
5519 if (verbose)
5520 EMSGN(_(e_listidx), n1);
5521 return FAIL;
5522 }
5523 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 }
5525 if (range)
5526 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005527 list_T *l;
5528 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529
5530 if (n2 < 0)
5531 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005532 else if (n2 >= len)
5533 n2 = len - 1;
5534 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005535 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 l = list_alloc();
5537 if (l == NULL)
5538 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 n1 <= n2; ++n1)
5541 {
5542 if (list_append_tv(l, &item->li_tv) == FAIL)
5543 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005544 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 return FAIL;
5546 }
5547 item = item->li_next;
5548 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 clear_tv(rettv);
5550 rettv->v_type = VAR_LIST;
5551 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005552 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 }
5554 else
5555 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005556 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 clear_tv(rettv);
5558 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 }
5560 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561
5562 case VAR_DICT:
5563 if (range)
5564 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005565 if (verbose)
5566 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567 if (len == -1)
5568 clear_tv(&var1);
5569 return FAIL;
5570 }
5571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573
5574 if (len == -1)
5575 {
5576 key = get_tv_string(&var1);
5577 if (*key == NUL)
5578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005579 if (verbose)
5580 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 clear_tv(&var1);
5582 return FAIL;
5583 }
5584 }
5585
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005586 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005588 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005589 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590 if (len == -1)
5591 clear_tv(&var1);
5592 if (item == NULL)
5593 return FAIL;
5594
5595 copy_tv(&item->di_tv, &var1);
5596 clear_tv(rettv);
5597 *rettv = var1;
5598 }
5599 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 }
5601 }
5602
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603 return OK;
5604}
5605
5606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 * Get an option value.
5608 * "arg" points to the '&' or '+' before the option name.
5609 * "arg" is advanced to character after the option name.
5610 * Return OK or FAIL.
5611 */
5612 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005615 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 int evaluate;
5617{
5618 char_u *option_end;
5619 long numval;
5620 char_u *stringval;
5621 int opt_type;
5622 int c;
5623 int working = (**arg == '+'); /* has("+option") */
5624 int ret = OK;
5625 int opt_flags;
5626
5627 /*
5628 * Isolate the option name and find its value.
5629 */
5630 option_end = find_option_end(arg, &opt_flags);
5631 if (option_end == NULL)
5632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 EMSG2(_("E112: Option name missing: %s"), *arg);
5635 return FAIL;
5636 }
5637
5638 if (!evaluate)
5639 {
5640 *arg = option_end;
5641 return OK;
5642 }
5643
5644 c = *option_end;
5645 *option_end = NUL;
5646 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 if (opt_type == -3) /* invalid name */
5650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 EMSG2(_("E113: Unknown option: %s"), *arg);
5653 ret = FAIL;
5654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
5657 if (opt_type == -2) /* hidden string option */
5658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv->v_type = VAR_STRING;
5660 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 }
5662 else if (opt_type == -1) /* hidden number option */
5663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 rettv->v_type = VAR_NUMBER;
5665 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
5667 else if (opt_type == 1) /* number option */
5668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 rettv->v_type = VAR_NUMBER;
5670 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672 else /* string option */
5673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 rettv->v_type = VAR_STRING;
5675 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 }
5678 else if (working && (opt_type == -2 || opt_type == -1))
5679 ret = FAIL;
5680
5681 *option_end = c; /* put back for error messages */
5682 *arg = option_end;
5683
5684 return ret;
5685}
5686
5687/*
5688 * Allocate a variable for a string constant.
5689 * Return OK or FAIL.
5690 */
5691 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 int evaluate;
5696{
5697 char_u *p;
5698 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 int extra = 0;
5700
5701 /*
5702 * Find the end of the string, skipping backslashed characters.
5703 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
5706 if (*p == '\\' && p[1] != NUL)
5707 {
5708 ++p;
5709 /* A "\<x>" form occupies at least 4 characters, and produces up
5710 * to 6 characters: reserve space for 2 extra */
5711 if (*p == '<')
5712 extra += 2;
5713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715
5716 if (*p != '"')
5717 {
5718 EMSG2(_("E114: Missing quote: %s"), *arg);
5719 return FAIL;
5720 }
5721
5722 /* If only parsing, set *arg and return here */
5723 if (!evaluate)
5724 {
5725 *arg = p + 1;
5726 return OK;
5727 }
5728
5729 /*
5730 * Copy the string into allocated memory, handling backslashed
5731 * characters.
5732 */
5733 name = alloc((unsigned)(p - *arg + extra));
5734 if (name == NULL)
5735 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 rettv->v_type = VAR_STRING;
5737 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
5741 if (*p == '\\')
5742 {
5743 switch (*++p)
5744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 case 'b': *name++ = BS; ++p; break;
5746 case 'e': *name++ = ESC; ++p; break;
5747 case 'f': *name++ = FF; ++p; break;
5748 case 'n': *name++ = NL; ++p; break;
5749 case 'r': *name++ = CAR; ++p; break;
5750 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
5752 case 'X': /* hex: "\x1", "\x12" */
5753 case 'x':
5754 case 'u': /* Unicode: "\u0023" */
5755 case 'U':
5756 if (vim_isxdigit(p[1]))
5757 {
5758 int n, nr;
5759 int c = toupper(*p);
5760
5761 if (c == 'X')
5762 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005763 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005765 else
5766 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 nr = 0;
5768 while (--n >= 0 && vim_isxdigit(p[1]))
5769 {
5770 ++p;
5771 nr = (nr << 4) + hex2nr(*p);
5772 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774#ifdef FEAT_MBYTE
5775 /* For "\u" store the number according to
5776 * 'encoding'. */
5777 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 else
5780#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 break;
5784
5785 /* octal: "\1", "\12", "\123" */
5786 case '0':
5787 case '1':
5788 case '2':
5789 case '3':
5790 case '4':
5791 case '5':
5792 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 case '7': *name = *p++ - '0';
5794 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 *name = (*name << 3) + *p++ - '0';
5797 if (*p >= '0' && *p <= '7')
5798 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 break;
5802
5803 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 if (extra != 0)
5806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 break;
5809 }
5810 /* FALLTHROUGH */
5811
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814 }
5815 }
5816 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 *arg = p + 1;
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 return OK;
5824}
5825
5826/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 * Return OK or FAIL.
5829 */
5830 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005831get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 int evaluate;
5835{
5836 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 int reduce = 0;
5839
5840 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 */
5843 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5844 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 break;
5849 ++reduce;
5850 ++p;
5851 }
5852 }
5853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 return FAIL;
5858 }
5859
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 if (!evaluate)
5862 {
5863 *arg = p + 1;
5864 return OK;
5865 }
5866
5867 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 */
5870 str = alloc((unsigned)((p - *arg) - reduce));
5871 if (str == NULL)
5872 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 rettv->v_type = VAR_STRING;
5874 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 break;
5882 ++p;
5883 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 *arg = p + 1;
5888
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 return OK;
5890}
5891
5892/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893 * Allocate a variable for a List and fill it from "*arg".
5894 * Return OK or FAIL.
5895 */
5896 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005897get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005899 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 int evaluate;
5901{
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 list_T *l = NULL;
5903 typval_T tv;
5904 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905
5906 if (evaluate)
5907 {
5908 l = list_alloc();
5909 if (l == NULL)
5910 return FAIL;
5911 }
5912
5913 *arg = skipwhite(*arg + 1);
5914 while (**arg != ']' && **arg != NUL)
5915 {
5916 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5917 goto failret;
5918 if (evaluate)
5919 {
5920 item = listitem_alloc();
5921 if (item != NULL)
5922 {
5923 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005924 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 list_append(l, item);
5926 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005927 else
5928 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 }
5930
5931 if (**arg == ']')
5932 break;
5933 if (**arg != ',')
5934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 goto failret;
5937 }
5938 *arg = skipwhite(*arg + 1);
5939 }
5940
5941 if (**arg != ']')
5942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944failret:
5945 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005946 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 return FAIL;
5948 }
5949
5950 *arg = skipwhite(*arg + 1);
5951 if (evaluate)
5952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005953 rettv->v_type = VAR_LIST;
5954 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 ++l->lv_refcount;
5956 }
5957
5958 return OK;
5959}
5960
5961/*
5962 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005963 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005965 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966list_alloc()
5967{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005968 list_T *l;
5969
5970 l = (list_T *)alloc_clear(sizeof(list_T));
5971 if (l != NULL)
5972 {
5973 /* Prepend the list to the list of lists for garbage collection. */
5974 if (first_list != NULL)
5975 first_list->lv_used_prev = l;
5976 l->lv_used_prev = NULL;
5977 l->lv_used_next = first_list;
5978 first_list = l;
5979 }
5980 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981}
5982
5983/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005984 * Allocate an empty list for a return value.
5985 * Returns OK or FAIL.
5986 */
5987 static int
5988rettv_list_alloc(rettv)
5989 typval_T *rettv;
5990{
5991 list_T *l = list_alloc();
5992
5993 if (l == NULL)
5994 return FAIL;
5995
5996 rettv->vval.v_list = l;
5997 rettv->v_type = VAR_LIST;
5998 ++l->lv_refcount;
5999 return OK;
6000}
6001
6002/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 * Unreference a list: decrement the reference count and free it when it
6004 * becomes zero.
6005 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006006 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006010 if (l != NULL && --l->lv_refcount <= 0)
6011 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012}
6013
6014/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006015 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 * Ignores the reference count.
6017 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006018 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006019list_free(l, recurse)
6020 list_T *l;
6021 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022{
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006025 /* Remove the list from the list of lists for garbage collection. */
6026 if (l->lv_used_prev == NULL)
6027 first_list = l->lv_used_next;
6028 else
6029 l->lv_used_prev->lv_used_next = l->lv_used_next;
6030 if (l->lv_used_next != NULL)
6031 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6032
Bram Moolenaard9fba312005-06-26 22:34:35 +00006033 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006035 /* Remove the item before deleting it. */
6036 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006037 if (recurse || (item->li_tv.v_type != VAR_LIST
6038 && item->li_tv.v_type != VAR_DICT))
6039 clear_tv(&item->li_tv);
6040 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 }
6042 vim_free(l);
6043}
6044
6045/*
6046 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006047 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006049 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050listitem_alloc()
6051{
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053}
6054
6055/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006056 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006058 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006062 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 vim_free(item);
6064}
6065
6066/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006067 * Remove a list item from a List and free it. Also clears the value.
6068 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006069 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006070listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006071 list_T *l;
6072 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006074 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075 listitem_free(item);
6076}
6077
6078/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 * Get the number of items in a list.
6080 */
6081 static long
6082list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 if (l == NULL)
6086 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006087 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088}
6089
6090/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006091 * Return TRUE when two lists have exactly the same values.
6092 */
6093 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 list_T *l1;
6096 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006098 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099{
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006102 if (l1 == NULL || l2 == NULL)
6103 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006104 if (l1 == l2)
6105 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006106 if (list_len(l1) != list_len(l2))
6107 return FALSE;
6108
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 for (item1 = l1->lv_first, item2 = l2->lv_first;
6110 item1 != NULL && item2 != NULL;
6111 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113 return FALSE;
6114 return item1 == NULL && item2 == NULL;
6115}
6116
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006117#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6118 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006119/*
6120 * Return the dictitem that an entry in a hashtable points to.
6121 */
6122 dictitem_T *
6123dict_lookup(hi)
6124 hashitem_T *hi;
6125{
6126 return HI2DI(hi);
6127}
6128#endif
6129
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 * Return TRUE when two dictionaries have exactly the same key/values.
6132 */
6133 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 dict_T *d1;
6136 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006137 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006138 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 hashitem_T *hi;
6141 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006144 if (d1 == NULL || d2 == NULL)
6145 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006146 if (d1 == d2)
6147 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 if (dict_len(d1) != dict_len(d2))
6149 return FALSE;
6150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006151 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 if (!HASHITEM_EMPTY(hi))
6155 {
6156 item2 = dict_find(d2, hi->hi_key, -1);
6157 if (item2 == NULL)
6158 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006159 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 return FALSE;
6161 --todo;
6162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 }
6164 return TRUE;
6165}
6166
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167static int tv_equal_recurse_limit;
6168
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006171 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006172 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173 */
6174 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006175tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 typval_T *tv1;
6177 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006178 int ic; /* ignore case */
6179 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180{
6181 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006182 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006184 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006189 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 * recursiveness to a limit. We guess they are equal then.
6191 * A fixed limit has the problem of still taking an awful long time.
6192 * Reduce the limit every time running into it. That should work fine for
6193 * deeply linked structures that are not recursively linked and catch
6194 * recursiveness quickly. */
6195 if (!recursive)
6196 tv_equal_recurse_limit = 1000;
6197 if (recursive_cnt >= tv_equal_recurse_limit)
6198 {
6199 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006200 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202
6203 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 ++recursive_cnt;
6207 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6208 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006209 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210
6211 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 ++recursive_cnt;
6213 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 case VAR_FUNC:
6218 return (tv1->vval.v_string != NULL
6219 && tv2->vval.v_string != NULL
6220 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6221
6222 case VAR_NUMBER:
6223 return tv1->vval.v_number == tv2->vval.v_number;
6224
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006225#ifdef FEAT_FLOAT
6226 case VAR_FLOAT:
6227 return tv1->vval.v_float == tv2->vval.v_float;
6228#endif
6229
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006230 case VAR_STRING:
6231 s1 = get_tv_string_buf(tv1, buf1);
6232 s2 = get_tv_string_buf(tv2, buf2);
6233 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006234 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006235
6236 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006237 return TRUE;
6238}
6239
6240/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006241 * Locate item with index "n" in list "l" and return it.
6242 * A negative index is counted from the end; -1 is the last item.
6243 * Returns NULL when "n" is out of range.
6244 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006245 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006247 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 long n;
6249{
Bram Moolenaar33570922005-01-25 22:26:29 +00006250 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251 long idx;
6252
6253 if (l == NULL)
6254 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006255
6256 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006258 n = l->lv_len + n;
6259
6260 /* Check for index out of range. */
6261 if (n < 0 || n >= l->lv_len)
6262 return NULL;
6263
6264 /* When there is a cached index may start search from there. */
6265 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267 if (n < l->lv_idx / 2)
6268 {
6269 /* closest to the start of the list */
6270 item = l->lv_first;
6271 idx = 0;
6272 }
6273 else if (n > (l->lv_idx + l->lv_len) / 2)
6274 {
6275 /* closest to the end of the list */
6276 item = l->lv_last;
6277 idx = l->lv_len - 1;
6278 }
6279 else
6280 {
6281 /* closest to the cached index */
6282 item = l->lv_idx_item;
6283 idx = l->lv_idx;
6284 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285 }
6286 else
6287 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006288 if (n < l->lv_len / 2)
6289 {
6290 /* closest to the start of the list */
6291 item = l->lv_first;
6292 idx = 0;
6293 }
6294 else
6295 {
6296 /* closest to the end of the list */
6297 item = l->lv_last;
6298 idx = l->lv_len - 1;
6299 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301
6302 while (n > idx)
6303 {
6304 /* search forward */
6305 item = item->li_next;
6306 ++idx;
6307 }
6308 while (n < idx)
6309 {
6310 /* search backward */
6311 item = item->li_prev;
6312 --idx;
6313 }
6314
6315 /* cache the used index */
6316 l->lv_idx = idx;
6317 l->lv_idx_item = item;
6318
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 return item;
6320}
6321
6322/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006323 * Get list item "l[idx]" as a number.
6324 */
6325 static long
6326list_find_nr(l, idx, errorp)
6327 list_T *l;
6328 long idx;
6329 int *errorp; /* set to TRUE when something wrong */
6330{
6331 listitem_T *li;
6332
6333 li = list_find(l, idx);
6334 if (li == NULL)
6335 {
6336 if (errorp != NULL)
6337 *errorp = TRUE;
6338 return -1L;
6339 }
6340 return get_tv_number_chk(&li->li_tv, errorp);
6341}
6342
6343/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006344 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6345 */
6346 char_u *
6347list_find_str(l, idx)
6348 list_T *l;
6349 long idx;
6350{
6351 listitem_T *li;
6352
6353 li = list_find(l, idx - 1);
6354 if (li == NULL)
6355 {
6356 EMSGN(_(e_listidx), idx);
6357 return NULL;
6358 }
6359 return get_tv_string(&li->li_tv);
6360}
6361
6362/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006363 * Locate "item" list "l" and return its index.
6364 * Returns -1 when "item" is not in the list.
6365 */
6366 static long
6367list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 list_T *l;
6369 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006370{
6371 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373
6374 if (l == NULL)
6375 return -1;
6376 idx = 0;
6377 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6378 ++idx;
6379 if (li == NULL)
6380 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006381 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006382}
6383
6384/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385 * Append item "item" to the end of list "l".
6386 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006387 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006389 list_T *l;
6390 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391{
6392 if (l->lv_last == NULL)
6393 {
6394 /* empty list */
6395 l->lv_first = item;
6396 l->lv_last = item;
6397 item->li_prev = NULL;
6398 }
6399 else
6400 {
6401 l->lv_last->li_next = item;
6402 item->li_prev = l->lv_last;
6403 l->lv_last = item;
6404 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006405 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406 item->li_next = NULL;
6407}
6408
6409/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006411 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006413 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 list_T *l;
6416 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006418 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419
Bram Moolenaar05159a02005-02-26 23:04:13 +00006420 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 copy_tv(tv, &li->li_tv);
6423 list_append(l, li);
6424 return OK;
6425}
6426
6427/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006428 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006429 * Return FAIL when out of memory.
6430 */
6431 int
6432list_append_dict(list, dict)
6433 list_T *list;
6434 dict_T *dict;
6435{
6436 listitem_T *li = listitem_alloc();
6437
6438 if (li == NULL)
6439 return FAIL;
6440 li->li_tv.v_type = VAR_DICT;
6441 li->li_tv.v_lock = 0;
6442 li->li_tv.vval.v_dict = dict;
6443 list_append(list, li);
6444 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 return OK;
6446}
6447
6448/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006449 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006450 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 * Returns FAIL when out of memory.
6452 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006453 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006454list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006455 list_T *l;
6456 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006457 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006458{
6459 listitem_T *li = listitem_alloc();
6460
6461 if (li == NULL)
6462 return FAIL;
6463 list_append(l, li);
6464 li->li_tv.v_type = VAR_STRING;
6465 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006466 if (str == NULL)
6467 li->li_tv.vval.v_string = NULL;
6468 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006469 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470 return FAIL;
6471 return OK;
6472}
6473
6474/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006475 * Append "n" to list "l".
6476 * Returns FAIL when out of memory.
6477 */
6478 static int
6479list_append_number(l, n)
6480 list_T *l;
6481 varnumber_T n;
6482{
6483 listitem_T *li;
6484
6485 li = listitem_alloc();
6486 if (li == NULL)
6487 return FAIL;
6488 li->li_tv.v_type = VAR_NUMBER;
6489 li->li_tv.v_lock = 0;
6490 li->li_tv.vval.v_number = n;
6491 list_append(l, li);
6492 return OK;
6493}
6494
6495/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497 * If "item" is NULL append at the end.
6498 * Return FAIL when out of memory.
6499 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006500 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 list_T *l;
6503 typval_T *tv;
6504 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505{
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507
6508 if (ni == NULL)
6509 return FAIL;
6510 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006511 list_insert(l, ni, item);
6512 return OK;
6513}
6514
6515 void
6516list_insert(l, ni, item)
6517 list_T *l;
6518 listitem_T *ni;
6519 listitem_T *item;
6520{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006521 if (item == NULL)
6522 /* Append new item at end of list. */
6523 list_append(l, ni);
6524 else
6525 {
6526 /* Insert new item before existing item. */
6527 ni->li_prev = item->li_prev;
6528 ni->li_next = item;
6529 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006530 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 ++l->lv_idx;
6533 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006535 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 l->lv_idx_item = NULL;
6538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006540 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542}
6543
6544/*
6545 * Extend "l1" with "l2".
6546 * If "bef" is NULL append at the end, otherwise insert before this item.
6547 * Returns FAIL when out of memory.
6548 */
6549 static int
6550list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 list_T *l1;
6552 list_T *l2;
6553 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554{
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006556 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006558 /* We also quit the loop when we have inserted the original item count of
6559 * the list, avoid a hang when we extend a list with itself. */
6560 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6562 return FAIL;
6563 return OK;
6564}
6565
6566/*
6567 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6568 * Return FAIL when out of memory.
6569 */
6570 static int
6571list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006572 list_T *l1;
6573 list_T *l2;
6574 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575{
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006578 if (l1 == NULL || l2 == NULL)
6579 return FAIL;
6580
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006582 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 if (l == NULL)
6584 return FAIL;
6585 tv->v_type = VAR_LIST;
6586 tv->vval.v_list = l;
6587
6588 /* append all items from the second list */
6589 return list_extend(l, l2, NULL);
6590}
6591
6592/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 * Returns NULL when out of memory.
6597 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006598 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006599list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006600 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006602 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603{
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 list_T *copy;
6605 listitem_T *item;
6606 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607
6608 if (orig == NULL)
6609 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610
6611 copy = list_alloc();
6612 if (copy != NULL)
6613 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 if (copyID != 0)
6615 {
6616 /* Do this before adding the items, because one of the items may
6617 * refer back to this list. */
6618 orig->lv_copyID = copyID;
6619 orig->lv_copylist = copy;
6620 }
6621 for (item = orig->lv_first; item != NULL && !got_int;
6622 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 {
6624 ni = listitem_alloc();
6625 if (ni == NULL)
6626 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006627 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628 {
6629 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6630 {
6631 vim_free(ni);
6632 break;
6633 }
6634 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006636 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 list_append(copy, ni);
6638 }
6639 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 if (item != NULL)
6641 {
6642 list_unref(copy);
6643 copy = NULL;
6644 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 }
6646
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 return copy;
6648}
6649
6650/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006652 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006653 * This used to be called list_remove, but that conflicts with a Sun header
6654 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006656 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006657vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006658 list_T *l;
6659 listitem_T *item;
6660 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661{
Bram Moolenaar33570922005-01-25 22:26:29 +00006662 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664 /* notify watchers */
6665 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006667 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006668 list_fix_watch(l, ip);
6669 if (ip == item2)
6670 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006672
6673 if (item2->li_next == NULL)
6674 l->lv_last = item->li_prev;
6675 else
6676 item2->li_next->li_prev = item->li_prev;
6677 if (item->li_prev == NULL)
6678 l->lv_first = item2->li_next;
6679 else
6680 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006681 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682}
6683
6684/*
6685 * Return an allocated string with the string representation of a list.
6686 * May return NULL.
6687 */
6688 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006689list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006690 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006691 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692{
6693 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694
6695 if (tv->vval.v_list == NULL)
6696 return NULL;
6697 ga_init2(&ga, (int)sizeof(char), 80);
6698 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006699 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006700 {
6701 vim_free(ga.ga_data);
6702 return NULL;
6703 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 ga_append(&ga, ']');
6705 ga_append(&ga, NUL);
6706 return (char_u *)ga.ga_data;
6707}
6708
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006709typedef struct join_S {
6710 char_u *s;
6711 char_u *tofree;
6712} join_T;
6713
6714 static int
6715list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6716 garray_T *gap; /* to store the result in */
6717 list_T *l;
6718 char_u *sep;
6719 int echo_style;
6720 int copyID;
6721 garray_T *join_gap; /* to keep each list item string */
6722{
6723 int i;
6724 join_T *p;
6725 int len;
6726 int sumlen = 0;
6727 int first = TRUE;
6728 char_u *tofree;
6729 char_u numbuf[NUMBUFLEN];
6730 listitem_T *item;
6731 char_u *s;
6732
6733 /* Stringify each item in the list. */
6734 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6735 {
6736 if (echo_style)
6737 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6738 else
6739 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6740 if (s == NULL)
6741 return FAIL;
6742
6743 len = (int)STRLEN(s);
6744 sumlen += len;
6745
Bram Moolenaarcde88542015-08-11 19:14:00 +02006746 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006747 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6748 if (tofree != NULL || s != numbuf)
6749 {
6750 p->s = s;
6751 p->tofree = tofree;
6752 }
6753 else
6754 {
6755 p->s = vim_strnsave(s, len);
6756 p->tofree = p->s;
6757 }
6758
6759 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006760 if (did_echo_string_emsg) /* recursion error, bail out */
6761 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006762 }
6763
6764 /* Allocate result buffer with its total size, avoid re-allocation and
6765 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6766 if (join_gap->ga_len >= 2)
6767 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6768 if (ga_grow(gap, sumlen + 2) == FAIL)
6769 return FAIL;
6770
6771 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6772 {
6773 if (first)
6774 first = FALSE;
6775 else
6776 ga_concat(gap, sep);
6777 p = ((join_T *)join_gap->ga_data) + i;
6778
6779 if (p->s != NULL)
6780 ga_concat(gap, p->s);
6781 line_breakcheck();
6782 }
6783
6784 return OK;
6785}
6786
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006787/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006788 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006789 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006790 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006791 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006792 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006793list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006795 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006797 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006798 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006800 garray_T join_ga;
6801 int retval;
6802 join_T *p;
6803 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006804
Bram Moolenaard39a7512015-04-16 22:51:22 +02006805 if (l->lv_len < 1)
6806 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006807 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6808 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6809
6810 /* Dispose each item in join_ga. */
6811 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006812 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006813 p = (join_T *)join_ga.ga_data;
6814 for (i = 0; i < join_ga.ga_len; ++i)
6815 {
6816 vim_free(p->tofree);
6817 ++p;
6818 }
6819 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006821
6822 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823}
6824
6825/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826 * Garbage collection for lists and dictionaries.
6827 *
6828 * We use reference counts to be able to free most items right away when they
6829 * are no longer used. But for composite items it's possible that it becomes
6830 * unused while the reference count is > 0: When there is a recursive
6831 * reference. Example:
6832 * :let l = [1, 2, 3]
6833 * :let d = {9: l}
6834 * :let l[1] = d
6835 *
6836 * Since this is quite unusual we handle this with garbage collection: every
6837 * once in a while find out which lists and dicts are not referenced from any
6838 * variable.
6839 *
6840 * Here is a good reference text about garbage collection (refers to Python
6841 * but it applies to all reference-counting mechanisms):
6842 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006843 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844
6845/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846 * Do garbage collection for lists and dicts.
6847 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006848 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006849 int
6850garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006851{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006853 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 buf_T *buf;
6855 win_T *wp;
6856 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006857 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006858 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006860#ifdef FEAT_WINDOWS
6861 tabpage_T *tp;
6862#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006864 /* Only do this once. */
6865 want_garbage_collect = FALSE;
6866 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006867 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006868
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 /* We advance by two because we add one for items referenced through
6870 * previous_funccal. */
6871 current_copyID += COPYID_INC;
6872 copyID = current_copyID;
6873
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 /*
6875 * 1. Go through all accessible variables and mark all lists and dicts
6876 * with copyID.
6877 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006878
6879 /* Don't free variables in the previous_funccal list unless they are only
6880 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006881 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006882 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6883 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6885 NULL);
6886 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6887 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 }
6889
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 /* script-local variables */
6891 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893
6894 /* buffer-local variables */
6895 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6897 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898
6899 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006900 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6902 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006903#ifdef FEAT_AUTOCMD
6904 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6906 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006907#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006909#ifdef FEAT_WINDOWS
6910 /* tabpage-local variables */
6911 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6913 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006914#endif
6915
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918
6919 /* function-local variables */
6920 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6921 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6923 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 }
6925
Bram Moolenaard812df62008-11-09 12:46:09 +00006926 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006928
Bram Moolenaar1dced572012-04-05 16:54:08 +02006929#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006931#endif
6932
Bram Moolenaardb913952012-06-29 12:54:53 +02006933#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006935#endif
6936
6937#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006939#endif
6940
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006942 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 /*
6944 * 2. Free lists and dictionaries that are not referenced.
6945 */
6946 did_free = free_unref_items(copyID);
6947
6948 /*
6949 * 3. Check if any funccal can be freed now.
6950 */
6951 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 if (can_free_funccal(*pfc, copyID))
6954 {
6955 fc = *pfc;
6956 *pfc = fc->caller;
6957 free_funccal(fc, TRUE);
6958 did_free = TRUE;
6959 did_free_funccal = TRUE;
6960 }
6961 else
6962 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006964 if (did_free_funccal)
6965 /* When a funccal was freed some more items might be garbage
6966 * collected, so run again. */
6967 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 else if (p_verbose > 0)
6970 {
6971 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6972 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973
6974 return did_free;
6975}
6976
6977/*
6978 * Free lists and dictionaries that are no longer referenced.
6979 */
6980 static int
6981free_unref_items(copyID)
6982 int copyID;
6983{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006984 dict_T *dd, *dd_next;
6985 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006986 int did_free = FALSE;
6987
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 */
6991 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006992 {
6993 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006996 /* Free the Dictionary and ordinary items it contains, but don't
6997 * recurse into Lists and Dictionaries, they will be in the list
6998 * of dicts or list of lists. */
6999 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007002 dd = dd_next;
7003 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004
7005 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 * Go through the list of lists and free items without the copyID.
7007 * But don't free a list that has a watcher (used in a for loop), these
7008 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 */
7010 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007011 {
7012 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007013 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7014 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007016 /* Free the List and ordinary items it contains, but don't recurse
7017 * into Lists and Dictionaries, they will be in the list of dicts
7018 * or list of lists. */
7019 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007022 ll = ll_next;
7023 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 return did_free;
7025}
7026
7027/*
7028 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 * "list_stack" is used to add lists to be marked. Can be NULL.
7030 *
7031 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 int
7034set_ref_in_ht(ht, copyID, list_stack)
7035 hashtab_T *ht;
7036 int copyID;
7037 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038{
7039 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 hashtab_T *cur_ht;
7043 ht_stack_T *ht_stack = NULL;
7044 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 cur_ht = ht;
7047 for (;;)
7048 {
7049 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051 /* Mark each item in the hashtab. If the item contains a hashtab
7052 * it is added to ht_stack, if it contains a list it is added to
7053 * list_stack. */
7054 todo = (int)cur_ht->ht_used;
7055 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7056 if (!HASHITEM_EMPTY(hi))
7057 {
7058 --todo;
7059 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7060 &ht_stack, list_stack);
7061 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063
7064 if (ht_stack == NULL)
7065 break;
7066
7067 /* take an item from the stack */
7068 cur_ht = ht_stack->ht;
7069 tempitem = ht_stack;
7070 ht_stack = ht_stack->prev;
7071 free(tempitem);
7072 }
7073
7074 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075}
7076
7077/*
7078 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7080 *
7081 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 int
7084set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 list_T *l;
7086 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007087 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007089 listitem_T *li;
7090 int abort = FALSE;
7091 list_T *cur_l;
7092 list_stack_T *list_stack = NULL;
7093 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 cur_l = l;
7096 for (;;)
7097 {
7098 if (!abort)
7099 /* Mark each item in the list. If the item contains a hashtab
7100 * it is added to ht_stack, if it contains a list it is added to
7101 * list_stack. */
7102 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7103 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7104 ht_stack, &list_stack);
7105 if (list_stack == NULL)
7106 break;
7107
7108 /* take an item from the stack */
7109 cur_l = list_stack->list;
7110 tempitem = list_stack;
7111 list_stack = list_stack->prev;
7112 free(tempitem);
7113 }
7114
7115 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116}
7117
7118/*
7119 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 * "list_stack" is used to add lists to be marked. Can be NULL.
7121 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7122 *
7123 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 int
7126set_ref_in_item(tv, copyID, ht_stack, list_stack)
7127 typval_T *tv;
7128 int copyID;
7129 ht_stack_T **ht_stack;
7130 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007131{
7132 dict_T *dd;
7133 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007134 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007135
7136 switch (tv->v_type)
7137 {
7138 case VAR_DICT:
7139 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007140 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007141 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007142 /* Didn't see this dict yet. */
7143 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007144 if (ht_stack == NULL)
7145 {
7146 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7147 }
7148 else
7149 {
7150 ht_stack_T *newitem = (ht_stack_T*)malloc(
7151 sizeof(ht_stack_T));
7152 if (newitem == NULL)
7153 abort = TRUE;
7154 else
7155 {
7156 newitem->ht = &dd->dv_hashtab;
7157 newitem->prev = *ht_stack;
7158 *ht_stack = newitem;
7159 }
7160 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007161 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007162 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007163
7164 case VAR_LIST:
7165 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007166 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007168 /* Didn't see this list yet. */
7169 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 if (list_stack == NULL)
7171 {
7172 abort = set_ref_in_list(ll, copyID, ht_stack);
7173 }
7174 else
7175 {
7176 list_stack_T *newitem = (list_stack_T*)malloc(
7177 sizeof(list_stack_T));
7178 if (newitem == NULL)
7179 abort = TRUE;
7180 else
7181 {
7182 newitem->list = ll;
7183 newitem->prev = *list_stack;
7184 *list_stack = newitem;
7185 }
7186 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007187 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007189 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007191}
7192
7193/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194 * Allocate an empty header for a dictionary.
7195 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007196 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197dict_alloc()
7198{
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007203 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007204 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007205 if (first_dict != NULL)
7206 first_dict->dv_used_prev = d;
7207 d->dv_used_next = first_dict;
7208 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007209 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007212 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007213 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007214 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007215 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007216 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218}
7219
7220/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007221 * Allocate an empty dict for a return value.
7222 * Returns OK or FAIL.
7223 */
7224 static int
7225rettv_dict_alloc(rettv)
7226 typval_T *rettv;
7227{
7228 dict_T *d = dict_alloc();
7229
7230 if (d == NULL)
7231 return FAIL;
7232
7233 rettv->vval.v_dict = d;
7234 rettv->v_type = VAR_DICT;
7235 ++d->dv_refcount;
7236 return OK;
7237}
7238
7239
7240/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241 * Unreference a Dictionary: decrement the reference count and free it when it
7242 * becomes zero.
7243 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007244 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007248 if (d != NULL && --d->dv_refcount <= 0)
7249 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250}
7251
7252/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007253 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 * Ignores the reference count.
7255 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007256 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007257dict_free(d, recurse)
7258 dict_T *d;
7259 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007261 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007263 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007265 /* Remove the dict from the list of dicts for garbage collection. */
7266 if (d->dv_used_prev == NULL)
7267 first_dict = d->dv_used_next;
7268 else
7269 d->dv_used_prev->dv_used_next = d->dv_used_next;
7270 if (d->dv_used_next != NULL)
7271 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7272
7273 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007274 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007275 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278 if (!HASHITEM_EMPTY(hi))
7279 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007280 /* Remove the item before deleting it, just in case there is
7281 * something recursive causing trouble. */
7282 di = HI2DI(hi);
7283 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007284 if (recurse || (di->di_tv.v_type != VAR_LIST
7285 && di->di_tv.v_type != VAR_DICT))
7286 clear_tv(&di->di_tv);
7287 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 --todo;
7289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 vim_free(d);
7293}
7294
7295/*
7296 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297 * The "key" is copied to the new item.
7298 * Note that the value of the item "di_tv" still needs to be initialized!
7299 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007301 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302dictitem_alloc(key)
7303 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304{
Bram Moolenaar33570922005-01-25 22:26:29 +00007305 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007307 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007311 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007312 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314}
7315
7316/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 * Make a copy of a Dictionary item.
7318 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322{
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007325 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7326 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 if (di != NULL)
7328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007330 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331 copy_tv(&org->di_tv, &di->di_tv);
7332 }
7333 return di;
7334}
7335
7336/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 * Remove item "item" from Dictionary "dict" and free it.
7338 */
7339 static void
7340dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 dict_T *dict;
7342 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343{
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 if (HASHITEM_EMPTY(hi))
7348 EMSG2(_(e_intern2), "dictitem_remove()");
7349 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 dictitem_free(item);
7352}
7353
7354/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355 * Free a dict item. Also clears the value.
7356 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007357 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007358dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007362 if (item->di_flags & DI_FLAGS_ALLOC)
7363 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364}
7365
7366/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7368 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 * Returns NULL when out of memory.
7371 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007372 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007373dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007376 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377{
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 dict_T *copy;
7379 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382
7383 if (orig == NULL)
7384 return NULL;
7385
7386 copy = dict_alloc();
7387 if (copy != NULL)
7388 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 if (copyID != 0)
7390 {
7391 orig->dv_copyID = copyID;
7392 orig->dv_copydict = copy;
7393 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007394 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 --todo;
7400
7401 di = dictitem_alloc(hi->hi_key);
7402 if (di == NULL)
7403 break;
7404 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007405 {
7406 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7407 copyID) == FAIL)
7408 {
7409 vim_free(di);
7410 break;
7411 }
7412 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007413 else
7414 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7415 if (dict_add(copy, di) == FAIL)
7416 {
7417 dictitem_free(di);
7418 break;
7419 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007421 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007422
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007424 if (todo > 0)
7425 {
7426 dict_unref(copy);
7427 copy = NULL;
7428 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429 }
7430
7431 return copy;
7432}
7433
7434/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007436 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007438 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007440 dict_T *d;
7441 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442{
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444}
7445
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007447 * Add a number or string entry to dictionary "d".
7448 * When "str" is NULL use number "nr", otherwise use "str".
7449 * Returns FAIL when out of memory and when key already exists.
7450 */
7451 int
7452dict_add_nr_str(d, key, nr, str)
7453 dict_T *d;
7454 char *key;
7455 long nr;
7456 char_u *str;
7457{
7458 dictitem_T *item;
7459
7460 item = dictitem_alloc((char_u *)key);
7461 if (item == NULL)
7462 return FAIL;
7463 item->di_tv.v_lock = 0;
7464 if (str == NULL)
7465 {
7466 item->di_tv.v_type = VAR_NUMBER;
7467 item->di_tv.vval.v_number = nr;
7468 }
7469 else
7470 {
7471 item->di_tv.v_type = VAR_STRING;
7472 item->di_tv.vval.v_string = vim_strsave(str);
7473 }
7474 if (dict_add(d, item) == FAIL)
7475 {
7476 dictitem_free(item);
7477 return FAIL;
7478 }
7479 return OK;
7480}
7481
7482/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007483 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007484 * Returns FAIL when out of memory and when key already exists.
7485 */
7486 int
7487dict_add_list(d, key, list)
7488 dict_T *d;
7489 char *key;
7490 list_T *list;
7491{
7492 dictitem_T *item;
7493
7494 item = dictitem_alloc((char_u *)key);
7495 if (item == NULL)
7496 return FAIL;
7497 item->di_tv.v_lock = 0;
7498 item->di_tv.v_type = VAR_LIST;
7499 item->di_tv.vval.v_list = list;
7500 if (dict_add(d, item) == FAIL)
7501 {
7502 dictitem_free(item);
7503 return FAIL;
7504 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007505 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007506 return OK;
7507}
7508
7509/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510 * Get the number of items in a Dictionary.
7511 */
7512 static long
7513dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007516 if (d == NULL)
7517 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007518 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007519}
7520
7521/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 * Find item "key[len]" in Dictionary "d".
7523 * If "len" is negative use strlen(key).
7524 * Returns NULL when not found.
7525 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007526 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 char_u *key;
7530 int len;
7531{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532#define AKEYLEN 200
7533 char_u buf[AKEYLEN];
7534 char_u *akey;
7535 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007536 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007538 if (len < 0)
7539 akey = key;
7540 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007541 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542 tofree = akey = vim_strnsave(key, len);
7543 if (akey == NULL)
7544 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007545 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007546 else
7547 {
7548 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007549 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 akey = buf;
7551 }
7552
Bram Moolenaar33570922005-01-25 22:26:29 +00007553 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 vim_free(tofree);
7555 if (HASHITEM_EMPTY(hi))
7556 return NULL;
7557 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558}
7559
7560/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007561 * Get a string item from a dictionary.
7562 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563 * Returns NULL if the entry doesn't exist or out of memory.
7564 */
7565 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007566get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007567 dict_T *d;
7568 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007569 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007570{
7571 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007572 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007573
7574 di = dict_find(d, key, -1);
7575 if (di == NULL)
7576 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007577 s = get_tv_string(&di->di_tv);
7578 if (save && s != NULL)
7579 s = vim_strsave(s);
7580 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007581}
7582
7583/*
7584 * Get a number item from a dictionary.
7585 * Returns 0 if the entry doesn't exist or out of memory.
7586 */
7587 long
7588get_dict_number(d, key)
7589 dict_T *d;
7590 char_u *key;
7591{
7592 dictitem_T *di;
7593
7594 di = dict_find(d, key, -1);
7595 if (di == NULL)
7596 return 0;
7597 return get_tv_number(&di->di_tv);
7598}
7599
7600/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 * Return an allocated string with the string representation of a Dictionary.
7602 * May return NULL.
7603 */
7604 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007605dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608{
7609 garray_T ga;
7610 int first = TRUE;
7611 char_u *tofree;
7612 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007613 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007616 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007617
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007618 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 return NULL;
7620 ga_init2(&ga, (int)sizeof(char), 80);
7621 ga_append(&ga, '{');
7622
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007623 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007624 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007626 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 --todo;
7629
7630 if (first)
7631 first = FALSE;
7632 else
7633 ga_concat(&ga, (char_u *)", ");
7634
7635 tofree = string_quote(hi->hi_key, FALSE);
7636 if (tofree != NULL)
7637 {
7638 ga_concat(&ga, tofree);
7639 vim_free(tofree);
7640 }
7641 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007642 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007643 if (s != NULL)
7644 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007646 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007647 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007648 line_breakcheck();
7649
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007652 if (todo > 0)
7653 {
7654 vim_free(ga.ga_data);
7655 return NULL;
7656 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657
7658 ga_append(&ga, '}');
7659 ga_append(&ga, NUL);
7660 return (char_u *)ga.ga_data;
7661}
7662
7663/*
7664 * Allocate a variable for a Dictionary and fill it from "*arg".
7665 * Return OK or FAIL. Returns NOTDONE for {expr}.
7666 */
7667 static int
7668get_dict_tv(arg, rettv, evaluate)
7669 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 int evaluate;
7672{
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 dict_T *d = NULL;
7674 typval_T tvkey;
7675 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007676 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007677 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680
7681 /*
7682 * First check if it's not a curly-braces thing: {expr}.
7683 * Must do this without evaluating, otherwise a function may be called
7684 * twice. Unfortunately this means we need to call eval1() twice for the
7685 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007686 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007688 if (*start != '}')
7689 {
7690 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7691 return FAIL;
7692 if (*start == '}')
7693 return NOTDONE;
7694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695
7696 if (evaluate)
7697 {
7698 d = dict_alloc();
7699 if (d == NULL)
7700 return FAIL;
7701 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007702 tvkey.v_type = VAR_UNKNOWN;
7703 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704
7705 *arg = skipwhite(*arg + 1);
7706 while (**arg != '}' && **arg != NUL)
7707 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007708 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 goto failret;
7710 if (**arg != ':')
7711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007712 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 goto failret;
7715 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007716 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007718 key = get_tv_string_buf_chk(&tvkey, buf);
7719 if (key == NULL || *key == NUL)
7720 {
7721 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7722 if (key != NULL)
7723 EMSG(_(e_emptykey));
7724 clear_tv(&tvkey);
7725 goto failret;
7726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728
7729 *arg = skipwhite(*arg + 1);
7730 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7731 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007732 if (evaluate)
7733 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734 goto failret;
7735 }
7736 if (evaluate)
7737 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 if (item != NULL)
7740 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007741 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 clear_tv(&tv);
7744 goto failret;
7745 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007746 item = dictitem_alloc(key);
7747 clear_tv(&tvkey);
7748 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007749 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007751 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007752 if (dict_add(d, item) == FAIL)
7753 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 }
7755 }
7756
7757 if (**arg == '}')
7758 break;
7759 if (**arg != ',')
7760 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007761 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 goto failret;
7763 }
7764 *arg = skipwhite(*arg + 1);
7765 }
7766
7767 if (**arg != '}')
7768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007769 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770failret:
7771 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007772 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007773 return FAIL;
7774 }
7775
7776 *arg = skipwhite(*arg + 1);
7777 if (evaluate)
7778 {
7779 rettv->v_type = VAR_DICT;
7780 rettv->vval.v_dict = d;
7781 ++d->dv_refcount;
7782 }
7783
7784 return OK;
7785}
7786
Bram Moolenaar8c711452005-01-14 21:53:12 +00007787/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788 * Return a string with the string representation of a variable.
7789 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007790 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007791 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007792 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007793 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 */
7795 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007796echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007798 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007799 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007800 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 static int recurse = 0;
7803 char_u *r = NULL;
7804
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007807 if (!did_echo_string_emsg)
7808 {
7809 /* Only give this message once for a recursive call to avoid
7810 * flooding the user with errors. And stop iterating over lists
7811 * and dicts. */
7812 did_echo_string_emsg = TRUE;
7813 EMSG(_("E724: variable nested too deep for displaying"));
7814 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007816 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 }
7818 ++recurse;
7819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 switch (tv->v_type)
7821 {
7822 case VAR_FUNC:
7823 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 r = tv->vval.v_string;
7825 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007826
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007828 if (tv->vval.v_list == NULL)
7829 {
7830 *tofree = NULL;
7831 r = NULL;
7832 }
7833 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7834 {
7835 *tofree = NULL;
7836 r = (char_u *)"[...]";
7837 }
7838 else
7839 {
7840 tv->vval.v_list->lv_copyID = copyID;
7841 *tofree = list2string(tv, copyID);
7842 r = *tofree;
7843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007845
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007847 if (tv->vval.v_dict == NULL)
7848 {
7849 *tofree = NULL;
7850 r = NULL;
7851 }
7852 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7853 {
7854 *tofree = NULL;
7855 r = (char_u *)"{...}";
7856 }
7857 else
7858 {
7859 tv->vval.v_dict->dv_copyID = copyID;
7860 *tofree = dict2string(tv, copyID);
7861 r = *tofree;
7862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 case VAR_STRING:
7866 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007867 *tofree = NULL;
7868 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007870
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 case VAR_FLOAT:
7873 *tofree = NULL;
7874 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7875 r = numbuf;
7876 break;
7877#endif
7878
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007881 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883
Bram Moolenaar8502c702014-06-17 12:51:16 +02007884 if (--recurse == 0)
7885 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007886 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887}
7888
7889/*
7890 * Return a string with the string representation of a variable.
7891 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7892 * "numbuf" is used for a number.
7893 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007894 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 */
7896 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007897tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007898 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899 char_u **tofree;
7900 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007901 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007902{
7903 switch (tv->v_type)
7904 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 case VAR_FUNC:
7906 *tofree = string_quote(tv->vval.v_string, TRUE);
7907 return *tofree;
7908 case VAR_STRING:
7909 *tofree = string_quote(tv->vval.v_string, FALSE);
7910 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007911#ifdef FEAT_FLOAT
7912 case VAR_FLOAT:
7913 *tofree = NULL;
7914 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7915 return numbuf;
7916#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007919 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007920 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007921 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007922 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007923 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007924 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007925}
7926
7927/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 * Return string "str" in ' quotes, doubling ' characters.
7929 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007930 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 */
7932 static char_u *
7933string_quote(str, function)
7934 char_u *str;
7935 int function;
7936{
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007938 char_u *p, *r, *s;
7939
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 len = (function ? 13 : 3);
7941 if (str != NULL)
7942 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007943 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 for (p = str; *p != NUL; mb_ptr_adv(p))
7945 if (*p == '\'')
7946 ++len;
7947 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007948 s = r = alloc(len);
7949 if (r != NULL)
7950 {
7951 if (function)
7952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007954 r += 10;
7955 }
7956 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 if (str != NULL)
7959 for (p = str; *p != NUL; )
7960 {
7961 if (*p == '\'')
7962 *r++ = '\'';
7963 MB_COPY_CHAR(p, r);
7964 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966 if (function)
7967 *r++ = ')';
7968 *r++ = NUL;
7969 }
7970 return s;
7971}
7972
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007973#ifdef FEAT_FLOAT
7974/*
7975 * Convert the string "text" to a floating point number.
7976 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7977 * this always uses a decimal point.
7978 * Returns the length of the text that was consumed.
7979 */
7980 static int
7981string2float(text, value)
7982 char_u *text;
7983 float_T *value; /* result stored here */
7984{
7985 char *s = (char *)text;
7986 float_T f;
7987
7988 f = strtod(s, &s);
7989 *value = f;
7990 return (int)((char_u *)s - text);
7991}
7992#endif
7993
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 * Get the value of an environment variable.
7996 * "arg" is pointing to the '$'. It is advanced to after the name.
7997 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007998 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 */
8000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008001get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 int evaluate;
8005{
8006 char_u *string = NULL;
8007 int len;
8008 int cc;
8009 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008010 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011
8012 ++*arg;
8013 name = *arg;
8014 len = get_env_len(arg);
8015 if (evaluate)
8016 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008017 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008018 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008019
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008020 cc = name[len];
8021 name[len] = NUL;
8022 /* first try vim_getenv(), fast for normal environment vars */
8023 string = vim_getenv(name, &mustfree);
8024 if (string != NULL && *string != NUL)
8025 {
8026 if (!mustfree)
8027 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008029 else
8030 {
8031 if (mustfree)
8032 vim_free(string);
8033
8034 /* next try expanding things like $VIM and ${HOME} */
8035 string = expand_env_save(name - 1);
8036 if (string != NULL && *string == '$')
8037 {
8038 vim_free(string);
8039 string = NULL;
8040 }
8041 }
8042 name[len] = cc;
8043
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044 rettv->v_type = VAR_STRING;
8045 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
8047
8048 return OK;
8049}
8050
8051/*
8052 * Array with names and number of arguments of all internal functions
8053 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8054 */
8055static struct fst
8056{
8057 char *f_name; /* function name */
8058 char f_min_argc; /* minimal number of arguments */
8059 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008060 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008061 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062} functions[] =
8063{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008064#ifdef FEAT_FLOAT
8065 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008066 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008068 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008069 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"append", 2, 2, f_append},
8071 {"argc", 0, 0, f_argc},
8072 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008073 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008074 {"argv", 0, 1, f_argv},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008075 {"assert_equal", 2, 3, f_assert_equal},
8076 {"assert_false", 1, 2, f_assert_false},
8077 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008079 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008081 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008084 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"bufexists", 1, 1, f_bufexists},
8086 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8087 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8088 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8089 {"buflisted", 1, 1, f_buflisted},
8090 {"bufloaded", 1, 1, f_bufloaded},
8091 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008092 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"bufwinnr", 1, 1, f_bufwinnr},
8094 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008095 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008096 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008097 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008098#ifdef FEAT_FLOAT
8099 {"ceil", 1, 1, f_ceil},
8100#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008101 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008102 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008104 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008106#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008107 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008108 {"complete_add", 1, 1, f_complete_add},
8109 {"complete_check", 0, 0, f_complete_check},
8110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008112 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#ifdef FEAT_FLOAT
8114 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008115 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008117 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008119 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008120 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"delete", 1, 1, f_delete},
8122 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008123 {"diff_filler", 1, 1, f_diff_filler},
8124 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008125 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008127 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"eventhandler", 0, 0, f_eventhandler},
8129 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008130 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008132#ifdef FEAT_FLOAT
8133 {"exp", 1, 1, f_exp},
8134#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008135 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008136 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008137 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8139 {"filereadable", 1, 1, f_filereadable},
8140 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008141 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008142 {"finddir", 1, 3, f_finddir},
8143 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008144#ifdef FEAT_FLOAT
8145 {"float2nr", 1, 1, f_float2nr},
8146 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008147 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008148#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008149 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"fnamemodify", 2, 2, f_fnamemodify},
8151 {"foldclosed", 1, 1, f_foldclosed},
8152 {"foldclosedend", 1, 1, f_foldclosedend},
8153 {"foldlevel", 1, 1, f_foldlevel},
8154 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008155 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008157 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008158 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008159 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008160 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008161 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"getchar", 0, 1, f_getchar},
8163 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008164 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"getcmdline", 0, 0, f_getcmdline},
8166 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008167 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008168 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008169 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008171 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008172 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"getfsize", 1, 1, f_getfsize},
8174 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008175 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008176 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008177 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008178 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008179 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008180 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008181 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008182 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008184 {"gettabvar", 2, 3, f_gettabvar},
8185 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"getwinposx", 0, 0, f_getwinposx},
8187 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008188 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008189 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008190 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008191 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008194 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008195 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8197 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8198 {"histadd", 2, 2, f_histadd},
8199 {"histdel", 1, 2, f_histdel},
8200 {"histget", 1, 2, f_histget},
8201 {"histnr", 1, 1, f_histnr},
8202 {"hlID", 1, 1, f_hlID},
8203 {"hlexists", 1, 1, f_hlexists},
8204 {"hostname", 0, 0, f_hostname},
8205 {"iconv", 3, 3, f_iconv},
8206 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008207 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008208 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008210 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"inputrestore", 0, 0, f_inputrestore},
8212 {"inputsave", 0, 0, f_inputsave},
8213 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008214 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008215 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008217 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008218 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008219 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008220 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008222 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"libcall", 3, 3, f_libcall},
8224 {"libcallnr", 3, 3, f_libcallnr},
8225 {"line", 1, 1, f_line},
8226 {"line2byte", 1, 1, f_line2byte},
8227 {"lispindent", 1, 1, f_lispindent},
8228 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008230 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008231 {"log10", 1, 1, f_log10},
8232#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008233#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008234 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008235#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008236 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008237 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008238 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008239 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008240 {"matchadd", 2, 5, f_matchadd},
8241 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008242 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008243 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008244 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008245 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008246 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008247 {"max", 1, 1, f_max},
8248 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008249#ifdef vim_mkdir
8250 {"mkdir", 1, 3, f_mkdir},
8251#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008252 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008253#ifdef FEAT_MZSCHEME
8254 {"mzeval", 1, 1, f_mzeval},
8255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008257 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008258 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008259 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008260#ifdef FEAT_FLOAT
8261 {"pow", 2, 2, f_pow},
8262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008264 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008265 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008266#ifdef FEAT_PYTHON3
8267 {"py3eval", 1, 1, f_py3eval},
8268#endif
8269#ifdef FEAT_PYTHON
8270 {"pyeval", 1, 1, f_pyeval},
8271#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008272 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008273 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008274 {"reltime", 0, 2, f_reltime},
8275 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"remote_expr", 2, 3, f_remote_expr},
8277 {"remote_foreground", 1, 1, f_remote_foreground},
8278 {"remote_peek", 1, 2, f_remote_peek},
8279 {"remote_read", 1, 1, f_remote_read},
8280 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008281 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008283 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008285 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008286#ifdef FEAT_FLOAT
8287 {"round", 1, 1, f_round},
8288#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008289 {"screenattr", 2, 2, f_screenattr},
8290 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008291 {"screencol", 0, 0, f_screencol},
8292 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008293 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008294 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008295 {"searchpair", 3, 7, f_searchpair},
8296 {"searchpairpos", 3, 7, f_searchpairpos},
8297 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"server2client", 2, 2, f_server2client},
8299 {"serverlist", 0, 0, f_serverlist},
8300 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008301 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"setcmdpos", 1, 1, f_setcmdpos},
8303 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008304 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008305 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008306 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008307 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008309 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008310 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008312#ifdef FEAT_CRYPT
8313 {"sha256", 1, 1, f_sha256},
8314#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008315 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008316 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008318#ifdef FEAT_FLOAT
8319 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008320 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008321#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008322 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008323 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008324 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008325 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008326 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327#ifdef FEAT_FLOAT
8328 {"sqrt", 1, 1, f_sqrt},
8329 {"str2float", 1, 1, f_str2float},
8330#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008331 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008332 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008333 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334#ifdef HAVE_STRFTIME
8335 {"strftime", 1, 2, f_strftime},
8336#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008338 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"strlen", 1, 1, f_strlen},
8340 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008341 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008343 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008344 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {"substitute", 4, 4, f_substitute},
8346 {"synID", 3, 3, f_synID},
8347 {"synIDattr", 2, 3, f_synIDattr},
8348 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008349 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008350 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008351 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008352 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008353 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008354 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008355 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008356 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008357 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008358#ifdef FEAT_FLOAT
8359 {"tan", 1, 1, f_tan},
8360 {"tanh", 1, 1, f_tanh},
8361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008363 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"tolower", 1, 1, f_tolower},
8365 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008366 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008367#ifdef FEAT_FLOAT
8368 {"trunc", 1, 1, f_trunc},
8369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008371 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008372 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008373 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008374 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {"virtcol", 1, 1, f_virtcol},
8376 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008377 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"winbufnr", 1, 1, f_winbufnr},
8379 {"wincol", 0, 0, f_wincol},
8380 {"winheight", 1, 1, f_winheight},
8381 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008382 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008384 {"winrestview", 1, 1, f_winrestview},
8385 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008387 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008388 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389};
8390
8391#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8392
8393/*
8394 * Function given to ExpandGeneric() to obtain the list of internal
8395 * or user defined function names.
8396 */
8397 char_u *
8398get_function_name(xp, idx)
8399 expand_T *xp;
8400 int idx;
8401{
8402 static int intidx = -1;
8403 char_u *name;
8404
8405 if (idx == 0)
8406 intidx = -1;
8407 if (intidx < 0)
8408 {
8409 name = get_user_func_name(xp, idx);
8410 if (name != NULL)
8411 return name;
8412 }
8413 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8414 {
8415 STRCPY(IObuff, functions[intidx].f_name);
8416 STRCAT(IObuff, "(");
8417 if (functions[intidx].f_max_argc == 0)
8418 STRCAT(IObuff, ")");
8419 return IObuff;
8420 }
8421
8422 return NULL;
8423}
8424
8425/*
8426 * Function given to ExpandGeneric() to obtain the list of internal or
8427 * user defined variable or function names.
8428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 char_u *
8430get_expr_name(xp, idx)
8431 expand_T *xp;
8432 int idx;
8433{
8434 static int intidx = -1;
8435 char_u *name;
8436
8437 if (idx == 0)
8438 intidx = -1;
8439 if (intidx < 0)
8440 {
8441 name = get_function_name(xp, idx);
8442 if (name != NULL)
8443 return name;
8444 }
8445 return get_user_var_name(xp, ++intidx);
8446}
8447
8448#endif /* FEAT_CMDL_COMPL */
8449
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008450#if defined(EBCDIC) || defined(PROTO)
8451/*
8452 * Compare struct fst by function name.
8453 */
8454 static int
8455compare_func_name(s1, s2)
8456 const void *s1;
8457 const void *s2;
8458{
8459 struct fst *p1 = (struct fst *)s1;
8460 struct fst *p2 = (struct fst *)s2;
8461
8462 return STRCMP(p1->f_name, p2->f_name);
8463}
8464
8465/*
8466 * Sort the function table by function name.
8467 * The sorting of the table above is ASCII dependant.
8468 * On machines using EBCDIC we have to sort it.
8469 */
8470 static void
8471sortFunctions()
8472{
8473 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8474
8475 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8476}
8477#endif
8478
8479
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480/*
8481 * Find internal function in table above.
8482 * Return index, or -1 if not found
8483 */
8484 static int
8485find_internal_func(name)
8486 char_u *name; /* name of the function */
8487{
8488 int first = 0;
8489 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8490 int cmp;
8491 int x;
8492
8493 /*
8494 * Find the function name in the table. Binary search.
8495 */
8496 while (first <= last)
8497 {
8498 x = first + ((unsigned)(last - first) >> 1);
8499 cmp = STRCMP(name, functions[x].f_name);
8500 if (cmp < 0)
8501 last = x - 1;
8502 else if (cmp > 0)
8503 first = x + 1;
8504 else
8505 return x;
8506 }
8507 return -1;
8508}
8509
8510/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008511 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8512 * name it contains, otherwise return "name".
8513 */
8514 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008515deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 char_u *name;
8517 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008518 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519{
Bram Moolenaar33570922005-01-25 22:26:29 +00008520 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008521 int cc;
8522
8523 cc = name[*lenp];
8524 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008525 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008527 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 {
8531 *lenp = 0;
8532 return (char_u *)""; /* just in case */
8533 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008534 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008535 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008536 }
8537
8538 return name;
8539}
8540
8541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 * Allocate a variable for the result of a function.
8543 * Return OK or FAIL.
8544 */
8545 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008546get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8547 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 char_u *name; /* name of the function */
8549 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 char_u **arg; /* argument, pointing to the '(' */
8552 linenr_T firstline; /* first line of range */
8553 linenr_T lastline; /* last line of range */
8554 int *doesrange; /* return: function handled range */
8555 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008556 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558 char_u *argp;
8559 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008560 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 int argcount = 0; /* number of arguments found */
8562
8563 /*
8564 * Get the arguments.
8565 */
8566 argp = *arg;
8567 while (argcount < MAX_FUNC_ARGS)
8568 {
8569 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8570 if (*argp == ')' || *argp == ',' || *argp == NUL)
8571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8573 {
8574 ret = FAIL;
8575 break;
8576 }
8577 ++argcount;
8578 if (*argp != ',')
8579 break;
8580 }
8581 if (*argp == ')')
8582 ++argp;
8583 else
8584 ret = FAIL;
8585
8586 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008588 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 {
8591 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008592 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008594 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596
8597 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600 *arg = skipwhite(argp);
8601 return ret;
8602}
8603
8604
8605/*
8606 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008607 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008608 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 */
8610 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008611call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008612 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008613 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008615 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008617 typval_T *argvars; /* vars for arguments, must have "argcount"
8618 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 linenr_T firstline; /* first line of range */
8620 linenr_T lastline; /* last line of range */
8621 int *doesrange; /* return: function handled range */
8622 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
8625 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626#define ERROR_UNKNOWN 0
8627#define ERROR_TOOMANY 1
8628#define ERROR_TOOFEW 2
8629#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008630#define ERROR_DICT 4
8631#define ERROR_NONE 5
8632#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 int error = ERROR_NONE;
8634 int i;
8635 int llen;
8636 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637#define FLEN_FIXED 40
8638 char_u fname_buf[FLEN_FIXED + 1];
8639 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008640 char_u *name;
8641
8642 /* Make a copy of the name, if it comes from a funcref variable it could
8643 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008644 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008645 if (name == NULL)
8646 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
8648 /*
8649 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8650 * Change <SNR>123_name() to K_SNR 123_name().
8651 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 llen = eval_fname_script(name);
8654 if (llen > 0)
8655 {
8656 fname_buf[0] = K_SPECIAL;
8657 fname_buf[1] = KS_EXTRA;
8658 fname_buf[2] = (int)KE_SNR;
8659 i = 3;
8660 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8661 {
8662 if (current_SID <= 0)
8663 error = ERROR_SCRIPT;
8664 else
8665 {
8666 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8667 i = (int)STRLEN(fname_buf);
8668 }
8669 }
8670 if (i + STRLEN(name + llen) < FLEN_FIXED)
8671 {
8672 STRCPY(fname_buf + i, name + llen);
8673 fname = fname_buf;
8674 }
8675 else
8676 {
8677 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8678 if (fname == NULL)
8679 error = ERROR_OTHER;
8680 else
8681 {
8682 mch_memmove(fname, fname_buf, (size_t)i);
8683 STRCPY(fname + i, name + llen);
8684 }
8685 }
8686 }
8687 else
8688 fname = name;
8689
8690 *doesrange = FALSE;
8691
8692
8693 /* execute the function if no errors detected and executing */
8694 if (evaluate && error == ERROR_NONE)
8695 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008696 char_u *rfname = fname;
8697
8698 /* Ignore "g:" before a function name. */
8699 if (fname[0] == 'g' && fname[1] == ':')
8700 rfname = fname + 2;
8701
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008702 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8703 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 error = ERROR_UNKNOWN;
8705
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008706 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {
8708 /*
8709 * User defined function.
8710 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008712
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008714 /* Trigger FuncUndefined event, may load the function. */
8715 if (fp == NULL
8716 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008717 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008718 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008720 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008721 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 }
8723#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008724 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008725 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008726 {
8727 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008728 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008729 }
8730
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 if (fp != NULL)
8732 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008733 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008735 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008737 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008739 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008740 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 else
8742 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008743 int did_save_redo = FALSE;
8744
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 /*
8746 * Call the user function.
8747 * Save and restore search patterns, script variables and
8748 * redo buffer.
8749 */
8750 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008751 if (!ins_compl_active())
8752 {
8753 saveRedobuff();
8754 did_save_redo = TRUE;
8755 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008756 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008758 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008759 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8760 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8761 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008762 /* Function was unreferenced while being used, free it
8763 * now. */
8764 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008765 if (did_save_redo)
8766 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 restore_search_patterns();
8768 error = ERROR_NONE;
8769 }
8770 }
8771 }
8772 else
8773 {
8774 /*
8775 * Find the function name in the table, call its implementation.
8776 */
8777 i = find_internal_func(fname);
8778 if (i >= 0)
8779 {
8780 if (argcount < functions[i].f_min_argc)
8781 error = ERROR_TOOFEW;
8782 else if (argcount > functions[i].f_max_argc)
8783 error = ERROR_TOOMANY;
8784 else
8785 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008786 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 error = ERROR_NONE;
8789 }
8790 }
8791 }
8792 /*
8793 * The function call (or "FuncUndefined" autocommand sequence) might
8794 * have been aborted by an error, an interrupt, or an explicitly thrown
8795 * exception that has not been caught so far. This situation can be
8796 * tested for by calling aborting(). For an error in an internal
8797 * function or for the "E132" error in call_user_func(), however, the
8798 * throw point at which the "force_abort" flag (temporarily reset by
8799 * emsg()) is normally updated has not been reached yet. We need to
8800 * update that flag first to make aborting() reliable.
8801 */
8802 update_force_abort();
8803 }
8804 if (error == ERROR_NONE)
8805 ret = OK;
8806
8807 /*
8808 * Report an error unless the argument evaluation or function call has been
8809 * cancelled due to an aborting error, an interrupt, or an exception.
8810 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008811 if (!aborting())
8812 {
8813 switch (error)
8814 {
8815 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008817 break;
8818 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008819 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008820 break;
8821 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008822 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008823 name);
8824 break;
8825 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008826 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008827 name);
8828 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008830 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008831 name);
8832 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008833 }
8834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (fname != name && fname != fname_buf)
8837 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008838 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
8840 return ret;
8841}
8842
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008843/*
8844 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008845 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008846 */
8847 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008848emsg_funcname(ermsg, name)
8849 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008850 char_u *name;
8851{
8852 char_u *p;
8853
8854 if (*name == K_SPECIAL)
8855 p = concat_str((char_u *)"<SNR>", name + 3);
8856 else
8857 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008858 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008859 if (p != name)
8860 vim_free(p);
8861}
8862
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008863/*
8864 * Return TRUE for a non-zero Number and a non-empty String.
8865 */
8866 static int
8867non_zero_arg(argvars)
8868 typval_T *argvars;
8869{
8870 return ((argvars[0].v_type == VAR_NUMBER
8871 && argvars[0].vval.v_number != 0)
8872 || (argvars[0].v_type == VAR_STRING
8873 && argvars[0].vval.v_string != NULL
8874 && *argvars[0].vval.v_string != NUL));
8875}
8876
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877/*********************************************
8878 * Implementation of the built-in functions
8879 */
8880
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008881#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008882static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8883
8884/*
8885 * Get the float value of "argvars[0]" into "f".
8886 * Returns FAIL when the argument is not a Number or Float.
8887 */
8888 static int
8889get_float_arg(argvars, f)
8890 typval_T *argvars;
8891 float_T *f;
8892{
8893 if (argvars[0].v_type == VAR_FLOAT)
8894 {
8895 *f = argvars[0].vval.v_float;
8896 return OK;
8897 }
8898 if (argvars[0].v_type == VAR_NUMBER)
8899 {
8900 *f = (float_T)argvars[0].vval.v_number;
8901 return OK;
8902 }
8903 EMSG(_("E808: Number or Float required"));
8904 return FAIL;
8905}
8906
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008907/*
8908 * "abs(expr)" function
8909 */
8910 static void
8911f_abs(argvars, rettv)
8912 typval_T *argvars;
8913 typval_T *rettv;
8914{
8915 if (argvars[0].v_type == VAR_FLOAT)
8916 {
8917 rettv->v_type = VAR_FLOAT;
8918 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8919 }
8920 else
8921 {
8922 varnumber_T n;
8923 int error = FALSE;
8924
8925 n = get_tv_number_chk(&argvars[0], &error);
8926 if (error)
8927 rettv->vval.v_number = -1;
8928 else if (n > 0)
8929 rettv->vval.v_number = n;
8930 else
8931 rettv->vval.v_number = -n;
8932 }
8933}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008934
8935/*
8936 * "acos()" function
8937 */
8938 static void
8939f_acos(argvars, rettv)
8940 typval_T *argvars;
8941 typval_T *rettv;
8942{
8943 float_T f;
8944
8945 rettv->v_type = VAR_FLOAT;
8946 if (get_float_arg(argvars, &f) == OK)
8947 rettv->vval.v_float = acos(f);
8948 else
8949 rettv->vval.v_float = 0.0;
8950}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008951#endif
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008954 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 */
8956 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008957f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *argvars;
8959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
Bram Moolenaar33570922005-01-25 22:26:29 +00008961 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008963 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008966 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008967 && !tv_check_lock(l->lv_lock,
8968 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008969 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008971 }
8972 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008973 EMSG(_(e_listreq));
8974}
8975
8976/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008977 * "and(expr, expr)" function
8978 */
8979 static void
8980f_and(argvars, rettv)
8981 typval_T *argvars;
8982 typval_T *rettv;
8983{
8984 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8985 & get_tv_number_chk(&argvars[1], NULL);
8986}
8987
8988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989 * "append(lnum, string/list)" function
8990 */
8991 static void
8992f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008993 typval_T *argvars;
8994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008995{
8996 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008997 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 list_T *l = NULL;
8999 listitem_T *li = NULL;
9000 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001 long added = 0;
9002
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009003 /* When coming here from Insert mode, sync undo, so that this can be
9004 * undone separately from what was previously inserted. */
9005 if (u_sync_once == 2)
9006 {
9007 u_sync_once = 1; /* notify that u_sync() was called */
9008 u_sync(TRUE);
9009 }
9010
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011 lnum = get_tv_lnum(argvars);
9012 if (lnum >= 0
9013 && lnum <= curbuf->b_ml.ml_line_count
9014 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009016 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009017 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009018 l = argvars[1].vval.v_list;
9019 if (l == NULL)
9020 return;
9021 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023 for (;;)
9024 {
9025 if (l == NULL)
9026 tv = &argvars[1]; /* append a string */
9027 else if (li == NULL)
9028 break; /* end of list */
9029 else
9030 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 line = get_tv_string_chk(tv);
9032 if (line == NULL) /* type error */
9033 {
9034 rettv->vval.v_number = 1; /* Failed */
9035 break;
9036 }
9037 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009038 ++added;
9039 if (l == NULL)
9040 break;
9041 li = li->li_next;
9042 }
9043
9044 appended_lines_mark(lnum, added);
9045 if (curwin->w_cursor.lnum > lnum)
9046 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 else
9049 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050}
9051
9052/*
9053 * "argc()" function
9054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009060 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061}
9062
9063/*
9064 * "argidx()" function
9065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009068 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
9074/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009075 * "arglistid()" function
9076 */
9077 static void
9078f_arglistid(argvars, rettv)
9079 typval_T *argvars UNUSED;
9080 typval_T *rettv;
9081{
9082 win_T *wp;
9083 tabpage_T *tp = NULL;
9084 long n;
9085
9086 rettv->vval.v_number = -1;
9087 if (argvars[0].v_type != VAR_UNKNOWN)
9088 {
9089 if (argvars[1].v_type != VAR_UNKNOWN)
9090 {
9091 n = get_tv_number(&argvars[1]);
9092 if (n >= 0)
9093 tp = find_tabpage(n);
9094 }
9095 else
9096 tp = curtab;
9097
9098 if (tp != NULL)
9099 {
9100 wp = find_win_by_nr(&argvars[0], tp);
9101 if (wp != NULL)
9102 rettv->vval.v_number = wp->w_alist->id;
9103 }
9104 }
9105 else
9106 rettv->vval.v_number = curwin->w_alist->id;
9107}
9108
9109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 * "argv(nr)" function
9111 */
9112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009114 typval_T *argvars;
9115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116{
9117 int idx;
9118
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009119 if (argvars[0].v_type != VAR_UNKNOWN)
9120 {
9121 idx = get_tv_number_chk(&argvars[0], NULL);
9122 if (idx >= 0 && idx < ARGCOUNT)
9123 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9124 else
9125 rettv->vval.v_string = NULL;
9126 rettv->v_type = VAR_STRING;
9127 }
9128 else if (rettv_list_alloc(rettv) == OK)
9129 for (idx = 0; idx < ARGCOUNT; ++idx)
9130 list_append_string(rettv->vval.v_list,
9131 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132}
9133
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009134static void prepare_assert_error __ARGS((garray_T*gap));
9135static 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));
9136static void assert_error __ARGS((garray_T *gap));
9137static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9138
9139/*
9140 * Prepare "gap" for an assert error and add the sourcing position.
9141 */
9142 static void
9143prepare_assert_error(gap)
9144 garray_T *gap;
9145{
9146 char buf[NUMBUFLEN];
9147
9148 ga_init2(gap, 1, 100);
9149 ga_concat(gap, sourcing_name);
9150 sprintf(buf, " line %ld", (long)sourcing_lnum);
9151 ga_concat(gap, (char_u *)buf);
9152 ga_concat(gap, (char_u *)": ");
9153}
9154
9155/*
9156 * Fill "gap" with information about an assert error.
9157 */
9158 static void
9159fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9160 garray_T *gap;
9161 typval_T *opt_msg_tv;
9162 char_u *exp_str;
9163 typval_T *exp_tv;
9164 typval_T *got_tv;
9165{
9166 char_u numbuf[NUMBUFLEN];
9167 char_u *tofree;
9168
9169 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9170 {
9171 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9172 vim_free(tofree);
9173 }
9174 else
9175 {
9176 ga_concat(gap, (char_u *)"Expected ");
9177 if (exp_str == NULL)
9178 {
9179 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9180 vim_free(tofree);
9181 }
9182 else
9183 ga_concat(gap, exp_str);
9184 ga_concat(gap, (char_u *)" but got ");
9185 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9186 vim_free(tofree);
9187 }
9188}
Bram Moolenaar43345542015-11-29 17:35:35 +01009189
9190/*
9191 * Add an assert error to v:errors.
9192 */
9193 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009194assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009195 garray_T *gap;
9196{
9197 struct vimvar *vp = &vimvars[VV_ERRORS];
9198
9199 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9200 /* Make sure v:errors is a list. */
9201 set_vim_var_list(VV_ERRORS, list_alloc());
9202 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9203}
9204
Bram Moolenaar43345542015-11-29 17:35:35 +01009205/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009206 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009207 */
9208 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009209f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009210 typval_T *argvars;
9211 typval_T *rettv UNUSED;
9212{
9213 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009214
9215 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9216 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009217 prepare_assert_error(&ga);
9218 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9219 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009220 ga_clear(&ga);
9221 }
9222}
9223
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009224/*
9225 * Common for assert_true() and assert_false().
9226 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009227 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009228assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009229 typval_T *argvars;
9230 int isTrue;
9231{
9232 int error = FALSE;
9233 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009234
9235 if (argvars[0].v_type != VAR_NUMBER
9236 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9237 || error)
9238 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009239 prepare_assert_error(&ga);
9240 fill_assert_error(&ga, &argvars[1],
9241 (char_u *)(isTrue ? "True " : "False "),
9242 NULL, &argvars[0]);
9243 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009244 ga_clear(&ga);
9245 }
9246}
9247
9248/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009249 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009250 */
9251 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009252f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009253 typval_T *argvars;
9254 typval_T *rettv UNUSED;
9255{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009257}
9258
9259/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009260 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009261 */
9262 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009263f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009264 typval_T *argvars;
9265 typval_T *rettv UNUSED;
9266{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009267 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009268}
9269
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009270#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009272 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009273 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009274 static void
9275f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009276 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009277 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009278{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009279 float_T f;
9280
9281 rettv->v_type = VAR_FLOAT;
9282 if (get_float_arg(argvars, &f) == OK)
9283 rettv->vval.v_float = asin(f);
9284 else
9285 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009286}
9287
9288/*
9289 * "atan()" function
9290 */
9291 static void
9292f_atan(argvars, rettv)
9293 typval_T *argvars;
9294 typval_T *rettv;
9295{
9296 float_T f;
9297
9298 rettv->v_type = VAR_FLOAT;
9299 if (get_float_arg(argvars, &f) == OK)
9300 rettv->vval.v_float = atan(f);
9301 else
9302 rettv->vval.v_float = 0.0;
9303}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009304
9305/*
9306 * "atan2()" function
9307 */
9308 static void
9309f_atan2(argvars, rettv)
9310 typval_T *argvars;
9311 typval_T *rettv;
9312{
9313 float_T fx, fy;
9314
9315 rettv->v_type = VAR_FLOAT;
9316 if (get_float_arg(argvars, &fx) == OK
9317 && get_float_arg(&argvars[1], &fy) == OK)
9318 rettv->vval.v_float = atan2(fx, fy);
9319 else
9320 rettv->vval.v_float = 0.0;
9321}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009322#endif
9323
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324/*
9325 * "browse(save, title, initdir, default)" function
9326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331{
9332#ifdef FEAT_BROWSE
9333 int save;
9334 char_u *title;
9335 char_u *initdir;
9336 char_u *defname;
9337 char_u buf[NUMBUFLEN];
9338 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 save = get_tv_number_chk(&argvars[0], &error);
9342 title = get_tv_string_chk(&argvars[1]);
9343 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9344 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 if (error || title == NULL || initdir == NULL || defname == NULL)
9347 rettv->vval.v_string = NULL;
9348 else
9349 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009350 do_browse(save ? BROWSE_SAVE : 0,
9351 title, defname, NULL, initdir, NULL, curbuf);
9352#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009354#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009356}
9357
9358/*
9359 * "browsedir(title, initdir)" function
9360 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009363 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009364 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009365{
9366#ifdef FEAT_BROWSE
9367 char_u *title;
9368 char_u *initdir;
9369 char_u buf[NUMBUFLEN];
9370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009371 title = get_tv_string_chk(&argvars[0]);
9372 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009374 if (title == NULL || initdir == NULL)
9375 rettv->vval.v_string = NULL;
9376 else
9377 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009378 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383}
9384
Bram Moolenaar33570922005-01-25 22:26:29 +00009385static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009386
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387/*
9388 * Find a buffer by number or exact name.
9389 */
9390 static buf_T *
9391find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009392 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009396 if (avar->v_type == VAR_NUMBER)
9397 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009398 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009400 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009401 if (buf == NULL)
9402 {
9403 /* No full path name match, try a match with a URL or a "nofile"
9404 * buffer, these don't use the full path. */
9405 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9406 if (buf->b_fname != NULL
9407 && (path_with_url(buf->b_fname)
9408#ifdef FEAT_QUICKFIX
9409 || bt_nofile(buf)
9410#endif
9411 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009412 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009413 break;
9414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 }
9416 return buf;
9417}
9418
9419/*
9420 * "bufexists(expr)" function
9421 */
9422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009424 typval_T *argvars;
9425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428}
9429
9430/*
9431 * "buflisted(expr)" function
9432 */
9433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009435 typval_T *argvars;
9436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
9438 buf_T *buf;
9439
9440 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442}
9443
9444/*
9445 * "bufloaded(expr)" function
9446 */
9447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *argvars;
9450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 buf_T *buf;
9453
9454 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456}
9457
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009458static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009459
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460/*
9461 * Get buffer by number or pattern.
9462 */
9463 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009464get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009465 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009466 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 int save_magic;
9470 char_u *save_cpo;
9471 buf_T *buf;
9472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 if (tv->v_type == VAR_NUMBER)
9474 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009475 if (tv->v_type != VAR_STRING)
9476 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (name == NULL || *name == NUL)
9478 return curbuf;
9479 if (name[0] == '$' && name[1] == NUL)
9480 return lastbuf;
9481
9482 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9483 save_magic = p_magic;
9484 p_magic = TRUE;
9485 save_cpo = p_cpo;
9486 p_cpo = (char_u *)"";
9487
9488 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009489 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490
9491 p_magic = save_magic;
9492 p_cpo = save_cpo;
9493
9494 /* If not found, try expanding the name, like done for bufexists(). */
9495 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497
9498 return buf;
9499}
9500
9501/*
9502 * "bufname(expr)" function
9503 */
9504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009506 typval_T *argvars;
9507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
9509 buf_T *buf;
9510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009511 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009513 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 --emsg_off;
9520}
9521
9522/*
9523 * "bufnr(expr)" function
9524 */
9525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009527 typval_T *argvars;
9528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009531 int error = FALSE;
9532 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009536 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009537 --emsg_off;
9538
9539 /* If the buffer isn't found and the second argument is not zero create a
9540 * new buffer. */
9541 if (buf == NULL
9542 && argvars[1].v_type != VAR_UNKNOWN
9543 && get_tv_number_chk(&argvars[1], &error) != 0
9544 && !error
9545 && (name = get_tv_string_chk(&argvars[0])) != NULL
9546 && !error)
9547 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9548
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553}
9554
9555/*
9556 * "bufwinnr(nr)" function
9557 */
9558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009560 typval_T *argvars;
9561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563#ifdef FEAT_WINDOWS
9564 win_T *wp;
9565 int winnr = 0;
9566#endif
9567 buf_T *buf;
9568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009571 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572#ifdef FEAT_WINDOWS
9573 for (wp = firstwin; wp; wp = wp->w_next)
9574 {
9575 ++winnr;
9576 if (wp->w_buffer == buf)
9577 break;
9578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582#endif
9583 --emsg_off;
9584}
9585
9586/*
9587 * "byte2line(byte)" function
9588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009591 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596#else
9597 long boff = 0;
9598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009599 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 (linenr_T)0, &boff);
9605#endif
9606}
9607
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009608 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009609byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 typval_T *argvars;
9611 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009612 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009613{
9614#ifdef FEAT_MBYTE
9615 char_u *t;
9616#endif
9617 char_u *str;
9618 long idx;
9619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 str = get_tv_string_chk(&argvars[0]);
9621 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009624 return;
9625
9626#ifdef FEAT_MBYTE
9627 t = str;
9628 for ( ; idx > 0; idx--)
9629 {
9630 if (*t == NUL) /* EOL reached */
9631 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009632 if (enc_utf8 && comp)
9633 t += utf_ptr2len(t);
9634 else
9635 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009636 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009637 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009638#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009639 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009641#endif
9642}
9643
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009644/*
9645 * "byteidx()" function
9646 */
9647 static void
9648f_byteidx(argvars, rettv)
9649 typval_T *argvars;
9650 typval_T *rettv;
9651{
9652 byteidx(argvars, rettv, FALSE);
9653}
9654
9655/*
9656 * "byteidxcomp()" function
9657 */
9658 static void
9659f_byteidxcomp(argvars, rettv)
9660 typval_T *argvars;
9661 typval_T *rettv;
9662{
9663 byteidx(argvars, rettv, TRUE);
9664}
9665
Bram Moolenaardb913952012-06-29 12:54:53 +02009666 int
9667func_call(name, args, selfdict, rettv)
9668 char_u *name;
9669 typval_T *args;
9670 dict_T *selfdict;
9671 typval_T *rettv;
9672{
9673 listitem_T *item;
9674 typval_T argv[MAX_FUNC_ARGS + 1];
9675 int argc = 0;
9676 int dummy;
9677 int r = 0;
9678
9679 for (item = args->vval.v_list->lv_first; item != NULL;
9680 item = item->li_next)
9681 {
9682 if (argc == MAX_FUNC_ARGS)
9683 {
9684 EMSG(_("E699: Too many arguments"));
9685 break;
9686 }
9687 /* Make a copy of each argument. This is needed to be able to set
9688 * v_lock to VAR_FIXED in the copy without changing the original list.
9689 */
9690 copy_tv(&item->li_tv, &argv[argc++]);
9691 }
9692
9693 if (item == NULL)
9694 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9695 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9696 &dummy, TRUE, selfdict);
9697
9698 /* Free the arguments. */
9699 while (argc > 0)
9700 clear_tv(&argv[--argc]);
9701
9702 return r;
9703}
9704
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009705/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009706 * "call(func, arglist)" function
9707 */
9708 static void
9709f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 typval_T *argvars;
9711 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009712{
9713 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009715
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009716 if (argvars[1].v_type != VAR_LIST)
9717 {
9718 EMSG(_(e_listreq));
9719 return;
9720 }
9721 if (argvars[1].vval.v_list == NULL)
9722 return;
9723
9724 if (argvars[0].v_type == VAR_FUNC)
9725 func = argvars[0].vval.v_string;
9726 else
9727 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009728 if (*func == NUL)
9729 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009730
Bram Moolenaare9a41262005-01-15 22:18:47 +00009731 if (argvars[2].v_type != VAR_UNKNOWN)
9732 {
9733 if (argvars[2].v_type != VAR_DICT)
9734 {
9735 EMSG(_(e_dictreq));
9736 return;
9737 }
9738 selfdict = argvars[2].vval.v_dict;
9739 }
9740
Bram Moolenaardb913952012-06-29 12:54:53 +02009741 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009742}
9743
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009744#ifdef FEAT_FLOAT
9745/*
9746 * "ceil({float})" function
9747 */
9748 static void
9749f_ceil(argvars, rettv)
9750 typval_T *argvars;
9751 typval_T *rettv;
9752{
9753 float_T f;
9754
9755 rettv->v_type = VAR_FLOAT;
9756 if (get_float_arg(argvars, &f) == OK)
9757 rettv->vval.v_float = ceil(f);
9758 else
9759 rettv->vval.v_float = 0.0;
9760}
9761#endif
9762
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009763/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009764 * "changenr()" function
9765 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009766 static void
9767f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009768 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009769 typval_T *rettv;
9770{
9771 rettv->vval.v_number = curbuf->b_u_seq_cur;
9772}
9773
9774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 * "char2nr(string)" function
9776 */
9777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009779 typval_T *argvars;
9780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
9782#ifdef FEAT_MBYTE
9783 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009784 {
9785 int utf8 = 0;
9786
9787 if (argvars[1].v_type != VAR_UNKNOWN)
9788 utf8 = get_tv_number_chk(&argvars[1], NULL);
9789
9790 if (utf8)
9791 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9792 else
9793 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 else
9796#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
9800/*
9801 * "cindent(lnum)" function
9802 */
9803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009805 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
9808#ifdef FEAT_CINDENT
9809 pos_T pos;
9810 linenr_T lnum;
9811
9812 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9815 {
9816 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 curwin->w_cursor = pos;
9819 }
9820 else
9821#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823}
9824
9825/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009826 * "clearmatches()" function
9827 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009828 static void
9829f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009830 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009831 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009832{
9833#ifdef FEAT_SEARCH_EXTRA
9834 clear_matches(curwin);
9835#endif
9836}
9837
9838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 * "col(string)" function
9840 */
9841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 typval_T *argvars;
9844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846 colnr_T col = 0;
9847 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009848 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009850 fp = var2fpos(&argvars[0], FALSE, &fnum);
9851 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 {
9853 if (fp->col == MAXCOL)
9854 {
9855 /* '> can be MAXCOL, get the length of the line then */
9856 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009857 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 else
9859 col = MAXCOL;
9860 }
9861 else
9862 {
9863 col = fp->col + 1;
9864#ifdef FEAT_VIRTUALEDIT
9865 /* col(".") when the cursor is on the NUL at the end of the line
9866 * because of "coladd" can be seen as an extra column. */
9867 if (virtual_active() && fp == &curwin->w_cursor)
9868 {
9869 char_u *p = ml_get_cursor();
9870
9871 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9872 curwin->w_virtcol - curwin->w_cursor.coladd))
9873 {
9874# ifdef FEAT_MBYTE
9875 int l;
9876
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009877 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 col += l;
9879# else
9880 if (*p != NUL && p[1] == NUL)
9881 ++col;
9882# endif
9883 }
9884 }
9885#endif
9886 }
9887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889}
9890
Bram Moolenaar572cb562005-08-05 21:35:02 +00009891#if defined(FEAT_INS_EXPAND)
9892/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009893 * "complete()" function
9894 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009895 static void
9896f_complete(argvars, rettv)
9897 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009898 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009899{
9900 int startcol;
9901
9902 if ((State & INSERT) == 0)
9903 {
9904 EMSG(_("E785: complete() can only be used in Insert mode"));
9905 return;
9906 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009907
9908 /* Check for undo allowed here, because if something was already inserted
9909 * the line was already saved for undo and this check isn't done. */
9910 if (!undo_allowed())
9911 return;
9912
Bram Moolenaarade00832006-03-10 21:46:58 +00009913 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9914 {
9915 EMSG(_(e_invarg));
9916 return;
9917 }
9918
9919 startcol = get_tv_number_chk(&argvars[0], NULL);
9920 if (startcol <= 0)
9921 return;
9922
9923 set_completion(startcol - 1, argvars[1].vval.v_list);
9924}
9925
9926/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009927 * "complete_add()" function
9928 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009929 static void
9930f_complete_add(argvars, rettv)
9931 typval_T *argvars;
9932 typval_T *rettv;
9933{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009934 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009935}
9936
9937/*
9938 * "complete_check()" function
9939 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009940 static void
9941f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009942 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009943 typval_T *rettv;
9944{
9945 int saved = RedrawingDisabled;
9946
9947 RedrawingDisabled = 0;
9948 ins_compl_check_keys(0);
9949 rettv->vval.v_number = compl_interrupted;
9950 RedrawingDisabled = saved;
9951}
9952#endif
9953
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954/*
9955 * "confirm(message, buttons[, default [, type]])" function
9956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009958f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009959 typval_T *argvars UNUSED;
9960 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961{
9962#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9963 char_u *message;
9964 char_u *buttons = NULL;
9965 char_u buf[NUMBUFLEN];
9966 char_u buf2[NUMBUFLEN];
9967 int def = 1;
9968 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009969 char_u *typestr;
9970 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972 message = get_tv_string_chk(&argvars[0]);
9973 if (message == NULL)
9974 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009975 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009977 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9978 if (buttons == NULL)
9979 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009980 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009982 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009983 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009985 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9986 if (typestr == NULL)
9987 error = TRUE;
9988 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 switch (TOUPPER_ASC(*typestr))
9991 {
9992 case 'E': type = VIM_ERROR; break;
9993 case 'Q': type = VIM_QUESTION; break;
9994 case 'I': type = VIM_INFO; break;
9995 case 'W': type = VIM_WARNING; break;
9996 case 'G': type = VIM_GENERIC; break;
9997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 }
9999 }
10000 }
10001 }
10002
10003 if (buttons == NULL || *buttons == NUL)
10004 buttons = (char_u *)_("&Ok");
10005
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010006 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010007 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010008 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009#endif
10010}
10011
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010012/*
10013 * "copy()" function
10014 */
10015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010017 typval_T *argvars;
10018 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010019{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010020 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010021}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010023#ifdef FEAT_FLOAT
10024/*
10025 * "cos()" function
10026 */
10027 static void
10028f_cos(argvars, rettv)
10029 typval_T *argvars;
10030 typval_T *rettv;
10031{
10032 float_T f;
10033
10034 rettv->v_type = VAR_FLOAT;
10035 if (get_float_arg(argvars, &f) == OK)
10036 rettv->vval.v_float = cos(f);
10037 else
10038 rettv->vval.v_float = 0.0;
10039}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010040
10041/*
10042 * "cosh()" function
10043 */
10044 static void
10045f_cosh(argvars, rettv)
10046 typval_T *argvars;
10047 typval_T *rettv;
10048{
10049 float_T f;
10050
10051 rettv->v_type = VAR_FLOAT;
10052 if (get_float_arg(argvars, &f) == OK)
10053 rettv->vval.v_float = cosh(f);
10054 else
10055 rettv->vval.v_float = 0.0;
10056}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010057#endif
10058
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010060 * "count()" function
10061 */
10062 static void
10063f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010064 typval_T *argvars;
10065 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010066{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010067 long n = 0;
10068 int ic = FALSE;
10069
Bram Moolenaare9a41262005-01-15 22:18:47 +000010070 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010071 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010072 listitem_T *li;
10073 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010074 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010075
Bram Moolenaare9a41262005-01-15 22:18:47 +000010076 if ((l = argvars[0].vval.v_list) != NULL)
10077 {
10078 li = l->lv_first;
10079 if (argvars[2].v_type != VAR_UNKNOWN)
10080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 int error = FALSE;
10082
10083 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010084 if (argvars[3].v_type != VAR_UNKNOWN)
10085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 idx = get_tv_number_chk(&argvars[3], &error);
10087 if (!error)
10088 {
10089 li = list_find(l, idx);
10090 if (li == NULL)
10091 EMSGN(_(e_listidx), idx);
10092 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010093 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094 if (error)
10095 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010096 }
10097
10098 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010099 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010100 ++n;
10101 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010102 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010103 else if (argvars[0].v_type == VAR_DICT)
10104 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010105 int todo;
10106 dict_T *d;
10107 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010108
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010109 if ((d = argvars[0].vval.v_dict) != NULL)
10110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 int error = FALSE;
10112
Bram Moolenaare9a41262005-01-15 22:18:47 +000010113 if (argvars[2].v_type != VAR_UNKNOWN)
10114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010115 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010116 if (argvars[3].v_type != VAR_UNKNOWN)
10117 EMSG(_(e_invarg));
10118 }
10119
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010120 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010121 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010122 {
10123 if (!HASHITEM_EMPTY(hi))
10124 {
10125 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010126 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010127 ++n;
10128 }
10129 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010130 }
10131 }
10132 else
10133 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010134 rettv->vval.v_number = n;
10135}
10136
10137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10139 *
10140 * Checks the existence of a cscope connection.
10141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010144 typval_T *argvars UNUSED;
10145 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
10147#ifdef FEAT_CSCOPE
10148 int num = 0;
10149 char_u *dbpath = NULL;
10150 char_u *prepend = NULL;
10151 char_u buf[NUMBUFLEN];
10152
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010153 if (argvars[0].v_type != VAR_UNKNOWN
10154 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156 num = (int)get_tv_number(&argvars[0]);
10157 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010158 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 }
10161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163#endif
10164}
10165
10166/*
10167 * "cursor(lnum, col)" function
10168 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010169 * Moves the cursor to the specified line and column.
10170 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010174 typval_T *argvars;
10175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176{
10177 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010178#ifdef FEAT_VIRTUALEDIT
10179 long coladd = 0;
10180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010182 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010183 if (argvars[1].v_type == VAR_UNKNOWN)
10184 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010185 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010186 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010187
Bram Moolenaar493c1782014-05-28 14:34:46 +020010188 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010189 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010190 line = pos.lnum;
10191 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010192#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010193 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010194#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010195 if (curswant >= 0)
10196 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010197 }
10198 else
10199 {
10200 line = get_tv_lnum(argvars);
10201 col = get_tv_number_chk(&argvars[1], NULL);
10202#ifdef FEAT_VIRTUALEDIT
10203 if (argvars[2].v_type != VAR_UNKNOWN)
10204 coladd = get_tv_number_chk(&argvars[2], NULL);
10205#endif
10206 }
10207 if (line < 0 || col < 0
10208#ifdef FEAT_VIRTUALEDIT
10209 || coladd < 0
10210#endif
10211 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 if (line > 0)
10214 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 if (col > 0)
10216 curwin->w_cursor.col = col - 1;
10217#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010218 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219#endif
10220
10221 /* Make sure the cursor is in a valid position. */
10222 check_cursor();
10223#ifdef FEAT_MBYTE
10224 /* Correct cursor for multi-byte character. */
10225 if (has_mbyte)
10226 mb_adjust_cursor();
10227#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010228
10229 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010230 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231}
10232
10233/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010234 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 */
10236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 typval_T *argvars;
10239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010241 int noref = 0;
10242
10243 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010245 if (noref < 0 || noref > 1)
10246 EMSG(_(e_invarg));
10247 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010248 {
10249 current_copyID += COPYID_INC;
10250 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252}
10253
10254/*
10255 * "delete()" function
10256 */
10257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010258f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010259 typval_T *argvars;
10260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261{
10262 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010265 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266}
10267
10268/*
10269 * "did_filetype()" function
10270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010272f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010273 typval_T *argvars UNUSED;
10274 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275{
10276#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278#endif
10279}
10280
10281/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010282 * "diff_filler()" function
10283 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010286 typval_T *argvars UNUSED;
10287 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010288{
10289#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010290 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010291#endif
10292}
10293
10294/*
10295 * "diff_hlID()" function
10296 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010299 typval_T *argvars UNUSED;
10300 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010301{
10302#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010304 static linenr_T prev_lnum = 0;
10305 static int changedtick = 0;
10306 static int fnum = 0;
10307 static int change_start = 0;
10308 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010309 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010310 int filler_lines;
10311 int col;
10312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010313 if (lnum < 0) /* ignore type error in {lnum} arg */
10314 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010315 if (lnum != prev_lnum
10316 || changedtick != curbuf->b_changedtick
10317 || fnum != curbuf->b_fnum)
10318 {
10319 /* New line, buffer, change: need to get the values. */
10320 filler_lines = diff_check(curwin, lnum);
10321 if (filler_lines < 0)
10322 {
10323 if (filler_lines == -1)
10324 {
10325 change_start = MAXCOL;
10326 change_end = -1;
10327 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10328 hlID = HLF_ADD; /* added line */
10329 else
10330 hlID = HLF_CHD; /* changed line */
10331 }
10332 else
10333 hlID = HLF_ADD; /* added line */
10334 }
10335 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010336 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010337 prev_lnum = lnum;
10338 changedtick = curbuf->b_changedtick;
10339 fnum = curbuf->b_fnum;
10340 }
10341
10342 if (hlID == HLF_CHD || hlID == HLF_TXD)
10343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010345 if (col >= change_start && col <= change_end)
10346 hlID = HLF_TXD; /* changed text */
10347 else
10348 hlID = HLF_CHD; /* changed line */
10349 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010350 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010351#endif
10352}
10353
10354/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010355 * "empty({expr})" function
10356 */
10357 static void
10358f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010359 typval_T *argvars;
10360 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010361{
10362 int n;
10363
10364 switch (argvars[0].v_type)
10365 {
10366 case VAR_STRING:
10367 case VAR_FUNC:
10368 n = argvars[0].vval.v_string == NULL
10369 || *argvars[0].vval.v_string == NUL;
10370 break;
10371 case VAR_NUMBER:
10372 n = argvars[0].vval.v_number == 0;
10373 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010374#ifdef FEAT_FLOAT
10375 case VAR_FLOAT:
10376 n = argvars[0].vval.v_float == 0.0;
10377 break;
10378#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010379 case VAR_LIST:
10380 n = argvars[0].vval.v_list == NULL
10381 || argvars[0].vval.v_list->lv_first == NULL;
10382 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010383 case VAR_DICT:
10384 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010385 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010386 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010387 default:
10388 EMSG2(_(e_intern2), "f_empty()");
10389 n = 0;
10390 }
10391
10392 rettv->vval.v_number = n;
10393}
10394
10395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 * "escape({string}, {chars})" function
10397 */
10398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010399f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010400 typval_T *argvars;
10401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402{
10403 char_u buf[NUMBUFLEN];
10404
Bram Moolenaar758711c2005-02-02 23:11:38 +000010405 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10406 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010407 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408}
10409
10410/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010411 * "eval()" function
10412 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010413 static void
10414f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010415 typval_T *argvars;
10416 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010417{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010418 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 s = get_tv_string_chk(&argvars[0]);
10421 if (s != NULL)
10422 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010423
Bram Moolenaar615b9972015-01-14 17:15:05 +010010424 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010425 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10426 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010427 if (p != NULL && !aborting())
10428 EMSG2(_(e_invexpr2), p);
10429 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010431 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010433 else if (*s != NUL)
10434 EMSG(_(e_trailing));
10435}
10436
10437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 * "eventhandler()" function
10439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010442 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446}
10447
10448/*
10449 * "executable()" function
10450 */
10451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010453 typval_T *argvars;
10454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010456 char_u *name = get_tv_string(&argvars[0]);
10457
10458 /* Check in $PATH and also check directly if there is a directory name. */
10459 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10460 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010461}
10462
10463/*
10464 * "exepath()" function
10465 */
10466 static void
10467f_exepath(argvars, rettv)
10468 typval_T *argvars;
10469 typval_T *rettv;
10470{
10471 char_u *p = NULL;
10472
Bram Moolenaarb5971142015-03-21 17:32:19 +010010473 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010474 rettv->v_type = VAR_STRING;
10475 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476}
10477
10478/*
10479 * "exists()" function
10480 */
10481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482f_exists(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 *p;
10487 char_u *name;
10488 int n = FALSE;
10489 int len = 0;
10490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 if (*p == '$') /* environment variable */
10493 {
10494 /* first try "normal" environment variables (fast) */
10495 if (mch_getenv(p + 1) != NULL)
10496 n = TRUE;
10497 else
10498 {
10499 /* try expanding things like $VIM and ${HOME} */
10500 p = expand_env_save(p);
10501 if (p != NULL && *p != '$')
10502 n = TRUE;
10503 vim_free(p);
10504 }
10505 }
10506 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010509 if (*skipwhite(p) != NUL)
10510 n = FALSE; /* trailing garbage */
10511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 else if (*p == '*') /* internal or user defined function */
10513 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010514 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 }
10516 else if (*p == ':')
10517 {
10518 n = cmd_exists(p + 1);
10519 }
10520 else if (*p == '#')
10521 {
10522#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010523 if (p[1] == '#')
10524 n = autocmd_supported(p + 2);
10525 else
10526 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527#endif
10528 }
10529 else /* internal variable */
10530 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010531 char_u *tofree;
10532 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010534 /* get_name_len() takes care of expanding curly braces */
10535 name = p;
10536 len = get_name_len(&p, &tofree, TRUE, FALSE);
10537 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010539 if (tofree != NULL)
10540 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010541 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010542 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010544 /* handle d.key, l[idx], f(expr) */
10545 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10546 if (n)
10547 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 }
10549 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010550 if (*p != NUL)
10551 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010553 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 }
10555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557}
10558
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010559#ifdef FEAT_FLOAT
10560/*
10561 * "exp()" function
10562 */
10563 static void
10564f_exp(argvars, rettv)
10565 typval_T *argvars;
10566 typval_T *rettv;
10567{
10568 float_T f;
10569
10570 rettv->v_type = VAR_FLOAT;
10571 if (get_float_arg(argvars, &f) == OK)
10572 rettv->vval.v_float = exp(f);
10573 else
10574 rettv->vval.v_float = 0.0;
10575}
10576#endif
10577
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578/*
10579 * "expand()" function
10580 */
10581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 typval_T *argvars;
10584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
10586 char_u *s;
10587 int len;
10588 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010589 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010592 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010595 if (argvars[1].v_type != VAR_UNKNOWN
10596 && argvars[2].v_type != VAR_UNKNOWN
10597 && get_tv_number_chk(&argvars[2], &error)
10598 && !error)
10599 {
10600 rettv->v_type = VAR_LIST;
10601 rettv->vval.v_list = NULL;
10602 }
10603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010604 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 if (*s == '%' || *s == '#' || *s == '<')
10606 {
10607 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010608 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010610 if (rettv->v_type == VAR_LIST)
10611 {
10612 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10613 list_append_string(rettv->vval.v_list, result, -1);
10614 else
10615 vim_free(result);
10616 }
10617 else
10618 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 }
10620 else
10621 {
10622 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010623 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 if (argvars[1].v_type != VAR_UNKNOWN
10625 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010626 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 if (!error)
10628 {
10629 ExpandInit(&xpc);
10630 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010631 if (p_wic)
10632 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010633 if (rettv->v_type == VAR_STRING)
10634 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10635 options, WILD_ALL);
10636 else if (rettv_list_alloc(rettv) != FAIL)
10637 {
10638 int i;
10639
10640 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10641 for (i = 0; i < xpc.xp_numfiles; i++)
10642 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10643 ExpandCleanup(&xpc);
10644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 }
10646 else
10647 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 }
10649}
10650
10651/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010652 * Go over all entries in "d2" and add them to "d1".
10653 * When "action" is "error" then a duplicate key is an error.
10654 * When "action" is "force" then a duplicate key is overwritten.
10655 * Otherwise duplicate keys are ignored ("action" is "keep").
10656 */
10657 void
10658dict_extend(d1, d2, action)
10659 dict_T *d1;
10660 dict_T *d2;
10661 char_u *action;
10662{
10663 dictitem_T *di1;
10664 hashitem_T *hi2;
10665 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010666 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010667
10668 todo = (int)d2->dv_hashtab.ht_used;
10669 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10670 {
10671 if (!HASHITEM_EMPTY(hi2))
10672 {
10673 --todo;
10674 di1 = dict_find(d1, hi2->hi_key, -1);
10675 if (d1->dv_scope != 0)
10676 {
10677 /* Disallow replacing a builtin function in l: and g:.
10678 * Check the key to be valid when adding to any
10679 * scope. */
10680 if (d1->dv_scope == VAR_DEF_SCOPE
10681 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10682 && var_check_func_name(hi2->hi_key,
10683 di1 == NULL))
10684 break;
10685 if (!valid_varname(hi2->hi_key))
10686 break;
10687 }
10688 if (di1 == NULL)
10689 {
10690 di1 = dictitem_copy(HI2DI(hi2));
10691 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10692 dictitem_free(di1);
10693 }
10694 else if (*action == 'e')
10695 {
10696 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10697 break;
10698 }
10699 else if (*action == 'f' && HI2DI(hi2) != di1)
10700 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010701 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10702 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010703 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010704 clear_tv(&di1->di_tv);
10705 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10706 }
10707 }
10708 }
10709}
10710
10711/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010712 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010713 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010714 */
10715 static void
10716f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010717 typval_T *argvars;
10718 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010719{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010720 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010721
Bram Moolenaare9a41262005-01-15 22:18:47 +000010722 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010723 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010724 list_T *l1, *l2;
10725 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010726 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010728
Bram Moolenaare9a41262005-01-15 22:18:47 +000010729 l1 = argvars[0].vval.v_list;
10730 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010731 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010732 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010733 {
10734 if (argvars[2].v_type != VAR_UNKNOWN)
10735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 before = get_tv_number_chk(&argvars[2], &error);
10737 if (error)
10738 return; /* type error; errmsg already given */
10739
Bram Moolenaar758711c2005-02-02 23:11:38 +000010740 if (before == l1->lv_len)
10741 item = NULL;
10742 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010743 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010744 item = list_find(l1, before);
10745 if (item == NULL)
10746 {
10747 EMSGN(_(e_listidx), before);
10748 return;
10749 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010750 }
10751 }
10752 else
10753 item = NULL;
10754 list_extend(l1, l2, item);
10755
Bram Moolenaare9a41262005-01-15 22:18:47 +000010756 copy_tv(&argvars[0], rettv);
10757 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010758 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010759 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10760 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010761 dict_T *d1, *d2;
10762 char_u *action;
10763 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010764
10765 d1 = argvars[0].vval.v_dict;
10766 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010767 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010768 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769 {
10770 /* Check the third argument. */
10771 if (argvars[2].v_type != VAR_UNKNOWN)
10772 {
10773 static char *(av[]) = {"keep", "force", "error"};
10774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 action = get_tv_string_chk(&argvars[2]);
10776 if (action == NULL)
10777 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010778 for (i = 0; i < 3; ++i)
10779 if (STRCMP(action, av[i]) == 0)
10780 break;
10781 if (i == 3)
10782 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010783 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010784 return;
10785 }
10786 }
10787 else
10788 action = (char_u *)"force";
10789
Bram Moolenaara9922d62013-05-30 13:01:18 +020010790 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010791
Bram Moolenaare9a41262005-01-15 22:18:47 +000010792 copy_tv(&argvars[0], rettv);
10793 }
10794 }
10795 else
10796 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010797}
10798
10799/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010800 * "feedkeys()" function
10801 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010802 static void
10803f_feedkeys(argvars, rettv)
10804 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010805 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010806{
10807 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010808 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010809 char_u *keys, *flags;
10810 char_u nbuf[NUMBUFLEN];
10811 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010812 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010813
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010814 /* This is not allowed in the sandbox. If the commands would still be
10815 * executed in the sandbox it would be OK, but it probably happens later,
10816 * when "sandbox" is no longer set. */
10817 if (check_secure())
10818 return;
10819
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010820 keys = get_tv_string(&argvars[0]);
10821 if (*keys != NUL)
10822 {
10823 if (argvars[1].v_type != VAR_UNKNOWN)
10824 {
10825 flags = get_tv_string_buf(&argvars[1], nbuf);
10826 for ( ; *flags != NUL; ++flags)
10827 {
10828 switch (*flags)
10829 {
10830 case 'n': remap = FALSE; break;
10831 case 'm': remap = TRUE; break;
10832 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010833 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010834 }
10835 }
10836 }
10837
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010838 /* Need to escape K_SPECIAL and CSI before putting the string in the
10839 * typeahead buffer. */
10840 keys_esc = vim_strsave_escape_csi(keys);
10841 if (keys_esc != NULL)
10842 {
10843 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010844 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010845 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010846 if (vgetc_busy)
10847 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010848 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010849 }
10850}
10851
10852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 * "filereadable()" function
10854 */
10855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010857 typval_T *argvars;
10858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010860 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 char_u *p;
10862 int n;
10863
Bram Moolenaarc236c162008-07-13 17:41:49 +000010864#ifndef O_NONBLOCK
10865# define O_NONBLOCK 0
10866#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010868 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10869 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 {
10871 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010872 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 }
10874 else
10875 n = FALSE;
10876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878}
10879
10880/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010881 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 * rights to write into.
10883 */
10884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 typval_T *argvars;
10887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010889 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890}
10891
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010892static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010893
10894 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010895findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010897 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010898 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010899{
10900#ifdef FEAT_SEARCHPATH
10901 char_u *fname;
10902 char_u *fresult = NULL;
10903 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10904 char_u *p;
10905 char_u pathbuf[NUMBUFLEN];
10906 int count = 1;
10907 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010908 int error = FALSE;
10909#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010910
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010911 rettv->vval.v_string = NULL;
10912 rettv->v_type = VAR_STRING;
10913
10914#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010916
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010917 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10920 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010921 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010922 else
10923 {
10924 if (*p != NUL)
10925 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010928 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010930 }
10931
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010932 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10933 error = TRUE;
10934
10935 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010937 do
10938 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010939 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010940 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 fresult = find_file_in_path_option(first ? fname : NULL,
10942 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010943 0, first, path,
10944 find_what,
10945 curbuf->b_ffname,
10946 find_what == FINDFILE_DIR
10947 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010948 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010949
10950 if (fresult != NULL && rettv->v_type == VAR_LIST)
10951 list_append_string(rettv->vval.v_list, fresult, -1);
10952
10953 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010955
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010956 if (rettv->v_type == VAR_STRING)
10957 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010958#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010959}
10960
Bram Moolenaar33570922005-01-25 22:26:29 +000010961static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10962static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010963
10964/*
10965 * Implementation of map() and filter().
10966 */
10967 static void
10968filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010969 typval_T *argvars;
10970 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010971 int map;
10972{
10973 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010975 listitem_T *li, *nli;
10976 list_T *l = NULL;
10977 dictitem_T *di;
10978 hashtab_T *ht;
10979 hashitem_T *hi;
10980 dict_T *d = NULL;
10981 typval_T save_val;
10982 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010983 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010984 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010985 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010986 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010987 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010988 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010989 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010990
Bram Moolenaare9a41262005-01-15 22:18:47 +000010991 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010992 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010993 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010994 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 return;
10996 }
10997 else if (argvars[0].v_type == VAR_DICT)
10998 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010999 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011000 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 return;
11002 }
11003 else
11004 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011005 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 return;
11007 }
11008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009 expr = get_tv_string_buf_chk(&argvars[1], buf);
11010 /* On type errors, the preceding call has already displayed an error
11011 * message. Avoid a misleading error message for an empty string that
11012 * was not passed as argument. */
11013 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011015 prepare_vimvar(VV_VAL, &save_val);
11016 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011017
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011018 /* We reset "did_emsg" to be able to detect whether an error
11019 * occurred during evaluation of the expression. */
11020 save_did_emsg = did_emsg;
11021 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011022
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011023 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011026 vimvars[VV_KEY].vv_type = VAR_STRING;
11027
11028 ht = &d->dv_hashtab;
11029 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011030 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011033 if (!HASHITEM_EMPTY(hi))
11034 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011035 int r;
11036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011037 --todo;
11038 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011039 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011040 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11041 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 break;
11043 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011044 r = filter_map_one(&di->di_tv, expr, map, &rem);
11045 clear_tv(&vimvars[VV_KEY].vv_tv);
11046 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 break;
11048 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011049 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011050 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11051 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011052 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011053 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011054 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 }
11056 }
11057 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 }
11059 else
11060 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011061 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 for (li = l->lv_first; li != NULL; li = nli)
11064 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011065 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011066 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011067 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011068 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011069 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011070 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011071 break;
11072 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011074 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011075 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011076 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011077
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011078 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011080
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011081 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011082 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011083
11084 copy_tv(&argvars[0], rettv);
11085}
11086
11087 static int
11088filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011089 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011090 char_u *expr;
11091 int map;
11092 int *remp;
11093{
Bram Moolenaar33570922005-01-25 22:26:29 +000011094 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011095 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011096 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011097
Bram Moolenaar33570922005-01-25 22:26:29 +000011098 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 s = expr;
11100 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011101 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011102 if (*s != NUL) /* check for trailing chars after expr */
11103 {
11104 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011105 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011106 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 }
11108 if (map)
11109 {
11110 /* map(): replace the list item value */
11111 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011112 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 *tv = rettv;
11114 }
11115 else
11116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 int error = FALSE;
11118
Bram Moolenaare9a41262005-01-15 22:18:47 +000011119 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 /* On type error, nothing has been removed; return FAIL to stop the
11123 * loop. The error message was given by get_tv_number_chk(). */
11124 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011125 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011127 retval = OK;
11128theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011130 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011131}
11132
11133/*
11134 * "filter()" function
11135 */
11136 static void
11137f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 typval_T *argvars;
11139 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011140{
11141 filter_map(argvars, rettv, FALSE);
11142}
11143
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011144/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011145 * "finddir({fname}[, {path}[, {count}]])" function
11146 */
11147 static void
11148f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011149 typval_T *argvars;
11150 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011152 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153}
11154
11155/*
11156 * "findfile({fname}[, {path}[, {count}]])" function
11157 */
11158 static void
11159f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011160 typval_T *argvars;
11161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011163 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011164}
11165
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011166#ifdef FEAT_FLOAT
11167/*
11168 * "float2nr({float})" function
11169 */
11170 static void
11171f_float2nr(argvars, rettv)
11172 typval_T *argvars;
11173 typval_T *rettv;
11174{
11175 float_T f;
11176
11177 if (get_float_arg(argvars, &f) == OK)
11178 {
11179 if (f < -0x7fffffff)
11180 rettv->vval.v_number = -0x7fffffff;
11181 else if (f > 0x7fffffff)
11182 rettv->vval.v_number = 0x7fffffff;
11183 else
11184 rettv->vval.v_number = (varnumber_T)f;
11185 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011186}
11187
11188/*
11189 * "floor({float})" function
11190 */
11191 static void
11192f_floor(argvars, rettv)
11193 typval_T *argvars;
11194 typval_T *rettv;
11195{
11196 float_T f;
11197
11198 rettv->v_type = VAR_FLOAT;
11199 if (get_float_arg(argvars, &f) == OK)
11200 rettv->vval.v_float = floor(f);
11201 else
11202 rettv->vval.v_float = 0.0;
11203}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011204
11205/*
11206 * "fmod()" function
11207 */
11208 static void
11209f_fmod(argvars, rettv)
11210 typval_T *argvars;
11211 typval_T *rettv;
11212{
11213 float_T fx, fy;
11214
11215 rettv->v_type = VAR_FLOAT;
11216 if (get_float_arg(argvars, &fx) == OK
11217 && get_float_arg(&argvars[1], &fy) == OK)
11218 rettv->vval.v_float = fmod(fx, fy);
11219 else
11220 rettv->vval.v_float = 0.0;
11221}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011222#endif
11223
Bram Moolenaar0d660222005-01-07 21:51:51 +000011224/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011225 * "fnameescape({string})" function
11226 */
11227 static void
11228f_fnameescape(argvars, rettv)
11229 typval_T *argvars;
11230 typval_T *rettv;
11231{
11232 rettv->vval.v_string = vim_strsave_fnameescape(
11233 get_tv_string(&argvars[0]), FALSE);
11234 rettv->v_type = VAR_STRING;
11235}
11236
11237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 * "fnamemodify({fname}, {mods})" function
11239 */
11240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011242 typval_T *argvars;
11243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244{
11245 char_u *fname;
11246 char_u *mods;
11247 int usedlen = 0;
11248 int len;
11249 char_u *fbuf = NULL;
11250 char_u buf[NUMBUFLEN];
11251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011252 fname = get_tv_string_chk(&argvars[0]);
11253 mods = get_tv_string_buf_chk(&argvars[1], buf);
11254 if (fname == NULL || mods == NULL)
11255 fname = NULL;
11256 else
11257 {
11258 len = (int)STRLEN(fname);
11259 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 vim_free(fbuf);
11268}
11269
Bram Moolenaar33570922005-01-25 22:26:29 +000011270static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271
11272/*
11273 * "foldclosed()" function
11274 */
11275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011277 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011278 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011279 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280{
11281#ifdef FEAT_FOLDING
11282 linenr_T lnum;
11283 linenr_T first, last;
11284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11287 {
11288 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11289 {
11290 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 return;
11295 }
11296 }
11297#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011302 * "foldclosed()" function
11303 */
11304 static void
11305f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011306 typval_T *argvars;
11307 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011308{
11309 foldclosed_both(argvars, rettv, FALSE);
11310}
11311
11312/*
11313 * "foldclosedend()" function
11314 */
11315 static void
11316f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011317 typval_T *argvars;
11318 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011319{
11320 foldclosed_both(argvars, rettv, TRUE);
11321}
11322
11323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 * "foldlevel()" function
11325 */
11326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011327f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011328 typval_T *argvars UNUSED;
11329 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330{
11331#ifdef FEAT_FOLDING
11332 linenr_T lnum;
11333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338}
11339
11340/*
11341 * "foldtext()" function
11342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011345 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347{
11348#ifdef FEAT_FOLDING
11349 linenr_T lnum;
11350 char_u *s;
11351 char_u *r;
11352 int len;
11353 char *txt;
11354#endif
11355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 rettv->v_type = VAR_STRING;
11357 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011359 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11360 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11361 <= curbuf->b_ml.ml_line_count
11362 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 {
11364 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011365 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11366 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 {
11368 if (!linewhite(lnum))
11369 break;
11370 ++lnum;
11371 }
11372
11373 /* Find interesting text in this line. */
11374 s = skipwhite(ml_get(lnum));
11375 /* skip C comment-start */
11376 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011379 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011380 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011381 {
11382 s = skipwhite(ml_get(lnum + 1));
11383 if (*s == '*')
11384 s = skipwhite(s + 1);
11385 }
11386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 txt = _("+-%s%3ld lines: ");
11388 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 + 20 /* for %3ld */
11391 + STRLEN(s))); /* concatenated */
11392 if (r != NULL)
11393 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011394 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11395 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11396 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 len = (int)STRLEN(r);
11398 STRCAT(r, s);
11399 /* remove 'foldmarker' and 'commentstring' */
11400 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 }
11403 }
11404#endif
11405}
11406
11407/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011408 * "foldtextresult(lnum)" function
11409 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011413 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011414{
11415#ifdef FEAT_FOLDING
11416 linenr_T lnum;
11417 char_u *text;
11418 char_u buf[51];
11419 foldinfo_T foldinfo;
11420 int fold_count;
11421#endif
11422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011423 rettv->v_type = VAR_STRING;
11424 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011425#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011427 /* treat illegal types and illegal string values for {lnum} the same */
11428 if (lnum < 0)
11429 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011430 fold_count = foldedCount(curwin, lnum, &foldinfo);
11431 if (fold_count > 0)
11432 {
11433 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11434 &foldinfo, buf);
11435 if (text == buf)
11436 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011438 }
11439#endif
11440}
11441
11442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 * "foreground()" function
11444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011447 typval_T *argvars UNUSED;
11448 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450#ifdef FEAT_GUI
11451 if (gui.in_use)
11452 gui_mch_set_foreground();
11453#else
11454# ifdef WIN32
11455 win32_set_foreground();
11456# endif
11457#endif
11458}
11459
11460/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011461 * "function()" function
11462 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 typval_T *argvars;
11466 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011467{
11468 char_u *s;
11469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011471 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011472 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011473 /* Don't check an autoload name for existence here. */
11474 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011475 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011476 else
11477 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011478 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011479 {
11480 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011481 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011482
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011483 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11484 * also be called from another script. Using trans_function_name()
11485 * would also work, but some plugins depend on the name being
11486 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011487 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011488 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011489 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011490 if (rettv->vval.v_string != NULL)
11491 {
11492 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011493 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011494 }
11495 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011496 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011497 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011499 }
11500}
11501
11502/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011503 * "garbagecollect()" function
11504 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011505 static void
11506f_garbagecollect(argvars, rettv)
11507 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011508 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011509{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011510 /* This is postponed until we are back at the toplevel, because we may be
11511 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11512 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011513
11514 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11515 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011516}
11517
11518/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011519 * "get()" function
11520 */
11521 static void
11522f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011523 typval_T *argvars;
11524 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011525{
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 listitem_T *li;
11527 list_T *l;
11528 dictitem_T *di;
11529 dict_T *d;
11530 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531
Bram Moolenaare9a41262005-01-15 22:18:47 +000011532 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011534 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011536 int error = FALSE;
11537
11538 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11539 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011540 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011543 else if (argvars[0].v_type == VAR_DICT)
11544 {
11545 if ((d = argvars[0].vval.v_dict) != NULL)
11546 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011547 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 if (di != NULL)
11549 tv = &di->di_tv;
11550 }
11551 }
11552 else
11553 EMSG2(_(e_listdictarg), "get()");
11554
11555 if (tv == NULL)
11556 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011557 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011558 copy_tv(&argvars[2], rettv);
11559 }
11560 else
11561 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011562}
11563
Bram Moolenaar342337a2005-07-21 21:11:17 +000011564static 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 +000011565
11566/*
11567 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011568 * Return a range (from start to end) of lines in rettv from the specified
11569 * buffer.
11570 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011571 */
11572 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011573get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011574 buf_T *buf;
11575 linenr_T start;
11576 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011577 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011578 typval_T *rettv;
11579{
11580 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011581
Bram Moolenaar959a1432013-12-14 12:17:38 +010011582 rettv->v_type = VAR_STRING;
11583 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011584 if (retlist && rettv_list_alloc(rettv) == FAIL)
11585 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011586
11587 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11588 return;
11589
11590 if (!retlist)
11591 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011592 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11593 p = ml_get_buf(buf, start, FALSE);
11594 else
11595 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011596 rettv->vval.v_string = vim_strsave(p);
11597 }
11598 else
11599 {
11600 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011601 return;
11602
11603 if (start < 1)
11604 start = 1;
11605 if (end > buf->b_ml.ml_line_count)
11606 end = buf->b_ml.ml_line_count;
11607 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011608 if (list_append_string(rettv->vval.v_list,
11609 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011610 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011611 }
11612}
11613
11614/*
11615 * "getbufline()" function
11616 */
11617 static void
11618f_getbufline(argvars, rettv)
11619 typval_T *argvars;
11620 typval_T *rettv;
11621{
11622 linenr_T lnum;
11623 linenr_T end;
11624 buf_T *buf;
11625
11626 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11627 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011628 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011629 --emsg_off;
11630
Bram Moolenaar661b1822005-07-28 22:36:45 +000011631 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011632 if (argvars[2].v_type == VAR_UNKNOWN)
11633 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011634 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011635 end = get_tv_lnum_buf(&argvars[2], buf);
11636
Bram Moolenaar342337a2005-07-21 21:11:17 +000011637 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011638}
11639
Bram Moolenaar0d660222005-01-07 21:51:51 +000011640/*
11641 * "getbufvar()" function
11642 */
11643 static void
11644f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011645 typval_T *argvars;
11646 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011647{
11648 buf_T *buf;
11649 buf_T *save_curbuf;
11650 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011651 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011652 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11655 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011657 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011658
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011659 rettv->v_type = VAR_STRING;
11660 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011661
11662 if (buf != NULL && varname != NULL)
11663 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011664 /* set curbuf to be our buf, temporarily */
11665 save_curbuf = curbuf;
11666 curbuf = buf;
11667
Bram Moolenaar0d660222005-01-07 21:51:51 +000011668 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011669 {
11670 if (get_option_tv(&varname, rettv, TRUE) == OK)
11671 done = TRUE;
11672 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011673 else if (STRCMP(varname, "changedtick") == 0)
11674 {
11675 rettv->v_type = VAR_NUMBER;
11676 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011677 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011678 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011679 else
11680 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011681 /* Look up the variable. */
11682 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11683 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11684 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011686 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011687 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011688 done = TRUE;
11689 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011690 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011691
11692 /* restore previous notion of curbuf */
11693 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011694 }
11695
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011696 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11697 /* use the default value */
11698 copy_tv(&argvars[2], rettv);
11699
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700 --emsg_off;
11701}
11702
11703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 * "getchar()" function
11705 */
11706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011708 typval_T *argvars;
11709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710{
11711 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011714 /* Position the cursor. Needed after a message that ends in a space. */
11715 windgoto(msg_row, msg_col);
11716
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 ++no_mapping;
11718 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011719 for (;;)
11720 {
11721 if (argvars[0].v_type == VAR_UNKNOWN)
11722 /* getchar(): blocking wait. */
11723 n = safe_vgetc();
11724 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11725 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011726 n = vpeekc_any();
11727 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011728 /* illegal argument or getchar(0) and no char avail: return zero */
11729 n = 0;
11730 else
11731 /* getchar(0) and char avail: return char */
11732 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011733
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011734 if (n == K_IGNORE)
11735 continue;
11736 break;
11737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 --no_mapping;
11739 --allow_keys;
11740
Bram Moolenaar219b8702006-11-01 14:32:36 +000011741 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11742 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11743 vimvars[VV_MOUSE_COL].vv_nr = 0;
11744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011745 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 if (IS_SPECIAL(n) || mod_mask != 0)
11747 {
11748 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11749 int i = 0;
11750
11751 /* Turn a special key into three bytes, plus modifier. */
11752 if (mod_mask != 0)
11753 {
11754 temp[i++] = K_SPECIAL;
11755 temp[i++] = KS_MODIFIER;
11756 temp[i++] = mod_mask;
11757 }
11758 if (IS_SPECIAL(n))
11759 {
11760 temp[i++] = K_SPECIAL;
11761 temp[i++] = K_SECOND(n);
11762 temp[i++] = K_THIRD(n);
11763 }
11764#ifdef FEAT_MBYTE
11765 else if (has_mbyte)
11766 i += (*mb_char2bytes)(n, temp + i);
11767#endif
11768 else
11769 temp[i++] = n;
11770 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771 rettv->v_type = VAR_STRING;
11772 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011773
11774#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011775 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011776 {
11777 int row = mouse_row;
11778 int col = mouse_col;
11779 win_T *win;
11780 linenr_T lnum;
11781# ifdef FEAT_WINDOWS
11782 win_T *wp;
11783# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011784 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011785
11786 if (row >= 0 && col >= 0)
11787 {
11788 /* Find the window at the mouse coordinates and compute the
11789 * text position. */
11790 win = mouse_find_win(&row, &col);
11791 (void)mouse_comp_pos(win, &row, &col, &lnum);
11792# ifdef FEAT_WINDOWS
11793 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011794 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011795# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011796 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011797 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11798 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11799 }
11800 }
11801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 }
11803}
11804
11805/*
11806 * "getcharmod()" function
11807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011810 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814}
11815
11816/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011817 * "getcharsearch()" function
11818 */
11819 static void
11820f_getcharsearch(argvars, rettv)
11821 typval_T *argvars UNUSED;
11822 typval_T *rettv;
11823{
11824 if (rettv_dict_alloc(rettv) != FAIL)
11825 {
11826 dict_T *dict = rettv->vval.v_dict;
11827
11828 dict_add_nr_str(dict, "char", 0L, last_csearch());
11829 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11830 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11831 }
11832}
11833
11834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 * "getcmdline()" function
11836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->v_type = VAR_STRING;
11843 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844}
11845
11846/*
11847 * "getcmdpos()" function
11848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011851 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855}
11856
11857/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011858 * "getcmdtype()" function
11859 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011860 static void
11861f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011862 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011863 typval_T *rettv;
11864{
11865 rettv->v_type = VAR_STRING;
11866 rettv->vval.v_string = alloc(2);
11867 if (rettv->vval.v_string != NULL)
11868 {
11869 rettv->vval.v_string[0] = get_cmdline_type();
11870 rettv->vval.v_string[1] = NUL;
11871 }
11872}
11873
11874/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011875 * "getcmdwintype()" function
11876 */
11877 static void
11878f_getcmdwintype(argvars, rettv)
11879 typval_T *argvars UNUSED;
11880 typval_T *rettv;
11881{
11882 rettv->v_type = VAR_STRING;
11883 rettv->vval.v_string = NULL;
11884#ifdef FEAT_CMDWIN
11885 rettv->vval.v_string = alloc(2);
11886 if (rettv->vval.v_string != NULL)
11887 {
11888 rettv->vval.v_string[0] = cmdwin_type;
11889 rettv->vval.v_string[1] = NUL;
11890 }
11891#endif
11892}
11893
11894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 * "getcwd()" function
11896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011899 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011902 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011905 rettv->vval.v_string = NULL;
11906 cwd = alloc(MAXPATHL);
11907 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011909 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11910 {
11911 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011913 if (rettv->vval.v_string != NULL)
11914 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011916 }
11917 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 }
11919}
11920
11921/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011922 * "getfontname()" function
11923 */
11924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011925f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011929 rettv->v_type = VAR_STRING;
11930 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011931#ifdef FEAT_GUI
11932 if (gui.in_use)
11933 {
11934 GuiFont font;
11935 char_u *name = NULL;
11936
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011937 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011938 {
11939 /* Get the "Normal" font. Either the name saved by
11940 * hl_set_font_name() or from the font ID. */
11941 font = gui.norm_font;
11942 name = hl_get_font_name();
11943 }
11944 else
11945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011947 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11948 return;
11949 font = gui_mch_get_font(name, FALSE);
11950 if (font == NOFONT)
11951 return; /* Invalid font name, return empty string. */
11952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011954 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011955 gui_mch_free_font(font);
11956 }
11957#endif
11958}
11959
11960/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011961 * "getfperm({fname})" function
11962 */
11963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011965 typval_T *argvars;
11966 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011967{
11968 char_u *fname;
11969 struct stat st;
11970 char_u *perm = NULL;
11971 char_u flags[] = "rwx";
11972 int i;
11973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011977 if (mch_stat((char *)fname, &st) >= 0)
11978 {
11979 perm = vim_strsave((char_u *)"---------");
11980 if (perm != NULL)
11981 {
11982 for (i = 0; i < 9; i++)
11983 {
11984 if (st.st_mode & (1 << (8 - i)))
11985 perm[i] = flags[i % 3];
11986 }
11987 }
11988 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011990}
11991
11992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 * "getfsize({fname})" function
11994 */
11995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011997 typval_T *argvars;
11998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999{
12000 char_u *fname;
12001 struct stat st;
12002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006
12007 if (mch_stat((char *)fname, &st) >= 0)
12008 {
12009 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012014
12015 /* non-perfect check for overflow */
12016 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12017 rettv->vval.v_number = -2;
12018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 }
12020 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022}
12023
12024/*
12025 * "getftime({fname})" function
12026 */
12027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012029 typval_T *argvars;
12030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031{
12032 char_u *fname;
12033 struct stat st;
12034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036
12037 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041}
12042
12043/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012044 * "getftype({fname})" function
12045 */
12046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047f_getftype(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 *type = NULL;
12054 char *t;
12055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012059 if (mch_lstat((char *)fname, &st) >= 0)
12060 {
12061#ifdef S_ISREG
12062 if (S_ISREG(st.st_mode))
12063 t = "file";
12064 else if (S_ISDIR(st.st_mode))
12065 t = "dir";
12066# ifdef S_ISLNK
12067 else if (S_ISLNK(st.st_mode))
12068 t = "link";
12069# endif
12070# ifdef S_ISBLK
12071 else if (S_ISBLK(st.st_mode))
12072 t = "bdev";
12073# endif
12074# ifdef S_ISCHR
12075 else if (S_ISCHR(st.st_mode))
12076 t = "cdev";
12077# endif
12078# ifdef S_ISFIFO
12079 else if (S_ISFIFO(st.st_mode))
12080 t = "fifo";
12081# endif
12082# ifdef S_ISSOCK
12083 else if (S_ISSOCK(st.st_mode))
12084 t = "fifo";
12085# endif
12086 else
12087 t = "other";
12088#else
12089# ifdef S_IFMT
12090 switch (st.st_mode & S_IFMT)
12091 {
12092 case S_IFREG: t = "file"; break;
12093 case S_IFDIR: t = "dir"; break;
12094# ifdef S_IFLNK
12095 case S_IFLNK: t = "link"; break;
12096# endif
12097# ifdef S_IFBLK
12098 case S_IFBLK: t = "bdev"; break;
12099# endif
12100# ifdef S_IFCHR
12101 case S_IFCHR: t = "cdev"; break;
12102# endif
12103# ifdef S_IFIFO
12104 case S_IFIFO: t = "fifo"; break;
12105# endif
12106# ifdef S_IFSOCK
12107 case S_IFSOCK: t = "socket"; break;
12108# endif
12109 default: t = "other";
12110 }
12111# else
12112 if (mch_isdir(fname))
12113 t = "dir";
12114 else
12115 t = "file";
12116# endif
12117#endif
12118 type = vim_strsave((char_u *)t);
12119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012121}
12122
12123/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012124 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012125 */
12126 static void
12127f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012128 typval_T *argvars;
12129 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130{
12131 linenr_T lnum;
12132 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012133 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134
12135 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012136 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012137 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012138 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012139 retlist = FALSE;
12140 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012142 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012144 retlist = TRUE;
12145 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012146
Bram Moolenaar342337a2005-07-21 21:11:17 +000012147 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148}
12149
12150/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012151 * "getmatches()" function
12152 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012153 static void
12154f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012155 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012156 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012157{
12158#ifdef FEAT_SEARCH_EXTRA
12159 dict_T *dict;
12160 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012161 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012162
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012163 if (rettv_list_alloc(rettv) == OK)
12164 {
12165 while (cur != NULL)
12166 {
12167 dict = dict_alloc();
12168 if (dict == NULL)
12169 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012170 if (cur->match.regprog == NULL)
12171 {
12172 /* match added with matchaddpos() */
12173 for (i = 0; i < MAXPOSMATCH; ++i)
12174 {
12175 llpos_T *llpos;
12176 char buf[6];
12177 list_T *l;
12178
12179 llpos = &cur->pos.pos[i];
12180 if (llpos->lnum == 0)
12181 break;
12182 l = list_alloc();
12183 if (l == NULL)
12184 break;
12185 list_append_number(l, (varnumber_T)llpos->lnum);
12186 if (llpos->col > 0)
12187 {
12188 list_append_number(l, (varnumber_T)llpos->col);
12189 list_append_number(l, (varnumber_T)llpos->len);
12190 }
12191 sprintf(buf, "pos%d", i + 1);
12192 dict_add_list(dict, buf, l);
12193 }
12194 }
12195 else
12196 {
12197 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12198 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012199 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012200 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12201 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012202# ifdef FEAT_CONCEAL
12203 if (cur->conceal_char)
12204 {
12205 char_u buf[MB_MAXBYTES + 1];
12206
12207 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12208 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12209 }
12210# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012211 list_append_dict(rettv->vval.v_list, dict);
12212 cur = cur->next;
12213 }
12214 }
12215#endif
12216}
12217
12218/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012219 * "getpid()" function
12220 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012221 static void
12222f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012223 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012224 typval_T *rettv;
12225{
12226 rettv->vval.v_number = mch_get_pid();
12227}
12228
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012229static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12230
12231/*
12232 * "getcurpos()" function
12233 */
12234 static void
12235f_getcurpos(argvars, rettv)
12236 typval_T *argvars;
12237 typval_T *rettv;
12238{
12239 getpos_both(argvars, rettv, TRUE);
12240}
12241
Bram Moolenaar18081e32008-02-20 19:11:07 +000012242/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012243 * "getpos(string)" function
12244 */
12245 static void
12246f_getpos(argvars, rettv)
12247 typval_T *argvars;
12248 typval_T *rettv;
12249{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012250 getpos_both(argvars, rettv, FALSE);
12251}
12252
12253 static void
12254getpos_both(argvars, rettv, getcurpos)
12255 typval_T *argvars;
12256 typval_T *rettv;
12257 int getcurpos;
12258{
Bram Moolenaara5525202006-03-02 22:52:09 +000012259 pos_T *fp;
12260 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012261 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012262
12263 if (rettv_list_alloc(rettv) == OK)
12264 {
12265 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012266 if (getcurpos)
12267 fp = &curwin->w_cursor;
12268 else
12269 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012270 if (fnum != -1)
12271 list_append_number(l, (varnumber_T)fnum);
12272 else
12273 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012274 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12275 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012276 list_append_number(l, (fp != NULL)
12277 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012278 : (varnumber_T)0);
12279 list_append_number(l,
12280#ifdef FEAT_VIRTUALEDIT
12281 (fp != NULL) ? (varnumber_T)fp->coladd :
12282#endif
12283 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012284 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012285 list_append_number(l, curwin->w_curswant == MAXCOL ?
12286 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012287 }
12288 else
12289 rettv->vval.v_number = FALSE;
12290}
12291
12292/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012293 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012294 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012295 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012296f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012297 typval_T *argvars UNUSED;
12298 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012299{
12300#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012301 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012302#endif
12303
Bram Moolenaar2641f772005-03-25 21:58:17 +000012304#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012305 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012306 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012307 wp = NULL;
12308 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12309 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012310 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012311 if (wp == NULL)
12312 return;
12313 }
12314
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012315 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012316 }
12317#endif
12318}
12319
12320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 * "getreg()" function
12322 */
12323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012325 typval_T *argvars;
12326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327{
12328 char_u *strregname;
12329 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012330 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012331 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012332 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012334 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012336 strregname = get_tv_string_chk(&argvars[0]);
12337 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012338 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012340 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012341 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12342 return_list = get_tv_number_chk(&argvars[2], &error);
12343 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012346 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012347
12348 if (error)
12349 return;
12350
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 regname = (strregname == NULL ? '"' : *strregname);
12352 if (regname == 0)
12353 regname = '"';
12354
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012355 if (return_list)
12356 {
12357 rettv->v_type = VAR_LIST;
12358 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12359 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012360 if (rettv->vval.v_list != NULL)
12361 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012362 }
12363 else
12364 {
12365 rettv->v_type = VAR_STRING;
12366 rettv->vval.v_string = get_reg_contents(regname,
12367 arg2 ? GREG_EXPR_SRC : 0);
12368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369}
12370
12371/*
12372 * "getregtype()" function
12373 */
12374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012375f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012376 typval_T *argvars;
12377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378{
12379 char_u *strregname;
12380 int regname;
12381 char_u buf[NUMBUFLEN + 2];
12382 long reglen = 0;
12383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012384 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 {
12386 strregname = get_tv_string_chk(&argvars[0]);
12387 if (strregname == NULL) /* type error; errmsg already given */
12388 {
12389 rettv->v_type = VAR_STRING;
12390 rettv->vval.v_string = NULL;
12391 return;
12392 }
12393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394 else
12395 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012396 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397
12398 regname = (strregname == NULL ? '"' : *strregname);
12399 if (regname == 0)
12400 regname = '"';
12401
12402 buf[0] = NUL;
12403 buf[1] = NUL;
12404 switch (get_reg_type(regname, &reglen))
12405 {
12406 case MLINE: buf[0] = 'V'; break;
12407 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 case MBLOCK:
12409 buf[0] = Ctrl_V;
12410 sprintf((char *)buf + 1, "%ld", reglen + 1);
12411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413 rettv->v_type = VAR_STRING;
12414 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415}
12416
12417/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012418 * "gettabvar()" function
12419 */
12420 static void
12421f_gettabvar(argvars, rettv)
12422 typval_T *argvars;
12423 typval_T *rettv;
12424{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012425 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012426 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012427 dictitem_T *v;
12428 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012429 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012430
12431 rettv->v_type = VAR_STRING;
12432 rettv->vval.v_string = NULL;
12433
12434 varname = get_tv_string_chk(&argvars[1]);
12435 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12436 if (tp != NULL && varname != NULL)
12437 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012438 /* Set tp to be our tabpage, temporarily. Also set the window to the
12439 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012440 if (switch_win(&oldcurwin, &oldtabpage,
12441 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012442 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012443 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012444 /* look up the variable */
12445 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12446 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12447 if (v != NULL)
12448 {
12449 copy_tv(&v->di_tv, rettv);
12450 done = TRUE;
12451 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012452 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012453
12454 /* restore previous notion of curwin */
12455 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012456 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012457
12458 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012459 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012460}
12461
12462/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012463 * "gettabwinvar()" function
12464 */
12465 static void
12466f_gettabwinvar(argvars, rettv)
12467 typval_T *argvars;
12468 typval_T *rettv;
12469{
12470 getwinvar(argvars, rettv, 1);
12471}
12472
12473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474 * "getwinposx()" function
12475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012477f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012478 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482#ifdef FEAT_GUI
12483 if (gui.in_use)
12484 {
12485 int x, y;
12486
12487 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 }
12490#endif
12491}
12492
12493/*
12494 * "getwinposy()" function
12495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012497f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012498 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502#ifdef FEAT_GUI
12503 if (gui.in_use)
12504 {
12505 int x, y;
12506
12507 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 }
12510#endif
12511}
12512
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012513/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012514 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012515 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012516 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012517find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012518 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012519 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012520{
12521#ifdef FEAT_WINDOWS
12522 win_T *wp;
12523#endif
12524 int nr;
12525
12526 nr = get_tv_number_chk(vp, NULL);
12527
12528#ifdef FEAT_WINDOWS
12529 if (nr < 0)
12530 return NULL;
12531 if (nr == 0)
12532 return curwin;
12533
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012534 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12535 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012536 if (--nr <= 0)
12537 break;
12538 return wp;
12539#else
12540 if (nr == 0 || nr == 1)
12541 return curwin;
12542 return NULL;
12543#endif
12544}
12545
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546/*
12547 * "getwinvar()" function
12548 */
12549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012550f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012551 typval_T *argvars;
12552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012554 getwinvar(argvars, rettv, 0);
12555}
12556
12557/*
12558 * getwinvar() and gettabwinvar()
12559 */
12560 static void
12561getwinvar(argvars, rettv, off)
12562 typval_T *argvars;
12563 typval_T *rettv;
12564 int off; /* 1 for gettabwinvar() */
12565{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012566 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012568 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012569 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012570 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012571#ifdef FEAT_WINDOWS
12572 win_T *oldcurwin;
12573 tabpage_T *oldtabpage;
12574 int need_switch_win;
12575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012577#ifdef FEAT_WINDOWS
12578 if (off == 1)
12579 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12580 else
12581 tp = curtab;
12582#endif
12583 win = find_win_by_nr(&argvars[off], tp);
12584 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012585 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012587 rettv->v_type = VAR_STRING;
12588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589
12590 if (win != NULL && varname != NULL)
12591 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012592#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012593 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012594 * otherwise the window is not valid. Only do this when needed,
12595 * autocommands get blocked. */
12596 need_switch_win = !(tp == curtab && win == curwin);
12597 if (!need_switch_win
12598 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12599#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012600 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012601 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012602 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012603 if (get_option_tv(&varname, rettv, 1) == OK)
12604 done = TRUE;
12605 }
12606 else
12607 {
12608 /* Look up the variable. */
12609 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12610 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12611 varname, FALSE);
12612 if (v != NULL)
12613 {
12614 copy_tv(&v->di_tv, rettv);
12615 done = TRUE;
12616 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012619
Bram Moolenaarba117c22015-09-29 16:53:22 +020012620#ifdef FEAT_WINDOWS
12621 if (need_switch_win)
12622 /* restore previous notion of curwin */
12623 restore_win(oldcurwin, oldtabpage, TRUE);
12624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625 }
12626
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012627 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12628 /* use the default return value */
12629 copy_tv(&argvars[off + 2], rettv);
12630
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 --emsg_off;
12632}
12633
12634/*
12635 * "glob()" function
12636 */
12637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 typval_T *argvars;
12640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012642 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012644 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012646 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012647 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012649 if (argvars[1].v_type != VAR_UNKNOWN)
12650 {
12651 if (get_tv_number_chk(&argvars[1], &error))
12652 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012653 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012654 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012655 if (get_tv_number_chk(&argvars[2], &error))
12656 {
12657 rettv->v_type = VAR_LIST;
12658 rettv->vval.v_list = NULL;
12659 }
12660 if (argvars[3].v_type != VAR_UNKNOWN
12661 && get_tv_number_chk(&argvars[3], &error))
12662 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012663 }
12664 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012665 if (!error)
12666 {
12667 ExpandInit(&xpc);
12668 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012669 if (p_wic)
12670 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012671 if (rettv->v_type == VAR_STRING)
12672 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012673 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012674 else if (rettv_list_alloc(rettv) != FAIL)
12675 {
12676 int i;
12677
12678 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12679 NULL, options, WILD_ALL_KEEP);
12680 for (i = 0; i < xpc.xp_numfiles; i++)
12681 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12682
12683 ExpandCleanup(&xpc);
12684 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012685 }
12686 else
12687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688}
12689
12690/*
12691 * "globpath()" function
12692 */
12693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012695 typval_T *argvars;
12696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012698 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012700 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012701 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012702 garray_T ga;
12703 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012705 /* When the optional second argument is non-zero, don't remove matches
12706 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012708 if (argvars[2].v_type != VAR_UNKNOWN)
12709 {
12710 if (get_tv_number_chk(&argvars[2], &error))
12711 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012712 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012713 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012714 if (get_tv_number_chk(&argvars[3], &error))
12715 {
12716 rettv->v_type = VAR_LIST;
12717 rettv->vval.v_list = NULL;
12718 }
12719 if (argvars[4].v_type != VAR_UNKNOWN
12720 && get_tv_number_chk(&argvars[4], &error))
12721 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012722 }
12723 }
12724 if (file != NULL && !error)
12725 {
12726 ga_init2(&ga, (int)sizeof(char_u *), 10);
12727 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12728 if (rettv->v_type == VAR_STRING)
12729 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12730 else if (rettv_list_alloc(rettv) != FAIL)
12731 for (i = 0; i < ga.ga_len; ++i)
12732 list_append_string(rettv->vval.v_list,
12733 ((char_u **)(ga.ga_data))[i], -1);
12734 ga_clear_strings(&ga);
12735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012736 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738}
12739
12740/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012741 * "glob2regpat()" function
12742 */
12743 static void
12744f_glob2regpat(argvars, rettv)
12745 typval_T *argvars;
12746 typval_T *rettv;
12747{
12748 char_u *pat = get_tv_string_chk(&argvars[0]);
12749
12750 rettv->v_type = VAR_STRING;
12751 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12752}
12753
12754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 * "has()" function
12756 */
12757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012759 typval_T *argvars;
12760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761{
12762 int i;
12763 char_u *name;
12764 int n = FALSE;
12765 static char *(has_list[]) =
12766 {
12767#ifdef AMIGA
12768 "amiga",
12769# ifdef FEAT_ARP
12770 "arp",
12771# endif
12772#endif
12773#ifdef __BEOS__
12774 "beos",
12775#endif
12776#ifdef MSDOS
12777# ifdef DJGPP
12778 "dos32",
12779# else
12780 "dos16",
12781# endif
12782#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012783#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 "mac",
12785#endif
12786#if defined(MACOS_X_UNIX)
12787 "macunix",
12788#endif
12789#ifdef OS2
12790 "os2",
12791#endif
12792#ifdef __QNX__
12793 "qnx",
12794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795#ifdef UNIX
12796 "unix",
12797#endif
12798#ifdef VMS
12799 "vms",
12800#endif
12801#ifdef WIN16
12802 "win16",
12803#endif
12804#ifdef WIN32
12805 "win32",
12806#endif
12807#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12808 "win32unix",
12809#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012810#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 "win64",
12812#endif
12813#ifdef EBCDIC
12814 "ebcdic",
12815#endif
12816#ifndef CASE_INSENSITIVE_FILENAME
12817 "fname_case",
12818#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012819#ifdef HAVE_ACL
12820 "acl",
12821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822#ifdef FEAT_ARABIC
12823 "arabic",
12824#endif
12825#ifdef FEAT_AUTOCMD
12826 "autocmd",
12827#endif
12828#ifdef FEAT_BEVAL
12829 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012830# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12831 "balloon_multiline",
12832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833#endif
12834#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12835 "builtin_terms",
12836# ifdef ALL_BUILTIN_TCAPS
12837 "all_builtin_terms",
12838# endif
12839#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012840#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12841 || defined(FEAT_GUI_W32) \
12842 || defined(FEAT_GUI_MOTIF))
12843 "browsefilter",
12844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845#ifdef FEAT_BYTEOFF
12846 "byte_offset",
12847#endif
12848#ifdef FEAT_CINDENT
12849 "cindent",
12850#endif
12851#ifdef FEAT_CLIENTSERVER
12852 "clientserver",
12853#endif
12854#ifdef FEAT_CLIPBOARD
12855 "clipboard",
12856#endif
12857#ifdef FEAT_CMDL_COMPL
12858 "cmdline_compl",
12859#endif
12860#ifdef FEAT_CMDHIST
12861 "cmdline_hist",
12862#endif
12863#ifdef FEAT_COMMENTS
12864 "comments",
12865#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012866#ifdef FEAT_CONCEAL
12867 "conceal",
12868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869#ifdef FEAT_CRYPT
12870 "cryptv",
12871#endif
12872#ifdef FEAT_CSCOPE
12873 "cscope",
12874#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012875#ifdef FEAT_CURSORBIND
12876 "cursorbind",
12877#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012878#ifdef CURSOR_SHAPE
12879 "cursorshape",
12880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881#ifdef DEBUG
12882 "debug",
12883#endif
12884#ifdef FEAT_CON_DIALOG
12885 "dialog_con",
12886#endif
12887#ifdef FEAT_GUI_DIALOG
12888 "dialog_gui",
12889#endif
12890#ifdef FEAT_DIFF
12891 "diff",
12892#endif
12893#ifdef FEAT_DIGRAPHS
12894 "digraphs",
12895#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012896#ifdef FEAT_DIRECTX
12897 "directx",
12898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899#ifdef FEAT_DND
12900 "dnd",
12901#endif
12902#ifdef FEAT_EMACS_TAGS
12903 "emacs_tags",
12904#endif
12905 "eval", /* always present, of course! */
12906#ifdef FEAT_EX_EXTRA
12907 "ex_extra",
12908#endif
12909#ifdef FEAT_SEARCH_EXTRA
12910 "extra_search",
12911#endif
12912#ifdef FEAT_FKMAP
12913 "farsi",
12914#endif
12915#ifdef FEAT_SEARCHPATH
12916 "file_in_path",
12917#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012918#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012919 "filterpipe",
12920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921#ifdef FEAT_FIND_ID
12922 "find_in_path",
12923#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012924#ifdef FEAT_FLOAT
12925 "float",
12926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927#ifdef FEAT_FOLDING
12928 "folding",
12929#endif
12930#ifdef FEAT_FOOTER
12931 "footer",
12932#endif
12933#if !defined(USE_SYSTEM) && defined(UNIX)
12934 "fork",
12935#endif
12936#ifdef FEAT_GETTEXT
12937 "gettext",
12938#endif
12939#ifdef FEAT_GUI
12940 "gui",
12941#endif
12942#ifdef FEAT_GUI_ATHENA
12943# ifdef FEAT_GUI_NEXTAW
12944 "gui_neXtaw",
12945# else
12946 "gui_athena",
12947# endif
12948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949#ifdef FEAT_GUI_GTK
12950 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012953#ifdef FEAT_GUI_GNOME
12954 "gui_gnome",
12955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956#ifdef FEAT_GUI_MAC
12957 "gui_mac",
12958#endif
12959#ifdef FEAT_GUI_MOTIF
12960 "gui_motif",
12961#endif
12962#ifdef FEAT_GUI_PHOTON
12963 "gui_photon",
12964#endif
12965#ifdef FEAT_GUI_W16
12966 "gui_win16",
12967#endif
12968#ifdef FEAT_GUI_W32
12969 "gui_win32",
12970#endif
12971#ifdef FEAT_HANGULIN
12972 "hangul_input",
12973#endif
12974#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12975 "iconv",
12976#endif
12977#ifdef FEAT_INS_EXPAND
12978 "insert_expand",
12979#endif
12980#ifdef FEAT_JUMPLIST
12981 "jumplist",
12982#endif
12983#ifdef FEAT_KEYMAP
12984 "keymap",
12985#endif
12986#ifdef FEAT_LANGMAP
12987 "langmap",
12988#endif
12989#ifdef FEAT_LIBCALL
12990 "libcall",
12991#endif
12992#ifdef FEAT_LINEBREAK
12993 "linebreak",
12994#endif
12995#ifdef FEAT_LISP
12996 "lispindent",
12997#endif
12998#ifdef FEAT_LISTCMDS
12999 "listcmds",
13000#endif
13001#ifdef FEAT_LOCALMAP
13002 "localmap",
13003#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013004#ifdef FEAT_LUA
13005# ifndef DYNAMIC_LUA
13006 "lua",
13007# endif
13008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009#ifdef FEAT_MENU
13010 "menu",
13011#endif
13012#ifdef FEAT_SESSION
13013 "mksession",
13014#endif
13015#ifdef FEAT_MODIFY_FNAME
13016 "modify_fname",
13017#endif
13018#ifdef FEAT_MOUSE
13019 "mouse",
13020#endif
13021#ifdef FEAT_MOUSESHAPE
13022 "mouseshape",
13023#endif
13024#if defined(UNIX) || defined(VMS)
13025# ifdef FEAT_MOUSE_DEC
13026 "mouse_dec",
13027# endif
13028# ifdef FEAT_MOUSE_GPM
13029 "mouse_gpm",
13030# endif
13031# ifdef FEAT_MOUSE_JSB
13032 "mouse_jsbterm",
13033# endif
13034# ifdef FEAT_MOUSE_NET
13035 "mouse_netterm",
13036# endif
13037# ifdef FEAT_MOUSE_PTERM
13038 "mouse_pterm",
13039# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013040# ifdef FEAT_MOUSE_SGR
13041 "mouse_sgr",
13042# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013043# ifdef FEAT_SYSMOUSE
13044 "mouse_sysmouse",
13045# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013046# ifdef FEAT_MOUSE_URXVT
13047 "mouse_urxvt",
13048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049# ifdef FEAT_MOUSE_XTERM
13050 "mouse_xterm",
13051# endif
13052#endif
13053#ifdef FEAT_MBYTE
13054 "multi_byte",
13055#endif
13056#ifdef FEAT_MBYTE_IME
13057 "multi_byte_ime",
13058#endif
13059#ifdef FEAT_MULTI_LANG
13060 "multi_lang",
13061#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013062#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013063#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013064 "mzscheme",
13065#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067#ifdef FEAT_OLE
13068 "ole",
13069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070#ifdef FEAT_PATH_EXTRA
13071 "path_extra",
13072#endif
13073#ifdef FEAT_PERL
13074#ifndef DYNAMIC_PERL
13075 "perl",
13076#endif
13077#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013078#ifdef FEAT_PERSISTENT_UNDO
13079 "persistent_undo",
13080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081#ifdef FEAT_PYTHON
13082#ifndef DYNAMIC_PYTHON
13083 "python",
13084#endif
13085#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013086#ifdef FEAT_PYTHON3
13087#ifndef DYNAMIC_PYTHON3
13088 "python3",
13089#endif
13090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091#ifdef FEAT_POSTSCRIPT
13092 "postscript",
13093#endif
13094#ifdef FEAT_PRINTER
13095 "printer",
13096#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013097#ifdef FEAT_PROFILE
13098 "profile",
13099#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013100#ifdef FEAT_RELTIME
13101 "reltime",
13102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103#ifdef FEAT_QUICKFIX
13104 "quickfix",
13105#endif
13106#ifdef FEAT_RIGHTLEFT
13107 "rightleft",
13108#endif
13109#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13110 "ruby",
13111#endif
13112#ifdef FEAT_SCROLLBIND
13113 "scrollbind",
13114#endif
13115#ifdef FEAT_CMDL_INFO
13116 "showcmd",
13117 "cmdline_info",
13118#endif
13119#ifdef FEAT_SIGNS
13120 "signs",
13121#endif
13122#ifdef FEAT_SMARTINDENT
13123 "smartindent",
13124#endif
13125#ifdef FEAT_SNIFF
13126 "sniff",
13127#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013128#ifdef STARTUPTIME
13129 "startuptime",
13130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131#ifdef FEAT_STL_OPT
13132 "statusline",
13133#endif
13134#ifdef FEAT_SUN_WORKSHOP
13135 "sun_workshop",
13136#endif
13137#ifdef FEAT_NETBEANS_INTG
13138 "netbeans_intg",
13139#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013140#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013141 "spell",
13142#endif
13143#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 "syntax",
13145#endif
13146#if defined(USE_SYSTEM) || !defined(UNIX)
13147 "system",
13148#endif
13149#ifdef FEAT_TAG_BINS
13150 "tag_binary",
13151#endif
13152#ifdef FEAT_TAG_OLDSTATIC
13153 "tag_old_static",
13154#endif
13155#ifdef FEAT_TAG_ANYWHITE
13156 "tag_any_white",
13157#endif
13158#ifdef FEAT_TCL
13159# ifndef DYNAMIC_TCL
13160 "tcl",
13161# endif
13162#endif
13163#ifdef TERMINFO
13164 "terminfo",
13165#endif
13166#ifdef FEAT_TERMRESPONSE
13167 "termresponse",
13168#endif
13169#ifdef FEAT_TEXTOBJ
13170 "textobjects",
13171#endif
13172#ifdef HAVE_TGETENT
13173 "tgetent",
13174#endif
13175#ifdef FEAT_TITLE
13176 "title",
13177#endif
13178#ifdef FEAT_TOOLBAR
13179 "toolbar",
13180#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013181#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13182 "unnamedplus",
13183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184#ifdef FEAT_USR_CMDS
13185 "user-commands", /* was accidentally included in 5.4 */
13186 "user_commands",
13187#endif
13188#ifdef FEAT_VIMINFO
13189 "viminfo",
13190#endif
13191#ifdef FEAT_VERTSPLIT
13192 "vertsplit",
13193#endif
13194#ifdef FEAT_VIRTUALEDIT
13195 "virtualedit",
13196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198#ifdef FEAT_VISUALEXTRA
13199 "visualextra",
13200#endif
13201#ifdef FEAT_VREPLACE
13202 "vreplace",
13203#endif
13204#ifdef FEAT_WILDIGN
13205 "wildignore",
13206#endif
13207#ifdef FEAT_WILDMENU
13208 "wildmenu",
13209#endif
13210#ifdef FEAT_WINDOWS
13211 "windows",
13212#endif
13213#ifdef FEAT_WAK
13214 "winaltkeys",
13215#endif
13216#ifdef FEAT_WRITEBACKUP
13217 "writebackup",
13218#endif
13219#ifdef FEAT_XIM
13220 "xim",
13221#endif
13222#ifdef FEAT_XFONTSET
13223 "xfontset",
13224#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013225#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013226 "xpm",
13227 "xpm_w32", /* for backward compatibility */
13228#else
13229# if defined(HAVE_XPM)
13230 "xpm",
13231# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233#ifdef USE_XSMP
13234 "xsmp",
13235#endif
13236#ifdef USE_XSMP_INTERACT
13237 "xsmp_interact",
13238#endif
13239#ifdef FEAT_XCLIPBOARD
13240 "xterm_clipboard",
13241#endif
13242#ifdef FEAT_XTERM_SAVE
13243 "xterm_save",
13244#endif
13245#if defined(UNIX) && defined(FEAT_X11)
13246 "X11",
13247#endif
13248 NULL
13249 };
13250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 for (i = 0; has_list[i] != NULL; ++i)
13253 if (STRICMP(name, has_list[i]) == 0)
13254 {
13255 n = TRUE;
13256 break;
13257 }
13258
13259 if (n == FALSE)
13260 {
13261 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013262 {
13263 if (name[5] == '-'
13264 && STRLEN(name) > 11
13265 && vim_isdigit(name[6])
13266 && vim_isdigit(name[8])
13267 && vim_isdigit(name[10]))
13268 {
13269 int major = atoi((char *)name + 6);
13270 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013271
13272 /* Expect "patch-9.9.01234". */
13273 n = (major < VIM_VERSION_MAJOR
13274 || (major == VIM_VERSION_MAJOR
13275 && (minor < VIM_VERSION_MINOR
13276 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013277 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013278 }
13279 else
13280 n = has_patch(atoi((char *)name + 5));
13281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 else if (STRICMP(name, "vim_starting") == 0)
13283 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013284#ifdef FEAT_MBYTE
13285 else if (STRICMP(name, "multi_byte_encoding") == 0)
13286 n = has_mbyte;
13287#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013288#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13289 else if (STRICMP(name, "balloon_multiline") == 0)
13290 n = multiline_balloon_available();
13291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292#ifdef DYNAMIC_TCL
13293 else if (STRICMP(name, "tcl") == 0)
13294 n = tcl_enabled(FALSE);
13295#endif
13296#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13297 else if (STRICMP(name, "iconv") == 0)
13298 n = iconv_enabled(FALSE);
13299#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013300#ifdef DYNAMIC_LUA
13301 else if (STRICMP(name, "lua") == 0)
13302 n = lua_enabled(FALSE);
13303#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013304#ifdef DYNAMIC_MZSCHEME
13305 else if (STRICMP(name, "mzscheme") == 0)
13306 n = mzscheme_enabled(FALSE);
13307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308#ifdef DYNAMIC_RUBY
13309 else if (STRICMP(name, "ruby") == 0)
13310 n = ruby_enabled(FALSE);
13311#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013312#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313#ifdef DYNAMIC_PYTHON
13314 else if (STRICMP(name, "python") == 0)
13315 n = python_enabled(FALSE);
13316#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013317#endif
13318#ifdef FEAT_PYTHON3
13319#ifdef DYNAMIC_PYTHON3
13320 else if (STRICMP(name, "python3") == 0)
13321 n = python3_enabled(FALSE);
13322#endif
13323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#ifdef DYNAMIC_PERL
13325 else if (STRICMP(name, "perl") == 0)
13326 n = perl_enabled(FALSE);
13327#endif
13328#ifdef FEAT_GUI
13329 else if (STRICMP(name, "gui_running") == 0)
13330 n = (gui.in_use || gui.starting);
13331# ifdef FEAT_GUI_W32
13332 else if (STRICMP(name, "gui_win32s") == 0)
13333 n = gui_is_win32s();
13334# endif
13335# ifdef FEAT_BROWSE
13336 else if (STRICMP(name, "browse") == 0)
13337 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13338# endif
13339#endif
13340#ifdef FEAT_SYN_HL
13341 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013342 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343#endif
13344#if defined(WIN3264)
13345 else if (STRICMP(name, "win95") == 0)
13346 n = mch_windows95();
13347#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013348#ifdef FEAT_NETBEANS_INTG
13349 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013350 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 }
13353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355}
13356
13357/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013358 * "has_key()" function
13359 */
13360 static void
13361f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013364{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013365 if (argvars[0].v_type != VAR_DICT)
13366 {
13367 EMSG(_(e_dictreq));
13368 return;
13369 }
13370 if (argvars[0].vval.v_dict == NULL)
13371 return;
13372
13373 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013374 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013375}
13376
13377/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013378 * "haslocaldir()" function
13379 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013380 static void
13381f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013382 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013383 typval_T *rettv;
13384{
13385 rettv->vval.v_number = (curwin->w_localdir != NULL);
13386}
13387
13388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 * "hasmapto()" function
13390 */
13391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013392f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013393 typval_T *argvars;
13394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395{
13396 char_u *name;
13397 char_u *mode;
13398 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013399 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013402 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 mode = (char_u *)"nvo";
13404 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013406 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013407 if (argvars[2].v_type != VAR_UNKNOWN)
13408 abbr = get_tv_number(&argvars[2]);
13409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410
Bram Moolenaar2c932302006-03-18 21:42:09 +000013411 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415}
13416
13417/*
13418 * "histadd()" function
13419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013422 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424{
13425#ifdef FEAT_CMDHIST
13426 int histype;
13427 char_u *str;
13428 char_u buf[NUMBUFLEN];
13429#endif
13430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013431 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 if (check_restricted() || check_secure())
13433 return;
13434#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13436 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 if (histype >= 0)
13438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 if (*str != NUL)
13441 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013442 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 return;
13446 }
13447 }
13448#endif
13449}
13450
13451/*
13452 * "histdel()" function
13453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013456 typval_T *argvars UNUSED;
13457 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458{
13459#ifdef FEAT_CMDHIST
13460 int n;
13461 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013462 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13465 if (str == NULL)
13466 n = 0;
13467 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013470 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013472 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 else
13475 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013476 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 get_tv_string_buf(&argvars[1], buf));
13478 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479#endif
13480}
13481
13482/*
13483 * "histget()" function
13484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013486f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013487 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489{
13490#ifdef FEAT_CMDHIST
13491 int type;
13492 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013495 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13496 if (str == NULL)
13497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499 {
13500 type = get_histtype(str);
13501 if (argvars[1].v_type == VAR_UNKNOWN)
13502 idx = get_history_idx(type);
13503 else
13504 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13505 /* -1 on type error */
13506 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512}
13513
13514/*
13515 * "histnr()" function
13516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013519 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521{
13522 int i;
13523
13524#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525 char_u *history = get_tv_string_chk(&argvars[0]);
13526
13527 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 if (i >= HIST_CMD && i < HIST_COUNT)
13529 i = get_history_idx(i);
13530 else
13531#endif
13532 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534}
13535
13536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 * "highlightID(name)" function
13538 */
13539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013541 typval_T *argvars;
13542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013544 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545}
13546
13547/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013548 * "highlight_exists()" function
13549 */
13550 static void
13551f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013552 typval_T *argvars;
13553 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013554{
13555 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13556}
13557
13558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 * "hostname()" function
13560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013563 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565{
13566 char_u hostname[256];
13567
13568 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->v_type = VAR_STRING;
13570 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571}
13572
13573/*
13574 * iconv() function
13575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013578 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580{
13581#ifdef FEAT_MBYTE
13582 char_u buf1[NUMBUFLEN];
13583 char_u buf2[NUMBUFLEN];
13584 char_u *from, *to, *str;
13585 vimconv_T vimconv;
13586#endif
13587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588 rettv->v_type = VAR_STRING;
13589 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590
13591#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 str = get_tv_string(&argvars[0]);
13593 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13594 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 vimconv.vc_type = CONV_NONE;
13596 convert_setup(&vimconv, from, to);
13597
13598 /* If the encodings are equal, no conversion needed. */
13599 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603
13604 convert_setup(&vimconv, NULL, NULL);
13605 vim_free(from);
13606 vim_free(to);
13607#endif
13608}
13609
13610/*
13611 * "indent()" function
13612 */
13613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013615 typval_T *argvars;
13616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618 linenr_T lnum;
13619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625}
13626
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013627/*
13628 * "index()" function
13629 */
13630 static void
13631f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013634{
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 list_T *l;
13636 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013637 long idx = 0;
13638 int ic = FALSE;
13639
13640 rettv->vval.v_number = -1;
13641 if (argvars[0].v_type != VAR_LIST)
13642 {
13643 EMSG(_(e_listreq));
13644 return;
13645 }
13646 l = argvars[0].vval.v_list;
13647 if (l != NULL)
13648 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013649 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013652 int error = FALSE;
13653
Bram Moolenaar758711c2005-02-02 23:11:38 +000013654 /* Start at specified item. Use the cached index that list_find()
13655 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013657 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013658 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013659 ic = get_tv_number_chk(&argvars[3], &error);
13660 if (error)
13661 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013662 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013663
Bram Moolenaar758711c2005-02-02 23:11:38 +000013664 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013665 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013666 {
13667 rettv->vval.v_number = idx;
13668 break;
13669 }
13670 }
13671}
13672
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673static int inputsecret_flag = 0;
13674
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013675static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13676
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013678 * This function is used by f_input() and f_inputdialog() functions. The third
13679 * argument to f_input() specifies the type of completion to use at the
13680 * prompt. The third argument to f_inputdialog() specifies the value to return
13681 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 */
13683 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013684get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013685 typval_T *argvars;
13686 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013687 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013689 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 char_u *p = NULL;
13691 int c;
13692 char_u buf[NUMBUFLEN];
13693 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013694 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013695 int xp_type = EXPAND_NOTHING;
13696 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700
13701#ifdef NO_CONSOLE_INPUT
13702 /* While starting up, there is no place to enter text. */
13703 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705#endif
13706
13707 cmd_silent = FALSE; /* Want to see the prompt. */
13708 if (prompt != NULL)
13709 {
13710 /* Only the part of the message after the last NL is considered as
13711 * prompt for the command line */
13712 p = vim_strrchr(prompt, '\n');
13713 if (p == NULL)
13714 p = prompt;
13715 else
13716 {
13717 ++p;
13718 c = *p;
13719 *p = NUL;
13720 msg_start();
13721 msg_clr_eos();
13722 msg_puts_attr(prompt, echo_attr);
13723 msg_didout = FALSE;
13724 msg_starthere();
13725 *p = c;
13726 }
13727 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 if (argvars[1].v_type != VAR_UNKNOWN)
13730 {
13731 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13732 if (defstr != NULL)
13733 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013735 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013736 {
13737 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013738 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013739 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013740
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013741 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013742 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013743
Bram Moolenaar4463f292005-09-25 22:20:24 +000013744 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13745 if (xp_name == NULL)
13746 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013747
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013748 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013749
Bram Moolenaar4463f292005-09-25 22:20:24 +000013750 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13751 &xp_arg) == FAIL)
13752 return;
13753 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013754 }
13755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013756 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013757 {
13758# ifdef FEAT_EX_EXTRA
13759 int save_ex_normal_busy = ex_normal_busy;
13760 ex_normal_busy = 0;
13761# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013762 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013763 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13764 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013765# ifdef FEAT_EX_EXTRA
13766 ex_normal_busy = save_ex_normal_busy;
13767# endif
13768 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013769 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013770 && argvars[1].v_type != VAR_UNKNOWN
13771 && argvars[2].v_type != VAR_UNKNOWN)
13772 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13773 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013774
13775 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 /* since the user typed this, no need to wait for return */
13778 need_wait_return = FALSE;
13779 msg_didout = FALSE;
13780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 cmd_silent = cmd_silent_save;
13782}
13783
13784/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013785 * "input()" function
13786 * Also handles inputsecret() when inputsecret is set.
13787 */
13788 static void
13789f_input(argvars, rettv)
13790 typval_T *argvars;
13791 typval_T *rettv;
13792{
13793 get_user_input(argvars, rettv, FALSE);
13794}
13795
13796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 * "inputdialog()" function
13798 */
13799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *argvars;
13802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803{
13804#if defined(FEAT_GUI_TEXTDIALOG)
13805 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13806 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13807 {
13808 char_u *message;
13809 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013810 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013812 message = get_tv_string_chk(&argvars[0]);
13813 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013814 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013815 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 else
13817 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013818 if (message != NULL && defstr != NULL
13819 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013820 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013821 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822 else
13823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013824 if (message != NULL && defstr != NULL
13825 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013826 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013827 rettv->vval.v_string = vim_strsave(
13828 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013832 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 }
13834 else
13835#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013836 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837}
13838
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013839/*
13840 * "inputlist()" function
13841 */
13842 static void
13843f_inputlist(argvars, rettv)
13844 typval_T *argvars;
13845 typval_T *rettv;
13846{
13847 listitem_T *li;
13848 int selected;
13849 int mouse_used;
13850
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013851#ifdef NO_CONSOLE_INPUT
13852 /* While starting up, there is no place to enter text. */
13853 if (no_console_input())
13854 return;
13855#endif
13856 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13857 {
13858 EMSG2(_(e_listarg), "inputlist()");
13859 return;
13860 }
13861
13862 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013863 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013864 lines_left = Rows; /* avoid more prompt */
13865 msg_scroll = TRUE;
13866 msg_clr_eos();
13867
13868 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13869 {
13870 msg_puts(get_tv_string(&li->li_tv));
13871 msg_putchar('\n');
13872 }
13873
13874 /* Ask for choice. */
13875 selected = prompt_for_number(&mouse_used);
13876 if (mouse_used)
13877 selected -= lines_left;
13878
13879 rettv->vval.v_number = selected;
13880}
13881
13882
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13884
13885/*
13886 * "inputrestore()" function
13887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013889f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013890 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892{
13893 if (ga_userinput.ga_len > 0)
13894 {
13895 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13897 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013898 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 }
13900 else if (p_verbose > 1)
13901 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013902 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013903 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 }
13905}
13906
13907/*
13908 * "inputsave()" function
13909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013911f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013912 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013915 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 if (ga_grow(&ga_userinput, 1) == OK)
13917 {
13918 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13919 + ga_userinput.ga_len);
13920 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013921 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 }
13923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013924 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925}
13926
13927/*
13928 * "inputsecret()" function
13929 */
13930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013932 typval_T *argvars;
13933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934{
13935 ++cmdline_star;
13936 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 --cmdline_star;
13939 --inputsecret_flag;
13940}
13941
13942/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013943 * "insert()" function
13944 */
13945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013946f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013947 typval_T *argvars;
13948 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013949{
13950 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013951 listitem_T *item;
13952 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013953 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013954
13955 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013957 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013958 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013959 {
13960 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013961 before = get_tv_number_chk(&argvars[2], &error);
13962 if (error)
13963 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013964
Bram Moolenaar758711c2005-02-02 23:11:38 +000013965 if (before == l->lv_len)
13966 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013967 else
13968 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013969 item = list_find(l, before);
13970 if (item == NULL)
13971 {
13972 EMSGN(_(e_listidx), before);
13973 l = NULL;
13974 }
13975 }
13976 if (l != NULL)
13977 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013978 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013979 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013980 }
13981 }
13982}
13983
13984/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013985 * "invert(expr)" function
13986 */
13987 static void
13988f_invert(argvars, rettv)
13989 typval_T *argvars;
13990 typval_T *rettv;
13991{
13992 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13993}
13994
13995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 * "isdirectory()" function
13997 */
13998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013999f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014000 typval_T *argvars;
14001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014003 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004}
14005
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014006/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014007 * "islocked()" function
14008 */
14009 static void
14010f_islocked(argvars, rettv)
14011 typval_T *argvars;
14012 typval_T *rettv;
14013{
14014 lval_T lv;
14015 char_u *end;
14016 dictitem_T *di;
14017
14018 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014019 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14020 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014021 if (end != NULL && lv.ll_name != NULL)
14022 {
14023 if (*end != NUL)
14024 EMSG(_(e_trailing));
14025 else
14026 {
14027 if (lv.ll_tv == NULL)
14028 {
14029 if (check_changedtick(lv.ll_name))
14030 rettv->vval.v_number = 1; /* always locked */
14031 else
14032 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014033 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014034 if (di != NULL)
14035 {
14036 /* Consider a variable locked when:
14037 * 1. the variable itself is locked
14038 * 2. the value of the variable is locked.
14039 * 3. the List or Dict value is locked.
14040 */
14041 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14042 || tv_islocked(&di->di_tv));
14043 }
14044 }
14045 }
14046 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014047 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014048 else if (lv.ll_newkey != NULL)
14049 EMSG2(_(e_dictkey), lv.ll_newkey);
14050 else if (lv.ll_list != NULL)
14051 /* List item. */
14052 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14053 else
14054 /* Dictionary item. */
14055 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14056 }
14057 }
14058
14059 clear_lval(&lv);
14060}
14061
Bram Moolenaar33570922005-01-25 22:26:29 +000014062static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014063
14064/*
14065 * Turn a dict into a list:
14066 * "what" == 0: list of keys
14067 * "what" == 1: list of values
14068 * "what" == 2: list of items
14069 */
14070 static void
14071dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014072 typval_T *argvars;
14073 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014074 int what;
14075{
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 list_T *l2;
14077 dictitem_T *di;
14078 hashitem_T *hi;
14079 listitem_T *li;
14080 listitem_T *li2;
14081 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014082 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014083
Bram Moolenaar8c711452005-01-14 21:53:12 +000014084 if (argvars[0].v_type != VAR_DICT)
14085 {
14086 EMSG(_(e_dictreq));
14087 return;
14088 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014089 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014090 return;
14091
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014092 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014093 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014095 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014098 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014100 --todo;
14101 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014102
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014103 li = listitem_alloc();
14104 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014105 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014106 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014107
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014108 if (what == 0)
14109 {
14110 /* keys() */
14111 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014112 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014113 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14114 }
14115 else if (what == 1)
14116 {
14117 /* values() */
14118 copy_tv(&di->di_tv, &li->li_tv);
14119 }
14120 else
14121 {
14122 /* items() */
14123 l2 = list_alloc();
14124 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014125 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014126 li->li_tv.vval.v_list = l2;
14127 if (l2 == NULL)
14128 break;
14129 ++l2->lv_refcount;
14130
14131 li2 = listitem_alloc();
14132 if (li2 == NULL)
14133 break;
14134 list_append(l2, li2);
14135 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014136 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014137 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14138
14139 li2 = listitem_alloc();
14140 if (li2 == NULL)
14141 break;
14142 list_append(l2, li2);
14143 copy_tv(&di->di_tv, &li2->li_tv);
14144 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014145 }
14146 }
14147}
14148
14149/*
14150 * "items(dict)" function
14151 */
14152 static void
14153f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014154 typval_T *argvars;
14155 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014156{
14157 dict_list(argvars, rettv, 2);
14158}
14159
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014161 * "join()" function
14162 */
14163 static void
14164f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 typval_T *argvars;
14166 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014167{
14168 garray_T ga;
14169 char_u *sep;
14170
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014171 if (argvars[0].v_type != VAR_LIST)
14172 {
14173 EMSG(_(e_listreq));
14174 return;
14175 }
14176 if (argvars[0].vval.v_list == NULL)
14177 return;
14178 if (argvars[1].v_type == VAR_UNKNOWN)
14179 sep = (char_u *)" ";
14180 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014181 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014182
14183 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184
14185 if (sep != NULL)
14186 {
14187 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014188 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 ga_append(&ga, NUL);
14190 rettv->vval.v_string = (char_u *)ga.ga_data;
14191 }
14192 else
14193 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014194}
14195
14196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014197 * "keys()" function
14198 */
14199 static void
14200f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 typval_T *argvars;
14202 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014203{
14204 dict_list(argvars, rettv, 0);
14205}
14206
14207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 * "last_buffer_nr()" function.
14209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014211f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014212 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214{
14215 int n = 0;
14216 buf_T *buf;
14217
14218 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14219 if (n < buf->b_fnum)
14220 n = buf->b_fnum;
14221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014222 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014223}
14224
14225/*
14226 * "len()" function
14227 */
14228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014229f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014230 typval_T *argvars;
14231 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014232{
14233 switch (argvars[0].v_type)
14234 {
14235 case VAR_STRING:
14236 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 rettv->vval.v_number = (varnumber_T)STRLEN(
14238 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014239 break;
14240 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014242 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014243 case VAR_DICT:
14244 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14245 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014246 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014247 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014248 break;
14249 }
14250}
14251
Bram Moolenaar33570922005-01-25 22:26:29 +000014252static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014253
14254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014256 typval_T *argvars;
14257 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014258 int type;
14259{
14260#ifdef FEAT_LIBCALL
14261 char_u *string_in;
14262 char_u **string_result;
14263 int nr_result;
14264#endif
14265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014267 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014268 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014269
14270 if (check_restricted() || check_secure())
14271 return;
14272
14273#ifdef FEAT_LIBCALL
14274 /* The first two args must be strings, otherwise its meaningless */
14275 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14276 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014277 string_in = NULL;
14278 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014279 string_in = argvars[2].vval.v_string;
14280 if (type == VAR_NUMBER)
14281 string_result = NULL;
14282 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014284 if (mch_libcall(argvars[0].vval.v_string,
14285 argvars[1].vval.v_string,
14286 string_in,
14287 argvars[2].vval.v_number,
14288 string_result,
14289 &nr_result) == OK
14290 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014292 }
14293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294}
14295
14296/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014297 * "libcall()" function
14298 */
14299 static void
14300f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014301 typval_T *argvars;
14302 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014303{
14304 libcall_common(argvars, rettv, VAR_STRING);
14305}
14306
14307/*
14308 * "libcallnr()" function
14309 */
14310 static void
14311f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014312 typval_T *argvars;
14313 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014314{
14315 libcall_common(argvars, rettv, VAR_NUMBER);
14316}
14317
14318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 * "line(string)" function
14320 */
14321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014322f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014323 typval_T *argvars;
14324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325{
14326 linenr_T lnum = 0;
14327 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014328 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014330 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 if (fp != NULL)
14332 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334}
14335
14336/*
14337 * "line2byte(lnum)" function
14338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014341 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343{
14344#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346#else
14347 linenr_T lnum;
14348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014349 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14354 if (rettv->vval.v_number >= 0)
14355 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356#endif
14357}
14358
14359/*
14360 * "lispindent(lnum)" function
14361 */
14362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366{
14367#ifdef FEAT_LISP
14368 pos_T pos;
14369 linenr_T lnum;
14370
14371 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14374 {
14375 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 curwin->w_cursor = pos;
14378 }
14379 else
14380#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382}
14383
14384/*
14385 * "localtime()" function
14386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014389 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014392 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393}
14394
Bram Moolenaar33570922005-01-25 22:26:29 +000014395static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396
14397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014399 typval_T *argvars;
14400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 int exact;
14402{
14403 char_u *keys;
14404 char_u *which;
14405 char_u buf[NUMBUFLEN];
14406 char_u *keys_buf = NULL;
14407 char_u *rhs;
14408 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014409 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014410 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014411 mapblock_T *mp;
14412 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413
14414 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415 rettv->v_type = VAR_STRING;
14416 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 if (*keys == NUL)
14420 return;
14421
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014422 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014426 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014427 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014428 if (argvars[3].v_type != VAR_UNKNOWN)
14429 get_dict = get_tv_number(&argvars[3]);
14430 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 else
14433 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 if (which == NULL)
14435 return;
14436
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 mode = get_map_mode(&which, 0);
14438
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014439 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014440 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014442
14443 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014445 /* Return a string. */
14446 if (rhs != NULL)
14447 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448
Bram Moolenaarbd743252010-10-20 21:23:33 +020014449 }
14450 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14451 {
14452 /* Return a dictionary. */
14453 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14454 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14455 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456
Bram Moolenaarbd743252010-10-20 21:23:33 +020014457 dict_add_nr_str(dict, "lhs", 0L, lhs);
14458 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14459 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14460 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14461 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14462 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14463 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014464 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014465 dict_add_nr_str(dict, "mode", 0L, mapmode);
14466
14467 vim_free(lhs);
14468 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 }
14470}
14471
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014472#ifdef FEAT_FLOAT
14473/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014474 * "log()" function
14475 */
14476 static void
14477f_log(argvars, rettv)
14478 typval_T *argvars;
14479 typval_T *rettv;
14480{
14481 float_T f;
14482
14483 rettv->v_type = VAR_FLOAT;
14484 if (get_float_arg(argvars, &f) == OK)
14485 rettv->vval.v_float = log(f);
14486 else
14487 rettv->vval.v_float = 0.0;
14488}
14489
14490/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014491 * "log10()" function
14492 */
14493 static void
14494f_log10(argvars, rettv)
14495 typval_T *argvars;
14496 typval_T *rettv;
14497{
14498 float_T f;
14499
14500 rettv->v_type = VAR_FLOAT;
14501 if (get_float_arg(argvars, &f) == OK)
14502 rettv->vval.v_float = log10(f);
14503 else
14504 rettv->vval.v_float = 0.0;
14505}
14506#endif
14507
Bram Moolenaar1dced572012-04-05 16:54:08 +020014508#ifdef FEAT_LUA
14509/*
14510 * "luaeval()" function
14511 */
14512 static void
14513f_luaeval(argvars, rettv)
14514 typval_T *argvars;
14515 typval_T *rettv;
14516{
14517 char_u *str;
14518 char_u buf[NUMBUFLEN];
14519
14520 str = get_tv_string_buf(&argvars[0], buf);
14521 do_luaeval(str, argvars + 1, rettv);
14522}
14523#endif
14524
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014526 * "map()" function
14527 */
14528 static void
14529f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014530 typval_T *argvars;
14531 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014532{
14533 filter_map(argvars, rettv, TRUE);
14534}
14535
14536/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014537 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 */
14539 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014540f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014541 typval_T *argvars;
14542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014544 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545}
14546
14547/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014548 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549 */
14550 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014551f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014552 typval_T *argvars;
14553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556}
14557
Bram Moolenaar33570922005-01-25 22:26:29 +000014558static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559
14560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014561find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014562 typval_T *argvars;
14563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 int type;
14565{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014566 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014567 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014568 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 char_u *pat;
14570 regmatch_T regmatch;
14571 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014572 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 char_u *save_cpo;
14574 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014575 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014576 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014577 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014578 list_T *l = NULL;
14579 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014580 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014581 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582
14583 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14584 save_cpo = p_cpo;
14585 p_cpo = (char_u *)"";
14586
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014587 rettv->vval.v_number = -1;
14588 if (type == 3)
14589 {
14590 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014591 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014592 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014593 }
14594 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014596 rettv->v_type = VAR_STRING;
14597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014600 if (argvars[0].v_type == VAR_LIST)
14601 {
14602 if ((l = argvars[0].vval.v_list) == NULL)
14603 goto theend;
14604 li = l->lv_first;
14605 }
14606 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014607 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014608 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014609 len = (long)STRLEN(str);
14610 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014612 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14613 if (pat == NULL)
14614 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014615
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014616 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 int error = FALSE;
14619
14620 start = get_tv_number_chk(&argvars[2], &error);
14621 if (error)
14622 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014623 if (l != NULL)
14624 {
14625 li = list_find(l, start);
14626 if (li == NULL)
14627 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014628 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014629 }
14630 else
14631 {
14632 if (start < 0)
14633 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014634 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014635 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014636 /* When "count" argument is there ignore matches before "start",
14637 * otherwise skip part of the string. Differs when pattern is "^"
14638 * or "\<". */
14639 if (argvars[3].v_type != VAR_UNKNOWN)
14640 startcol = start;
14641 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014642 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014643 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014644 len -= start;
14645 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014646 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014647
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014648 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649 nth = get_tv_number_chk(&argvars[3], &error);
14650 if (error)
14651 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 }
14653
14654 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14655 if (regmatch.regprog != NULL)
14656 {
14657 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014658
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014659 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014660 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014661 if (l != NULL)
14662 {
14663 if (li == NULL)
14664 {
14665 match = FALSE;
14666 break;
14667 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014668 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014669 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014670 if (str == NULL)
14671 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014672 }
14673
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014674 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014675
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014676 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014677 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014678 if (l == NULL && !match)
14679 break;
14680
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014681 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014682 if (l != NULL)
14683 {
14684 li = li->li_next;
14685 ++idx;
14686 }
14687 else
14688 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014689#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014690 startcol = (colnr_T)(regmatch.startp[0]
14691 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014692#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014693 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014694#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014695 if (startcol > (colnr_T)len
14696 || str + startcol <= regmatch.startp[0])
14697 {
14698 match = FALSE;
14699 break;
14700 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014701 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014702 }
14703
14704 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014706 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014707 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014708 int i;
14709
14710 /* return list with matched string and submatches */
14711 for (i = 0; i < NSUBEXP; ++i)
14712 {
14713 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014714 {
14715 if (list_append_string(rettv->vval.v_list,
14716 (char_u *)"", 0) == FAIL)
14717 break;
14718 }
14719 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014720 regmatch.startp[i],
14721 (int)(regmatch.endp[i] - regmatch.startp[i]))
14722 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014723 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014724 }
14725 }
14726 else if (type == 2)
14727 {
14728 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014729 if (l != NULL)
14730 copy_tv(&li->li_tv, rettv);
14731 else
14732 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014734 }
14735 else if (l != NULL)
14736 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 else
14738 {
14739 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014740 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 (varnumber_T)(regmatch.startp[0] - str);
14742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014743 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014745 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 }
14747 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014748 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 }
14750
14751theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 p_cpo = save_cpo;
14754}
14755
14756/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014757 * "match()" function
14758 */
14759 static void
14760f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014761 typval_T *argvars;
14762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014763{
14764 find_some_match(argvars, rettv, 1);
14765}
14766
14767/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014768 * "matchadd()" function
14769 */
14770 static void
14771f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014772 typval_T *argvars UNUSED;
14773 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014774{
14775#ifdef FEAT_SEARCH_EXTRA
14776 char_u buf[NUMBUFLEN];
14777 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14778 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14779 int prio = 10; /* default priority */
14780 int id = -1;
14781 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014782 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014783
14784 rettv->vval.v_number = -1;
14785
14786 if (grp == NULL || pat == NULL)
14787 return;
14788 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014789 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014790 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014791 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014792 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014793 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014794 if (argvars[4].v_type != VAR_UNKNOWN)
14795 {
14796 if (argvars[4].v_type != VAR_DICT)
14797 {
14798 EMSG(_(e_dictreq));
14799 return;
14800 }
14801 if (dict_find(argvars[4].vval.v_dict,
14802 (char_u *)"conceal", -1) != NULL)
14803 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14804 (char_u *)"conceal", FALSE);
14805 }
14806 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014807 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014808 if (error == TRUE)
14809 return;
14810 if (id >= 1 && id <= 3)
14811 {
14812 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14813 return;
14814 }
14815
Bram Moolenaar6561d522015-07-21 15:48:27 +020014816 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14817 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014818#endif
14819}
14820
14821/*
14822 * "matchaddpos()" function
14823 */
14824 static void
14825f_matchaddpos(argvars, rettv)
14826 typval_T *argvars UNUSED;
14827 typval_T *rettv UNUSED;
14828{
14829#ifdef FEAT_SEARCH_EXTRA
14830 char_u buf[NUMBUFLEN];
14831 char_u *group;
14832 int prio = 10;
14833 int id = -1;
14834 int error = FALSE;
14835 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014836 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014837
14838 rettv->vval.v_number = -1;
14839
14840 group = get_tv_string_buf_chk(&argvars[0], buf);
14841 if (group == NULL)
14842 return;
14843
14844 if (argvars[1].v_type != VAR_LIST)
14845 {
14846 EMSG2(_(e_listarg), "matchaddpos()");
14847 return;
14848 }
14849 l = argvars[1].vval.v_list;
14850 if (l == NULL)
14851 return;
14852
14853 if (argvars[2].v_type != VAR_UNKNOWN)
14854 {
14855 prio = get_tv_number_chk(&argvars[2], &error);
14856 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014857 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014858 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014859 if (argvars[4].v_type != VAR_UNKNOWN)
14860 {
14861 if (argvars[4].v_type != VAR_DICT)
14862 {
14863 EMSG(_(e_dictreq));
14864 return;
14865 }
14866 if (dict_find(argvars[4].vval.v_dict,
14867 (char_u *)"conceal", -1) != NULL)
14868 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14869 (char_u *)"conceal", FALSE);
14870 }
14871 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014872 }
14873 if (error == TRUE)
14874 return;
14875
14876 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14877 if (id == 1 || id == 2)
14878 {
14879 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14880 return;
14881 }
14882
Bram Moolenaar6561d522015-07-21 15:48:27 +020014883 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14884 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014885#endif
14886}
14887
14888/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014889 * "matcharg()" function
14890 */
14891 static void
14892f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014893 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014894 typval_T *rettv;
14895{
14896 if (rettv_list_alloc(rettv) == OK)
14897 {
14898#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014899 int id = get_tv_number(&argvars[0]);
14900 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014901
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014902 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014903 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014904 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14905 {
14906 list_append_string(rettv->vval.v_list,
14907 syn_id2name(m->hlg_id), -1);
14908 list_append_string(rettv->vval.v_list, m->pattern, -1);
14909 }
14910 else
14911 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014912 list_append_string(rettv->vval.v_list, NULL, -1);
14913 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014914 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014915 }
14916#endif
14917 }
14918}
14919
14920/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014921 * "matchdelete()" function
14922 */
14923 static void
14924f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014925 typval_T *argvars UNUSED;
14926 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014927{
14928#ifdef FEAT_SEARCH_EXTRA
14929 rettv->vval.v_number = match_delete(curwin,
14930 (int)get_tv_number(&argvars[0]), TRUE);
14931#endif
14932}
14933
14934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935 * "matchend()" function
14936 */
14937 static void
14938f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014939 typval_T *argvars;
14940 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014941{
14942 find_some_match(argvars, rettv, 0);
14943}
14944
14945/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014946 * "matchlist()" function
14947 */
14948 static void
14949f_matchlist(argvars, rettv)
14950 typval_T *argvars;
14951 typval_T *rettv;
14952{
14953 find_some_match(argvars, rettv, 3);
14954}
14955
14956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014957 * "matchstr()" function
14958 */
14959 static void
14960f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014961 typval_T *argvars;
14962 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014963{
14964 find_some_match(argvars, rettv, 2);
14965}
14966
Bram Moolenaar33570922005-01-25 22:26:29 +000014967static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014968
14969 static void
14970max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014971 typval_T *argvars;
14972 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014973 int domax;
14974{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014975 long n = 0;
14976 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014977 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014978
14979 if (argvars[0].v_type == VAR_LIST)
14980 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014981 list_T *l;
14982 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014983
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014984 l = argvars[0].vval.v_list;
14985 if (l != NULL)
14986 {
14987 li = l->lv_first;
14988 if (li != NULL)
14989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014990 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014991 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014992 {
14993 li = li->li_next;
14994 if (li == NULL)
14995 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014996 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014997 if (domax ? i > n : i < n)
14998 n = i;
14999 }
15000 }
15001 }
15002 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015003 else if (argvars[0].v_type == VAR_DICT)
15004 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015006 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015007 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015008 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015009
15010 d = argvars[0].vval.v_dict;
15011 if (d != NULL)
15012 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015013 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015014 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015016 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015017 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015018 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015019 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015020 if (first)
15021 {
15022 n = i;
15023 first = FALSE;
15024 }
15025 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015026 n = i;
15027 }
15028 }
15029 }
15030 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015031 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015032 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015033 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015034}
15035
15036/*
15037 * "max()" function
15038 */
15039 static void
15040f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015041 typval_T *argvars;
15042 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015043{
15044 max_min(argvars, rettv, TRUE);
15045}
15046
15047/*
15048 * "min()" function
15049 */
15050 static void
15051f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015052 typval_T *argvars;
15053 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015054{
15055 max_min(argvars, rettv, FALSE);
15056}
15057
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015058static int mkdir_recurse __ARGS((char_u *dir, int prot));
15059
15060/*
15061 * Create the directory in which "dir" is located, and higher levels when
15062 * needed.
15063 */
15064 static int
15065mkdir_recurse(dir, prot)
15066 char_u *dir;
15067 int prot;
15068{
15069 char_u *p;
15070 char_u *updir;
15071 int r = FAIL;
15072
15073 /* Get end of directory name in "dir".
15074 * We're done when it's "/" or "c:/". */
15075 p = gettail_sep(dir);
15076 if (p <= get_past_head(dir))
15077 return OK;
15078
15079 /* If the directory exists we're done. Otherwise: create it.*/
15080 updir = vim_strnsave(dir, (int)(p - dir));
15081 if (updir == NULL)
15082 return FAIL;
15083 if (mch_isdir(updir))
15084 r = OK;
15085 else if (mkdir_recurse(updir, prot) == OK)
15086 r = vim_mkdir_emsg(updir, prot);
15087 vim_free(updir);
15088 return r;
15089}
15090
15091#ifdef vim_mkdir
15092/*
15093 * "mkdir()" function
15094 */
15095 static void
15096f_mkdir(argvars, rettv)
15097 typval_T *argvars;
15098 typval_T *rettv;
15099{
15100 char_u *dir;
15101 char_u buf[NUMBUFLEN];
15102 int prot = 0755;
15103
15104 rettv->vval.v_number = FAIL;
15105 if (check_restricted() || check_secure())
15106 return;
15107
15108 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015109 if (*dir == NUL)
15110 rettv->vval.v_number = FAIL;
15111 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015112 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015113 if (*gettail(dir) == NUL)
15114 /* remove trailing slashes */
15115 *gettail_sep(dir) = NUL;
15116
15117 if (argvars[1].v_type != VAR_UNKNOWN)
15118 {
15119 if (argvars[2].v_type != VAR_UNKNOWN)
15120 prot = get_tv_number_chk(&argvars[2], NULL);
15121 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15122 mkdir_recurse(dir, prot);
15123 }
15124 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015125 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015126}
15127#endif
15128
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130 * "mode()" function
15131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015133f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015134 typval_T *argvars;
15135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015137 char_u buf[3];
15138
15139 buf[1] = NUL;
15140 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 if (VIsual_active)
15143 {
15144 if (VIsual_select)
15145 buf[0] = VIsual_mode + 's' - 'v';
15146 else
15147 buf[0] = VIsual_mode;
15148 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015149 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015150 || State == CONFIRM)
15151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015153 if (State == ASKMORE)
15154 buf[1] = 'm';
15155 else if (State == CONFIRM)
15156 buf[1] = '?';
15157 }
15158 else if (State == EXTERNCMD)
15159 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 else if (State & INSERT)
15161 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015162#ifdef FEAT_VREPLACE
15163 if (State & VREPLACE_FLAG)
15164 {
15165 buf[0] = 'R';
15166 buf[1] = 'v';
15167 }
15168 else
15169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170 if (State & REPLACE_FLAG)
15171 buf[0] = 'R';
15172 else
15173 buf[0] = 'i';
15174 }
15175 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015178 if (exmode_active)
15179 buf[1] = 'v';
15180 }
15181 else if (exmode_active)
15182 {
15183 buf[0] = 'c';
15184 buf[1] = 'e';
15185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015189 if (finish_op)
15190 buf[1] = 'o';
15191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015193 /* Clear out the minor mode when the argument is not a non-zero number or
15194 * non-empty string. */
15195 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015196 buf[1] = NUL;
15197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015198 rettv->vval.v_string = vim_strsave(buf);
15199 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200}
15201
Bram Moolenaar429fa852013-04-15 12:27:36 +020015202#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015203/*
15204 * "mzeval()" function
15205 */
15206 static void
15207f_mzeval(argvars, rettv)
15208 typval_T *argvars;
15209 typval_T *rettv;
15210{
15211 char_u *str;
15212 char_u buf[NUMBUFLEN];
15213
15214 str = get_tv_string_buf(&argvars[0], buf);
15215 do_mzeval(str, rettv);
15216}
Bram Moolenaar75676462013-01-30 14:55:42 +010015217
15218 void
15219mzscheme_call_vim(name, args, rettv)
15220 char_u *name;
15221 typval_T *args;
15222 typval_T *rettv;
15223{
15224 typval_T argvars[3];
15225
15226 argvars[0].v_type = VAR_STRING;
15227 argvars[0].vval.v_string = name;
15228 copy_tv(args, &argvars[1]);
15229 argvars[2].v_type = VAR_UNKNOWN;
15230 f_call(argvars, rettv);
15231 clear_tv(&argvars[1]);
15232}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015233#endif
15234
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236 * "nextnonblank()" function
15237 */
15238 static void
15239f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 typval_T *argvars;
15241 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242{
15243 linenr_T lnum;
15244
15245 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015248 {
15249 lnum = 0;
15250 break;
15251 }
15252 if (*skipwhite(ml_get(lnum)) != NUL)
15253 break;
15254 }
15255 rettv->vval.v_number = lnum;
15256}
15257
15258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 * "nr2char()" function
15260 */
15261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015263 typval_T *argvars;
15264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265{
15266 char_u buf[NUMBUFLEN];
15267
15268#ifdef FEAT_MBYTE
15269 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015270 {
15271 int utf8 = 0;
15272
15273 if (argvars[1].v_type != VAR_UNKNOWN)
15274 utf8 = get_tv_number_chk(&argvars[1], NULL);
15275 if (utf8)
15276 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15277 else
15278 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280 else
15281#endif
15282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015283 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284 buf[1] = NUL;
15285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286 rettv->v_type = VAR_STRING;
15287 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015288}
15289
15290/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015291 * "or(expr, expr)" function
15292 */
15293 static void
15294f_or(argvars, rettv)
15295 typval_T *argvars;
15296 typval_T *rettv;
15297{
15298 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15299 | get_tv_number_chk(&argvars[1], NULL);
15300}
15301
15302/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015303 * "pathshorten()" function
15304 */
15305 static void
15306f_pathshorten(argvars, rettv)
15307 typval_T *argvars;
15308 typval_T *rettv;
15309{
15310 char_u *p;
15311
15312 rettv->v_type = VAR_STRING;
15313 p = get_tv_string_chk(&argvars[0]);
15314 if (p == NULL)
15315 rettv->vval.v_string = NULL;
15316 else
15317 {
15318 p = vim_strsave(p);
15319 rettv->vval.v_string = p;
15320 if (p != NULL)
15321 shorten_dir(p);
15322 }
15323}
15324
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015325#ifdef FEAT_FLOAT
15326/*
15327 * "pow()" function
15328 */
15329 static void
15330f_pow(argvars, rettv)
15331 typval_T *argvars;
15332 typval_T *rettv;
15333{
15334 float_T fx, fy;
15335
15336 rettv->v_type = VAR_FLOAT;
15337 if (get_float_arg(argvars, &fx) == OK
15338 && get_float_arg(&argvars[1], &fy) == OK)
15339 rettv->vval.v_float = pow(fx, fy);
15340 else
15341 rettv->vval.v_float = 0.0;
15342}
15343#endif
15344
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015345/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015346 * "prevnonblank()" function
15347 */
15348 static void
15349f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015350 typval_T *argvars;
15351 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015352{
15353 linenr_T lnum;
15354
15355 lnum = get_tv_lnum(argvars);
15356 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15357 lnum = 0;
15358 else
15359 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15360 --lnum;
15361 rettv->vval.v_number = lnum;
15362}
15363
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015364#ifdef HAVE_STDARG_H
15365/* This dummy va_list is here because:
15366 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15367 * - locally in the function results in a "used before set" warning
15368 * - using va_start() to initialize it gives "function with fixed args" error */
15369static va_list ap;
15370#endif
15371
Bram Moolenaar8c711452005-01-14 21:53:12 +000015372/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015373 * "printf()" function
15374 */
15375 static void
15376f_printf(argvars, rettv)
15377 typval_T *argvars;
15378 typval_T *rettv;
15379{
15380 rettv->v_type = VAR_STRING;
15381 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015382#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015383 {
15384 char_u buf[NUMBUFLEN];
15385 int len;
15386 char_u *s;
15387 int saved_did_emsg = did_emsg;
15388 char *fmt;
15389
15390 /* Get the required length, allocate the buffer and do it for real. */
15391 did_emsg = FALSE;
15392 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015393 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015394 if (!did_emsg)
15395 {
15396 s = alloc(len + 1);
15397 if (s != NULL)
15398 {
15399 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015400 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015401 }
15402 }
15403 did_emsg |= saved_did_emsg;
15404 }
15405#endif
15406}
15407
15408/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015409 * "pumvisible()" function
15410 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015411 static void
15412f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015413 typval_T *argvars UNUSED;
15414 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015415{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015416#ifdef FEAT_INS_EXPAND
15417 if (pum_visible())
15418 rettv->vval.v_number = 1;
15419#endif
15420}
15421
Bram Moolenaardb913952012-06-29 12:54:53 +020015422#ifdef FEAT_PYTHON3
15423/*
15424 * "py3eval()" function
15425 */
15426 static void
15427f_py3eval(argvars, rettv)
15428 typval_T *argvars;
15429 typval_T *rettv;
15430{
15431 char_u *str;
15432 char_u buf[NUMBUFLEN];
15433
15434 str = get_tv_string_buf(&argvars[0], buf);
15435 do_py3eval(str, rettv);
15436}
15437#endif
15438
15439#ifdef FEAT_PYTHON
15440/*
15441 * "pyeval()" function
15442 */
15443 static void
15444f_pyeval(argvars, rettv)
15445 typval_T *argvars;
15446 typval_T *rettv;
15447{
15448 char_u *str;
15449 char_u buf[NUMBUFLEN];
15450
15451 str = get_tv_string_buf(&argvars[0], buf);
15452 do_pyeval(str, rettv);
15453}
15454#endif
15455
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015456/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015457 * "range()" function
15458 */
15459 static void
15460f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015461 typval_T *argvars;
15462 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015463{
15464 long start;
15465 long end;
15466 long stride = 1;
15467 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015468 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015471 if (argvars[1].v_type == VAR_UNKNOWN)
15472 {
15473 end = start - 1;
15474 start = 0;
15475 }
15476 else
15477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015478 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015479 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015480 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015481 }
15482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015483 if (error)
15484 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015485 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015486 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015487 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015488 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015489 else
15490 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015491 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015492 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015493 if (list_append_number(rettv->vval.v_list,
15494 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015495 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015496 }
15497}
15498
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015499/*
15500 * "readfile()" function
15501 */
15502 static void
15503f_readfile(argvars, rettv)
15504 typval_T *argvars;
15505 typval_T *rettv;
15506{
15507 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015508 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015509 char_u *fname;
15510 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015511 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15512 int io_size = sizeof(buf);
15513 int readlen; /* size of last fread() */
15514 char_u *prev = NULL; /* previously read bytes, if any */
15515 long prevlen = 0; /* length of data in prev */
15516 long prevsize = 0; /* size of prev buffer */
15517 long maxline = MAXLNUM;
15518 long cnt = 0;
15519 char_u *p; /* position in buf */
15520 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015521
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015522 if (argvars[1].v_type != VAR_UNKNOWN)
15523 {
15524 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15525 binary = TRUE;
15526 if (argvars[2].v_type != VAR_UNKNOWN)
15527 maxline = get_tv_number(&argvars[2]);
15528 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015529
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015530 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015531 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015532
15533 /* Always open the file in binary mode, library functions have a mind of
15534 * their own about CR-LF conversion. */
15535 fname = get_tv_string(&argvars[0]);
15536 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15537 {
15538 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15539 return;
15540 }
15541
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015542 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015543 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015544 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015545
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015546 /* This for loop processes what was read, but is also entered at end
15547 * of file so that either:
15548 * - an incomplete line gets written
15549 * - a "binary" file gets an empty line at the end if it ends in a
15550 * newline. */
15551 for (p = buf, start = buf;
15552 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15553 ++p)
15554 {
15555 if (*p == '\n' || readlen <= 0)
15556 {
15557 listitem_T *li;
15558 char_u *s = NULL;
15559 long_u len = p - start;
15560
15561 /* Finished a line. Remove CRs before NL. */
15562 if (readlen > 0 && !binary)
15563 {
15564 while (len > 0 && start[len - 1] == '\r')
15565 --len;
15566 /* removal may cross back to the "prev" string */
15567 if (len == 0)
15568 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15569 --prevlen;
15570 }
15571 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015572 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015573 else
15574 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015575 /* Change "prev" buffer to be the right size. This way
15576 * the bytes are only copied once, and very long lines are
15577 * allocated only once. */
15578 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015579 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015580 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015581 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015582 prev = NULL; /* the list will own the string */
15583 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015584 }
15585 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015586 if (s == NULL)
15587 {
15588 do_outofmem_msg((long_u) prevlen + len + 1);
15589 failed = TRUE;
15590 break;
15591 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015592
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015593 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015594 {
15595 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015596 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015597 break;
15598 }
15599 li->li_tv.v_type = VAR_STRING;
15600 li->li_tv.v_lock = 0;
15601 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015602 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015603
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015604 start = p + 1; /* step over newline */
15605 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015606 break;
15607 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015608 else if (*p == NUL)
15609 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015610#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015611 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15612 * when finding the BF and check the previous two bytes. */
15613 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015614 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015615 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15616 * + 1, these may be in the "prev" string. */
15617 char_u back1 = p >= buf + 1 ? p[-1]
15618 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15619 char_u back2 = p >= buf + 2 ? p[-2]
15620 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15621 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015622
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015623 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015624 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015625 char_u *dest = p - 2;
15626
15627 /* Usually a BOM is at the beginning of a file, and so at
15628 * the beginning of a line; then we can just step over it.
15629 */
15630 if (start == dest)
15631 start = p + 1;
15632 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015633 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015634 /* have to shuffle buf to close gap */
15635 int adjust_prevlen = 0;
15636
15637 if (dest < buf)
15638 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015639 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015640 dest = buf;
15641 }
15642 if (readlen > p - buf + 1)
15643 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15644 readlen -= 3 - adjust_prevlen;
15645 prevlen -= adjust_prevlen;
15646 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015647 }
15648 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015649 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015650#endif
15651 } /* for */
15652
15653 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15654 break;
15655 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015656 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015657 /* There's part of a line in buf, store it in "prev". */
15658 if (p - start + prevlen >= prevsize)
15659 {
15660 /* need bigger "prev" buffer */
15661 char_u *newprev;
15662
15663 /* A common use case is ordinary text files and "prev" gets a
15664 * fragment of a line, so the first allocation is made
15665 * small, to avoid repeatedly 'allocing' large and
15666 * 'reallocing' small. */
15667 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015668 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015669 else
15670 {
15671 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015672 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015673 prevsize = grow50pc > growmin ? grow50pc : growmin;
15674 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015675 newprev = prev == NULL ? alloc(prevsize)
15676 : vim_realloc(prev, prevsize);
15677 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015678 {
15679 do_outofmem_msg((long_u)prevsize);
15680 failed = TRUE;
15681 break;
15682 }
15683 prev = newprev;
15684 }
15685 /* Add the line part to end of "prev". */
15686 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015687 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015688 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015689 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015690
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015691 /*
15692 * For a negative line count use only the lines at the end of the file,
15693 * free the rest.
15694 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015695 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015696 while (cnt > -maxline)
15697 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015698 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015699 --cnt;
15700 }
15701
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015702 if (failed)
15703 {
15704 list_free(rettv->vval.v_list, TRUE);
15705 /* readfile doc says an empty list is returned on error */
15706 rettv->vval.v_list = list_alloc();
15707 }
15708
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015709 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015710 fclose(fd);
15711}
15712
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015713#if defined(FEAT_RELTIME)
15714static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15715
15716/*
15717 * Convert a List to proftime_T.
15718 * Return FAIL when there is something wrong.
15719 */
15720 static int
15721list2proftime(arg, tm)
15722 typval_T *arg;
15723 proftime_T *tm;
15724{
15725 long n1, n2;
15726 int error = FALSE;
15727
15728 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15729 || arg->vval.v_list->lv_len != 2)
15730 return FAIL;
15731 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15732 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15733# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015734 tm->HighPart = n1;
15735 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015736# else
15737 tm->tv_sec = n1;
15738 tm->tv_usec = n2;
15739# endif
15740 return error ? FAIL : OK;
15741}
15742#endif /* FEAT_RELTIME */
15743
15744/*
15745 * "reltime()" function
15746 */
15747 static void
15748f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015749 typval_T *argvars UNUSED;
15750 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015751{
15752#ifdef FEAT_RELTIME
15753 proftime_T res;
15754 proftime_T start;
15755
15756 if (argvars[0].v_type == VAR_UNKNOWN)
15757 {
15758 /* No arguments: get current time. */
15759 profile_start(&res);
15760 }
15761 else if (argvars[1].v_type == VAR_UNKNOWN)
15762 {
15763 if (list2proftime(&argvars[0], &res) == FAIL)
15764 return;
15765 profile_end(&res);
15766 }
15767 else
15768 {
15769 /* Two arguments: compute the difference. */
15770 if (list2proftime(&argvars[0], &start) == FAIL
15771 || list2proftime(&argvars[1], &res) == FAIL)
15772 return;
15773 profile_sub(&res, &start);
15774 }
15775
15776 if (rettv_list_alloc(rettv) == OK)
15777 {
15778 long n1, n2;
15779
15780# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015781 n1 = res.HighPart;
15782 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015783# else
15784 n1 = res.tv_sec;
15785 n2 = res.tv_usec;
15786# endif
15787 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15788 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15789 }
15790#endif
15791}
15792
15793/*
15794 * "reltimestr()" function
15795 */
15796 static void
15797f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015798 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015799 typval_T *rettv;
15800{
15801#ifdef FEAT_RELTIME
15802 proftime_T tm;
15803#endif
15804
15805 rettv->v_type = VAR_STRING;
15806 rettv->vval.v_string = NULL;
15807#ifdef FEAT_RELTIME
15808 if (list2proftime(&argvars[0], &tm) == OK)
15809 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15810#endif
15811}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015812
Bram Moolenaar0d660222005-01-07 21:51:51 +000015813#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15814static void make_connection __ARGS((void));
15815static int check_connection __ARGS((void));
15816
15817 static void
15818make_connection()
15819{
15820 if (X_DISPLAY == NULL
15821# ifdef FEAT_GUI
15822 && !gui.in_use
15823# endif
15824 )
15825 {
15826 x_force_connect = TRUE;
15827 setup_term_clip();
15828 x_force_connect = FALSE;
15829 }
15830}
15831
15832 static int
15833check_connection()
15834{
15835 make_connection();
15836 if (X_DISPLAY == NULL)
15837 {
15838 EMSG(_("E240: No connection to Vim server"));
15839 return FAIL;
15840 }
15841 return OK;
15842}
15843#endif
15844
15845#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015846static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015847
15848 static void
15849remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015850 typval_T *argvars;
15851 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015852 int expr;
15853{
15854 char_u *server_name;
15855 char_u *keys;
15856 char_u *r = NULL;
15857 char_u buf[NUMBUFLEN];
15858# ifdef WIN32
15859 HWND w;
15860# else
15861 Window w;
15862# endif
15863
15864 if (check_restricted() || check_secure())
15865 return;
15866
15867# ifdef FEAT_X11
15868 if (check_connection() == FAIL)
15869 return;
15870# endif
15871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015872 server_name = get_tv_string_chk(&argvars[0]);
15873 if (server_name == NULL)
15874 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015875 keys = get_tv_string_buf(&argvars[1], buf);
15876# ifdef WIN32
15877 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15878# else
15879 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15880 < 0)
15881# endif
15882 {
15883 if (r != NULL)
15884 EMSG(r); /* sending worked but evaluation failed */
15885 else
15886 EMSG2(_("E241: Unable to send to %s"), server_name);
15887 return;
15888 }
15889
15890 rettv->vval.v_string = r;
15891
15892 if (argvars[2].v_type != VAR_UNKNOWN)
15893 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015894 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015895 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015896 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015897
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015898 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015899 v.di_tv.v_type = VAR_STRING;
15900 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015901 idvar = get_tv_string_chk(&argvars[2]);
15902 if (idvar != NULL)
15903 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015904 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015905 }
15906}
15907#endif
15908
15909/*
15910 * "remote_expr()" function
15911 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015912 static void
15913f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015914 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015915 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015916{
15917 rettv->v_type = VAR_STRING;
15918 rettv->vval.v_string = NULL;
15919#ifdef FEAT_CLIENTSERVER
15920 remote_common(argvars, rettv, TRUE);
15921#endif
15922}
15923
15924/*
15925 * "remote_foreground()" function
15926 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015927 static void
15928f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015929 typval_T *argvars UNUSED;
15930 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015931{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932#ifdef FEAT_CLIENTSERVER
15933# ifdef WIN32
15934 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015935 {
15936 char_u *server_name = get_tv_string_chk(&argvars[0]);
15937
15938 if (server_name != NULL)
15939 serverForeground(server_name);
15940 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941# else
15942 /* Send a foreground() expression to the server. */
15943 argvars[1].v_type = VAR_STRING;
15944 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15945 argvars[2].v_type = VAR_UNKNOWN;
15946 remote_common(argvars, rettv, TRUE);
15947 vim_free(argvars[1].vval.v_string);
15948# endif
15949#endif
15950}
15951
Bram Moolenaar0d660222005-01-07 21:51:51 +000015952 static void
15953f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015954 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015956{
15957#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015958 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015959 char_u *s = NULL;
15960# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015961 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015962# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015963 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015964
15965 if (check_restricted() || check_secure())
15966 {
15967 rettv->vval.v_number = -1;
15968 return;
15969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015970 serverid = get_tv_string_chk(&argvars[0]);
15971 if (serverid == NULL)
15972 {
15973 rettv->vval.v_number = -1;
15974 return; /* type error; errmsg already given */
15975 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015976# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015977 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015978 if (n == 0)
15979 rettv->vval.v_number = -1;
15980 else
15981 {
15982 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15983 rettv->vval.v_number = (s != NULL);
15984 }
15985# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015986 if (check_connection() == FAIL)
15987 return;
15988
15989 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015990 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015991# endif
15992
15993 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015995 char_u *retvar;
15996
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 v.di_tv.v_type = VAR_STRING;
15998 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015999 retvar = get_tv_string_chk(&argvars[1]);
16000 if (retvar != NULL)
16001 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016003 }
16004#else
16005 rettv->vval.v_number = -1;
16006#endif
16007}
16008
Bram Moolenaar0d660222005-01-07 21:51:51 +000016009 static void
16010f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016011 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016012 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016013{
16014 char_u *r = NULL;
16015
16016#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016017 char_u *serverid = get_tv_string_chk(&argvars[0]);
16018
16019 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016020 {
16021# ifdef WIN32
16022 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016023 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016024
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016025 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016026 if (n != 0)
16027 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16028 if (r == NULL)
16029# else
16030 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016032# endif
16033 EMSG(_("E277: Unable to read a server reply"));
16034 }
16035#endif
16036 rettv->v_type = VAR_STRING;
16037 rettv->vval.v_string = r;
16038}
16039
16040/*
16041 * "remote_send()" function
16042 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016043 static void
16044f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016045 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047{
16048 rettv->v_type = VAR_STRING;
16049 rettv->vval.v_string = NULL;
16050#ifdef FEAT_CLIENTSERVER
16051 remote_common(argvars, rettv, FALSE);
16052#endif
16053}
16054
16055/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016056 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016057 */
16058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016059f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016060 typval_T *argvars;
16061 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016062{
Bram Moolenaar33570922005-01-25 22:26:29 +000016063 list_T *l;
16064 listitem_T *item, *item2;
16065 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016066 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016067 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016068 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016069 dict_T *d;
16070 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016071 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016072
Bram Moolenaar8c711452005-01-14 21:53:12 +000016073 if (argvars[0].v_type == VAR_DICT)
16074 {
16075 if (argvars[2].v_type != VAR_UNKNOWN)
16076 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016077 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016078 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 key = get_tv_string_chk(&argvars[1]);
16081 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016083 di = dict_find(d, key, -1);
16084 if (di == NULL)
16085 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016086 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16087 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016088 {
16089 *rettv = di->di_tv;
16090 init_tv(&di->di_tv);
16091 dictitem_remove(d, di);
16092 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016094 }
16095 }
16096 else if (argvars[0].v_type != VAR_LIST)
16097 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016098 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016099 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016101 int error = FALSE;
16102
16103 idx = get_tv_number_chk(&argvars[1], &error);
16104 if (error)
16105 ; /* type error: do nothing, errmsg already given */
16106 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016107 EMSGN(_(e_listidx), idx);
16108 else
16109 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016110 if (argvars[2].v_type == VAR_UNKNOWN)
16111 {
16112 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016113 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016114 *rettv = item->li_tv;
16115 vim_free(item);
16116 }
16117 else
16118 {
16119 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016120 end = get_tv_number_chk(&argvars[2], &error);
16121 if (error)
16122 ; /* type error: do nothing */
16123 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016124 EMSGN(_(e_listidx), end);
16125 else
16126 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016127 int cnt = 0;
16128
16129 for (li = item; li != NULL; li = li->li_next)
16130 {
16131 ++cnt;
16132 if (li == item2)
16133 break;
16134 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016135 if (li == NULL) /* didn't find "item2" after "item" */
16136 EMSG(_(e_invrange));
16137 else
16138 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016139 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016140 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016141 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016142 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016143 l->lv_first = item;
16144 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016145 item->li_prev = NULL;
16146 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016147 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016148 }
16149 }
16150 }
16151 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016152 }
16153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154}
16155
16156/*
16157 * "rename({from}, {to})" function
16158 */
16159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016160f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016161 typval_T *argvars;
16162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163{
16164 char_u buf[NUMBUFLEN];
16165
16166 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016169 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16170 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171}
16172
16173/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016174 * "repeat()" function
16175 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016176 static void
16177f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 typval_T *argvars;
16179 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016180{
16181 char_u *p;
16182 int n;
16183 int slen;
16184 int len;
16185 char_u *r;
16186 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016187
16188 n = get_tv_number(&argvars[1]);
16189 if (argvars[0].v_type == VAR_LIST)
16190 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016191 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016192 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016193 if (list_extend(rettv->vval.v_list,
16194 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016195 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016196 }
16197 else
16198 {
16199 p = get_tv_string(&argvars[0]);
16200 rettv->v_type = VAR_STRING;
16201 rettv->vval.v_string = NULL;
16202
16203 slen = (int)STRLEN(p);
16204 len = slen * n;
16205 if (len <= 0)
16206 return;
16207
16208 r = alloc(len + 1);
16209 if (r != NULL)
16210 {
16211 for (i = 0; i < n; i++)
16212 mch_memmove(r + i * slen, p, (size_t)slen);
16213 r[len] = NUL;
16214 }
16215
16216 rettv->vval.v_string = r;
16217 }
16218}
16219
16220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 * "resolve()" function
16222 */
16223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016224f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016225 typval_T *argvars;
16226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227{
16228 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016229#ifdef HAVE_READLINK
16230 char_u *buf = NULL;
16231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016233 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234#ifdef FEAT_SHORTCUT
16235 {
16236 char_u *v = NULL;
16237
16238 v = mch_resolve_shortcut(p);
16239 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016240 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016242 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 }
16244#else
16245# ifdef HAVE_READLINK
16246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 char_u *cpy;
16248 int len;
16249 char_u *remain = NULL;
16250 char_u *q;
16251 int is_relative_to_current = FALSE;
16252 int has_trailing_pathsep = FALSE;
16253 int limit = 100;
16254
16255 p = vim_strsave(p);
16256
16257 if (p[0] == '.' && (vim_ispathsep(p[1])
16258 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16259 is_relative_to_current = TRUE;
16260
16261 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016262 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016265 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267
16268 q = getnextcomp(p);
16269 if (*q != NUL)
16270 {
16271 /* Separate the first path component in "p", and keep the
16272 * remainder (beginning with the path separator). */
16273 remain = vim_strsave(q - 1);
16274 q[-1] = NUL;
16275 }
16276
Bram Moolenaard9462e32011-04-11 21:35:11 +020016277 buf = alloc(MAXPATHL + 1);
16278 if (buf == NULL)
16279 goto fail;
16280
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 for (;;)
16282 {
16283 for (;;)
16284 {
16285 len = readlink((char *)p, (char *)buf, MAXPATHL);
16286 if (len <= 0)
16287 break;
16288 buf[len] = NUL;
16289
16290 if (limit-- == 0)
16291 {
16292 vim_free(p);
16293 vim_free(remain);
16294 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016295 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 goto fail;
16297 }
16298
16299 /* Ensure that the result will have a trailing path separator
16300 * if the argument has one. */
16301 if (remain == NULL && has_trailing_pathsep)
16302 add_pathsep(buf);
16303
16304 /* Separate the first path component in the link value and
16305 * concatenate the remainders. */
16306 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16307 if (*q != NUL)
16308 {
16309 if (remain == NULL)
16310 remain = vim_strsave(q - 1);
16311 else
16312 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016313 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 if (cpy != NULL)
16315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 vim_free(remain);
16317 remain = cpy;
16318 }
16319 }
16320 q[-1] = NUL;
16321 }
16322
16323 q = gettail(p);
16324 if (q > p && *q == NUL)
16325 {
16326 /* Ignore trailing path separator. */
16327 q[-1] = NUL;
16328 q = gettail(p);
16329 }
16330 if (q > p && !mch_isFullName(buf))
16331 {
16332 /* symlink is relative to directory of argument */
16333 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16334 if (cpy != NULL)
16335 {
16336 STRCPY(cpy, p);
16337 STRCPY(gettail(cpy), buf);
16338 vim_free(p);
16339 p = cpy;
16340 }
16341 }
16342 else
16343 {
16344 vim_free(p);
16345 p = vim_strsave(buf);
16346 }
16347 }
16348
16349 if (remain == NULL)
16350 break;
16351
16352 /* Append the first path component of "remain" to "p". */
16353 q = getnextcomp(remain + 1);
16354 len = q - remain - (*q != NUL);
16355 cpy = vim_strnsave(p, STRLEN(p) + len);
16356 if (cpy != NULL)
16357 {
16358 STRNCAT(cpy, remain, len);
16359 vim_free(p);
16360 p = cpy;
16361 }
16362 /* Shorten "remain". */
16363 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016364 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365 else
16366 {
16367 vim_free(remain);
16368 remain = NULL;
16369 }
16370 }
16371
16372 /* If the result is a relative path name, make it explicitly relative to
16373 * the current directory if and only if the argument had this form. */
16374 if (!vim_ispathsep(*p))
16375 {
16376 if (is_relative_to_current
16377 && *p != NUL
16378 && !(p[0] == '.'
16379 && (p[1] == NUL
16380 || vim_ispathsep(p[1])
16381 || (p[1] == '.'
16382 && (p[2] == NUL
16383 || vim_ispathsep(p[2]))))))
16384 {
16385 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016386 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 if (cpy != NULL)
16388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 vim_free(p);
16390 p = cpy;
16391 }
16392 }
16393 else if (!is_relative_to_current)
16394 {
16395 /* Strip leading "./". */
16396 q = p;
16397 while (q[0] == '.' && vim_ispathsep(q[1]))
16398 q += 2;
16399 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016400 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 }
16402 }
16403
16404 /* Ensure that the result will have no trailing path separator
16405 * if the argument had none. But keep "/" or "//". */
16406 if (!has_trailing_pathsep)
16407 {
16408 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016409 if (after_pathsep(p, q))
16410 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 }
16412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016413 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 }
16415# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016416 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417# endif
16418#endif
16419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016420 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421
16422#ifdef HAVE_READLINK
16423fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016424 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016426 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427}
16428
16429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 */
16432 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016434 typval_T *argvars;
16435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436{
Bram Moolenaar33570922005-01-25 22:26:29 +000016437 list_T *l;
16438 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440 if (argvars[0].v_type != VAR_LIST)
16441 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016442 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016443 && !tv_check_lock(l->lv_lock,
16444 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445 {
16446 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016447 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016448 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449 while (li != NULL)
16450 {
16451 ni = li->li_prev;
16452 list_append(l, li);
16453 li = ni;
16454 }
16455 rettv->vval.v_list = l;
16456 rettv->v_type = VAR_LIST;
16457 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016458 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460}
16461
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016462#define SP_NOMOVE 0x01 /* don't move cursor */
16463#define SP_REPEAT 0x02 /* repeat to find outer pair */
16464#define SP_RETCOUNT 0x04 /* return matchcount */
16465#define SP_SETPCMARK 0x08 /* set previous context mark */
16466#define SP_START 0x10 /* accept match at start position */
16467#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16468#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016469
Bram Moolenaar33570922005-01-25 22:26:29 +000016470static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016471
16472/*
16473 * Get flags for a search function.
16474 * Possibly sets "p_ws".
16475 * Returns BACKWARD, FORWARD or zero (for an error).
16476 */
16477 static int
16478get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016479 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 int *flagsp;
16481{
16482 int dir = FORWARD;
16483 char_u *flags;
16484 char_u nbuf[NUMBUFLEN];
16485 int mask;
16486
16487 if (varp->v_type != VAR_UNKNOWN)
16488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016489 flags = get_tv_string_buf_chk(varp, nbuf);
16490 if (flags == NULL)
16491 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492 while (*flags != NUL)
16493 {
16494 switch (*flags)
16495 {
16496 case 'b': dir = BACKWARD; break;
16497 case 'w': p_ws = TRUE; break;
16498 case 'W': p_ws = FALSE; break;
16499 default: mask = 0;
16500 if (flagsp != NULL)
16501 switch (*flags)
16502 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016503 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016504 case 'e': mask = SP_END; break;
16505 case 'm': mask = SP_RETCOUNT; break;
16506 case 'n': mask = SP_NOMOVE; break;
16507 case 'p': mask = SP_SUBPAT; break;
16508 case 'r': mask = SP_REPEAT; break;
16509 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510 }
16511 if (mask == 0)
16512 {
16513 EMSG2(_(e_invarg2), flags);
16514 dir = 0;
16515 }
16516 else
16517 *flagsp |= mask;
16518 }
16519 if (dir == 0)
16520 break;
16521 ++flags;
16522 }
16523 }
16524 return dir;
16525}
16526
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016528 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016530 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016531search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016532 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016533 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016534 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016536 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 char_u *pat;
16538 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016539 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 int save_p_ws = p_ws;
16541 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016542 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016543 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016544 proftime_T tm;
16545#ifdef FEAT_RELTIME
16546 long time_limit = 0;
16547#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016548 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016549 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016552 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016553 if (dir == 0)
16554 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016555 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016556 if (flags & SP_START)
16557 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016558 if (flags & SP_END)
16559 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016560
Bram Moolenaar76929292008-01-06 19:07:36 +000016561 /* Optional arguments: line number to stop searching and timeout. */
16562 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016563 {
16564 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16565 if (lnum_stop < 0)
16566 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016567#ifdef FEAT_RELTIME
16568 if (argvars[3].v_type != VAR_UNKNOWN)
16569 {
16570 time_limit = get_tv_number_chk(&argvars[3], NULL);
16571 if (time_limit < 0)
16572 goto theend;
16573 }
16574#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016575 }
16576
Bram Moolenaar76929292008-01-06 19:07:36 +000016577#ifdef FEAT_RELTIME
16578 /* Set the time limit, if there is one. */
16579 profile_setlimit(time_limit, &tm);
16580#endif
16581
Bram Moolenaar231334e2005-07-25 20:46:57 +000016582 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016583 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016584 * Check to make sure only those flags are set.
16585 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16586 * flags cannot be set. Check for that condition also.
16587 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016588 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016589 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016591 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016592 goto theend;
16593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016595 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016596 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016597 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016598 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016600 if (flags & SP_SUBPAT)
16601 retval = subpatnum;
16602 else
16603 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016604 if (flags & SP_SETPCMARK)
16605 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016607 if (match_pos != NULL)
16608 {
16609 /* Store the match cursor position */
16610 match_pos->lnum = pos.lnum;
16611 match_pos->col = pos.col + 1;
16612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613 /* "/$" will put the cursor after the end of the line, may need to
16614 * correct that here */
16615 check_cursor();
16616 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016617
16618 /* If 'n' flag is used: restore cursor position. */
16619 if (flags & SP_NOMOVE)
16620 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016621 else
16622 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016623theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016625
16626 return retval;
16627}
16628
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016629#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016630
16631/*
16632 * round() is not in C90, use ceil() or floor() instead.
16633 */
16634 float_T
16635vim_round(f)
16636 float_T f;
16637{
16638 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16639}
16640
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016641/*
16642 * "round({float})" function
16643 */
16644 static void
16645f_round(argvars, rettv)
16646 typval_T *argvars;
16647 typval_T *rettv;
16648{
16649 float_T f;
16650
16651 rettv->v_type = VAR_FLOAT;
16652 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016653 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016654 else
16655 rettv->vval.v_float = 0.0;
16656}
16657#endif
16658
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016659/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016660 * "screenattr()" function
16661 */
16662 static void
16663f_screenattr(argvars, rettv)
16664 typval_T *argvars UNUSED;
16665 typval_T *rettv;
16666{
16667 int row;
16668 int col;
16669 int c;
16670
16671 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16672 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16673 if (row < 0 || row >= screen_Rows
16674 || col < 0 || col >= screen_Columns)
16675 c = -1;
16676 else
16677 c = ScreenAttrs[LineOffset[row] + col];
16678 rettv->vval.v_number = c;
16679}
16680
16681/*
16682 * "screenchar()" function
16683 */
16684 static void
16685f_screenchar(argvars, rettv)
16686 typval_T *argvars UNUSED;
16687 typval_T *rettv;
16688{
16689 int row;
16690 int col;
16691 int off;
16692 int c;
16693
16694 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16695 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16696 if (row < 0 || row >= screen_Rows
16697 || col < 0 || col >= screen_Columns)
16698 c = -1;
16699 else
16700 {
16701 off = LineOffset[row] + col;
16702#ifdef FEAT_MBYTE
16703 if (enc_utf8 && ScreenLinesUC[off] != 0)
16704 c = ScreenLinesUC[off];
16705 else
16706#endif
16707 c = ScreenLines[off];
16708 }
16709 rettv->vval.v_number = c;
16710}
16711
16712/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016713 * "screencol()" function
16714 *
16715 * First column is 1 to be consistent with virtcol().
16716 */
16717 static void
16718f_screencol(argvars, rettv)
16719 typval_T *argvars UNUSED;
16720 typval_T *rettv;
16721{
16722 rettv->vval.v_number = screen_screencol() + 1;
16723}
16724
16725/*
16726 * "screenrow()" function
16727 */
16728 static void
16729f_screenrow(argvars, rettv)
16730 typval_T *argvars UNUSED;
16731 typval_T *rettv;
16732{
16733 rettv->vval.v_number = screen_screenrow() + 1;
16734}
16735
16736/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016737 * "search()" function
16738 */
16739 static void
16740f_search(argvars, rettv)
16741 typval_T *argvars;
16742 typval_T *rettv;
16743{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016744 int flags = 0;
16745
16746 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747}
16748
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016750 * "searchdecl()" function
16751 */
16752 static void
16753f_searchdecl(argvars, rettv)
16754 typval_T *argvars;
16755 typval_T *rettv;
16756{
16757 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016758 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016759 int error = FALSE;
16760 char_u *name;
16761
16762 rettv->vval.v_number = 1; /* default: FAIL */
16763
16764 name = get_tv_string_chk(&argvars[0]);
16765 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016766 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016767 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016768 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16769 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16770 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016771 if (!error && name != NULL)
16772 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016773 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016774}
16775
16776/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016777 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016779 static int
16780searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016781 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016782 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783{
16784 char_u *spat, *mpat, *epat;
16785 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 int dir;
16788 int flags = 0;
16789 char_u nbuf1[NUMBUFLEN];
16790 char_u nbuf2[NUMBUFLEN];
16791 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016792 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016793 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016794 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016797 spat = get_tv_string_chk(&argvars[0]);
16798 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16799 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16800 if (spat == NULL || mpat == NULL || epat == NULL)
16801 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 /* Handle the optional fourth argument: flags */
16804 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016805 if (dir == 0)
16806 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016807
16808 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016809 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16810 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016811 if ((flags & (SP_END | SP_SUBPAT)) != 0
16812 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016813 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016814 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016815 goto theend;
16816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016818 /* Using 'r' implies 'W', otherwise it doesn't work. */
16819 if (flags & SP_REPEAT)
16820 p_ws = FALSE;
16821
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016822 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016823 if (argvars[3].v_type == VAR_UNKNOWN
16824 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 skip = (char_u *)"";
16826 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016828 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016829 if (argvars[5].v_type != VAR_UNKNOWN)
16830 {
16831 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16832 if (lnum_stop < 0)
16833 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016834#ifdef FEAT_RELTIME
16835 if (argvars[6].v_type != VAR_UNKNOWN)
16836 {
16837 time_limit = get_tv_number_chk(&argvars[6], NULL);
16838 if (time_limit < 0)
16839 goto theend;
16840 }
16841#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016842 }
16843 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016844 if (skip == NULL)
16845 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016847 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016848 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016849
16850theend:
16851 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016852
16853 return retval;
16854}
16855
16856/*
16857 * "searchpair()" function
16858 */
16859 static void
16860f_searchpair(argvars, rettv)
16861 typval_T *argvars;
16862 typval_T *rettv;
16863{
16864 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16865}
16866
16867/*
16868 * "searchpairpos()" function
16869 */
16870 static void
16871f_searchpairpos(argvars, rettv)
16872 typval_T *argvars;
16873 typval_T *rettv;
16874{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016875 pos_T match_pos;
16876 int lnum = 0;
16877 int col = 0;
16878
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016879 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016880 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016881
16882 if (searchpair_cmn(argvars, &match_pos) > 0)
16883 {
16884 lnum = match_pos.lnum;
16885 col = match_pos.col;
16886 }
16887
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016888 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16889 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016890}
16891
16892/*
16893 * Search for a start/middle/end thing.
16894 * Used by searchpair(), see its documentation for the details.
16895 * Returns 0 or -1 for no match,
16896 */
16897 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016898do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16899 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016900 char_u *spat; /* start pattern */
16901 char_u *mpat; /* middle pattern */
16902 char_u *epat; /* end pattern */
16903 int dir; /* BACKWARD or FORWARD */
16904 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016905 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016906 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016907 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016908 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016909{
16910 char_u *save_cpo;
16911 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16912 long retval = 0;
16913 pos_T pos;
16914 pos_T firstpos;
16915 pos_T foundpos;
16916 pos_T save_cursor;
16917 pos_T save_pos;
16918 int n;
16919 int r;
16920 int nest = 1;
16921 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016922 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016923 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016924
16925 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16926 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016927 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016928
Bram Moolenaar76929292008-01-06 19:07:36 +000016929#ifdef FEAT_RELTIME
16930 /* Set the time limit, if there is one. */
16931 profile_setlimit(time_limit, &tm);
16932#endif
16933
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016934 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16935 * start/middle/end (pat3, for the top pair). */
16936 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16937 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16938 if (pat2 == NULL || pat3 == NULL)
16939 goto theend;
16940 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16941 if (*mpat == NUL)
16942 STRCPY(pat3, pat2);
16943 else
16944 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16945 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016946 if (flags & SP_START)
16947 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016948
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 save_cursor = curwin->w_cursor;
16950 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016951 clearpos(&firstpos);
16952 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 pat = pat3;
16954 for (;;)
16955 {
16956 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016957 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16959 /* didn't find it or found the first match again: FAIL */
16960 break;
16961
16962 if (firstpos.lnum == 0)
16963 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016964 if (equalpos(pos, foundpos))
16965 {
16966 /* Found the same position again. Can happen with a pattern that
16967 * has "\zs" at the end and searching backwards. Advance one
16968 * character and try again. */
16969 if (dir == BACKWARD)
16970 decl(&pos);
16971 else
16972 incl(&pos);
16973 }
16974 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016976 /* clear the start flag to avoid getting stuck here */
16977 options &= ~SEARCH_START;
16978
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 /* If the skip pattern matches, ignore this match. */
16980 if (*skip != NUL)
16981 {
16982 save_pos = curwin->w_cursor;
16983 curwin->w_cursor = pos;
16984 r = eval_to_bool(skip, &err, NULL, FALSE);
16985 curwin->w_cursor = save_pos;
16986 if (err)
16987 {
16988 /* Evaluating {skip} caused an error, break here. */
16989 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016990 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 break;
16992 }
16993 if (r)
16994 continue;
16995 }
16996
16997 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16998 {
16999 /* Found end when searching backwards or start when searching
17000 * forward: nested pair. */
17001 ++nest;
17002 pat = pat2; /* nested, don't search for middle */
17003 }
17004 else
17005 {
17006 /* Found end when searching forward or start when searching
17007 * backward: end of (nested) pair; or found middle in outer pair. */
17008 if (--nest == 1)
17009 pat = pat3; /* outer level, search for middle */
17010 }
17011
17012 if (nest == 0)
17013 {
17014 /* Found the match: return matchcount or line number. */
17015 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017016 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017018 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017019 if (flags & SP_SETPCMARK)
17020 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 curwin->w_cursor = pos;
17022 if (!(flags & SP_REPEAT))
17023 break;
17024 nest = 1; /* search for next unmatched */
17025 }
17026 }
17027
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017028 if (match_pos != NULL)
17029 {
17030 /* Store the match cursor position */
17031 match_pos->lnum = curwin->w_cursor.lnum;
17032 match_pos->col = curwin->w_cursor.col + 1;
17033 }
17034
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017036 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 curwin->w_cursor = save_cursor;
17038
17039theend:
17040 vim_free(pat2);
17041 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017042 if (p_cpo == empty_option)
17043 p_cpo = save_cpo;
17044 else
17045 /* Darn, evaluating the {skip} expression changed the value. */
17046 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017047
17048 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049}
17050
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017051/*
17052 * "searchpos()" function
17053 */
17054 static void
17055f_searchpos(argvars, rettv)
17056 typval_T *argvars;
17057 typval_T *rettv;
17058{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017059 pos_T match_pos;
17060 int lnum = 0;
17061 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017062 int n;
17063 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017064
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017065 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017066 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017067
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017068 n = search_cmn(argvars, &match_pos, &flags);
17069 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017070 {
17071 lnum = match_pos.lnum;
17072 col = match_pos.col;
17073 }
17074
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017075 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17076 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017077 if (flags & SP_SUBPAT)
17078 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017079}
17080
17081
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082 static void
17083f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017084 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017087#ifdef FEAT_CLIENTSERVER
17088 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017089 char_u *server = get_tv_string_chk(&argvars[0]);
17090 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017093 if (server == NULL || reply == NULL)
17094 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 if (check_restricted() || check_secure())
17096 return;
17097# ifdef FEAT_X11
17098 if (check_connection() == FAIL)
17099 return;
17100# endif
17101
17102 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104 EMSG(_("E258: Unable to send to client"));
17105 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 rettv->vval.v_number = 0;
17108#else
17109 rettv->vval.v_number = -1;
17110#endif
17111}
17112
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113 static void
17114f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017115 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017116 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017117{
17118 char_u *r = NULL;
17119
17120#ifdef FEAT_CLIENTSERVER
17121# ifdef WIN32
17122 r = serverGetVimNames();
17123# else
17124 make_connection();
17125 if (X_DISPLAY != NULL)
17126 r = serverGetVimNames(X_DISPLAY);
17127# endif
17128#endif
17129 rettv->v_type = VAR_STRING;
17130 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131}
17132
17133/*
17134 * "setbufvar()" function
17135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017137f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017138 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017139 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140{
17141 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 char_u nbuf[NUMBUFLEN];
17146
17147 if (check_restricted() || check_secure())
17148 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017149 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17150 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017151 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 varp = &argvars[2];
17153
17154 if (buf != NULL && varname != NULL && varp != NULL)
17155 {
17156 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158
17159 if (*varname == '&')
17160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017161 long numval;
17162 char_u *strval;
17163 int error = FALSE;
17164
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017166 numval = get_tv_number_chk(varp, &error);
17167 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017168 if (!error && strval != NULL)
17169 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 }
17171 else
17172 {
17173 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17174 if (bufvarname != NULL)
17175 {
17176 STRCPY(bufvarname, "b:");
17177 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017178 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 vim_free(bufvarname);
17180 }
17181 }
17182
17183 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186}
17187
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017188 static void
17189f_setcharsearch(argvars, rettv)
17190 typval_T *argvars;
17191 typval_T *rettv UNUSED;
17192{
17193 dict_T *d;
17194 dictitem_T *di;
17195 char_u *csearch;
17196
17197 if (argvars[0].v_type != VAR_DICT)
17198 {
17199 EMSG(_(e_dictreq));
17200 return;
17201 }
17202
17203 if ((d = argvars[0].vval.v_dict) != NULL)
17204 {
17205 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17206 if (csearch != NULL)
17207 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017208#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017209 if (enc_utf8)
17210 {
17211 int pcc[MAX_MCO];
17212 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017213
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017214 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17215 }
17216 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017217#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017218 set_last_csearch(PTR2CHAR(csearch),
17219 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017220 }
17221
17222 di = dict_find(d, (char_u *)"forward", -1);
17223 if (di != NULL)
17224 set_csearch_direction(get_tv_number(&di->di_tv)
17225 ? FORWARD : BACKWARD);
17226
17227 di = dict_find(d, (char_u *)"until", -1);
17228 if (di != NULL)
17229 set_csearch_until(!!get_tv_number(&di->di_tv));
17230 }
17231}
17232
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233/*
17234 * "setcmdpos()" function
17235 */
17236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017237f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017238 typval_T *argvars;
17239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017241 int pos = (int)get_tv_number(&argvars[0]) - 1;
17242
17243 if (pos >= 0)
17244 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245}
17246
17247/*
17248 * "setline()" function
17249 */
17250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017251f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017252 typval_T *argvars;
17253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254{
17255 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017256 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017257 list_T *l = NULL;
17258 listitem_T *li = NULL;
17259 long added = 0;
17260 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017262 lnum = get_tv_lnum(&argvars[0]);
17263 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017265 l = argvars[1].vval.v_list;
17266 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017268 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017269 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017270
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017271 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017272 for (;;)
17273 {
17274 if (l != NULL)
17275 {
17276 /* list argument, get next string */
17277 if (li == NULL)
17278 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017280 li = li->li_next;
17281 }
17282
17283 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017284 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017285 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017286
17287 /* When coming here from Insert mode, sync undo, so that this can be
17288 * undone separately from what was previously inserted. */
17289 if (u_sync_once == 2)
17290 {
17291 u_sync_once = 1; /* notify that u_sync() was called */
17292 u_sync(TRUE);
17293 }
17294
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017295 if (lnum <= curbuf->b_ml.ml_line_count)
17296 {
17297 /* existing line, replace it */
17298 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17299 {
17300 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017301 if (lnum == curwin->w_cursor.lnum)
17302 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017303 rettv->vval.v_number = 0; /* OK */
17304 }
17305 }
17306 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17307 {
17308 /* lnum is one past the last line, append the line */
17309 ++added;
17310 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17311 rettv->vval.v_number = 0; /* OK */
17312 }
17313
17314 if (l == NULL) /* only one string argument */
17315 break;
17316 ++lnum;
17317 }
17318
17319 if (added > 0)
17320 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321}
17322
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017323static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17324
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017326 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017327 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017328 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017329set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017330 win_T *wp UNUSED;
17331 typval_T *list_arg UNUSED;
17332 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017333 typval_T *rettv;
17334{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017335#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017336 char_u *act;
17337 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017338#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017339
Bram Moolenaar2641f772005-03-25 21:58:17 +000017340 rettv->vval.v_number = -1;
17341
17342#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017343 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017344 EMSG(_(e_listreq));
17345 else
17346 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017347 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017348
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017349 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017350 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017351 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017352 if (act == NULL)
17353 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017354 if (*act == 'a' || *act == 'r')
17355 action = *act;
17356 }
17357
Bram Moolenaar81484f42012-12-05 15:16:47 +010017358 if (l != NULL && set_errorlist(wp, l, action,
17359 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017360 rettv->vval.v_number = 0;
17361 }
17362#endif
17363}
17364
17365/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017366 * "setloclist()" function
17367 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017368 static void
17369f_setloclist(argvars, rettv)
17370 typval_T *argvars;
17371 typval_T *rettv;
17372{
17373 win_T *win;
17374
17375 rettv->vval.v_number = -1;
17376
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017377 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017378 if (win != NULL)
17379 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17380}
17381
17382/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017383 * "setmatches()" function
17384 */
17385 static void
17386f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017387 typval_T *argvars UNUSED;
17388 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017389{
17390#ifdef FEAT_SEARCH_EXTRA
17391 list_T *l;
17392 listitem_T *li;
17393 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017394 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017395
17396 rettv->vval.v_number = -1;
17397 if (argvars[0].v_type != VAR_LIST)
17398 {
17399 EMSG(_(e_listreq));
17400 return;
17401 }
17402 if ((l = argvars[0].vval.v_list) != NULL)
17403 {
17404
17405 /* To some extent make sure that we are dealing with a list from
17406 * "getmatches()". */
17407 li = l->lv_first;
17408 while (li != NULL)
17409 {
17410 if (li->li_tv.v_type != VAR_DICT
17411 || (d = li->li_tv.vval.v_dict) == NULL)
17412 {
17413 EMSG(_(e_invarg));
17414 return;
17415 }
17416 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017417 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17418 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017419 && dict_find(d, (char_u *)"priority", -1) != NULL
17420 && dict_find(d, (char_u *)"id", -1) != NULL))
17421 {
17422 EMSG(_(e_invarg));
17423 return;
17424 }
17425 li = li->li_next;
17426 }
17427
17428 clear_matches(curwin);
17429 li = l->lv_first;
17430 while (li != NULL)
17431 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017432 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017433 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017434 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017435 char_u *group;
17436 int priority;
17437 int id;
17438 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017439
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017440 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017441 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17442 {
17443 if (s == NULL)
17444 {
17445 s = list_alloc();
17446 if (s == NULL)
17447 return;
17448 }
17449
17450 /* match from matchaddpos() */
17451 for (i = 1; i < 9; i++)
17452 {
17453 sprintf((char *)buf, (char *)"pos%d", i);
17454 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17455 {
17456 if (di->di_tv.v_type != VAR_LIST)
17457 return;
17458
17459 list_append_tv(s, &di->di_tv);
17460 s->lv_refcount++;
17461 }
17462 else
17463 break;
17464 }
17465 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017466
17467 group = get_dict_string(d, (char_u *)"group", FALSE);
17468 priority = (int)get_dict_number(d, (char_u *)"priority");
17469 id = (int)get_dict_number(d, (char_u *)"id");
17470 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17471 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17472 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017473 if (i == 0)
17474 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017475 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017476 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017477 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017478 }
17479 else
17480 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017481 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017482 list_unref(s);
17483 s = NULL;
17484 }
17485
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017486 li = li->li_next;
17487 }
17488 rettv->vval.v_number = 0;
17489 }
17490#endif
17491}
17492
17493/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017494 * "setpos()" function
17495 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017496 static void
17497f_setpos(argvars, rettv)
17498 typval_T *argvars;
17499 typval_T *rettv;
17500{
17501 pos_T pos;
17502 int fnum;
17503 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017504 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017505
Bram Moolenaar08250432008-02-13 11:42:46 +000017506 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017507 name = get_tv_string_chk(argvars);
17508 if (name != NULL)
17509 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017510 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017511 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017512 if (--pos.col < 0)
17513 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017514 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017515 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017516 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017517 if (fnum == curbuf->b_fnum)
17518 {
17519 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017520 if (curswant >= 0)
17521 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017522 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017523 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017524 }
17525 else
17526 EMSG(_(e_invarg));
17527 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017528 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17529 {
17530 /* set mark */
17531 if (setmark_pos(name[1], &pos, fnum) == OK)
17532 rettv->vval.v_number = 0;
17533 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017534 else
17535 EMSG(_(e_invarg));
17536 }
17537 }
17538}
17539
17540/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017541 * "setqflist()" function
17542 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017543 static void
17544f_setqflist(argvars, rettv)
17545 typval_T *argvars;
17546 typval_T *rettv;
17547{
17548 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17549}
17550
17551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 * "setreg()" function
17553 */
17554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017555f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 typval_T *argvars;
17557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558{
17559 int regname;
17560 char_u *strregname;
17561 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017562 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 int append;
17564 char_u yank_type;
17565 long block_len;
17566
17567 block_len = -1;
17568 yank_type = MAUTO;
17569 append = FALSE;
17570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017571 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017572 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017574 if (strregname == NULL)
17575 return; /* type error; errmsg already given */
17576 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 if (regname == 0 || regname == '@')
17578 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017580 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017582 stropt = get_tv_string_chk(&argvars[2]);
17583 if (stropt == NULL)
17584 return; /* type error */
17585 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 switch (*stropt)
17587 {
17588 case 'a': case 'A': /* append */
17589 append = TRUE;
17590 break;
17591 case 'v': case 'c': /* character-wise selection */
17592 yank_type = MCHAR;
17593 break;
17594 case 'V': case 'l': /* line-wise selection */
17595 yank_type = MLINE;
17596 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 case 'b': case Ctrl_V: /* block-wise selection */
17598 yank_type = MBLOCK;
17599 if (VIM_ISDIGIT(stropt[1]))
17600 {
17601 ++stropt;
17602 block_len = getdigits(&stropt) - 1;
17603 --stropt;
17604 }
17605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 }
17607 }
17608
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017609 if (argvars[1].v_type == VAR_LIST)
17610 {
17611 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017612 char_u **allocval;
17613 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017614 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017615 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017616 int len = argvars[1].vval.v_list->lv_len;
17617 listitem_T *li;
17618
Bram Moolenaar7d647822014-04-05 21:28:56 +020017619 /* First half: use for pointers to result lines; second half: use for
17620 * pointers to allocated copies. */
17621 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017622 if (lstval == NULL)
17623 return;
17624 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017625 allocval = lstval + len + 2;
17626 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017627
17628 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17629 li = li->li_next)
17630 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017631 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017632 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017633 goto free_lstval;
17634 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017635 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017636 /* Need to make a copy, next get_tv_string_buf_chk() will
17637 * overwrite the string. */
17638 strval = vim_strsave(buf);
17639 if (strval == NULL)
17640 goto free_lstval;
17641 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017642 }
17643 *curval++ = strval;
17644 }
17645 *curval++ = NULL;
17646
17647 write_reg_contents_lst(regname, lstval, -1,
17648 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017649free_lstval:
17650 while (curallocval > allocval)
17651 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017652 vim_free(lstval);
17653 }
17654 else
17655 {
17656 strval = get_tv_string_chk(&argvars[1]);
17657 if (strval == NULL)
17658 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017659 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663}
17664
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017665/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017666 * "settabvar()" function
17667 */
17668 static void
17669f_settabvar(argvars, rettv)
17670 typval_T *argvars;
17671 typval_T *rettv;
17672{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017673#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017674 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017675 tabpage_T *tp;
17676#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017677 char_u *varname, *tabvarname;
17678 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017679
17680 rettv->vval.v_number = 0;
17681
17682 if (check_restricted() || check_secure())
17683 return;
17684
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017685#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017686 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017687#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017688 varname = get_tv_string_chk(&argvars[1]);
17689 varp = &argvars[2];
17690
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017691 if (varname != NULL && varp != NULL
17692#ifdef FEAT_WINDOWS
17693 && tp != NULL
17694#endif
17695 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017696 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017697#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017698 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017699 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017700#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017701
17702 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17703 if (tabvarname != NULL)
17704 {
17705 STRCPY(tabvarname, "t:");
17706 STRCPY(tabvarname + 2, varname);
17707 set_var(tabvarname, varp, TRUE);
17708 vim_free(tabvarname);
17709 }
17710
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017711#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017712 /* Restore current tabpage */
17713 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017714 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017715#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017716 }
17717}
17718
17719/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017720 * "settabwinvar()" function
17721 */
17722 static void
17723f_settabwinvar(argvars, rettv)
17724 typval_T *argvars;
17725 typval_T *rettv;
17726{
17727 setwinvar(argvars, rettv, 1);
17728}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729
17730/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017731 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017734f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017735 typval_T *argvars;
17736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017738 setwinvar(argvars, rettv, 0);
17739}
17740
17741/*
17742 * "setwinvar()" and "settabwinvar()" functions
17743 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017744
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017745 static void
17746setwinvar(argvars, rettv, off)
17747 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017748 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017749 int off;
17750{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 win_T *win;
17752#ifdef FEAT_WINDOWS
17753 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017754 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017755 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756#endif
17757 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017758 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017760 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761
17762 if (check_restricted() || check_secure())
17763 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017764
17765#ifdef FEAT_WINDOWS
17766 if (off == 1)
17767 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17768 else
17769 tp = curtab;
17770#endif
17771 win = find_win_by_nr(&argvars[off], tp);
17772 varname = get_tv_string_chk(&argvars[off + 1]);
17773 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774
17775 if (win != NULL && varname != NULL && varp != NULL)
17776 {
17777#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017778 need_switch_win = !(tp == curtab && win == curwin);
17779 if (!need_switch_win
17780 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017783 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017785 long numval;
17786 char_u *strval;
17787 int error = FALSE;
17788
17789 ++varname;
17790 numval = get_tv_number_chk(varp, &error);
17791 strval = get_tv_string_buf_chk(varp, nbuf);
17792 if (!error && strval != NULL)
17793 set_option_value(varname, numval, strval, OPT_LOCAL);
17794 }
17795 else
17796 {
17797 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17798 if (winvarname != NULL)
17799 {
17800 STRCPY(winvarname, "w:");
17801 STRCPY(winvarname + 2, varname);
17802 set_var(winvarname, varp, TRUE);
17803 vim_free(winvarname);
17804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 }
17806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017808 if (need_switch_win)
17809 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810#endif
17811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812}
17813
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017814#ifdef FEAT_CRYPT
17815/*
17816 * "sha256({string})" function
17817 */
17818 static void
17819f_sha256(argvars, rettv)
17820 typval_T *argvars;
17821 typval_T *rettv;
17822{
17823 char_u *p;
17824
17825 p = get_tv_string(&argvars[0]);
17826 rettv->vval.v_string = vim_strsave(
17827 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17828 rettv->v_type = VAR_STRING;
17829}
17830#endif /* FEAT_CRYPT */
17831
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017833 * "shellescape({string})" function
17834 */
17835 static void
17836f_shellescape(argvars, rettv)
17837 typval_T *argvars;
17838 typval_T *rettv;
17839{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017840 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017841 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017842 rettv->v_type = VAR_STRING;
17843}
17844
17845/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017846 * shiftwidth() function
17847 */
17848 static void
17849f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017850 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017851 typval_T *rettv;
17852{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017853 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017854}
17855
17856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017857 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 */
17859 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017861 typval_T *argvars;
17862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017864 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866 p = get_tv_string(&argvars[0]);
17867 rettv->vval.v_string = vim_strsave(p);
17868 simplify_filename(rettv->vval.v_string); /* simplify in place */
17869 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870}
17871
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017872#ifdef FEAT_FLOAT
17873/*
17874 * "sin()" function
17875 */
17876 static void
17877f_sin(argvars, rettv)
17878 typval_T *argvars;
17879 typval_T *rettv;
17880{
17881 float_T f;
17882
17883 rettv->v_type = VAR_FLOAT;
17884 if (get_float_arg(argvars, &f) == OK)
17885 rettv->vval.v_float = sin(f);
17886 else
17887 rettv->vval.v_float = 0.0;
17888}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017889
17890/*
17891 * "sinh()" function
17892 */
17893 static void
17894f_sinh(argvars, rettv)
17895 typval_T *argvars;
17896 typval_T *rettv;
17897{
17898 float_T f;
17899
17900 rettv->v_type = VAR_FLOAT;
17901 if (get_float_arg(argvars, &f) == OK)
17902 rettv->vval.v_float = sinh(f);
17903 else
17904 rettv->vval.v_float = 0.0;
17905}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017906#endif
17907
Bram Moolenaar0d660222005-01-07 21:51:51 +000017908static int
17909#ifdef __BORLANDC__
17910 _RTLENTRYF
17911#endif
17912 item_compare __ARGS((const void *s1, const void *s2));
17913static int
17914#ifdef __BORLANDC__
17915 _RTLENTRYF
17916#endif
17917 item_compare2 __ARGS((const void *s1, const void *s2));
17918
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017919/* struct used in the array that's given to qsort() */
17920typedef struct
17921{
17922 listitem_T *item;
17923 int idx;
17924} sortItem_T;
17925
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017927static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017928static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017929static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017930static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017931static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017932static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017933#define ITEM_COMPARE_FAIL 999
17934
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017936 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017938 static int
17939#ifdef __BORLANDC__
17940_RTLENTRYF
17941#endif
17942item_compare(s1, s2)
17943 const void *s1;
17944 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017946 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017947 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017948 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017949 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017950 int res;
17951 char_u numbuf1[NUMBUFLEN];
17952 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017954 si1 = (sortItem_T *)s1;
17955 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017956 tv1 = &si1->item->li_tv;
17957 tv2 = &si2->item->li_tv;
17958 /* tv2string() puts quotes around a string and allocates memory. Don't do
17959 * that for string variables. Use a single quote when comparing with a
17960 * non-string to do what the docs promise. */
17961 if (tv1->v_type == VAR_STRING)
17962 {
17963 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17964 p1 = (char_u *)"'";
17965 else
17966 p1 = tv1->vval.v_string;
17967 }
17968 else
17969 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17970 if (tv2->v_type == VAR_STRING)
17971 {
17972 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17973 p2 = (char_u *)"'";
17974 else
17975 p2 = tv2->vval.v_string;
17976 }
17977 else
17978 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017979 if (p1 == NULL)
17980 p1 = (char_u *)"";
17981 if (p2 == NULL)
17982 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017983 if (!item_compare_numeric)
17984 {
17985 if (item_compare_ic)
17986 res = STRICMP(p1, p2);
17987 else
17988 res = STRCMP(p1, p2);
17989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017991 {
17992 double n1, n2;
17993 n1 = strtod((char *)p1, (char **)&p1);
17994 n2 = strtod((char *)p2, (char **)&p2);
17995 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17996 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017997
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017998 /* When the result would be zero, compare the item indexes. Makes the
17999 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018000 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018001 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018002
Bram Moolenaar0d660222005-01-07 21:51:51 +000018003 vim_free(tofree1);
18004 vim_free(tofree2);
18005 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006}
18007
18008 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018009#ifdef __BORLANDC__
18010_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018012item_compare2(s1, s2)
18013 const void *s1;
18014 const void *s2;
18015{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018016 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018019 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018020 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018022 /* shortcut after failure in previous call; compare all items equal */
18023 if (item_compare_func_err)
18024 return 0;
18025
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018026 si1 = (sortItem_T *)s1;
18027 si2 = (sortItem_T *)s2;
18028
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018029 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018030 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018031 copy_tv(&si1->item->li_tv, &argv[0]);
18032 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018033
18034 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018035 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018036 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18037 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018038 clear_tv(&argv[0]);
18039 clear_tv(&argv[1]);
18040
18041 if (res == FAIL)
18042 res = ITEM_COMPARE_FAIL;
18043 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018044 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18045 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018046 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018047 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018048
18049 /* When the result would be zero, compare the pointers themselves. Makes
18050 * the sort stable. */
18051 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018052 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018053
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054 return res;
18055}
18056
18057/*
18058 * "sort({list})" function
18059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018061do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018062 typval_T *argvars;
18063 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018064 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065{
Bram Moolenaar33570922005-01-25 22:26:29 +000018066 list_T *l;
18067 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018068 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018069 long len;
18070 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071
Bram Moolenaar0d660222005-01-07 21:51:51 +000018072 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018073 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 else
18075 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018076 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018077 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018078 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18079 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018080 return;
18081 rettv->vval.v_list = l;
18082 rettv->v_type = VAR_LIST;
18083 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084
Bram Moolenaar0d660222005-01-07 21:51:51 +000018085 len = list_len(l);
18086 if (len <= 1)
18087 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088
Bram Moolenaar0d660222005-01-07 21:51:51 +000018089 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018090 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018091 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018092 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018093 if (argvars[1].v_type != VAR_UNKNOWN)
18094 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018095 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018096 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018097 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018098 else
18099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018100 int error = FALSE;
18101
18102 i = get_tv_number_chk(&argvars[1], &error);
18103 if (error)
18104 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018105 if (i == 1)
18106 item_compare_ic = TRUE;
18107 else
18108 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018109 if (item_compare_func != NULL)
18110 {
18111 if (STRCMP(item_compare_func, "n") == 0)
18112 {
18113 item_compare_func = NULL;
18114 item_compare_numeric = TRUE;
18115 }
18116 else if (STRCMP(item_compare_func, "i") == 0)
18117 {
18118 item_compare_func = NULL;
18119 item_compare_ic = TRUE;
18120 }
18121 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018122 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018123
18124 if (argvars[2].v_type != VAR_UNKNOWN)
18125 {
18126 /* optional third argument: {dict} */
18127 if (argvars[2].v_type != VAR_DICT)
18128 {
18129 EMSG(_(e_dictreq));
18130 return;
18131 }
18132 item_compare_selfdict = argvars[2].vval.v_dict;
18133 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135
Bram Moolenaar0d660222005-01-07 21:51:51 +000018136 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018137 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018138 if (ptrs == NULL)
18139 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140
Bram Moolenaar327aa022014-03-25 18:24:23 +010018141 i = 0;
18142 if (sort)
18143 {
18144 /* sort(): ptrs will be the list to sort */
18145 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018146 {
18147 ptrs[i].item = li;
18148 ptrs[i].idx = i;
18149 ++i;
18150 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018151
18152 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018153 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018154 /* test the compare function */
18155 if (item_compare_func != NULL
18156 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018157 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018158 EMSG(_("E702: Sort compare function failed"));
18159 else
18160 {
18161 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018162 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018163 item_compare_func == NULL ? item_compare : item_compare2);
18164
18165 if (!item_compare_func_err)
18166 {
18167 /* Clear the List and append the items in sorted order. */
18168 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18169 l->lv_len = 0;
18170 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018171 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018172 }
18173 }
18174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018176 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018177 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18178
18179 /* f_uniq(): ptrs will be a stack of items to remove */
18180 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018181 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018182 item_compare_func_ptr = item_compare_func
18183 ? item_compare2 : item_compare;
18184
18185 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18186 li = li->li_next)
18187 {
18188 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18189 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018190 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018191 if (item_compare_func_err)
18192 {
18193 EMSG(_("E882: Uniq compare function failed"));
18194 break;
18195 }
18196 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018198 if (!item_compare_func_err)
18199 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018200 while (--i >= 0)
18201 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018202 li = ptrs[i].item->li_next;
18203 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018204 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018205 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018206 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018207 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018208 list_fix_watch(l, li);
18209 listitem_free(li);
18210 l->lv_len--;
18211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018212 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018213 }
18214
18215 vim_free(ptrs);
18216 }
18217}
18218
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018219/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018220 * "sort({list})" function
18221 */
18222 static void
18223f_sort(argvars, rettv)
18224 typval_T *argvars;
18225 typval_T *rettv;
18226{
18227 do_sort_uniq(argvars, rettv, TRUE);
18228}
18229
18230/*
18231 * "uniq({list})" function
18232 */
18233 static void
18234f_uniq(argvars, rettv)
18235 typval_T *argvars;
18236 typval_T *rettv;
18237{
18238 do_sort_uniq(argvars, rettv, FALSE);
18239}
18240
18241/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018242 * "soundfold({word})" function
18243 */
18244 static void
18245f_soundfold(argvars, rettv)
18246 typval_T *argvars;
18247 typval_T *rettv;
18248{
18249 char_u *s;
18250
18251 rettv->v_type = VAR_STRING;
18252 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018253#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018254 rettv->vval.v_string = eval_soundfold(s);
18255#else
18256 rettv->vval.v_string = vim_strsave(s);
18257#endif
18258}
18259
18260/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018261 * "spellbadword()" function
18262 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018263 static void
18264f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018265 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018266 typval_T *rettv;
18267{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018268 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018269 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018270 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018271
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018272 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018273 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018274
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018275#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018276 if (argvars[0].v_type == VAR_UNKNOWN)
18277 {
18278 /* Find the start and length of the badly spelled word. */
18279 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18280 if (len != 0)
18281 word = ml_get_cursor();
18282 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018283 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018284 {
18285 char_u *str = get_tv_string_chk(&argvars[0]);
18286 int capcol = -1;
18287
18288 if (str != NULL)
18289 {
18290 /* Check the argument for spelling. */
18291 while (*str != NUL)
18292 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018293 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018294 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018295 {
18296 word = str;
18297 break;
18298 }
18299 str += len;
18300 }
18301 }
18302 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018303#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018304
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018305 list_append_string(rettv->vval.v_list, word, len);
18306 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018307 attr == HLF_SPB ? "bad" :
18308 attr == HLF_SPR ? "rare" :
18309 attr == HLF_SPL ? "local" :
18310 attr == HLF_SPC ? "caps" :
18311 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018312}
18313
18314/*
18315 * "spellsuggest()" function
18316 */
18317 static void
18318f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018319 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018320 typval_T *rettv;
18321{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018322#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018323 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018324 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018325 int maxcount;
18326 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018327 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018328 listitem_T *li;
18329 int need_capital = FALSE;
18330#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018331
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018332 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018333 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018334
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018335#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018336 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018337 {
18338 str = get_tv_string(&argvars[0]);
18339 if (argvars[1].v_type != VAR_UNKNOWN)
18340 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018341 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018342 if (maxcount <= 0)
18343 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018344 if (argvars[2].v_type != VAR_UNKNOWN)
18345 {
18346 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18347 if (typeerr)
18348 return;
18349 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018350 }
18351 else
18352 maxcount = 25;
18353
Bram Moolenaar4770d092006-01-12 23:22:24 +000018354 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018355
18356 for (i = 0; i < ga.ga_len; ++i)
18357 {
18358 str = ((char_u **)ga.ga_data)[i];
18359
18360 li = listitem_alloc();
18361 if (li == NULL)
18362 vim_free(str);
18363 else
18364 {
18365 li->li_tv.v_type = VAR_STRING;
18366 li->li_tv.v_lock = 0;
18367 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018368 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018369 }
18370 }
18371 ga_clear(&ga);
18372 }
18373#endif
18374}
18375
Bram Moolenaar0d660222005-01-07 21:51:51 +000018376 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018377f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018378 typval_T *argvars;
18379 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018380{
18381 char_u *str;
18382 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018383 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018384 regmatch_T regmatch;
18385 char_u patbuf[NUMBUFLEN];
18386 char_u *save_cpo;
18387 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018388 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018389 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018390 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018391
18392 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18393 save_cpo = p_cpo;
18394 p_cpo = (char_u *)"";
18395
18396 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018397 if (argvars[1].v_type != VAR_UNKNOWN)
18398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018399 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18400 if (pat == NULL)
18401 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018402 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018403 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018404 }
18405 if (pat == NULL || *pat == NUL)
18406 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018407
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018408 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018410 if (typeerr)
18411 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18414 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018416 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018417 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018419 if (*str == NUL)
18420 match = FALSE; /* empty item at the end */
18421 else
18422 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018423 if (match)
18424 end = regmatch.startp[0];
18425 else
18426 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018427 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18428 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018429 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018430 if (list_append_string(rettv->vval.v_list, str,
18431 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018432 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018433 }
18434 if (!match)
18435 break;
18436 /* Advance to just after the match. */
18437 if (regmatch.endp[0] > str)
18438 col = 0;
18439 else
18440 {
18441 /* Don't get stuck at the same match. */
18442#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018443 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018444#else
18445 col = 1;
18446#endif
18447 }
18448 str = regmatch.endp[0];
18449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450
Bram Moolenaar473de612013-06-08 18:19:48 +020018451 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453
Bram Moolenaar0d660222005-01-07 21:51:51 +000018454 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455}
18456
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018457#ifdef FEAT_FLOAT
18458/*
18459 * "sqrt()" function
18460 */
18461 static void
18462f_sqrt(argvars, rettv)
18463 typval_T *argvars;
18464 typval_T *rettv;
18465{
18466 float_T f;
18467
18468 rettv->v_type = VAR_FLOAT;
18469 if (get_float_arg(argvars, &f) == OK)
18470 rettv->vval.v_float = sqrt(f);
18471 else
18472 rettv->vval.v_float = 0.0;
18473}
18474
18475/*
18476 * "str2float()" function
18477 */
18478 static void
18479f_str2float(argvars, rettv)
18480 typval_T *argvars;
18481 typval_T *rettv;
18482{
18483 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18484
18485 if (*p == '+')
18486 p = skipwhite(p + 1);
18487 (void)string2float(p, &rettv->vval.v_float);
18488 rettv->v_type = VAR_FLOAT;
18489}
18490#endif
18491
Bram Moolenaar2c932302006-03-18 21:42:09 +000018492/*
18493 * "str2nr()" function
18494 */
18495 static void
18496f_str2nr(argvars, rettv)
18497 typval_T *argvars;
18498 typval_T *rettv;
18499{
18500 int base = 10;
18501 char_u *p;
18502 long n;
18503
18504 if (argvars[1].v_type != VAR_UNKNOWN)
18505 {
18506 base = get_tv_number(&argvars[1]);
18507 if (base != 8 && base != 10 && base != 16)
18508 {
18509 EMSG(_(e_invarg));
18510 return;
18511 }
18512 }
18513
18514 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018515 if (*p == '+')
18516 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018517 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018518 rettv->vval.v_number = n;
18519}
18520
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521#ifdef HAVE_STRFTIME
18522/*
18523 * "strftime({format}[, {time}])" function
18524 */
18525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018526f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018527 typval_T *argvars;
18528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529{
18530 char_u result_buf[256];
18531 struct tm *curtime;
18532 time_t seconds;
18533 char_u *p;
18534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018535 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018537 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018538 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 seconds = time(NULL);
18540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018541 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 curtime = localtime(&seconds);
18543 /* MSVC returns NULL for an invalid value of seconds. */
18544 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018545 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 else
18547 {
18548# ifdef FEAT_MBYTE
18549 vimconv_T conv;
18550 char_u *enc;
18551
18552 conv.vc_type = CONV_NONE;
18553 enc = enc_locale();
18554 convert_setup(&conv, p_enc, enc);
18555 if (conv.vc_type != CONV_NONE)
18556 p = string_convert(&conv, p, NULL);
18557# endif
18558 if (p != NULL)
18559 (void)strftime((char *)result_buf, sizeof(result_buf),
18560 (char *)p, curtime);
18561 else
18562 result_buf[0] = NUL;
18563
18564# ifdef FEAT_MBYTE
18565 if (conv.vc_type != CONV_NONE)
18566 vim_free(p);
18567 convert_setup(&conv, enc, p_enc);
18568 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018569 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 else
18571# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573
18574# ifdef FEAT_MBYTE
18575 /* Release conversion descriptors */
18576 convert_setup(&conv, NULL, NULL);
18577 vim_free(enc);
18578# endif
18579 }
18580}
18581#endif
18582
18583/*
18584 * "stridx()" function
18585 */
18586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018588 typval_T *argvars;
18589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590{
18591 char_u buf[NUMBUFLEN];
18592 char_u *needle;
18593 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018594 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018596 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018598 needle = get_tv_string_chk(&argvars[1]);
18599 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018600 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018601 if (needle == NULL || haystack == NULL)
18602 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603
Bram Moolenaar33570922005-01-25 22:26:29 +000018604 if (argvars[2].v_type != VAR_UNKNOWN)
18605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018606 int error = FALSE;
18607
18608 start_idx = get_tv_number_chk(&argvars[2], &error);
18609 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018610 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018611 if (start_idx >= 0)
18612 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018613 }
18614
18615 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18616 if (pos != NULL)
18617 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618}
18619
18620/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018621 * "string()" function
18622 */
18623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018625 typval_T *argvars;
18626 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018627{
18628 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018629 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018631 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018632 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018633 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018634 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636}
18637
18638/*
18639 * "strlen()" function
18640 */
18641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018642f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018643 typval_T *argvars;
18644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 rettv->vval.v_number = (varnumber_T)(STRLEN(
18647 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648}
18649
18650/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018651 * "strchars()" function
18652 */
18653 static void
18654f_strchars(argvars, rettv)
18655 typval_T *argvars;
18656 typval_T *rettv;
18657{
18658 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018659 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018660#ifdef FEAT_MBYTE
18661 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018662 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018663#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018664
18665 if (argvars[1].v_type != VAR_UNKNOWN)
18666 skipcc = get_tv_number_chk(&argvars[1], NULL);
18667 if (skipcc < 0 || skipcc > 1)
18668 EMSG(_(e_invarg));
18669 else
18670 {
18671#ifdef FEAT_MBYTE
18672 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18673 while (*s != NUL)
18674 {
18675 func_mb_ptr2char_adv(&s);
18676 ++len;
18677 }
18678 rettv->vval.v_number = len;
18679#else
18680 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18681#endif
18682 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018683}
18684
18685/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018686 * "strdisplaywidth()" function
18687 */
18688 static void
18689f_strdisplaywidth(argvars, rettv)
18690 typval_T *argvars;
18691 typval_T *rettv;
18692{
18693 char_u *s = get_tv_string(&argvars[0]);
18694 int col = 0;
18695
18696 if (argvars[1].v_type != VAR_UNKNOWN)
18697 col = get_tv_number(&argvars[1]);
18698
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018699 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018700}
18701
18702/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018703 * "strwidth()" function
18704 */
18705 static void
18706f_strwidth(argvars, rettv)
18707 typval_T *argvars;
18708 typval_T *rettv;
18709{
18710 char_u *s = get_tv_string(&argvars[0]);
18711
18712 rettv->vval.v_number = (varnumber_T)(
18713#ifdef FEAT_MBYTE
18714 mb_string2cells(s, -1)
18715#else
18716 STRLEN(s)
18717#endif
18718 );
18719}
18720
18721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 * "strpart()" function
18723 */
18724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018725f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018726 typval_T *argvars;
18727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728{
18729 char_u *p;
18730 int n;
18731 int len;
18732 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018733 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018735 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 slen = (int)STRLEN(p);
18737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018738 n = get_tv_number_chk(&argvars[1], &error);
18739 if (error)
18740 len = 0;
18741 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 else
18744 len = slen - n; /* default len: all bytes that are available. */
18745
18746 /*
18747 * Only return the overlap between the specified part and the actual
18748 * string.
18749 */
18750 if (n < 0)
18751 {
18752 len += n;
18753 n = 0;
18754 }
18755 else if (n > slen)
18756 n = slen;
18757 if (len < 0)
18758 len = 0;
18759 else if (n + len > slen)
18760 len = slen - n;
18761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762 rettv->v_type = VAR_STRING;
18763 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764}
18765
18766/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018767 * "strridx()" function
18768 */
18769 static void
18770f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 typval_T *argvars;
18772 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018773{
18774 char_u buf[NUMBUFLEN];
18775 char_u *needle;
18776 char_u *haystack;
18777 char_u *rest;
18778 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018779 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018781 needle = get_tv_string_chk(&argvars[1]);
18782 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018783
18784 rettv->vval.v_number = -1;
18785 if (needle == NULL || haystack == NULL)
18786 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018787
18788 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018789 if (argvars[2].v_type != VAR_UNKNOWN)
18790 {
18791 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018792 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018793 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018794 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018795 }
18796 else
18797 end_idx = haystack_len;
18798
Bram Moolenaar0d660222005-01-07 21:51:51 +000018799 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018800 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018801 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018802 lastmatch = haystack + end_idx;
18803 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018804 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018805 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018806 for (rest = haystack; *rest != '\0'; ++rest)
18807 {
18808 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018809 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018810 break;
18811 lastmatch = rest;
18812 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018813 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018814
18815 if (lastmatch == NULL)
18816 rettv->vval.v_number = -1;
18817 else
18818 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18819}
18820
18821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 * "strtrans()" function
18823 */
18824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018825f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018826 typval_T *argvars;
18827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018829 rettv->v_type = VAR_STRING;
18830 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831}
18832
18833/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018834 * "submatch()" function
18835 */
18836 static void
18837f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 typval_T *argvars;
18839 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018840{
Bram Moolenaar41571762014-04-02 19:00:58 +020018841 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018842 int no;
18843 int retList = 0;
18844
18845 no = (int)get_tv_number_chk(&argvars[0], &error);
18846 if (error)
18847 return;
18848 error = FALSE;
18849 if (argvars[1].v_type != VAR_UNKNOWN)
18850 retList = get_tv_number_chk(&argvars[1], &error);
18851 if (error)
18852 return;
18853
18854 if (retList == 0)
18855 {
18856 rettv->v_type = VAR_STRING;
18857 rettv->vval.v_string = reg_submatch(no);
18858 }
18859 else
18860 {
18861 rettv->v_type = VAR_LIST;
18862 rettv->vval.v_list = reg_submatch_list(no);
18863 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018864}
18865
18866/*
18867 * "substitute()" function
18868 */
18869 static void
18870f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018871 typval_T *argvars;
18872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018873{
18874 char_u patbuf[NUMBUFLEN];
18875 char_u subbuf[NUMBUFLEN];
18876 char_u flagsbuf[NUMBUFLEN];
18877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018878 char_u *str = get_tv_string_chk(&argvars[0]);
18879 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18880 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18881 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18882
Bram Moolenaar0d660222005-01-07 21:51:51 +000018883 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018884 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18885 rettv->vval.v_string = NULL;
18886 else
18887 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018888}
18889
18890/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018891 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018894f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018895 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897{
18898 int id = 0;
18899#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018900 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 long col;
18902 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018903 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018905 lnum = get_tv_lnum(argvars); /* -1 on type error */
18906 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18907 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018909 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018910 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018911 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912#endif
18913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018914 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915}
18916
18917/*
18918 * "synIDattr(id, what [, mode])" function
18919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018921f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924{
18925 char_u *p = NULL;
18926#ifdef FEAT_SYN_HL
18927 int id;
18928 char_u *what;
18929 char_u *mode;
18930 char_u modebuf[NUMBUFLEN];
18931 int modec;
18932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018933 id = get_tv_number(&argvars[0]);
18934 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018935 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018937 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018939 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 modec = 0; /* replace invalid with current */
18941 }
18942 else
18943 {
18944#ifdef FEAT_GUI
18945 if (gui.in_use)
18946 modec = 'g';
18947 else
18948#endif
18949 if (t_colors > 1)
18950 modec = 'c';
18951 else
18952 modec = 't';
18953 }
18954
18955
18956 switch (TOLOWER_ASC(what[0]))
18957 {
18958 case 'b':
18959 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18960 p = highlight_color(id, what, modec);
18961 else /* bold */
18962 p = highlight_has_attr(id, HL_BOLD, modec);
18963 break;
18964
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018965 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 p = highlight_color(id, what, modec);
18967 break;
18968
18969 case 'i':
18970 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18971 p = highlight_has_attr(id, HL_INVERSE, modec);
18972 else /* italic */
18973 p = highlight_has_attr(id, HL_ITALIC, modec);
18974 break;
18975
18976 case 'n': /* name */
18977 p = get_highlight_name(NULL, id - 1);
18978 break;
18979
18980 case 'r': /* reverse */
18981 p = highlight_has_attr(id, HL_INVERSE, modec);
18982 break;
18983
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018984 case 's':
18985 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18986 p = highlight_color(id, what, modec);
18987 else /* standout */
18988 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 break;
18990
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018991 case 'u':
18992 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18993 /* underline */
18994 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18995 else
18996 /* undercurl */
18997 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 break;
18999 }
19000
19001 if (p != NULL)
19002 p = vim_strsave(p);
19003#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019004 rettv->v_type = VAR_STRING;
19005 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006}
19007
19008/*
19009 * "synIDtrans(id)" function
19010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019012f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019013 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015{
19016 int id;
19017
19018#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019019 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020
19021 if (id > 0)
19022 id = syn_get_final_id(id);
19023 else
19024#endif
19025 id = 0;
19026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019027 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028}
19029
19030/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019031 * "synconcealed(lnum, col)" function
19032 */
19033 static void
19034f_synconcealed(argvars, rettv)
19035 typval_T *argvars UNUSED;
19036 typval_T *rettv;
19037{
19038#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19039 long lnum;
19040 long col;
19041 int syntax_flags = 0;
19042 int cchar;
19043 int matchid = 0;
19044 char_u str[NUMBUFLEN];
19045#endif
19046
19047 rettv->v_type = VAR_LIST;
19048 rettv->vval.v_list = NULL;
19049
19050#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19051 lnum = get_tv_lnum(argvars); /* -1 on type error */
19052 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19053
19054 vim_memset(str, NUL, sizeof(str));
19055
19056 if (rettv_list_alloc(rettv) != FAIL)
19057 {
19058 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19059 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19060 && curwin->w_p_cole > 0)
19061 {
19062 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19063 syntax_flags = get_syntax_info(&matchid);
19064
19065 /* get the conceal character */
19066 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19067 {
19068 cchar = syn_get_sub_char();
19069 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19070 cchar = lcs_conceal;
19071 if (cchar != NUL)
19072 {
19073# ifdef FEAT_MBYTE
19074 if (has_mbyte)
19075 (*mb_char2bytes)(cchar, str);
19076 else
19077# endif
19078 str[0] = cchar;
19079 }
19080 }
19081 }
19082
19083 list_append_number(rettv->vval.v_list,
19084 (syntax_flags & HL_CONCEAL) != 0);
19085 /* -1 to auto-determine strlen */
19086 list_append_string(rettv->vval.v_list, str, -1);
19087 list_append_number(rettv->vval.v_list, matchid);
19088 }
19089#endif
19090}
19091
19092/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019093 * "synstack(lnum, col)" function
19094 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019095 static void
19096f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019097 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019098 typval_T *rettv;
19099{
19100#ifdef FEAT_SYN_HL
19101 long lnum;
19102 long col;
19103 int i;
19104 int id;
19105#endif
19106
19107 rettv->v_type = VAR_LIST;
19108 rettv->vval.v_list = NULL;
19109
19110#ifdef FEAT_SYN_HL
19111 lnum = get_tv_lnum(argvars); /* -1 on type error */
19112 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19113
19114 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019115 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019116 && rettv_list_alloc(rettv) != FAIL)
19117 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019118 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019119 for (i = 0; ; ++i)
19120 {
19121 id = syn_get_stack_item(i);
19122 if (id < 0)
19123 break;
19124 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19125 break;
19126 }
19127 }
19128#endif
19129}
19130
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019132get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019133 typval_T *argvars;
19134 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019135 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019137 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019139 char_u *infile = NULL;
19140 char_u buf[NUMBUFLEN];
19141 int err = FALSE;
19142 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019143 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019144 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019146 rettv->v_type = VAR_STRING;
19147 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019148 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019149 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019150
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019151 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019152 {
19153 /*
19154 * Write the string to a temp file, to be used for input of the shell
19155 * command.
19156 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019157 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019158 {
19159 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019160 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019161 }
19162
19163 fd = mch_fopen((char *)infile, WRITEBIN);
19164 if (fd == NULL)
19165 {
19166 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019167 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019168 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019169 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019170 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019171 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19172 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019173 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019174 else
19175 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019176 size_t len;
19177
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019178 p = get_tv_string_buf_chk(&argvars[1], buf);
19179 if (p == NULL)
19180 {
19181 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019182 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019183 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019184 len = STRLEN(p);
19185 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019186 err = TRUE;
19187 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019188 if (fclose(fd) != 0)
19189 err = TRUE;
19190 if (err)
19191 {
19192 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019193 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019194 }
19195 }
19196
Bram Moolenaar52a72462014-08-29 15:53:52 +020019197 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19198 * echoes typeahead, that messes up the display. */
19199 if (!msg_silent)
19200 flags += SHELL_COOKED;
19201
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019202 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019204 int len;
19205 listitem_T *li;
19206 char_u *s = NULL;
19207 char_u *start;
19208 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019209 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210
Bram Moolenaar52a72462014-08-29 15:53:52 +020019211 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019212 if (res == NULL)
19213 goto errret;
19214
19215 list = list_alloc();
19216 if (list == NULL)
19217 goto errret;
19218
19219 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019221 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019222 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019223 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019224 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019225
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019226 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019227 if (s == NULL)
19228 goto errret;
19229
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019230 for (p = s; start < end; ++p, ++start)
19231 *p = *start == NUL ? NL : *start;
19232 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019233
19234 li = listitem_alloc();
19235 if (li == NULL)
19236 {
19237 vim_free(s);
19238 goto errret;
19239 }
19240 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019241 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019242 li->li_tv.vval.v_string = s;
19243 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019245
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019246 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019247 rettv->v_type = VAR_LIST;
19248 rettv->vval.v_list = list;
19249 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019251 else
19252 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019253 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019254#ifdef USE_CR
19255 /* translate <CR> into <NL> */
19256 if (res != NULL)
19257 {
19258 char_u *s;
19259
19260 for (s = res; *s; ++s)
19261 {
19262 if (*s == CAR)
19263 *s = NL;
19264 }
19265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266#else
19267# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019268 /* translate <CR><NL> into <NL> */
19269 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019271 char_u *s, *d;
19272
19273 d = res;
19274 for (s = res; *s; ++s)
19275 {
19276 if (s[0] == CAR && s[1] == NL)
19277 ++s;
19278 *d++ = *s;
19279 }
19280 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282# endif
19283#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019284 rettv->vval.v_string = res;
19285 res = NULL;
19286 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019287
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019288errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019289 if (infile != NULL)
19290 {
19291 mch_remove(infile);
19292 vim_free(infile);
19293 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019294 if (res != NULL)
19295 vim_free(res);
19296 if (list != NULL)
19297 list_free(list, TRUE);
19298}
19299
19300/*
19301 * "system()" function
19302 */
19303 static void
19304f_system(argvars, rettv)
19305 typval_T *argvars;
19306 typval_T *rettv;
19307{
19308 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19309}
19310
19311/*
19312 * "systemlist()" function
19313 */
19314 static void
19315f_systemlist(argvars, rettv)
19316 typval_T *argvars;
19317 typval_T *rettv;
19318{
19319 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320}
19321
19322/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019323 * "tabpagebuflist()" function
19324 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019325 static void
19326f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019327 typval_T *argvars UNUSED;
19328 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019329{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019330#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019331 tabpage_T *tp;
19332 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019333
19334 if (argvars[0].v_type == VAR_UNKNOWN)
19335 wp = firstwin;
19336 else
19337 {
19338 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19339 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019340 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019341 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019342 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019343 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019344 for (; wp != NULL; wp = wp->w_next)
19345 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019346 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019347 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019348 }
19349#endif
19350}
19351
19352
19353/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019354 * "tabpagenr()" function
19355 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019356 static void
19357f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019358 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019359 typval_T *rettv;
19360{
19361 int nr = 1;
19362#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019363 char_u *arg;
19364
19365 if (argvars[0].v_type != VAR_UNKNOWN)
19366 {
19367 arg = get_tv_string_chk(&argvars[0]);
19368 nr = 0;
19369 if (arg != NULL)
19370 {
19371 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019372 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019373 else
19374 EMSG2(_(e_invexpr2), arg);
19375 }
19376 }
19377 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019378 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019379#endif
19380 rettv->vval.v_number = nr;
19381}
19382
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019383
19384#ifdef FEAT_WINDOWS
19385static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19386
19387/*
19388 * Common code for tabpagewinnr() and winnr().
19389 */
19390 static int
19391get_winnr(tp, argvar)
19392 tabpage_T *tp;
19393 typval_T *argvar;
19394{
19395 win_T *twin;
19396 int nr = 1;
19397 win_T *wp;
19398 char_u *arg;
19399
19400 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19401 if (argvar->v_type != VAR_UNKNOWN)
19402 {
19403 arg = get_tv_string_chk(argvar);
19404 if (arg == NULL)
19405 nr = 0; /* type error; errmsg already given */
19406 else if (STRCMP(arg, "$") == 0)
19407 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19408 else if (STRCMP(arg, "#") == 0)
19409 {
19410 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19411 if (twin == NULL)
19412 nr = 0;
19413 }
19414 else
19415 {
19416 EMSG2(_(e_invexpr2), arg);
19417 nr = 0;
19418 }
19419 }
19420
19421 if (nr > 0)
19422 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19423 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019424 {
19425 if (wp == NULL)
19426 {
19427 /* didn't find it in this tabpage */
19428 nr = 0;
19429 break;
19430 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019431 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019432 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019433 return nr;
19434}
19435#endif
19436
19437/*
19438 * "tabpagewinnr()" function
19439 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019440 static void
19441f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019442 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019443 typval_T *rettv;
19444{
19445 int nr = 1;
19446#ifdef FEAT_WINDOWS
19447 tabpage_T *tp;
19448
19449 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19450 if (tp == NULL)
19451 nr = 0;
19452 else
19453 nr = get_winnr(tp, &argvars[1]);
19454#endif
19455 rettv->vval.v_number = nr;
19456}
19457
19458
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019459/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019460 * "tagfiles()" function
19461 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019462 static void
19463f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019464 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019465 typval_T *rettv;
19466{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019467 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019468 tagname_T tn;
19469 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019470
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019471 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019472 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019473 fname = alloc(MAXPATHL);
19474 if (fname == NULL)
19475 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019476
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019477 for (first = TRUE; ; first = FALSE)
19478 if (get_tagfname(&tn, first, fname) == FAIL
19479 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019480 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019481 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019482 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019483}
19484
19485/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019486 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019487 */
19488 static void
19489f_taglist(argvars, rettv)
19490 typval_T *argvars;
19491 typval_T *rettv;
19492{
19493 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019494
19495 tag_pattern = get_tv_string(&argvars[0]);
19496
19497 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019498 if (*tag_pattern == NUL)
19499 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019501 if (rettv_list_alloc(rettv) == OK)
19502 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019503}
19504
19505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 * "tempname()" function
19507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019510 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512{
19513 static int x = 'A';
19514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019515 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019516 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517
19518 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19519 * names. Skip 'I' and 'O', they are used for shell redirection. */
19520 do
19521 {
19522 if (x == 'Z')
19523 x = '0';
19524 else if (x == '9')
19525 x = 'A';
19526 else
19527 {
19528#ifdef EBCDIC
19529 if (x == 'I')
19530 x = 'J';
19531 else if (x == 'R')
19532 x = 'S';
19533 else
19534#endif
19535 ++x;
19536 }
19537 } while (x == 'I' || x == 'O');
19538}
19539
19540/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019541 * "test(list)" function: Just checking the walls...
19542 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019543 static void
19544f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019545 typval_T *argvars UNUSED;
19546 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019547{
19548 /* Used for unit testing. Change the code below to your liking. */
19549#if 0
19550 listitem_T *li;
19551 list_T *l;
19552 char_u *bad, *good;
19553
19554 if (argvars[0].v_type != VAR_LIST)
19555 return;
19556 l = argvars[0].vval.v_list;
19557 if (l == NULL)
19558 return;
19559 li = l->lv_first;
19560 if (li == NULL)
19561 return;
19562 bad = get_tv_string(&li->li_tv);
19563 li = li->li_next;
19564 if (li == NULL)
19565 return;
19566 good = get_tv_string(&li->li_tv);
19567 rettv->vval.v_number = test_edit_score(bad, good);
19568#endif
19569}
19570
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019571#ifdef FEAT_FLOAT
19572/*
19573 * "tan()" function
19574 */
19575 static void
19576f_tan(argvars, rettv)
19577 typval_T *argvars;
19578 typval_T *rettv;
19579{
19580 float_T f;
19581
19582 rettv->v_type = VAR_FLOAT;
19583 if (get_float_arg(argvars, &f) == OK)
19584 rettv->vval.v_float = tan(f);
19585 else
19586 rettv->vval.v_float = 0.0;
19587}
19588
19589/*
19590 * "tanh()" function
19591 */
19592 static void
19593f_tanh(argvars, rettv)
19594 typval_T *argvars;
19595 typval_T *rettv;
19596{
19597 float_T f;
19598
19599 rettv->v_type = VAR_FLOAT;
19600 if (get_float_arg(argvars, &f) == OK)
19601 rettv->vval.v_float = tanh(f);
19602 else
19603 rettv->vval.v_float = 0.0;
19604}
19605#endif
19606
Bram Moolenaard52d9742005-08-21 22:20:28 +000019607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 * "tolower(string)" function
19609 */
19610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 typval_T *argvars;
19613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614{
19615 char_u *p;
19616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019617 p = vim_strsave(get_tv_string(&argvars[0]));
19618 rettv->v_type = VAR_STRING;
19619 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620
19621 if (p != NULL)
19622 while (*p != NUL)
19623 {
19624#ifdef FEAT_MBYTE
19625 int l;
19626
19627 if (enc_utf8)
19628 {
19629 int c, lc;
19630
19631 c = utf_ptr2char(p);
19632 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019633 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 /* TODO: reallocate string when byte count changes. */
19635 if (utf_char2len(lc) == l)
19636 utf_char2bytes(lc, p);
19637 p += l;
19638 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019639 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 p += l; /* skip multi-byte character */
19641 else
19642#endif
19643 {
19644 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19645 ++p;
19646 }
19647 }
19648}
19649
19650/*
19651 * "toupper(string)" function
19652 */
19653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019654f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019655 typval_T *argvars;
19656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019658 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019659 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660}
19661
19662/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019663 * "tr(string, fromstr, tostr)" function
19664 */
19665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019666f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019667 typval_T *argvars;
19668 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019669{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019670 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019671 char_u *fromstr;
19672 char_u *tostr;
19673 char_u *p;
19674#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019675 int inlen;
19676 int fromlen;
19677 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019678 int idx;
19679 char_u *cpstr;
19680 int cplen;
19681 int first = TRUE;
19682#endif
19683 char_u buf[NUMBUFLEN];
19684 char_u buf2[NUMBUFLEN];
19685 garray_T ga;
19686
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019687 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019688 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19689 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019690
19691 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692 rettv->v_type = VAR_STRING;
19693 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019694 if (fromstr == NULL || tostr == NULL)
19695 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019696 ga_init2(&ga, (int)sizeof(char), 80);
19697
19698#ifdef FEAT_MBYTE
19699 if (!has_mbyte)
19700#endif
19701 /* not multi-byte: fromstr and tostr must be the same length */
19702 if (STRLEN(fromstr) != STRLEN(tostr))
19703 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019704#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019705error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019706#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019707 EMSG2(_(e_invarg2), fromstr);
19708 ga_clear(&ga);
19709 return;
19710 }
19711
19712 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019713 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019714 {
19715#ifdef FEAT_MBYTE
19716 if (has_mbyte)
19717 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019718 inlen = (*mb_ptr2len)(in_str);
19719 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019720 cplen = inlen;
19721 idx = 0;
19722 for (p = fromstr; *p != NUL; p += fromlen)
19723 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019724 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019725 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019726 {
19727 for (p = tostr; *p != NUL; p += tolen)
19728 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019729 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019730 if (idx-- == 0)
19731 {
19732 cplen = tolen;
19733 cpstr = p;
19734 break;
19735 }
19736 }
19737 if (*p == NUL) /* tostr is shorter than fromstr */
19738 goto error;
19739 break;
19740 }
19741 ++idx;
19742 }
19743
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019744 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019745 {
19746 /* Check that fromstr and tostr have the same number of
19747 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019748 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019749 first = FALSE;
19750 for (p = tostr; *p != NUL; p += tolen)
19751 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019752 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019753 --idx;
19754 }
19755 if (idx != 0)
19756 goto error;
19757 }
19758
Bram Moolenaarcde88542015-08-11 19:14:00 +020019759 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019760 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019761 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019762
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019763 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019764 }
19765 else
19766#endif
19767 {
19768 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019769 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019770 if (p != NULL)
19771 ga_append(&ga, tostr[p - fromstr]);
19772 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019773 ga_append(&ga, *in_str);
19774 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019775 }
19776 }
19777
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019778 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019779 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019780 ga_append(&ga, NUL);
19781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019783}
19784
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019785#ifdef FEAT_FLOAT
19786/*
19787 * "trunc({float})" function
19788 */
19789 static void
19790f_trunc(argvars, rettv)
19791 typval_T *argvars;
19792 typval_T *rettv;
19793{
19794 float_T f;
19795
19796 rettv->v_type = VAR_FLOAT;
19797 if (get_float_arg(argvars, &f) == OK)
19798 /* trunc() is not in C90, use floor() or ceil() instead. */
19799 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19800 else
19801 rettv->vval.v_float = 0.0;
19802}
19803#endif
19804
Bram Moolenaar8299df92004-07-10 09:47:34 +000019805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 * "type(expr)" function
19807 */
19808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 typval_T *argvars;
19811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019813 int n;
19814
19815 switch (argvars[0].v_type)
19816 {
19817 case VAR_NUMBER: n = 0; break;
19818 case VAR_STRING: n = 1; break;
19819 case VAR_FUNC: n = 2; break;
19820 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019821 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019822#ifdef FEAT_FLOAT
19823 case VAR_FLOAT: n = 5; break;
19824#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019825 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19826 }
19827 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828}
19829
19830/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019831 * "undofile(name)" function
19832 */
19833 static void
19834f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019835 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019836 typval_T *rettv;
19837{
19838 rettv->v_type = VAR_STRING;
19839#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019840 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019841 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019842
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019843 if (*fname == NUL)
19844 {
19845 /* If there is no file name there will be no undo file. */
19846 rettv->vval.v_string = NULL;
19847 }
19848 else
19849 {
19850 char_u *ffname = FullName_save(fname, FALSE);
19851
19852 if (ffname != NULL)
19853 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19854 vim_free(ffname);
19855 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019856 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019857#else
19858 rettv->vval.v_string = NULL;
19859#endif
19860}
19861
19862/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019863 * "undotree()" function
19864 */
19865 static void
19866f_undotree(argvars, rettv)
19867 typval_T *argvars UNUSED;
19868 typval_T *rettv;
19869{
19870 if (rettv_dict_alloc(rettv) == OK)
19871 {
19872 dict_T *dict = rettv->vval.v_dict;
19873 list_T *list;
19874
Bram Moolenaar730cde92010-06-27 05:18:54 +020019875 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019876 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019877 dict_add_nr_str(dict, "save_last",
19878 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019879 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19880 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019881 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019882
19883 list = list_alloc();
19884 if (list != NULL)
19885 {
19886 u_eval_tree(curbuf->b_u_oldhead, list);
19887 dict_add_list(dict, "entries", list);
19888 }
19889 }
19890}
19891
19892/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019893 * "values(dict)" function
19894 */
19895 static void
19896f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019897 typval_T *argvars;
19898 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019899{
19900 dict_list(argvars, rettv, 1);
19901}
19902
19903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 * "virtcol(string)" function
19905 */
19906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019907f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 typval_T *argvars;
19909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910{
19911 colnr_T vcol = 0;
19912 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019913 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019915 fp = var2fpos(&argvars[0], FALSE, &fnum);
19916 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19917 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 {
19919 getvvcol(curwin, fp, NULL, NULL, &vcol);
19920 ++vcol;
19921 }
19922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924}
19925
19926/*
19927 * "visualmode()" function
19928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019931 typval_T *argvars UNUSED;
19932 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 char_u str[2];
19935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019936 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 str[0] = curbuf->b_visual_mode_eval;
19938 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019939 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940
19941 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019942 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944}
19945
19946/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019947 * "wildmenumode()" function
19948 */
19949 static void
19950f_wildmenumode(argvars, rettv)
19951 typval_T *argvars UNUSED;
19952 typval_T *rettv UNUSED;
19953{
19954#ifdef FEAT_WILDMENU
19955 if (wild_menu_showing)
19956 rettv->vval.v_number = 1;
19957#endif
19958}
19959
19960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961 * "winbufnr(nr)" function
19962 */
19963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019965 typval_T *argvars;
19966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967{
19968 win_T *wp;
19969
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019970 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975}
19976
19977/*
19978 * "wincol()" function
19979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019981f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019982 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984{
19985 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019986 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987}
19988
19989/*
19990 * "winheight(nr)" function
19991 */
19992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019993f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 typval_T *argvars;
19995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996{
19997 win_T *wp;
19998
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019999 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020003 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004}
20005
20006/*
20007 * "winline()" function
20008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020011 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013{
20014 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016}
20017
20018/*
20019 * "winnr()" function
20020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020023 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025{
20026 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020027
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020029 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020031 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032}
20033
20034/*
20035 * "winrestcmd()" function
20036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020038f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020039 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041{
20042#ifdef FEAT_WINDOWS
20043 win_T *wp;
20044 int winnr = 1;
20045 garray_T ga;
20046 char_u buf[50];
20047
20048 ga_init2(&ga, (int)sizeof(char), 70);
20049 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20050 {
20051 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20052 ga_concat(&ga, buf);
20053# ifdef FEAT_VERTSPLIT
20054 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20055 ga_concat(&ga, buf);
20056# endif
20057 ++winnr;
20058 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020059 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020065 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066}
20067
20068/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020069 * "winrestview()" function
20070 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020071 static void
20072f_winrestview(argvars, rettv)
20073 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020074 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020075{
20076 dict_T *dict;
20077
20078 if (argvars[0].v_type != VAR_DICT
20079 || (dict = argvars[0].vval.v_dict) == NULL)
20080 EMSG(_(e_invarg));
20081 else
20082 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020083 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20084 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20085 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20086 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020087#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020088 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20089 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020090#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020091 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20092 {
20093 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20094 curwin->w_set_curswant = FALSE;
20095 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020096
Bram Moolenaar82c25852014-05-28 16:47:16 +020020097 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20098 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020099#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020100 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20101 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020102#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020103 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20104 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20105 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20106 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020107
20108 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020109 win_new_height(curwin, curwin->w_height);
20110# ifdef FEAT_VERTSPLIT
20111 win_new_width(curwin, W_WIDTH(curwin));
20112# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020113 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020114
Bram Moolenaarb851a962014-10-31 15:45:52 +010020115 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020116 curwin->w_topline = 1;
20117 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20118 curwin->w_topline = curbuf->b_ml.ml_line_count;
20119#ifdef FEAT_DIFF
20120 check_topfill(curwin, TRUE);
20121#endif
20122 }
20123}
20124
20125/*
20126 * "winsaveview()" function
20127 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020128 static void
20129f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020130 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020131 typval_T *rettv;
20132{
20133 dict_T *dict;
20134
Bram Moolenaara800b422010-06-27 01:15:55 +020020135 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020136 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020137 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020138
20139 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20140 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20141#ifdef FEAT_VIRTUALEDIT
20142 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20143#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020144 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020145 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20146
20147 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20148#ifdef FEAT_DIFF
20149 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20150#endif
20151 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20152 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20153}
20154
20155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156 * "winwidth(nr)" function
20157 */
20158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020159f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020160 typval_T *argvars;
20161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162{
20163 win_T *wp;
20164
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020165 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 else
20169#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020170 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173#endif
20174}
20175
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020177 * Write list of strings to file
20178 */
20179 static int
20180write_list(fd, list, binary)
20181 FILE *fd;
20182 list_T *list;
20183 int binary;
20184{
20185 listitem_T *li;
20186 int c;
20187 int ret = OK;
20188 char_u *s;
20189
20190 for (li = list->lv_first; li != NULL; li = li->li_next)
20191 {
20192 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20193 {
20194 if (*s == '\n')
20195 c = putc(NUL, fd);
20196 else
20197 c = putc(*s, fd);
20198 if (c == EOF)
20199 {
20200 ret = FAIL;
20201 break;
20202 }
20203 }
20204 if (!binary || li->li_next != NULL)
20205 if (putc('\n', fd) == EOF)
20206 {
20207 ret = FAIL;
20208 break;
20209 }
20210 if (ret == FAIL)
20211 {
20212 EMSG(_(e_write));
20213 break;
20214 }
20215 }
20216 return ret;
20217}
20218
20219/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020220 * "writefile()" function
20221 */
20222 static void
20223f_writefile(argvars, rettv)
20224 typval_T *argvars;
20225 typval_T *rettv;
20226{
20227 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020228 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020229 char_u *fname;
20230 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020231 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020232
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020233 if (check_restricted() || check_secure())
20234 return;
20235
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020236 if (argvars[0].v_type != VAR_LIST)
20237 {
20238 EMSG2(_(e_listarg), "writefile()");
20239 return;
20240 }
20241 if (argvars[0].vval.v_list == NULL)
20242 return;
20243
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020244 if (argvars[2].v_type != VAR_UNKNOWN)
20245 {
20246 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20247 binary = TRUE;
20248 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20249 append = TRUE;
20250 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020251
20252 /* Always open the file in binary mode, library functions have a mind of
20253 * their own about CR-LF conversion. */
20254 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020255 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20256 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020257 {
20258 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20259 ret = -1;
20260 }
20261 else
20262 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020263 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20264 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020265 fclose(fd);
20266 }
20267
20268 rettv->vval.v_number = ret;
20269}
20270
20271/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020272 * "xor(expr, expr)" function
20273 */
20274 static void
20275f_xor(argvars, rettv)
20276 typval_T *argvars;
20277 typval_T *rettv;
20278{
20279 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20280 ^ get_tv_number_chk(&argvars[1], NULL);
20281}
20282
20283
20284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020286 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 */
20288 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020289var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020290 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020291 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020292 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020294 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020296 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297
Bram Moolenaara5525202006-03-02 22:52:09 +000020298 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020299 if (varp->v_type == VAR_LIST)
20300 {
20301 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020302 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020303 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020304 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020305
20306 l = varp->vval.v_list;
20307 if (l == NULL)
20308 return NULL;
20309
20310 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020311 pos.lnum = list_find_nr(l, 0L, &error);
20312 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020313 return NULL; /* invalid line number */
20314
20315 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020316 pos.col = list_find_nr(l, 1L, &error);
20317 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020318 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020319 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020320
20321 /* We accept "$" for the column number: last column. */
20322 li = list_find(l, 1L);
20323 if (li != NULL && li->li_tv.v_type == VAR_STRING
20324 && li->li_tv.vval.v_string != NULL
20325 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20326 pos.col = len + 1;
20327
Bram Moolenaara5525202006-03-02 22:52:09 +000020328 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020329 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020330 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020331 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020332
Bram Moolenaara5525202006-03-02 22:52:09 +000020333#ifdef FEAT_VIRTUALEDIT
20334 /* Get the virtual offset. Defaults to zero. */
20335 pos.coladd = list_find_nr(l, 2L, &error);
20336 if (error)
20337 pos.coladd = 0;
20338#endif
20339
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020340 return &pos;
20341 }
20342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020343 name = get_tv_string_chk(varp);
20344 if (name == NULL)
20345 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020346 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020348 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20349 {
20350 if (VIsual_active)
20351 return &VIsual;
20352 return &curwin->w_cursor;
20353 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020354 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020356 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20358 return NULL;
20359 return pp;
20360 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020361
20362#ifdef FEAT_VIRTUALEDIT
20363 pos.coladd = 0;
20364#endif
20365
Bram Moolenaar477933c2007-07-17 14:32:23 +000020366 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020367 {
20368 pos.col = 0;
20369 if (name[1] == '0') /* "w0": first visible line */
20370 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020371 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020372 pos.lnum = curwin->w_topline;
20373 return &pos;
20374 }
20375 else if (name[1] == '$') /* "w$": last visible line */
20376 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020377 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020378 pos.lnum = curwin->w_botline - 1;
20379 return &pos;
20380 }
20381 }
20382 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020384 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 {
20386 pos.lnum = curbuf->b_ml.ml_line_count;
20387 pos.col = 0;
20388 }
20389 else
20390 {
20391 pos.lnum = curwin->w_cursor.lnum;
20392 pos.col = (colnr_T)STRLEN(ml_get_curline());
20393 }
20394 return &pos;
20395 }
20396 return NULL;
20397}
20398
20399/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020400 * Convert list in "arg" into a position and optional file number.
20401 * When "fnump" is NULL there is no file number, only 3 items.
20402 * Note that the column is passed on as-is, the caller may want to decrement
20403 * it to use 1 for the first column.
20404 * Return FAIL when conversion is not possible, doesn't check the position for
20405 * validity.
20406 */
20407 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020408list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020409 typval_T *arg;
20410 pos_T *posp;
20411 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020412 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020413{
20414 list_T *l = arg->vval.v_list;
20415 long i = 0;
20416 long n;
20417
Bram Moolenaar493c1782014-05-28 14:34:46 +020020418 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20419 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020420 if (arg->v_type != VAR_LIST
20421 || l == NULL
20422 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020423 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020424 return FAIL;
20425
20426 if (fnump != NULL)
20427 {
20428 n = list_find_nr(l, i++, NULL); /* fnum */
20429 if (n < 0)
20430 return FAIL;
20431 if (n == 0)
20432 n = curbuf->b_fnum; /* current buffer */
20433 *fnump = n;
20434 }
20435
20436 n = list_find_nr(l, i++, NULL); /* lnum */
20437 if (n < 0)
20438 return FAIL;
20439 posp->lnum = n;
20440
20441 n = list_find_nr(l, i++, NULL); /* col */
20442 if (n < 0)
20443 return FAIL;
20444 posp->col = n;
20445
20446#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020447 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020448 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020449 posp->coladd = 0;
20450 else
20451 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020452#endif
20453
Bram Moolenaar493c1782014-05-28 14:34:46 +020020454 if (curswantp != NULL)
20455 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20456
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020457 return OK;
20458}
20459
20460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 * Get the length of an environment variable name.
20462 * Advance "arg" to the first character after the name.
20463 * Return 0 for error.
20464 */
20465 static int
20466get_env_len(arg)
20467 char_u **arg;
20468{
20469 char_u *p;
20470 int len;
20471
20472 for (p = *arg; vim_isIDc(*p); ++p)
20473 ;
20474 if (p == *arg) /* no name found */
20475 return 0;
20476
20477 len = (int)(p - *arg);
20478 *arg = p;
20479 return len;
20480}
20481
20482/*
20483 * Get the length of the name of a function or internal variable.
20484 * "arg" is advanced to the first non-white character after the name.
20485 * Return 0 if something is wrong.
20486 */
20487 static int
20488get_id_len(arg)
20489 char_u **arg;
20490{
20491 char_u *p;
20492 int len;
20493
20494 /* Find the end of the name. */
20495 for (p = *arg; eval_isnamec(*p); ++p)
20496 ;
20497 if (p == *arg) /* no name found */
20498 return 0;
20499
20500 len = (int)(p - *arg);
20501 *arg = skipwhite(p);
20502
20503 return len;
20504}
20505
20506/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020507 * Get the length of the name of a variable or function.
20508 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020510 * Return -1 if curly braces expansion failed.
20511 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 * If the name contains 'magic' {}'s, expand them and return the
20513 * expanded name in an allocated string via 'alias' - caller must free.
20514 */
20515 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020516get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 char_u **arg;
20518 char_u **alias;
20519 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020520 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521{
20522 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 char_u *p;
20524 char_u *expr_start;
20525 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526
20527 *alias = NULL; /* default to no alias */
20528
20529 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20530 && (*arg)[2] == (int)KE_SNR)
20531 {
20532 /* hard coded <SNR>, already translated */
20533 *arg += 3;
20534 return get_id_len(arg) + 3;
20535 }
20536 len = eval_fname_script(*arg);
20537 if (len > 0)
20538 {
20539 /* literal "<SID>", "s:" or "<SNR>" */
20540 *arg += len;
20541 }
20542
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020544 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020546 p = find_name_end(*arg, &expr_start, &expr_end,
20547 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 if (expr_start != NULL)
20549 {
20550 char_u *temp_string;
20551
20552 if (!evaluate)
20553 {
20554 len += (int)(p - *arg);
20555 *arg = skipwhite(p);
20556 return len;
20557 }
20558
20559 /*
20560 * Include any <SID> etc in the expanded string:
20561 * Thus the -len here.
20562 */
20563 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20564 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020565 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 *alias = temp_string;
20567 *arg = skipwhite(p);
20568 return (int)STRLEN(temp_string);
20569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570
20571 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020572 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 EMSG2(_(e_invexpr2), *arg);
20574
20575 return len;
20576}
20577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020578/*
20579 * Find the end of a variable or function name, taking care of magic braces.
20580 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20581 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020582 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020583 * Return a pointer to just after the name. Equal to "arg" if there is no
20584 * valid name.
20585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020587find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 char_u *arg;
20589 char_u **expr_start;
20590 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020591 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020593 int mb_nest = 0;
20594 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595 char_u *p;
20596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020597 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020599 *expr_start = NULL;
20600 *expr_end = NULL;
20601 }
20602
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020603 /* Quick check for valid starting character. */
20604 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20605 return arg;
20606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020607 for (p = arg; *p != NUL
20608 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020609 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020610 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020612 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020613 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020614 if (*p == '\'')
20615 {
20616 /* skip over 'string' to avoid counting [ and ] inside it. */
20617 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20618 ;
20619 if (*p == NUL)
20620 break;
20621 }
20622 else if (*p == '"')
20623 {
20624 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20625 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20626 if (*p == '\\' && p[1] != NUL)
20627 ++p;
20628 if (*p == NUL)
20629 break;
20630 }
20631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634 if (*p == '[')
20635 ++br_nest;
20636 else if (*p == ']')
20637 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020640 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020642 if (*p == '{')
20643 {
20644 mb_nest++;
20645 if (expr_start != NULL && *expr_start == NULL)
20646 *expr_start = p;
20647 }
20648 else if (*p == '}')
20649 {
20650 mb_nest--;
20651 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20652 *expr_end = p;
20653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 }
20656
20657 return p;
20658}
20659
20660/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020661 * Expands out the 'magic' {}'s in a variable/function name.
20662 * Note that this can call itself recursively, to deal with
20663 * constructs like foo{bar}{baz}{bam}
20664 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20665 * "in_start" ^
20666 * "expr_start" ^
20667 * "expr_end" ^
20668 * "in_end" ^
20669 *
20670 * Returns a new allocated string, which the caller must free.
20671 * Returns NULL for failure.
20672 */
20673 static char_u *
20674make_expanded_name(in_start, expr_start, expr_end, in_end)
20675 char_u *in_start;
20676 char_u *expr_start;
20677 char_u *expr_end;
20678 char_u *in_end;
20679{
20680 char_u c1;
20681 char_u *retval = NULL;
20682 char_u *temp_result;
20683 char_u *nextcmd = NULL;
20684
20685 if (expr_end == NULL || in_end == NULL)
20686 return NULL;
20687 *expr_start = NUL;
20688 *expr_end = NUL;
20689 c1 = *in_end;
20690 *in_end = NUL;
20691
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020692 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020693 if (temp_result != NULL && nextcmd == NULL)
20694 {
20695 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20696 + (in_end - expr_end) + 1));
20697 if (retval != NULL)
20698 {
20699 STRCPY(retval, in_start);
20700 STRCAT(retval, temp_result);
20701 STRCAT(retval, expr_end + 1);
20702 }
20703 }
20704 vim_free(temp_result);
20705
20706 *in_end = c1; /* put char back for error messages */
20707 *expr_start = '{';
20708 *expr_end = '}';
20709
20710 if (retval != NULL)
20711 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020712 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020713 if (expr_start != NULL)
20714 {
20715 /* Further expansion! */
20716 temp_result = make_expanded_name(retval, expr_start,
20717 expr_end, temp_result);
20718 vim_free(retval);
20719 retval = temp_result;
20720 }
20721 }
20722
20723 return retval;
20724}
20725
20726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020728 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 */
20730 static int
20731eval_isnamec(c)
20732 int c;
20733{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020734 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20735}
20736
20737/*
20738 * Return TRUE if character "c" can be used as the first character in a
20739 * variable or function name (excluding '{' and '}').
20740 */
20741 static int
20742eval_isnamec1(c)
20743 int c;
20744{
20745 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746}
20747
20748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 * Set number v: variable to "val".
20750 */
20751 void
20752set_vim_var_nr(idx, val)
20753 int idx;
20754 long val;
20755{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020756 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757}
20758
20759/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020760 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 */
20762 long
20763get_vim_var_nr(idx)
20764 int idx;
20765{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020766 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767}
20768
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020769/*
20770 * Get string v: variable value. Uses a static buffer, can only be used once.
20771 */
20772 char_u *
20773get_vim_var_str(idx)
20774 int idx;
20775{
20776 return get_tv_string(&vimvars[idx].vv_tv);
20777}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020778
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020780 * Get List v: variable value. Caller must take care of reference count when
20781 * needed.
20782 */
20783 list_T *
20784get_vim_var_list(idx)
20785 int idx;
20786{
20787 return vimvars[idx].vv_list;
20788}
20789
20790/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020791 * Set v:char to character "c".
20792 */
20793 void
20794set_vim_var_char(c)
20795 int c;
20796{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020797 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020798
20799#ifdef FEAT_MBYTE
20800 if (has_mbyte)
20801 buf[(*mb_char2bytes)(c, buf)] = NUL;
20802 else
20803#endif
20804 {
20805 buf[0] = c;
20806 buf[1] = NUL;
20807 }
20808 set_vim_var_string(VV_CHAR, buf, -1);
20809}
20810
20811/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020812 * Set v:count to "count" and v:count1 to "count1".
20813 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 */
20815 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020816set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 long count;
20818 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020819 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020821 if (set_prevcount)
20822 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020823 vimvars[VV_COUNT].vv_nr = count;
20824 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825}
20826
20827/*
20828 * Set string v: variable to a copy of "val".
20829 */
20830 void
20831set_vim_var_string(idx, val, len)
20832 int idx;
20833 char_u *val;
20834 int len; /* length of "val" to use or -1 (whole string) */
20835{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020836 /* Need to do this (at least) once, since we can't initialize a union.
20837 * Will always be invoked when "v:progname" is set. */
20838 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20839
Bram Moolenaare9a41262005-01-15 22:18:47 +000020840 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020842 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020844 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020846 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847}
20848
20849/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020850 * Set List v: variable to "val".
20851 */
20852 void
20853set_vim_var_list(idx, val)
20854 int idx;
20855 list_T *val;
20856{
20857 list_unref(vimvars[idx].vv_list);
20858 vimvars[idx].vv_list = val;
20859 if (val != NULL)
20860 ++val->lv_refcount;
20861}
20862
20863/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020864 * Set Dictionary v: variable to "val".
20865 */
20866 void
20867set_vim_var_dict(idx, val)
20868 int idx;
20869 dict_T *val;
20870{
20871 int todo;
20872 hashitem_T *hi;
20873
20874 dict_unref(vimvars[idx].vv_dict);
20875 vimvars[idx].vv_dict = val;
20876 if (val != NULL)
20877 {
20878 ++val->dv_refcount;
20879
20880 /* Set readonly */
20881 todo = (int)val->dv_hashtab.ht_used;
20882 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20883 {
20884 if (HASHITEM_EMPTY(hi))
20885 continue;
20886 --todo;
20887 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20888 }
20889 }
20890}
20891
20892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 * Set v:register if needed.
20894 */
20895 void
20896set_reg_var(c)
20897 int c;
20898{
20899 char_u regname;
20900
20901 if (c == 0 || c == ' ')
20902 regname = '"';
20903 else
20904 regname = c;
20905 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020906 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 set_vim_var_string(VV_REG, &regname, 1);
20908}
20909
20910/*
20911 * Get or set v:exception. If "oldval" == NULL, return the current value.
20912 * Otherwise, restore the value to "oldval" and return NULL.
20913 * Must always be called in pairs to save and restore v:exception! Does not
20914 * take care of memory allocations.
20915 */
20916 char_u *
20917v_exception(oldval)
20918 char_u *oldval;
20919{
20920 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020921 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922
Bram Moolenaare9a41262005-01-15 22:18:47 +000020923 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 return NULL;
20925}
20926
20927/*
20928 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20929 * Otherwise, restore the value to "oldval" and return NULL.
20930 * Must always be called in pairs to save and restore v:throwpoint! Does not
20931 * take care of memory allocations.
20932 */
20933 char_u *
20934v_throwpoint(oldval)
20935 char_u *oldval;
20936{
20937 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020938 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939
Bram Moolenaare9a41262005-01-15 22:18:47 +000020940 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 return NULL;
20942}
20943
20944#if defined(FEAT_AUTOCMD) || defined(PROTO)
20945/*
20946 * Set v:cmdarg.
20947 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20948 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20949 * Must always be called in pairs!
20950 */
20951 char_u *
20952set_cmdarg(eap, oldarg)
20953 exarg_T *eap;
20954 char_u *oldarg;
20955{
20956 char_u *oldval;
20957 char_u *newval;
20958 unsigned len;
20959
Bram Moolenaare9a41262005-01-15 22:18:47 +000020960 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020961 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020963 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020964 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020965 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 }
20967
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020968 if (eap->force_bin == FORCE_BIN)
20969 len = 6;
20970 else if (eap->force_bin == FORCE_NOBIN)
20971 len = 8;
20972 else
20973 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020974
20975 if (eap->read_edit)
20976 len += 7;
20977
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020978 if (eap->force_ff != 0)
20979 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20980# ifdef FEAT_MBYTE
20981 if (eap->force_enc != 0)
20982 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020983 if (eap->bad_char != 0)
20984 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020985# endif
20986
20987 newval = alloc(len + 1);
20988 if (newval == NULL)
20989 return NULL;
20990
20991 if (eap->force_bin == FORCE_BIN)
20992 sprintf((char *)newval, " ++bin");
20993 else if (eap->force_bin == FORCE_NOBIN)
20994 sprintf((char *)newval, " ++nobin");
20995 else
20996 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020997
20998 if (eap->read_edit)
20999 STRCAT(newval, " ++edit");
21000
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021001 if (eap->force_ff != 0)
21002 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21003 eap->cmd + eap->force_ff);
21004# ifdef FEAT_MBYTE
21005 if (eap->force_enc != 0)
21006 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21007 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021008 if (eap->bad_char == BAD_KEEP)
21009 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21010 else if (eap->bad_char == BAD_DROP)
21011 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21012 else if (eap->bad_char != 0)
21013 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021014# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021015 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021016 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017}
21018#endif
21019
21020/*
21021 * Get the value of internal variable "name".
21022 * Return OK or FAIL.
21023 */
21024 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021025get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 char_u *name;
21027 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021028 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021029 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021030 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021031 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032{
21033 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021034 typval_T *tv = NULL;
21035 typval_T atv;
21036 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038
21039 /* truncate the name, so that we can use strcmp() */
21040 cc = name[len];
21041 name[len] = NUL;
21042
21043 /*
21044 * Check for "b:changedtick".
21045 */
21046 if (STRCMP(name, "b:changedtick") == 0)
21047 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021048 atv.v_type = VAR_NUMBER;
21049 atv.vval.v_number = curbuf->b_changedtick;
21050 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021051 }
21052
21053 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 * Check for user-defined variables.
21055 */
21056 else
21057 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021058 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021060 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021061 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021062 if (dip != NULL)
21063 *dip = v;
21064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 }
21066
Bram Moolenaare9a41262005-01-15 22:18:47 +000021067 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021069 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021070 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 ret = FAIL;
21072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021073 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021074 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075
21076 name[len] = cc;
21077
21078 return ret;
21079}
21080
21081/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021082 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21083 * Also handle function call with Funcref variable: func(expr)
21084 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21085 */
21086 static int
21087handle_subscript(arg, rettv, evaluate, verbose)
21088 char_u **arg;
21089 typval_T *rettv;
21090 int evaluate; /* do more than finding the end */
21091 int verbose; /* give error messages */
21092{
21093 int ret = OK;
21094 dict_T *selfdict = NULL;
21095 char_u *s;
21096 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021097 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021098
21099 while (ret == OK
21100 && (**arg == '['
21101 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021102 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021103 && !vim_iswhite(*(*arg - 1)))
21104 {
21105 if (**arg == '(')
21106 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021107 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021108 if (evaluate)
21109 {
21110 functv = *rettv;
21111 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021112
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021113 /* Invoke the function. Recursive! */
21114 s = functv.vval.v_string;
21115 }
21116 else
21117 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021118 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021119 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21120 &len, evaluate, selfdict);
21121
21122 /* Clear the funcref afterwards, so that deleting it while
21123 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021124 if (evaluate)
21125 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021126
21127 /* Stop the expression evaluation when immediately aborting on
21128 * error, or when an interrupt occurred or an exception was thrown
21129 * but not caught. */
21130 if (aborting())
21131 {
21132 if (ret == OK)
21133 clear_tv(rettv);
21134 ret = FAIL;
21135 }
21136 dict_unref(selfdict);
21137 selfdict = NULL;
21138 }
21139 else /* **arg == '[' || **arg == '.' */
21140 {
21141 dict_unref(selfdict);
21142 if (rettv->v_type == VAR_DICT)
21143 {
21144 selfdict = rettv->vval.v_dict;
21145 if (selfdict != NULL)
21146 ++selfdict->dv_refcount;
21147 }
21148 else
21149 selfdict = NULL;
21150 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21151 {
21152 clear_tv(rettv);
21153 ret = FAIL;
21154 }
21155 }
21156 }
21157 dict_unref(selfdict);
21158 return ret;
21159}
21160
21161/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021162 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021163 * value).
21164 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021165 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021166alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021167{
Bram Moolenaar33570922005-01-25 22:26:29 +000021168 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021169}
21170
21171/*
21172 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 * The string "s" must have been allocated, it is consumed.
21174 * Return NULL for out of memory, the variable otherwise.
21175 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021176 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021177alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 char_u *s;
21179{
Bram Moolenaar33570922005-01-25 22:26:29 +000021180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021182 rettv = alloc_tv();
21183 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 rettv->v_type = VAR_STRING;
21186 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 }
21188 else
21189 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021190 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191}
21192
21193/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021194 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021196 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021197free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021198 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199{
21200 if (varp != NULL)
21201 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021202 switch (varp->v_type)
21203 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021204 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021205 func_unref(varp->vval.v_string);
21206 /*FALLTHROUGH*/
21207 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021208 vim_free(varp->vval.v_string);
21209 break;
21210 case VAR_LIST:
21211 list_unref(varp->vval.v_list);
21212 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021213 case VAR_DICT:
21214 dict_unref(varp->vval.v_dict);
21215 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021216 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021217#ifdef FEAT_FLOAT
21218 case VAR_FLOAT:
21219#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021220 case VAR_UNKNOWN:
21221 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021222 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021223 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021224 break;
21225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 vim_free(varp);
21227 }
21228}
21229
21230/*
21231 * Free the memory for a variable value and set the value to NULL or 0.
21232 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021233 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021234clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021235 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236{
21237 if (varp != NULL)
21238 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021239 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021241 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021242 func_unref(varp->vval.v_string);
21243 /*FALLTHROUGH*/
21244 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021245 vim_free(varp->vval.v_string);
21246 varp->vval.v_string = NULL;
21247 break;
21248 case VAR_LIST:
21249 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021250 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021251 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021252 case VAR_DICT:
21253 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021254 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021255 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021256 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021257 varp->vval.v_number = 0;
21258 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021259#ifdef FEAT_FLOAT
21260 case VAR_FLOAT:
21261 varp->vval.v_float = 0.0;
21262 break;
21263#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021264 case VAR_UNKNOWN:
21265 break;
21266 default:
21267 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021269 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 }
21271}
21272
21273/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021274 * Set the value of a variable to NULL without freeing items.
21275 */
21276 static void
21277init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021278 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021279{
21280 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021282}
21283
21284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285 * Get the number value of a variable.
21286 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021287 * For incompatible types, return 0.
21288 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21289 * caller of incompatible types: it sets *denote to TRUE if "denote"
21290 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 */
21292 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021293get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021294 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021296 int error = FALSE;
21297
21298 return get_tv_number_chk(varp, &error); /* return 0L on error */
21299}
21300
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021301 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021302get_tv_number_chk(varp, denote)
21303 typval_T *varp;
21304 int *denote;
21305{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021306 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021308 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021310 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021311 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021312#ifdef FEAT_FLOAT
21313 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021314 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021315 break;
21316#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021317 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021318 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021319 break;
21320 case VAR_STRING:
21321 if (varp->vval.v_string != NULL)
21322 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021323 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021324 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021325 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021326 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021327 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021328 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021329 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021330 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021331 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021332 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021335 if (denote == NULL) /* useful for values that must be unsigned */
21336 n = -1;
21337 else
21338 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021339 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340}
21341
21342/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021343 * Get the lnum from the first argument.
21344 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021345 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 */
21347 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021348get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021349 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350{
Bram Moolenaar33570922005-01-25 22:26:29 +000021351 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 linenr_T lnum;
21353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021354 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 if (lnum == 0) /* no valid number, try using line() */
21356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021357 rettv.v_type = VAR_NUMBER;
21358 f_line(argvars, &rettv);
21359 lnum = rettv.vval.v_number;
21360 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 }
21362 return lnum;
21363}
21364
21365/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021366 * Get the lnum from the first argument.
21367 * Also accepts "$", then "buf" is used.
21368 * Returns 0 on error.
21369 */
21370 static linenr_T
21371get_tv_lnum_buf(argvars, buf)
21372 typval_T *argvars;
21373 buf_T *buf;
21374{
21375 if (argvars[0].v_type == VAR_STRING
21376 && argvars[0].vval.v_string != NULL
21377 && argvars[0].vval.v_string[0] == '$'
21378 && buf != NULL)
21379 return buf->b_ml.ml_line_count;
21380 return get_tv_number_chk(&argvars[0], NULL);
21381}
21382
21383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 * Get the string value of a variable.
21385 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021386 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21387 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 * If the String variable has never been set, return an empty string.
21389 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021390 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21391 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 */
21393 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021394get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021395 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396{
21397 static char_u mybuf[NUMBUFLEN];
21398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021399 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400}
21401
21402 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021403get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021404 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021405 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021407 char_u *res = get_tv_string_buf_chk(varp, buf);
21408
21409 return res != NULL ? res : (char_u *)"";
21410}
21411
Bram Moolenaar7d647822014-04-05 21:28:56 +020021412/*
21413 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21414 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021415 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021416get_tv_string_chk(varp)
21417 typval_T *varp;
21418{
21419 static char_u mybuf[NUMBUFLEN];
21420
21421 return get_tv_string_buf_chk(varp, mybuf);
21422}
21423
21424 static char_u *
21425get_tv_string_buf_chk(varp, buf)
21426 typval_T *varp;
21427 char_u *buf;
21428{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021429 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021431 case VAR_NUMBER:
21432 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21433 return buf;
21434 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021435 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021436 break;
21437 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021438 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021439 break;
21440 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021441 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021442 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021443#ifdef FEAT_FLOAT
21444 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021445 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021446 break;
21447#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021448 case VAR_STRING:
21449 if (varp->vval.v_string != NULL)
21450 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021451 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021452 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021453 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021454 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021456 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457}
21458
21459/*
21460 * Find variable "name" in the list of variables.
21461 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021462 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021463 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021464 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021467find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021469 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021470 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474
Bram Moolenaara7043832005-01-21 11:56:39 +000021475 ht = find_var_ht(name, &varname);
21476 if (htp != NULL)
21477 *htp = ht;
21478 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021480 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481}
21482
21483/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021484 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021485 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021487 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021488find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021490 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021491 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021492 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021493{
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 hashitem_T *hi;
21495
21496 if (*varname == NUL)
21497 {
21498 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021499 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021500 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021501 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021502 case 'g': return &globvars_var;
21503 case 'v': return &vimvars_var;
21504 case 'b': return &curbuf->b_bufvar;
21505 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021506#ifdef FEAT_WINDOWS
21507 case 't': return &curtab->tp_winvar;
21508#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021509 case 'l': return current_funccal == NULL
21510 ? NULL : &current_funccal->l_vars_var;
21511 case 'a': return current_funccal == NULL
21512 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021513 }
21514 return NULL;
21515 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021516
21517 hi = hash_find(ht, varname);
21518 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021519 {
21520 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021521 * worked find the variable again. Don't auto-load a script if it was
21522 * loaded already, otherwise it would be loaded every time when
21523 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021524 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021525 {
21526 /* Note: script_autoload() may make "hi" invalid. It must either
21527 * be obtained again or not used. */
21528 if (!script_autoload(varname, FALSE) || aborting())
21529 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021530 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021531 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021532 if (HASHITEM_EMPTY(hi))
21533 return NULL;
21534 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021536}
21537
21538/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021539 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021540 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021541 * Set "varname" to the start of name without ':'.
21542 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021543 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021544find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545 char_u *name;
21546 char_u **varname;
21547{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021548 hashitem_T *hi;
21549
Bram Moolenaar73627d02015-08-11 15:46:09 +020021550 if (name[0] == NUL)
21551 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 if (name[1] != ':')
21553 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021554 /* The name must not start with a colon or #. */
21555 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 return NULL;
21557 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021558
21559 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021560 hi = hash_find(&compat_hashtab, name);
21561 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021562 return &compat_hashtab;
21563
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021565 return &globvarht; /* global variable */
21566 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 }
21568 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021569 if (*name == 'g') /* global variable */
21570 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021571 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21572 */
21573 if (vim_strchr(name + 2, ':') != NULL
21574 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021575 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021577 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021579 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021580#ifdef FEAT_WINDOWS
21581 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021582 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021583#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021584 if (*name == 'v') /* v: variable */
21585 return &vimvarht;
21586 if (*name == 'a' && current_funccal != NULL) /* function argument */
21587 return &current_funccal->l_avars.dv_hashtab;
21588 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21589 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 if (*name == 's' /* script variable */
21591 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21592 return &SCRIPT_VARS(current_SID);
21593 return NULL;
21594}
21595
21596/*
21597 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021598 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 * Returns NULL when it doesn't exist.
21600 */
21601 char_u *
21602get_var_value(name)
21603 char_u *name;
21604{
Bram Moolenaar33570922005-01-25 22:26:29 +000021605 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021607 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 if (v == NULL)
21609 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021610 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611}
21612
21613/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021614 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 * sourcing this script and when executing functions defined in the script.
21616 */
21617 void
21618new_script_vars(id)
21619 scid_T id;
21620{
Bram Moolenaara7043832005-01-21 11:56:39 +000021621 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021622 hashtab_T *ht;
21623 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021624
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21626 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021627 /* Re-allocating ga_data means that an ht_array pointing to
21628 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021629 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021630 for (i = 1; i <= ga_scripts.ga_len; ++i)
21631 {
21632 ht = &SCRIPT_VARS(i);
21633 if (ht->ht_mask == HT_INIT_SIZE - 1)
21634 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021635 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021637 }
21638
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 while (ga_scripts.ga_len < id)
21640 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021641 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021642 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021643 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 }
21646 }
21647}
21648
21649/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021650 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21651 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 */
21653 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021654init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021655 dict_T *dict;
21656 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021657 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658{
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021660 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021661 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021662 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021663 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 dict_var->di_tv.vval.v_dict = dict;
21665 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021666 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21668 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669}
21670
21671/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021672 * Unreference a dictionary initialized by init_var_dict().
21673 */
21674 void
21675unref_var_dict(dict)
21676 dict_T *dict;
21677{
21678 /* Now the dict needs to be freed if no one else is using it, go back to
21679 * normal reference counting. */
21680 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21681 dict_unref(dict);
21682}
21683
21684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021686 * Frees all allocated variables and the value they contain.
21687 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688 */
21689 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021690vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021691 hashtab_T *ht;
21692{
21693 vars_clear_ext(ht, TRUE);
21694}
21695
21696/*
21697 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21698 */
21699 static void
21700vars_clear_ext(ht, free_val)
21701 hashtab_T *ht;
21702 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703{
Bram Moolenaara7043832005-01-21 11:56:39 +000021704 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021705 hashitem_T *hi;
21706 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707
Bram Moolenaar33570922005-01-25 22:26:29 +000021708 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021709 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021710 for (hi = ht->ht_array; todo > 0; ++hi)
21711 {
21712 if (!HASHITEM_EMPTY(hi))
21713 {
21714 --todo;
21715
Bram Moolenaar33570922005-01-25 22:26:29 +000021716 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021717 * ht_array might change then. hash_clear() takes care of it
21718 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 v = HI2DI(hi);
21720 if (free_val)
21721 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021722 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021723 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021724 }
21725 }
21726 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021727 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728}
21729
Bram Moolenaara7043832005-01-21 11:56:39 +000021730/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021731 * Delete a variable from hashtab "ht" at item "hi".
21732 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021735delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 hashtab_T *ht;
21737 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738{
Bram Moolenaar33570922005-01-25 22:26:29 +000021739 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021740
21741 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021742 clear_tv(&di->di_tv);
21743 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744}
21745
21746/*
21747 * List the value of one internal variable.
21748 */
21749 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021750list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021753 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021755 char_u *tofree;
21756 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021757 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021758
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021759 current_copyID += COPYID_INC;
21760 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021761 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021762 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021763 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764}
21765
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021767list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 char_u *prefix;
21769 char_u *name;
21770 int type;
21771 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021772 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773{
Bram Moolenaar31859182007-08-14 20:41:13 +000021774 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21775 msg_start();
21776 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 if (name != NULL) /* "a:" vars don't have a name stored */
21778 msg_puts(name);
21779 msg_putchar(' ');
21780 msg_advance(22);
21781 if (type == VAR_NUMBER)
21782 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021783 else if (type == VAR_FUNC)
21784 msg_putchar('*');
21785 else if (type == VAR_LIST)
21786 {
21787 msg_putchar('[');
21788 if (*string == '[')
21789 ++string;
21790 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021791 else if (type == VAR_DICT)
21792 {
21793 msg_putchar('{');
21794 if (*string == '{')
21795 ++string;
21796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 else
21798 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021799
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021801
21802 if (type == VAR_FUNC)
21803 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021804 if (*first)
21805 {
21806 msg_clr_eos();
21807 *first = FALSE;
21808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809}
21810
21811/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021812 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 * If the variable already exists, the value is updated.
21814 * Otherwise the variable is created.
21815 */
21816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021817set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021819 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021820 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821{
Bram Moolenaar33570922005-01-25 22:26:29 +000021822 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021826 ht = find_var_ht(name, &varname);
21827 if (ht == NULL || *varname == NUL)
21828 {
21829 EMSG2(_(e_illvar), name);
21830 return;
21831 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021832 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021833
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021834 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21835 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021836
Bram Moolenaar33570922005-01-25 22:26:29 +000021837 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021840 if (var_check_ro(v->di_flags, name, FALSE)
21841 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 return;
21843 if (v->di_tv.v_type != tv->v_type
21844 && !((v->di_tv.v_type == VAR_STRING
21845 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021846 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021847 || tv->v_type == VAR_NUMBER))
21848#ifdef FEAT_FLOAT
21849 && !((v->di_tv.v_type == VAR_NUMBER
21850 || v->di_tv.v_type == VAR_FLOAT)
21851 && (tv->v_type == VAR_NUMBER
21852 || tv->v_type == VAR_FLOAT))
21853#endif
21854 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021855 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021856 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021857 return;
21858 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021859
21860 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021861 * Handle setting internal v: variables separately where needed to
21862 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 */
21864 if (ht == &vimvarht)
21865 {
21866 if (v->di_tv.v_type == VAR_STRING)
21867 {
21868 vim_free(v->di_tv.vval.v_string);
21869 if (copy || tv->v_type != VAR_STRING)
21870 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21871 else
21872 {
21873 /* Take over the string to avoid an extra alloc/free. */
21874 v->di_tv.vval.v_string = tv->vval.v_string;
21875 tv->vval.v_string = NULL;
21876 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021877 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021878 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021879 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021880 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021881 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021882 if (STRCMP(varname, "searchforward") == 0)
21883 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021884#ifdef FEAT_SEARCH_EXTRA
21885 else if (STRCMP(varname, "hlsearch") == 0)
21886 {
21887 no_hlsearch = !v->di_tv.vval.v_number;
21888 redraw_all_later(SOME_VALID);
21889 }
21890#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021891 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021892 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021893 else if (v->di_tv.v_type != tv->v_type)
21894 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 }
21896
21897 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 }
21899 else /* add a new variable */
21900 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021901 /* Can't add "v:" variable. */
21902 if (ht == &vimvarht)
21903 {
21904 EMSG2(_(e_illvar), name);
21905 return;
21906 }
21907
Bram Moolenaar92124a32005-06-17 22:03:40 +000021908 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021909 if (!valid_varname(varname))
21910 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021912 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21913 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021914 if (v == NULL)
21915 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021916 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021917 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021919 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 return;
21921 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021922 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021924
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021925 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021926 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021927 else
21928 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021930 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021931 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933}
21934
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021935/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021936 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021937 * Also give an error message.
21938 */
21939 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021940var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021941 int flags;
21942 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021943 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021944{
21945 if (flags & DI_FLAGS_RO)
21946 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021947 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021948 return TRUE;
21949 }
21950 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21951 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021952 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021953 return TRUE;
21954 }
21955 return FALSE;
21956}
21957
21958/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021959 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21960 * Also give an error message.
21961 */
21962 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021963var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021964 int flags;
21965 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021966 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021967{
21968 if (flags & DI_FLAGS_FIX)
21969 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021970 EMSG2(_("E795: Cannot delete variable %s"),
21971 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021972 return TRUE;
21973 }
21974 return FALSE;
21975}
21976
21977/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021978 * Check if a funcref is assigned to a valid variable name.
21979 * Return TRUE and give an error if not.
21980 */
21981 static int
21982var_check_func_name(name, new_var)
21983 char_u *name; /* points to start of variable name */
21984 int new_var; /* TRUE when creating the variable */
21985{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021986 /* Allow for w: b: s: and t:. */
21987 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021988 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21989 ? name[2] : name[0]))
21990 {
21991 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21992 name);
21993 return TRUE;
21994 }
21995 /* Don't allow hiding a function. When "v" is not NULL we might be
21996 * assigning another function to the same var, the type is checked
21997 * below. */
21998 if (new_var && function_exists(name))
21999 {
22000 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22001 name);
22002 return TRUE;
22003 }
22004 return FALSE;
22005}
22006
22007/*
22008 * Check if a variable name is valid.
22009 * Return FALSE and give an error if not.
22010 */
22011 static int
22012valid_varname(varname)
22013 char_u *varname;
22014{
22015 char_u *p;
22016
22017 for (p = varname; *p != NUL; ++p)
22018 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22019 && *p != AUTOLOAD_CHAR)
22020 {
22021 EMSG2(_(e_illvar), varname);
22022 return FALSE;
22023 }
22024 return TRUE;
22025}
22026
22027/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022028 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022029 * Also give an error message, using "name" or _("name") when use_gettext is
22030 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022031 */
22032 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022033tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022034 int lock;
22035 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022036 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022037{
22038 if (lock & VAR_LOCKED)
22039 {
22040 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022041 name == NULL ? (char_u *)_("Unknown")
22042 : use_gettext ? (char_u *)_(name)
22043 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022044 return TRUE;
22045 }
22046 if (lock & VAR_FIXED)
22047 {
22048 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022049 name == NULL ? (char_u *)_("Unknown")
22050 : use_gettext ? (char_u *)_(name)
22051 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022052 return TRUE;
22053 }
22054 return FALSE;
22055}
22056
22057/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022058 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022059 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022060 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022061 * It is OK for "from" and "to" to point to the same item. This is used to
22062 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022063 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022064 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022065copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 typval_T *from;
22067 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022069 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022070 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022071 switch (from->v_type)
22072 {
22073 case VAR_NUMBER:
22074 to->vval.v_number = from->vval.v_number;
22075 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022076#ifdef FEAT_FLOAT
22077 case VAR_FLOAT:
22078 to->vval.v_float = from->vval.v_float;
22079 break;
22080#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022081 case VAR_STRING:
22082 case VAR_FUNC:
22083 if (from->vval.v_string == NULL)
22084 to->vval.v_string = NULL;
22085 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022086 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022087 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022088 if (from->v_type == VAR_FUNC)
22089 func_ref(to->vval.v_string);
22090 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022091 break;
22092 case VAR_LIST:
22093 if (from->vval.v_list == NULL)
22094 to->vval.v_list = NULL;
22095 else
22096 {
22097 to->vval.v_list = from->vval.v_list;
22098 ++to->vval.v_list->lv_refcount;
22099 }
22100 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022101 case VAR_DICT:
22102 if (from->vval.v_dict == NULL)
22103 to->vval.v_dict = NULL;
22104 else
22105 {
22106 to->vval.v_dict = from->vval.v_dict;
22107 ++to->vval.v_dict->dv_refcount;
22108 }
22109 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022110 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022111 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022112 break;
22113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114}
22115
22116/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022117 * Make a copy of an item.
22118 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022119 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22120 * reference to an already copied list/dict can be used.
22121 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022122 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022123 static int
22124item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022125 typval_T *from;
22126 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022127 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022128 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022129{
22130 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022131 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022132
Bram Moolenaar33570922005-01-25 22:26:29 +000022133 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022134 {
22135 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022136 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022137 }
22138 ++recurse;
22139
22140 switch (from->v_type)
22141 {
22142 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022143#ifdef FEAT_FLOAT
22144 case VAR_FLOAT:
22145#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022146 case VAR_STRING:
22147 case VAR_FUNC:
22148 copy_tv(from, to);
22149 break;
22150 case VAR_LIST:
22151 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022152 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022153 if (from->vval.v_list == NULL)
22154 to->vval.v_list = NULL;
22155 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22156 {
22157 /* use the copy made earlier */
22158 to->vval.v_list = from->vval.v_list->lv_copylist;
22159 ++to->vval.v_list->lv_refcount;
22160 }
22161 else
22162 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22163 if (to->vval.v_list == NULL)
22164 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022165 break;
22166 case VAR_DICT:
22167 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022168 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022169 if (from->vval.v_dict == NULL)
22170 to->vval.v_dict = NULL;
22171 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22172 {
22173 /* use the copy made earlier */
22174 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22175 ++to->vval.v_dict->dv_refcount;
22176 }
22177 else
22178 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22179 if (to->vval.v_dict == NULL)
22180 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022181 break;
22182 default:
22183 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022184 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022185 }
22186 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022187 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022188}
22189
22190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 * ":echo expr1 ..." print each argument separated with a space, add a
22192 * newline at the end.
22193 * ":echon expr1 ..." print each argument plain.
22194 */
22195 void
22196ex_echo(eap)
22197 exarg_T *eap;
22198{
22199 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022200 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022201 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 char_u *p;
22203 int needclr = TRUE;
22204 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022205 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206
22207 if (eap->skip)
22208 ++emsg_skip;
22209 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22210 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022211 /* If eval1() causes an error message the text from the command may
22212 * still need to be cleared. E.g., "echo 22,44". */
22213 need_clr_eos = needclr;
22214
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022216 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 {
22218 /*
22219 * Report the invalid expression unless the expression evaluation
22220 * has been cancelled due to an aborting error, an interrupt, or an
22221 * exception.
22222 */
22223 if (!aborting())
22224 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022225 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 break;
22227 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022228 need_clr_eos = FALSE;
22229
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 if (!eap->skip)
22231 {
22232 if (atstart)
22233 {
22234 atstart = FALSE;
22235 /* Call msg_start() after eval1(), evaluating the expression
22236 * may cause a message to appear. */
22237 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022238 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022239 /* Mark the saved text as finishing the line, so that what
22240 * follows is displayed on a new line when scrolling back
22241 * at the more prompt. */
22242 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 }
22246 else if (eap->cmdidx == CMD_echo)
22247 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022248 current_copyID += COPYID_INC;
22249 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022250 if (p != NULL)
22251 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022253 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022255 if (*p != TAB && needclr)
22256 {
22257 /* remove any text still there from the command */
22258 msg_clr_eos();
22259 needclr = FALSE;
22260 }
22261 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 }
22263 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022264 {
22265#ifdef FEAT_MBYTE
22266 if (has_mbyte)
22267 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022268 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022269
22270 (void)msg_outtrans_len_attr(p, i, echo_attr);
22271 p += i - 1;
22272 }
22273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022275 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022278 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022280 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 arg = skipwhite(arg);
22282 }
22283 eap->nextcmd = check_nextcmd(arg);
22284
22285 if (eap->skip)
22286 --emsg_skip;
22287 else
22288 {
22289 /* remove text that may still be there from the command */
22290 if (needclr)
22291 msg_clr_eos();
22292 if (eap->cmdidx == CMD_echo)
22293 msg_end();
22294 }
22295}
22296
22297/*
22298 * ":echohl {name}".
22299 */
22300 void
22301ex_echohl(eap)
22302 exarg_T *eap;
22303{
22304 int id;
22305
22306 id = syn_name2id(eap->arg);
22307 if (id == 0)
22308 echo_attr = 0;
22309 else
22310 echo_attr = syn_id2attr(id);
22311}
22312
22313/*
22314 * ":execute expr1 ..." execute the result of an expression.
22315 * ":echomsg expr1 ..." Print a message
22316 * ":echoerr expr1 ..." Print an error
22317 * Each gets spaces around each argument and a newline at the end for
22318 * echo commands
22319 */
22320 void
22321ex_execute(eap)
22322 exarg_T *eap;
22323{
22324 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022325 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 int ret = OK;
22327 char_u *p;
22328 garray_T ga;
22329 int len;
22330 int save_did_emsg;
22331
22332 ga_init2(&ga, 1, 80);
22333
22334 if (eap->skip)
22335 ++emsg_skip;
22336 while (*arg != NUL && *arg != '|' && *arg != '\n')
22337 {
22338 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022339 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 {
22341 /*
22342 * Report the invalid expression unless the expression evaluation
22343 * has been cancelled due to an aborting error, an interrupt, or an
22344 * exception.
22345 */
22346 if (!aborting())
22347 EMSG2(_(e_invexpr2), p);
22348 ret = FAIL;
22349 break;
22350 }
22351
22352 if (!eap->skip)
22353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022354 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 len = (int)STRLEN(p);
22356 if (ga_grow(&ga, len + 2) == FAIL)
22357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022358 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359 ret = FAIL;
22360 break;
22361 }
22362 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 ga.ga_len += len;
22366 }
22367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022368 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 arg = skipwhite(arg);
22370 }
22371
22372 if (ret != FAIL && ga.ga_data != NULL)
22373 {
22374 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022377 out_flush();
22378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 else if (eap->cmdidx == CMD_echoerr)
22380 {
22381 /* We don't want to abort following commands, restore did_emsg. */
22382 save_did_emsg = did_emsg;
22383 EMSG((char_u *)ga.ga_data);
22384 if (!force_abort)
22385 did_emsg = save_did_emsg;
22386 }
22387 else if (eap->cmdidx == CMD_execute)
22388 do_cmdline((char_u *)ga.ga_data,
22389 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22390 }
22391
22392 ga_clear(&ga);
22393
22394 if (eap->skip)
22395 --emsg_skip;
22396
22397 eap->nextcmd = check_nextcmd(arg);
22398}
22399
22400/*
22401 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22402 * "arg" points to the "&" or '+' when called, to "option" when returning.
22403 * Returns NULL when no option name found. Otherwise pointer to the char
22404 * after the option name.
22405 */
22406 static char_u *
22407find_option_end(arg, opt_flags)
22408 char_u **arg;
22409 int *opt_flags;
22410{
22411 char_u *p = *arg;
22412
22413 ++p;
22414 if (*p == 'g' && p[1] == ':')
22415 {
22416 *opt_flags = OPT_GLOBAL;
22417 p += 2;
22418 }
22419 else if (*p == 'l' && p[1] == ':')
22420 {
22421 *opt_flags = OPT_LOCAL;
22422 p += 2;
22423 }
22424 else
22425 *opt_flags = 0;
22426
22427 if (!ASCII_ISALPHA(*p))
22428 return NULL;
22429 *arg = p;
22430
22431 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22432 p += 4; /* termcap option */
22433 else
22434 while (ASCII_ISALPHA(*p))
22435 ++p;
22436 return p;
22437}
22438
22439/*
22440 * ":function"
22441 */
22442 void
22443ex_function(eap)
22444 exarg_T *eap;
22445{
22446 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022447 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 int j;
22449 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022451 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 char_u *name = NULL;
22453 char_u *p;
22454 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022455 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 garray_T newargs;
22457 garray_T newlines;
22458 int varargs = FALSE;
22459 int mustend = FALSE;
22460 int flags = 0;
22461 ufunc_T *fp;
22462 int indent;
22463 int nesting;
22464 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022465 dictitem_T *v;
22466 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022467 static int func_nr = 0; /* number for nameless function */
22468 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022469 hashtab_T *ht;
22470 int todo;
22471 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022472 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473
22474 /*
22475 * ":function" without argument: list functions.
22476 */
22477 if (ends_excmd(*eap->arg))
22478 {
22479 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022480 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022481 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022482 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022483 {
22484 if (!HASHITEM_EMPTY(hi))
22485 {
22486 --todo;
22487 fp = HI2UF(hi);
22488 if (!isdigit(*fp->uf_name))
22489 list_func_head(fp, FALSE);
22490 }
22491 }
22492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 eap->nextcmd = check_nextcmd(eap->arg);
22494 return;
22495 }
22496
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022497 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022498 * ":function /pat": list functions matching pattern.
22499 */
22500 if (*eap->arg == '/')
22501 {
22502 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22503 if (!eap->skip)
22504 {
22505 regmatch_T regmatch;
22506
22507 c = *p;
22508 *p = NUL;
22509 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22510 *p = c;
22511 if (regmatch.regprog != NULL)
22512 {
22513 regmatch.rm_ic = p_ic;
22514
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022515 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022516 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22517 {
22518 if (!HASHITEM_EMPTY(hi))
22519 {
22520 --todo;
22521 fp = HI2UF(hi);
22522 if (!isdigit(*fp->uf_name)
22523 && vim_regexec(&regmatch, fp->uf_name, 0))
22524 list_func_head(fp, FALSE);
22525 }
22526 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022527 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022528 }
22529 }
22530 if (*p == '/')
22531 ++p;
22532 eap->nextcmd = check_nextcmd(p);
22533 return;
22534 }
22535
22536 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022537 * Get the function name. There are these situations:
22538 * func normal function name
22539 * "name" == func, "fudi.fd_dict" == NULL
22540 * dict.func new dictionary entry
22541 * "name" == NULL, "fudi.fd_dict" set,
22542 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22543 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022544 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022545 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22546 * dict.func existing dict entry that's not a Funcref
22547 * "name" == NULL, "fudi.fd_dict" set,
22548 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022549 * s:func script-local function name
22550 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022553 name = trans_function_name(&p, eap->skip, 0, &fudi);
22554 paren = (vim_strchr(p, '(') != NULL);
22555 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 {
22557 /*
22558 * Return on an invalid expression in braces, unless the expression
22559 * evaluation has been cancelled due to an aborting error, an
22560 * interrupt, or an exception.
22561 */
22562 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022563 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022564 if (!eap->skip && fudi.fd_newkey != NULL)
22565 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022566 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 else
22570 eap->skip = TRUE;
22571 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022572
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 /* An error in a function call during evaluation of an expression in magic
22574 * braces should not cause the function not to be defined. */
22575 saved_did_emsg = did_emsg;
22576 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577
22578 /*
22579 * ":function func" with only function name: list function.
22580 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022581 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 {
22583 if (!ends_excmd(*skipwhite(p)))
22584 {
22585 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022586 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587 }
22588 eap->nextcmd = check_nextcmd(p);
22589 if (eap->nextcmd != NULL)
22590 *p = NUL;
22591 if (!eap->skip && !got_int)
22592 {
22593 fp = find_func(name);
22594 if (fp != NULL)
22595 {
22596 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022597 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022599 if (FUNCLINE(fp, j) == NULL)
22600 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 msg_putchar('\n');
22602 msg_outnum((long)(j + 1));
22603 if (j < 9)
22604 msg_putchar(' ');
22605 if (j < 99)
22606 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022607 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608 out_flush(); /* show a line at a time */
22609 ui_breakcheck();
22610 }
22611 if (!got_int)
22612 {
22613 msg_putchar('\n');
22614 msg_puts((char_u *)" endfunction");
22615 }
22616 }
22617 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022618 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022620 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 }
22622
22623 /*
22624 * ":function name(arg1, arg2)" Define function.
22625 */
22626 p = skipwhite(p);
22627 if (*p != '(')
22628 {
22629 if (!eap->skip)
22630 {
22631 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022632 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 }
22634 /* attempt to continue by skipping some text */
22635 if (vim_strchr(p, '(') != NULL)
22636 p = vim_strchr(p, '(');
22637 }
22638 p = skipwhite(p + 1);
22639
22640 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22641 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22642
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022643 if (!eap->skip)
22644 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022645 /* Check the name of the function. Unless it's a dictionary function
22646 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022647 if (name != NULL)
22648 arg = name;
22649 else
22650 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022651 if (arg != NULL && (fudi.fd_di == NULL
22652 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022653 {
22654 if (*arg == K_SPECIAL)
22655 j = 3;
22656 else
22657 j = 0;
22658 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22659 : eval_isnamec(arg[j])))
22660 ++j;
22661 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022662 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022663 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022664 /* Disallow using the g: dict. */
22665 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22666 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022667 }
22668
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 /*
22670 * Isolate the arguments: "arg1, arg2, ...)"
22671 */
22672 while (*p != ')')
22673 {
22674 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22675 {
22676 varargs = TRUE;
22677 p += 3;
22678 mustend = TRUE;
22679 }
22680 else
22681 {
22682 arg = p;
22683 while (ASCII_ISALNUM(*p) || *p == '_')
22684 ++p;
22685 if (arg == p || isdigit(*arg)
22686 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22687 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22688 {
22689 if (!eap->skip)
22690 EMSG2(_("E125: Illegal argument: %s"), arg);
22691 break;
22692 }
22693 if (ga_grow(&newargs, 1) == FAIL)
22694 goto erret;
22695 c = *p;
22696 *p = NUL;
22697 arg = vim_strsave(arg);
22698 if (arg == NULL)
22699 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022700
22701 /* Check for duplicate argument name. */
22702 for (i = 0; i < newargs.ga_len; ++i)
22703 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22704 {
22705 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022706 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022707 goto erret;
22708 }
22709
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22711 *p = c;
22712 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 if (*p == ',')
22714 ++p;
22715 else
22716 mustend = TRUE;
22717 }
22718 p = skipwhite(p);
22719 if (mustend && *p != ')')
22720 {
22721 if (!eap->skip)
22722 EMSG2(_(e_invarg2), eap->arg);
22723 break;
22724 }
22725 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022726 if (*p != ')')
22727 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 ++p; /* skip the ')' */
22729
Bram Moolenaare9a41262005-01-15 22:18:47 +000022730 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 for (;;)
22732 {
22733 p = skipwhite(p);
22734 if (STRNCMP(p, "range", 5) == 0)
22735 {
22736 flags |= FC_RANGE;
22737 p += 5;
22738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022739 else if (STRNCMP(p, "dict", 4) == 0)
22740 {
22741 flags |= FC_DICT;
22742 p += 4;
22743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 else if (STRNCMP(p, "abort", 5) == 0)
22745 {
22746 flags |= FC_ABORT;
22747 p += 5;
22748 }
22749 else
22750 break;
22751 }
22752
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022753 /* When there is a line break use what follows for the function body.
22754 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22755 if (*p == '\n')
22756 line_arg = p + 1;
22757 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 EMSG(_(e_trailing));
22759
22760 /*
22761 * Read the body of the function, until ":endfunction" is found.
22762 */
22763 if (KeyTyped)
22764 {
22765 /* Check if the function already exists, don't let the user type the
22766 * whole function before telling him it doesn't work! For a script we
22767 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022768 if (!eap->skip && !eap->forceit)
22769 {
22770 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22771 EMSG(_(e_funcdict));
22772 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022773 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022776 if (!eap->skip && did_emsg)
22777 goto erret;
22778
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 msg_putchar('\n'); /* don't overwrite the function name */
22780 cmdline_row = msg_row;
22781 }
22782
22783 indent = 2;
22784 nesting = 0;
22785 for (;;)
22786 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022787 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022788 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022789 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022790 saved_wait_return = FALSE;
22791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022793 sourcing_lnum_off = sourcing_lnum;
22794
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022795 if (line_arg != NULL)
22796 {
22797 /* Use eap->arg, split up in parts by line breaks. */
22798 theline = line_arg;
22799 p = vim_strchr(theline, '\n');
22800 if (p == NULL)
22801 line_arg += STRLEN(line_arg);
22802 else
22803 {
22804 *p = NUL;
22805 line_arg = p + 1;
22806 }
22807 }
22808 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 theline = getcmdline(':', 0L, indent);
22810 else
22811 theline = eap->getline(':', eap->cookie, indent);
22812 if (KeyTyped)
22813 lines_left = Rows - 1;
22814 if (theline == NULL)
22815 {
22816 EMSG(_("E126: Missing :endfunction"));
22817 goto erret;
22818 }
22819
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022820 /* Detect line continuation: sourcing_lnum increased more than one. */
22821 if (sourcing_lnum > sourcing_lnum_off + 1)
22822 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22823 else
22824 sourcing_lnum_off = 0;
22825
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 if (skip_until != NULL)
22827 {
22828 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22829 * don't check for ":endfunc". */
22830 if (STRCMP(theline, skip_until) == 0)
22831 {
22832 vim_free(skip_until);
22833 skip_until = NULL;
22834 }
22835 }
22836 else
22837 {
22838 /* skip ':' and blanks*/
22839 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22840 ;
22841
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022842 /* Check for "endfunction". */
22843 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022845 if (line_arg == NULL)
22846 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 break;
22848 }
22849
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022850 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 * at "end". */
22852 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22853 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022854 else if (STRNCMP(p, "if", 2) == 0
22855 || STRNCMP(p, "wh", 2) == 0
22856 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 || STRNCMP(p, "try", 3) == 0)
22858 indent += 2;
22859
22860 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022861 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022863 if (*p == '!')
22864 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022866 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22867 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022869 ++nesting;
22870 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 }
22872 }
22873
22874 /* Check for ":append" or ":insert". */
22875 p = skip_range(p, NULL);
22876 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22877 || (p[0] == 'i'
22878 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22879 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22880 skip_until = vim_strsave((char_u *)".");
22881
22882 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22883 arg = skipwhite(skiptowhite(p));
22884 if (arg[0] == '<' && arg[1] =='<'
22885 && ((p[0] == 'p' && p[1] == 'y'
22886 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22887 || (p[0] == 'p' && p[1] == 'e'
22888 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22889 || (p[0] == 't' && p[1] == 'c'
22890 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022891 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22892 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22894 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022895 || (p[0] == 'm' && p[1] == 'z'
22896 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 ))
22898 {
22899 /* ":python <<" continues until a dot, like ":append" */
22900 p = skipwhite(arg + 2);
22901 if (*p == NUL)
22902 skip_until = vim_strsave((char_u *)".");
22903 else
22904 skip_until = vim_strsave(p);
22905 }
22906 }
22907
22908 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022909 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022910 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022911 if (line_arg == NULL)
22912 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022914 }
22915
22916 /* Copy the line to newly allocated memory. get_one_sourceline()
22917 * allocates 250 bytes per line, this saves 80% on average. The cost
22918 * is an extra alloc/free. */
22919 p = vim_strsave(theline);
22920 if (p != NULL)
22921 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022922 if (line_arg == NULL)
22923 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022924 theline = p;
22925 }
22926
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022927 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22928
22929 /* Add NULL lines for continuation lines, so that the line count is
22930 * equal to the index in the growarray. */
22931 while (sourcing_lnum_off-- > 0)
22932 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022933
22934 /* Check for end of eap->arg. */
22935 if (line_arg != NULL && *line_arg == NUL)
22936 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 }
22938
22939 /* Don't define the function when skipping commands or when an error was
22940 * detected. */
22941 if (eap->skip || did_emsg)
22942 goto erret;
22943
22944 /*
22945 * If there are no errors, add the function
22946 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022947 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022948 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022949 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022951 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022952 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022953 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022954 goto erret;
22955 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022956
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022957 fp = find_func(name);
22958 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022960 if (!eap->forceit)
22961 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022962 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022963 goto erret;
22964 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022965 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022966 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022967 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022968 name);
22969 goto erret;
22970 }
22971 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022972 ga_clear_strings(&(fp->uf_args));
22973 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022974 vim_free(name);
22975 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 }
22978 else
22979 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022980 char numbuf[20];
22981
22982 fp = NULL;
22983 if (fudi.fd_newkey == NULL && !eap->forceit)
22984 {
22985 EMSG(_(e_funcdict));
22986 goto erret;
22987 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022988 if (fudi.fd_di == NULL)
22989 {
22990 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022991 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022992 goto erret;
22993 }
22994 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022995 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022996 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022997
22998 /* Give the function a sequential number. Can only be used with a
22999 * Funcref! */
23000 vim_free(name);
23001 sprintf(numbuf, "%d", ++func_nr);
23002 name = vim_strsave((char_u *)numbuf);
23003 if (name == NULL)
23004 goto erret;
23005 }
23006
23007 if (fp == NULL)
23008 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023009 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023010 {
23011 int slen, plen;
23012 char_u *scriptname;
23013
23014 /* Check that the autoload name matches the script name. */
23015 j = FAIL;
23016 if (sourcing_name != NULL)
23017 {
23018 scriptname = autoload_name(name);
23019 if (scriptname != NULL)
23020 {
23021 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023022 plen = (int)STRLEN(p);
23023 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023024 if (slen > plen && fnamecmp(p,
23025 sourcing_name + slen - plen) == 0)
23026 j = OK;
23027 vim_free(scriptname);
23028 }
23029 }
23030 if (j == FAIL)
23031 {
23032 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23033 goto erret;
23034 }
23035 }
23036
23037 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 if (fp == NULL)
23039 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023040
23041 if (fudi.fd_dict != NULL)
23042 {
23043 if (fudi.fd_di == NULL)
23044 {
23045 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023046 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023047 if (fudi.fd_di == NULL)
23048 {
23049 vim_free(fp);
23050 goto erret;
23051 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023052 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23053 {
23054 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023055 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023056 goto erret;
23057 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023058 }
23059 else
23060 /* overwrite existing dict entry */
23061 clear_tv(&fudi.fd_di->di_tv);
23062 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023063 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023064 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023065 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023066
23067 /* behave like "dict" was used */
23068 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023069 }
23070
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023072 STRCPY(fp->uf_name, name);
23073 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023075 fp->uf_args = newargs;
23076 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023077#ifdef FEAT_PROFILE
23078 fp->uf_tml_count = NULL;
23079 fp->uf_tml_total = NULL;
23080 fp->uf_tml_self = NULL;
23081 fp->uf_profiling = FALSE;
23082 if (prof_def_func())
23083 func_do_profile(fp);
23084#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023085 fp->uf_varargs = varargs;
23086 fp->uf_flags = flags;
23087 fp->uf_calls = 0;
23088 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023089 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090
23091erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 ga_clear_strings(&newargs);
23093 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023094ret_free:
23095 vim_free(skip_until);
23096 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023099 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100}
23101
23102/*
23103 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023104 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023106 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023107 * TFN_INT: internal function name OK
23108 * TFN_QUIET: be quiet
23109 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 * Advances "pp" to just after the function name (if no error).
23111 */
23112 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023113trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 char_u **pp;
23115 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023116 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023117 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023119 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120 char_u *start;
23121 char_u *end;
23122 int lead;
23123 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023125 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023126
23127 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023128 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023130
23131 /* Check for hard coded <SNR>: already translated function ID (from a user
23132 * command). */
23133 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23134 && (*pp)[2] == (int)KE_SNR)
23135 {
23136 *pp += 3;
23137 len = get_id_len(pp) + 3;
23138 return vim_strnsave(start, len);
23139 }
23140
23141 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23142 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023144 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023146
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023147 /* Note that TFN_ flags use the same values as GLV_ flags. */
23148 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023149 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023150 if (end == start)
23151 {
23152 if (!skip)
23153 EMSG(_("E129: Function name required"));
23154 goto theend;
23155 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023156 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023157 {
23158 /*
23159 * Report an invalid expression in braces, unless the expression
23160 * evaluation has been cancelled due to an aborting error, an
23161 * interrupt, or an exception.
23162 */
23163 if (!aborting())
23164 {
23165 if (end != NULL)
23166 EMSG2(_(e_invarg2), start);
23167 }
23168 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023169 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023170 goto theend;
23171 }
23172
23173 if (lv.ll_tv != NULL)
23174 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023175 if (fdp != NULL)
23176 {
23177 fdp->fd_dict = lv.ll_dict;
23178 fdp->fd_newkey = lv.ll_newkey;
23179 lv.ll_newkey = NULL;
23180 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023181 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023182 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23183 {
23184 name = vim_strsave(lv.ll_tv->vval.v_string);
23185 *pp = end;
23186 }
23187 else
23188 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023189 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23190 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023191 EMSG(_(e_funcref));
23192 else
23193 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023194 name = NULL;
23195 }
23196 goto theend;
23197 }
23198
23199 if (lv.ll_name == NULL)
23200 {
23201 /* Error found, but continue after the function name. */
23202 *pp = end;
23203 goto theend;
23204 }
23205
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023206 /* Check if the name is a Funcref. If so, use the value. */
23207 if (lv.ll_exp_name != NULL)
23208 {
23209 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023210 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023211 if (name == lv.ll_exp_name)
23212 name = NULL;
23213 }
23214 else
23215 {
23216 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023217 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023218 if (name == *pp)
23219 name = NULL;
23220 }
23221 if (name != NULL)
23222 {
23223 name = vim_strsave(name);
23224 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023225 if (STRNCMP(name, "<SNR>", 5) == 0)
23226 {
23227 /* Change "<SNR>" to the byte sequence. */
23228 name[0] = K_SPECIAL;
23229 name[1] = KS_EXTRA;
23230 name[2] = (int)KE_SNR;
23231 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23232 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023233 goto theend;
23234 }
23235
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023236 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023237 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023238 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023239 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23240 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23241 {
23242 /* When there was "s:" already or the name expanded to get a
23243 * leading "s:" then remove it. */
23244 lv.ll_name += 2;
23245 len -= 2;
23246 lead = 2;
23247 }
23248 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023249 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023250 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023251 /* skip over "s:" and "g:" */
23252 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023253 lv.ll_name += 2;
23254 len = (int)(end - lv.ll_name);
23255 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023256
23257 /*
23258 * Copy the function name to allocated memory.
23259 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23260 * Accept <SNR>123_name() outside a script.
23261 */
23262 if (skip)
23263 lead = 0; /* do nothing */
23264 else if (lead > 0)
23265 {
23266 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023267 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23268 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023269 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023270 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023271 if (current_SID <= 0)
23272 {
23273 EMSG(_(e_usingsid));
23274 goto theend;
23275 }
23276 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23277 lead += (int)STRLEN(sid_buf);
23278 }
23279 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023280 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023281 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023282 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023283 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023284 goto theend;
23285 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023286 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023287 {
23288 char_u *cp = vim_strchr(lv.ll_name, ':');
23289
23290 if (cp != NULL && cp < end)
23291 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023292 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023293 goto theend;
23294 }
23295 }
23296
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023297 name = alloc((unsigned)(len + lead + 1));
23298 if (name != NULL)
23299 {
23300 if (lead > 0)
23301 {
23302 name[0] = K_SPECIAL;
23303 name[1] = KS_EXTRA;
23304 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023305 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023306 STRCPY(name + 3, sid_buf);
23307 }
23308 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023309 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023310 }
23311 *pp = end;
23312
23313theend:
23314 clear_lval(&lv);
23315 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316}
23317
23318/*
23319 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23320 * Return 2 if "p" starts with "s:".
23321 * Return 0 otherwise.
23322 */
23323 static int
23324eval_fname_script(p)
23325 char_u *p;
23326{
23327 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23328 || STRNICMP(p + 1, "SNR>", 4) == 0))
23329 return 5;
23330 if (p[0] == 's' && p[1] == ':')
23331 return 2;
23332 return 0;
23333}
23334
23335/*
23336 * Return TRUE if "p" starts with "<SID>" or "s:".
23337 * Only works if eval_fname_script() returned non-zero for "p"!
23338 */
23339 static int
23340eval_fname_sid(p)
23341 char_u *p;
23342{
23343 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23344}
23345
23346/*
23347 * List the head of the function: "name(arg1, arg2)".
23348 */
23349 static void
23350list_func_head(fp, indent)
23351 ufunc_T *fp;
23352 int indent;
23353{
23354 int j;
23355
23356 msg_start();
23357 if (indent)
23358 MSG_PUTS(" ");
23359 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023360 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 {
23362 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023363 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 }
23365 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023366 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023368 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 {
23370 if (j)
23371 MSG_PUTS(", ");
23372 msg_puts(FUNCARG(fp, j));
23373 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023374 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375 {
23376 if (j)
23377 MSG_PUTS(", ");
23378 MSG_PUTS("...");
23379 }
23380 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023381 if (fp->uf_flags & FC_ABORT)
23382 MSG_PUTS(" abort");
23383 if (fp->uf_flags & FC_RANGE)
23384 MSG_PUTS(" range");
23385 if (fp->uf_flags & FC_DICT)
23386 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023387 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023388 if (p_verbose > 0)
23389 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390}
23391
23392/*
23393 * Find a function by name, return pointer to it in ufuncs.
23394 * Return NULL for unknown function.
23395 */
23396 static ufunc_T *
23397find_func(name)
23398 char_u *name;
23399{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023400 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023402 hi = hash_find(&func_hashtab, name);
23403 if (!HASHITEM_EMPTY(hi))
23404 return HI2UF(hi);
23405 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406}
23407
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023408#if defined(EXITFREE) || defined(PROTO)
23409 void
23410free_all_functions()
23411{
23412 hashitem_T *hi;
23413
23414 /* Need to start all over every time, because func_free() may change the
23415 * hash table. */
23416 while (func_hashtab.ht_used > 0)
23417 for (hi = func_hashtab.ht_array; ; ++hi)
23418 if (!HASHITEM_EMPTY(hi))
23419 {
23420 func_free(HI2UF(hi));
23421 break;
23422 }
23423}
23424#endif
23425
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023426 int
23427translated_function_exists(name)
23428 char_u *name;
23429{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023430 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023431 return find_internal_func(name) >= 0;
23432 return find_func(name) != NULL;
23433}
23434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023435/*
23436 * Return TRUE if a function "name" exists.
23437 */
23438 static int
23439function_exists(name)
23440 char_u *name;
23441{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023442 char_u *nm = name;
23443 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023444 int n = FALSE;
23445
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023446 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23447 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023448 nm = skipwhite(nm);
23449
23450 /* Only accept "funcname", "funcname ", "funcname (..." and
23451 * "funcname(...", not "funcname!...". */
23452 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023453 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023454 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023455 return n;
23456}
23457
Bram Moolenaara1544c02013-05-30 12:35:52 +020023458 char_u *
23459get_expanded_name(name, check)
23460 char_u *name;
23461 int check;
23462{
23463 char_u *nm = name;
23464 char_u *p;
23465
23466 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23467
23468 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023469 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023470 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023471
Bram Moolenaara1544c02013-05-30 12:35:52 +020023472 vim_free(p);
23473 return NULL;
23474}
23475
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023476/*
23477 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023478 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23479 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023480 */
23481 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023482builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023483 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023484 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023485{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023486 char_u *p;
23487
23488 if (!ASCII_ISLOWER(name[0]))
23489 return FALSE;
23490 p = vim_strchr(name, AUTOLOAD_CHAR);
23491 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023492}
23493
Bram Moolenaar05159a02005-02-26 23:04:13 +000023494#if defined(FEAT_PROFILE) || defined(PROTO)
23495/*
23496 * Start profiling function "fp".
23497 */
23498 static void
23499func_do_profile(fp)
23500 ufunc_T *fp;
23501{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023502 int len = fp->uf_lines.ga_len;
23503
23504 if (len == 0)
23505 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023506 fp->uf_tm_count = 0;
23507 profile_zero(&fp->uf_tm_self);
23508 profile_zero(&fp->uf_tm_total);
23509 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023510 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023511 if (fp->uf_tml_total == NULL)
23512 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023513 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023514 if (fp->uf_tml_self == NULL)
23515 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023516 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023517 fp->uf_tml_idx = -1;
23518 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23519 || fp->uf_tml_self == NULL)
23520 return; /* out of memory */
23521
23522 fp->uf_profiling = TRUE;
23523}
23524
23525/*
23526 * Dump the profiling results for all functions in file "fd".
23527 */
23528 void
23529func_dump_profile(fd)
23530 FILE *fd;
23531{
23532 hashitem_T *hi;
23533 int todo;
23534 ufunc_T *fp;
23535 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023536 ufunc_T **sorttab;
23537 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023538
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023539 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023540 if (todo == 0)
23541 return; /* nothing to dump */
23542
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023543 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023544
Bram Moolenaar05159a02005-02-26 23:04:13 +000023545 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23546 {
23547 if (!HASHITEM_EMPTY(hi))
23548 {
23549 --todo;
23550 fp = HI2UF(hi);
23551 if (fp->uf_profiling)
23552 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023553 if (sorttab != NULL)
23554 sorttab[st_len++] = fp;
23555
Bram Moolenaar05159a02005-02-26 23:04:13 +000023556 if (fp->uf_name[0] == K_SPECIAL)
23557 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23558 else
23559 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23560 if (fp->uf_tm_count == 1)
23561 fprintf(fd, "Called 1 time\n");
23562 else
23563 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23564 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23565 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23566 fprintf(fd, "\n");
23567 fprintf(fd, "count total (s) self (s)\n");
23568
23569 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23570 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023571 if (FUNCLINE(fp, i) == NULL)
23572 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023573 prof_func_line(fd, fp->uf_tml_count[i],
23574 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023575 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23576 }
23577 fprintf(fd, "\n");
23578 }
23579 }
23580 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023581
23582 if (sorttab != NULL && st_len > 0)
23583 {
23584 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23585 prof_total_cmp);
23586 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23587 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23588 prof_self_cmp);
23589 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23590 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023591
23592 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023593}
Bram Moolenaar73830342005-02-28 22:48:19 +000023594
23595 static void
23596prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23597 FILE *fd;
23598 ufunc_T **sorttab;
23599 int st_len;
23600 char *title;
23601 int prefer_self; /* when equal print only self time */
23602{
23603 int i;
23604 ufunc_T *fp;
23605
23606 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23607 fprintf(fd, "count total (s) self (s) function\n");
23608 for (i = 0; i < 20 && i < st_len; ++i)
23609 {
23610 fp = sorttab[i];
23611 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23612 prefer_self);
23613 if (fp->uf_name[0] == K_SPECIAL)
23614 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23615 else
23616 fprintf(fd, " %s()\n", fp->uf_name);
23617 }
23618 fprintf(fd, "\n");
23619}
23620
23621/*
23622 * Print the count and times for one function or function line.
23623 */
23624 static void
23625prof_func_line(fd, count, total, self, prefer_self)
23626 FILE *fd;
23627 int count;
23628 proftime_T *total;
23629 proftime_T *self;
23630 int prefer_self; /* when equal print only self time */
23631{
23632 if (count > 0)
23633 {
23634 fprintf(fd, "%5d ", count);
23635 if (prefer_self && profile_equal(total, self))
23636 fprintf(fd, " ");
23637 else
23638 fprintf(fd, "%s ", profile_msg(total));
23639 if (!prefer_self && profile_equal(total, self))
23640 fprintf(fd, " ");
23641 else
23642 fprintf(fd, "%s ", profile_msg(self));
23643 }
23644 else
23645 fprintf(fd, " ");
23646}
23647
23648/*
23649 * Compare function for total time sorting.
23650 */
23651 static int
23652#ifdef __BORLANDC__
23653_RTLENTRYF
23654#endif
23655prof_total_cmp(s1, s2)
23656 const void *s1;
23657 const void *s2;
23658{
23659 ufunc_T *p1, *p2;
23660
23661 p1 = *(ufunc_T **)s1;
23662 p2 = *(ufunc_T **)s2;
23663 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23664}
23665
23666/*
23667 * Compare function for self time sorting.
23668 */
23669 static int
23670#ifdef __BORLANDC__
23671_RTLENTRYF
23672#endif
23673prof_self_cmp(s1, s2)
23674 const void *s1;
23675 const void *s2;
23676{
23677 ufunc_T *p1, *p2;
23678
23679 p1 = *(ufunc_T **)s1;
23680 p2 = *(ufunc_T **)s2;
23681 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23682}
23683
Bram Moolenaar05159a02005-02-26 23:04:13 +000023684#endif
23685
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023686/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023687 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023688 * Return TRUE if a package was loaded.
23689 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023690 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023691script_autoload(name, reload)
23692 char_u *name;
23693 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023694{
23695 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023696 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023697 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023698 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023699
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023700 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023701 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023702 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023703 return FALSE;
23704
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023705 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023706
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023707 /* Find the name in the list of previously loaded package names. Skip
23708 * "autoload/", it's always the same. */
23709 for (i = 0; i < ga_loaded.ga_len; ++i)
23710 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23711 break;
23712 if (!reload && i < ga_loaded.ga_len)
23713 ret = FALSE; /* was loaded already */
23714 else
23715 {
23716 /* Remember the name if it wasn't loaded already. */
23717 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23718 {
23719 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23720 tofree = NULL;
23721 }
23722
23723 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023724 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023725 ret = TRUE;
23726 }
23727
23728 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023729 return ret;
23730}
23731
23732/*
23733 * Return the autoload script name for a function or variable name.
23734 * Returns NULL when out of memory.
23735 */
23736 static char_u *
23737autoload_name(name)
23738 char_u *name;
23739{
23740 char_u *p;
23741 char_u *scriptname;
23742
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023743 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023744 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23745 if (scriptname == NULL)
23746 return FALSE;
23747 STRCPY(scriptname, "autoload/");
23748 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023749 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023750 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023751 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023752 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023753 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023754}
23755
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23757
23758/*
23759 * Function given to ExpandGeneric() to obtain the list of user defined
23760 * function names.
23761 */
23762 char_u *
23763get_user_func_name(xp, idx)
23764 expand_T *xp;
23765 int idx;
23766{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023767 static long_u done;
23768 static hashitem_T *hi;
23769 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770
23771 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023772 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023773 done = 0;
23774 hi = func_hashtab.ht_array;
23775 }
23776 if (done < func_hashtab.ht_used)
23777 {
23778 if (done++ > 0)
23779 ++hi;
23780 while (HASHITEM_EMPTY(hi))
23781 ++hi;
23782 fp = HI2UF(hi);
23783
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023784 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023785 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023786
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023787 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23788 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023789
23790 cat_func_name(IObuff, fp);
23791 if (xp->xp_context != EXPAND_USER_FUNC)
23792 {
23793 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023794 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 STRCAT(IObuff, ")");
23796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 return IObuff;
23798 }
23799 return NULL;
23800}
23801
23802#endif /* FEAT_CMDL_COMPL */
23803
23804/*
23805 * Copy the function name of "fp" to buffer "buf".
23806 * "buf" must be able to hold the function name plus three bytes.
23807 * Takes care of script-local function names.
23808 */
23809 static void
23810cat_func_name(buf, fp)
23811 char_u *buf;
23812 ufunc_T *fp;
23813{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023814 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815 {
23816 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023817 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 }
23819 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023820 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821}
23822
23823/*
23824 * ":delfunction {name}"
23825 */
23826 void
23827ex_delfunction(eap)
23828 exarg_T *eap;
23829{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023830 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023831 char_u *p;
23832 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023833 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834
23835 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 name = trans_function_name(&p, eap->skip, 0, &fudi);
23837 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023839 {
23840 if (fudi.fd_dict != NULL && !eap->skip)
23841 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 if (!ends_excmd(*skipwhite(p)))
23845 {
23846 vim_free(name);
23847 EMSG(_(e_trailing));
23848 return;
23849 }
23850 eap->nextcmd = check_nextcmd(p);
23851 if (eap->nextcmd != NULL)
23852 *p = NUL;
23853
23854 if (!eap->skip)
23855 fp = find_func(name);
23856 vim_free(name);
23857
23858 if (!eap->skip)
23859 {
23860 if (fp == NULL)
23861 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023862 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 return;
23864 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023865 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 {
23867 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23868 return;
23869 }
23870
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023871 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023873 /* Delete the dict item that refers to the function, it will
23874 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023875 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877 else
23878 func_free(fp);
23879 }
23880}
23881
23882/*
23883 * Free a function and remove it from the list of functions.
23884 */
23885 static void
23886func_free(fp)
23887 ufunc_T *fp;
23888{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023889 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023890
23891 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023892 ga_clear_strings(&(fp->uf_args));
23893 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023894#ifdef FEAT_PROFILE
23895 vim_free(fp->uf_tml_count);
23896 vim_free(fp->uf_tml_total);
23897 vim_free(fp->uf_tml_self);
23898#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023899
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023900 /* remove the function from the function hashtable */
23901 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23902 if (HASHITEM_EMPTY(hi))
23903 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023904 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023905 hash_remove(&func_hashtab, hi);
23906
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023907 vim_free(fp);
23908}
23909
23910/*
23911 * Unreference a Function: decrement the reference count and free it when it
23912 * becomes zero. Only for numbered functions.
23913 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023914 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023915func_unref(name)
23916 char_u *name;
23917{
23918 ufunc_T *fp;
23919
23920 if (name != NULL && isdigit(*name))
23921 {
23922 fp = find_func(name);
23923 if (fp == NULL)
23924 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023925 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023926 {
23927 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023928 * when "uf_calls" becomes zero. */
23929 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023930 func_free(fp);
23931 }
23932 }
23933}
23934
23935/*
23936 * Count a reference to a Function.
23937 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023938 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023939func_ref(name)
23940 char_u *name;
23941{
23942 ufunc_T *fp;
23943
23944 if (name != NULL && isdigit(*name))
23945 {
23946 fp = find_func(name);
23947 if (fp == NULL)
23948 EMSG2(_(e_intern2), "func_ref()");
23949 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023950 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 }
23952}
23953
23954/*
23955 * Call a user function.
23956 */
23957 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023958call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 ufunc_T *fp; /* pointer to function */
23960 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023961 typval_T *argvars; /* arguments */
23962 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 linenr_T firstline; /* first line of range */
23964 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023965 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966{
Bram Moolenaar33570922005-01-25 22:26:29 +000023967 char_u *save_sourcing_name;
23968 linenr_T save_sourcing_lnum;
23969 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023970 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023971 int save_did_emsg;
23972 static int depth = 0;
23973 dictitem_T *v;
23974 int fixvar_idx = 0; /* index in fixvar[] */
23975 int i;
23976 int ai;
23977 char_u numbuf[NUMBUFLEN];
23978 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023979 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023980#ifdef FEAT_PROFILE
23981 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023982 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984
23985 /* If depth of calling is getting too high, don't execute the function */
23986 if (depth >= p_mfd)
23987 {
23988 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023989 rettv->v_type = VAR_NUMBER;
23990 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 return;
23992 }
23993 ++depth;
23994
23995 line_breakcheck(); /* check for CTRL-C hit */
23996
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023997 fc = (funccall_T *)alloc(sizeof(funccall_T));
23998 fc->caller = current_funccal;
23999 current_funccal = fc;
24000 fc->func = fp;
24001 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024002 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024003 fc->linenr = 0;
24004 fc->returned = FALSE;
24005 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024007 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24008 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009
Bram Moolenaar33570922005-01-25 22:26:29 +000024010 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024011 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024012 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24013 * each argument variable and saves a lot of time.
24014 */
24015 /*
24016 * Init l: variables.
24017 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024018 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024019 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024020 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024021 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24022 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024023 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024024 name = v->di_key;
24025 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024026 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024027 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024028 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024029 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024030 v->di_tv.vval.v_dict = selfdict;
24031 ++selfdict->dv_refcount;
24032 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024033
Bram Moolenaar33570922005-01-25 22:26:29 +000024034 /*
24035 * Init a: variables.
24036 * Set a:0 to "argcount".
24037 * Set a:000 to a list with room for the "..." arguments.
24038 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024039 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024040 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024041 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024042 /* Use "name" to avoid a warning from some compiler that checks the
24043 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024044 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024045 name = v->di_key;
24046 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024047 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024048 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024049 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024050 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024051 v->di_tv.vval.v_list = &fc->l_varlist;
24052 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24053 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24054 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024055
24056 /*
24057 * Set a:firstline to "firstline" and a:lastline to "lastline".
24058 * Set a:name to named arguments.
24059 * Set a:N to the "..." arguments.
24060 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024061 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024062 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024063 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024064 (varnumber_T)lastline);
24065 for (i = 0; i < argcount; ++i)
24066 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024067 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024068 if (ai < 0)
24069 /* named argument a:name */
24070 name = FUNCARG(fp, i);
24071 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024072 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024073 /* "..." argument a:1, a:2, etc. */
24074 sprintf((char *)numbuf, "%d", ai + 1);
24075 name = numbuf;
24076 }
24077 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24078 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024079 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024080 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24081 }
24082 else
24083 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024084 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24085 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024086 if (v == NULL)
24087 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024088 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024089 }
24090 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024091 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024092
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024093 /* Note: the values are copied directly to avoid alloc/free.
24094 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024095 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024096 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024097
24098 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24099 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024100 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24101 fc->l_listitems[ai].li_tv = argvars[i];
24102 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024103 }
24104 }
24105
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106 /* Don't redraw while executing the function. */
24107 ++RedrawingDisabled;
24108 save_sourcing_name = sourcing_name;
24109 save_sourcing_lnum = sourcing_lnum;
24110 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024111 /* need space for function name + ("function " + 3) or "[number]" */
24112 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24113 + STRLEN(fp->uf_name) + 20;
24114 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 if (sourcing_name != NULL)
24116 {
24117 if (save_sourcing_name != NULL
24118 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024119 sprintf((char *)sourcing_name, "%s[%d]..",
24120 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 else
24122 STRCPY(sourcing_name, "function ");
24123 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24124
24125 if (p_verbose >= 12)
24126 {
24127 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024128 verbose_enter_scroll();
24129
Bram Moolenaar555b2802005-05-19 21:08:39 +000024130 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 if (p_verbose >= 14)
24132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024134 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024135 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024136 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137
24138 msg_puts((char_u *)"(");
24139 for (i = 0; i < argcount; ++i)
24140 {
24141 if (i > 0)
24142 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024143 if (argvars[i].v_type == VAR_NUMBER)
24144 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 else
24146 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024147 /* Do not want errors such as E724 here. */
24148 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024149 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024150 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024151 if (s != NULL)
24152 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024153 if (vim_strsize(s) > MSG_BUF_CLEN)
24154 {
24155 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24156 s = buf;
24157 }
24158 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024159 vim_free(tofree);
24160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024161 }
24162 }
24163 msg_puts((char_u *)")");
24164 }
24165 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024166
24167 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 --no_wait_return;
24169 }
24170 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024171#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024172 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024173 {
24174 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24175 func_do_profile(fp);
24176 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024177 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024178 {
24179 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024180 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024181 profile_zero(&fp->uf_tm_children);
24182 }
24183 script_prof_save(&wait_start);
24184 }
24185#endif
24186
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024188 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 save_did_emsg = did_emsg;
24190 did_emsg = FALSE;
24191
24192 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024193 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24195
24196 --RedrawingDisabled;
24197
24198 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024199 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024201 clear_tv(rettv);
24202 rettv->v_type = VAR_NUMBER;
24203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 }
24205
Bram Moolenaar05159a02005-02-26 23:04:13 +000024206#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024207 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024208 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024209 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024210 profile_end(&call_start);
24211 profile_sub_wait(&wait_start, &call_start);
24212 profile_add(&fp->uf_tm_total, &call_start);
24213 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024214 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024215 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024216 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24217 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024218 }
24219 }
24220#endif
24221
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222 /* when being verbose, mention the return value */
24223 if (p_verbose >= 12)
24224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024226 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024229 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024230 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024231 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024232 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024233 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024235 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024236 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024237 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024238 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024239
Bram Moolenaar555b2802005-05-19 21:08:39 +000024240 /* The value may be very long. Skip the middle part, so that we
24241 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024242 * truncate it at the end. Don't want errors such as E724 here. */
24243 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024244 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024245 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024246 if (s != NULL)
24247 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024248 if (vim_strsize(s) > MSG_BUF_CLEN)
24249 {
24250 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24251 s = buf;
24252 }
24253 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024254 vim_free(tofree);
24255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256 }
24257 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024258
24259 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 --no_wait_return;
24261 }
24262
24263 vim_free(sourcing_name);
24264 sourcing_name = save_sourcing_name;
24265 sourcing_lnum = save_sourcing_lnum;
24266 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024267#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024268 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024269 script_prof_restore(&wait_start);
24270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271
24272 if (p_verbose >= 12 && sourcing_name != NULL)
24273 {
24274 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024275 verbose_enter_scroll();
24276
Bram Moolenaar555b2802005-05-19 21:08:39 +000024277 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024279
24280 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 --no_wait_return;
24282 }
24283
24284 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024285 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024287
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024288 /* If the a:000 list and the l: and a: dicts are not referenced we can
24289 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024290 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24291 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24292 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24293 {
24294 free_funccal(fc, FALSE);
24295 }
24296 else
24297 {
24298 hashitem_T *hi;
24299 listitem_T *li;
24300 int todo;
24301
24302 /* "fc" is still in use. This can happen when returning "a:000" or
24303 * assigning "l:" to a global variable.
24304 * Link "fc" in the list for garbage collection later. */
24305 fc->caller = previous_funccal;
24306 previous_funccal = fc;
24307
24308 /* Make a copy of the a: variables, since we didn't do that above. */
24309 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24310 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24311 {
24312 if (!HASHITEM_EMPTY(hi))
24313 {
24314 --todo;
24315 v = HI2DI(hi);
24316 copy_tv(&v->di_tv, &v->di_tv);
24317 }
24318 }
24319
24320 /* Make a copy of the a:000 items, since we didn't do that above. */
24321 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24322 copy_tv(&li->li_tv, &li->li_tv);
24323 }
24324}
24325
24326/*
24327 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024328 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024329 */
24330 static int
24331can_free_funccal(fc, copyID)
24332 funccall_T *fc;
24333 int copyID;
24334{
24335 return (fc->l_varlist.lv_copyID != copyID
24336 && fc->l_vars.dv_copyID != copyID
24337 && fc->l_avars.dv_copyID != copyID);
24338}
24339
24340/*
24341 * Free "fc" and what it contains.
24342 */
24343 static void
24344free_funccal(fc, free_val)
24345 funccall_T *fc;
24346 int free_val; /* a: vars were allocated */
24347{
24348 listitem_T *li;
24349
24350 /* The a: variables typevals may not have been allocated, only free the
24351 * allocated variables. */
24352 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24353
24354 /* free all l: variables */
24355 vars_clear(&fc->l_vars.dv_hashtab);
24356
24357 /* Free the a:000 variables if they were allocated. */
24358 if (free_val)
24359 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24360 clear_tv(&li->li_tv);
24361
24362 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363}
24364
24365/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024366 * Add a number variable "name" to dict "dp" with value "nr".
24367 */
24368 static void
24369add_nr_var(dp, v, name, nr)
24370 dict_T *dp;
24371 dictitem_T *v;
24372 char *name;
24373 varnumber_T nr;
24374{
24375 STRCPY(v->di_key, name);
24376 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24377 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24378 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024379 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024380 v->di_tv.vval.v_number = nr;
24381}
24382
24383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384 * ":return [expr]"
24385 */
24386 void
24387ex_return(eap)
24388 exarg_T *eap;
24389{
24390 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024391 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 int returning = FALSE;
24393
24394 if (current_funccal == NULL)
24395 {
24396 EMSG(_("E133: :return not inside a function"));
24397 return;
24398 }
24399
24400 if (eap->skip)
24401 ++emsg_skip;
24402
24403 eap->nextcmd = NULL;
24404 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024405 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 {
24407 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024408 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024410 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411 }
24412 /* It's safer to return also on error. */
24413 else if (!eap->skip)
24414 {
24415 /*
24416 * Return unless the expression evaluation has been cancelled due to an
24417 * aborting error, an interrupt, or an exception.
24418 */
24419 if (!aborting())
24420 returning = do_return(eap, FALSE, TRUE, NULL);
24421 }
24422
24423 /* When skipping or the return gets pending, advance to the next command
24424 * in this line (!returning). Otherwise, ignore the rest of the line.
24425 * Following lines will be ignored by get_func_line(). */
24426 if (returning)
24427 eap->nextcmd = NULL;
24428 else if (eap->nextcmd == NULL) /* no argument */
24429 eap->nextcmd = check_nextcmd(arg);
24430
24431 if (eap->skip)
24432 --emsg_skip;
24433}
24434
24435/*
24436 * Return from a function. Possibly makes the return pending. Also called
24437 * for a pending return at the ":endtry" or after returning from an extra
24438 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024439 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024440 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441 * FALSE when the return gets pending.
24442 */
24443 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024444do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 exarg_T *eap;
24446 int reanimate;
24447 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024448 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449{
24450 int idx;
24451 struct condstack *cstack = eap->cstack;
24452
24453 if (reanimate)
24454 /* Undo the return. */
24455 current_funccal->returned = FALSE;
24456
24457 /*
24458 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24459 * not in its finally clause (which then is to be executed next) is found.
24460 * In this case, make the ":return" pending for execution at the ":endtry".
24461 * Otherwise, return normally.
24462 */
24463 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24464 if (idx >= 0)
24465 {
24466 cstack->cs_pending[idx] = CSTP_RETURN;
24467
24468 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024469 /* A pending return again gets pending. "rettv" points to an
24470 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024472 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473 else
24474 {
24475 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024476 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024478 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024480 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 {
24482 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024483 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024484 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485 else
24486 EMSG(_(e_outofmem));
24487 }
24488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024489 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490
24491 if (reanimate)
24492 {
24493 /* The pending return value could be overwritten by a ":return"
24494 * without argument in a finally clause; reset the default
24495 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024496 current_funccal->rettv->v_type = VAR_NUMBER;
24497 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 }
24499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024500 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024501 }
24502 else
24503 {
24504 current_funccal->returned = TRUE;
24505
24506 /* If the return is carried out now, store the return value. For
24507 * a return immediately after reanimation, the value is already
24508 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024509 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024511 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024512 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024514 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 }
24516 }
24517
24518 return idx < 0;
24519}
24520
24521/*
24522 * Free the variable with a pending return value.
24523 */
24524 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024525discard_pending_return(rettv)
24526 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527{
Bram Moolenaar33570922005-01-25 22:26:29 +000024528 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529}
24530
24531/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024532 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 * is an allocated string. Used by report_pending() for verbose messages.
24534 */
24535 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024536get_return_cmd(rettv)
24537 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024539 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024540 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024541 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024543 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024544 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024545 if (s == NULL)
24546 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024547
24548 STRCPY(IObuff, ":return ");
24549 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24550 if (STRLEN(s) + 8 >= IOSIZE)
24551 STRCPY(IObuff + IOSIZE - 4, "...");
24552 vim_free(tofree);
24553 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554}
24555
24556/*
24557 * Get next function line.
24558 * Called by do_cmdline() to get the next line.
24559 * Returns allocated string, or NULL for end of function.
24560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 char_u *
24562get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024563 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024565 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566{
Bram Moolenaar33570922005-01-25 22:26:29 +000024567 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024568 ufunc_T *fp = fcp->func;
24569 char_u *retval;
24570 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571
24572 /* If breakpoints have been added/deleted need to check for it. */
24573 if (fcp->dbg_tick != debug_tick)
24574 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024575 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 sourcing_lnum);
24577 fcp->dbg_tick = debug_tick;
24578 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024579#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024580 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024581 func_line_end(cookie);
24582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583
Bram Moolenaar05159a02005-02-26 23:04:13 +000024584 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024585 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24586 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587 retval = NULL;
24588 else
24589 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024590 /* Skip NULL lines (continuation lines). */
24591 while (fcp->linenr < gap->ga_len
24592 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24593 ++fcp->linenr;
24594 if (fcp->linenr >= gap->ga_len)
24595 retval = NULL;
24596 else
24597 {
24598 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24599 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024600#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024601 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024602 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024603#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 }
24606
24607 /* Did we encounter a breakpoint? */
24608 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24609 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024610 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024612 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613 sourcing_lnum);
24614 fcp->dbg_tick = debug_tick;
24615 }
24616
24617 return retval;
24618}
24619
Bram Moolenaar05159a02005-02-26 23:04:13 +000024620#if defined(FEAT_PROFILE) || defined(PROTO)
24621/*
24622 * Called when starting to read a function line.
24623 * "sourcing_lnum" must be correct!
24624 * When skipping lines it may not actually be executed, but we won't find out
24625 * until later and we need to store the time now.
24626 */
24627 void
24628func_line_start(cookie)
24629 void *cookie;
24630{
24631 funccall_T *fcp = (funccall_T *)cookie;
24632 ufunc_T *fp = fcp->func;
24633
24634 if (fp->uf_profiling && sourcing_lnum >= 1
24635 && sourcing_lnum <= fp->uf_lines.ga_len)
24636 {
24637 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024638 /* Skip continuation lines. */
24639 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24640 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024641 fp->uf_tml_execed = FALSE;
24642 profile_start(&fp->uf_tml_start);
24643 profile_zero(&fp->uf_tml_children);
24644 profile_get_wait(&fp->uf_tml_wait);
24645 }
24646}
24647
24648/*
24649 * Called when actually executing a function line.
24650 */
24651 void
24652func_line_exec(cookie)
24653 void *cookie;
24654{
24655 funccall_T *fcp = (funccall_T *)cookie;
24656 ufunc_T *fp = fcp->func;
24657
24658 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24659 fp->uf_tml_execed = TRUE;
24660}
24661
24662/*
24663 * Called when done with a function line.
24664 */
24665 void
24666func_line_end(cookie)
24667 void *cookie;
24668{
24669 funccall_T *fcp = (funccall_T *)cookie;
24670 ufunc_T *fp = fcp->func;
24671
24672 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24673 {
24674 if (fp->uf_tml_execed)
24675 {
24676 ++fp->uf_tml_count[fp->uf_tml_idx];
24677 profile_end(&fp->uf_tml_start);
24678 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024679 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024680 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24681 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024682 }
24683 fp->uf_tml_idx = -1;
24684 }
24685}
24686#endif
24687
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688/*
24689 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024690 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691 */
24692 int
24693func_has_ended(cookie)
24694 void *cookie;
24695{
Bram Moolenaar33570922005-01-25 22:26:29 +000024696 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697
24698 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24699 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024700 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024701 || fcp->returned);
24702}
24703
24704/*
24705 * return TRUE if cookie indicates a function which "abort"s on errors.
24706 */
24707 int
24708func_has_abort(cookie)
24709 void *cookie;
24710{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024711 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024712}
24713
24714#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24715typedef enum
24716{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024717 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24718 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24719 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024720} var_flavour_T;
24721
24722static var_flavour_T var_flavour __ARGS((char_u *varname));
24723
24724 static var_flavour_T
24725var_flavour(varname)
24726 char_u *varname;
24727{
24728 char_u *p = varname;
24729
24730 if (ASCII_ISUPPER(*p))
24731 {
24732 while (*(++p))
24733 if (ASCII_ISLOWER(*p))
24734 return VAR_FLAVOUR_SESSION;
24735 return VAR_FLAVOUR_VIMINFO;
24736 }
24737 else
24738 return VAR_FLAVOUR_DEFAULT;
24739}
24740#endif
24741
24742#if defined(FEAT_VIMINFO) || defined(PROTO)
24743/*
24744 * Restore global vars that start with a capital from the viminfo file
24745 */
24746 int
24747read_viminfo_varlist(virp, writing)
24748 vir_T *virp;
24749 int writing;
24750{
24751 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024752 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024753 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754
24755 if (!writing && (find_viminfo_parameter('!') != NULL))
24756 {
24757 tab = vim_strchr(virp->vir_line + 1, '\t');
24758 if (tab != NULL)
24759 {
24760 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024761 switch (*tab)
24762 {
24763 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024764#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024765 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024766#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024767 case 'D': type = VAR_DICT; break;
24768 case 'L': type = VAR_LIST; break;
24769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770
24771 tab = vim_strchr(tab, '\t');
24772 if (tab != NULL)
24773 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024774 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024775 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024776 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024778#ifdef FEAT_FLOAT
24779 else if (type == VAR_FLOAT)
24780 (void)string2float(tab + 1, &tv.vval.v_float);
24781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024783 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024784 if (type == VAR_DICT || type == VAR_LIST)
24785 {
24786 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24787
24788 if (etv == NULL)
24789 /* Failed to parse back the dict or list, use it as a
24790 * string. */
24791 tv.v_type = VAR_STRING;
24792 else
24793 {
24794 vim_free(tv.vval.v_string);
24795 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024796 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024797 }
24798 }
24799
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024800 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024801
24802 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024803 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024804 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24805 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024806 }
24807 }
24808 }
24809
24810 return viminfo_readline(virp);
24811}
24812
24813/*
24814 * Write global vars that start with a capital to the viminfo file
24815 */
24816 void
24817write_viminfo_varlist(fp)
24818 FILE *fp;
24819{
Bram Moolenaar33570922005-01-25 22:26:29 +000024820 hashitem_T *hi;
24821 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024822 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024823 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024824 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024825 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024826 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024827
24828 if (find_viminfo_parameter('!') == NULL)
24829 return;
24830
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024831 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024832
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024833 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024834 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024835 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024836 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024838 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024839 this_var = HI2DI(hi);
24840 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024841 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024842 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024843 {
24844 case VAR_STRING: s = "STR"; break;
24845 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024846#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024847 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024848#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024849 case VAR_DICT: s = "DIC"; break;
24850 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024851 default: continue;
24852 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024853 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024854 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024855 if (p != NULL)
24856 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024857 vim_free(tofree);
24858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024859 }
24860 }
24861}
24862#endif
24863
24864#if defined(FEAT_SESSION) || defined(PROTO)
24865 int
24866store_session_globals(fd)
24867 FILE *fd;
24868{
Bram Moolenaar33570922005-01-25 22:26:29 +000024869 hashitem_T *hi;
24870 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024871 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872 char_u *p, *t;
24873
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024874 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024875 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024877 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024879 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024880 this_var = HI2DI(hi);
24881 if ((this_var->di_tv.v_type == VAR_NUMBER
24882 || this_var->di_tv.v_type == VAR_STRING)
24883 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024884 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024885 /* Escape special characters with a backslash. Turn a LF and
24886 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024887 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024888 (char_u *)"\\\"\n\r");
24889 if (p == NULL) /* out of memory */
24890 break;
24891 for (t = p; *t != NUL; ++t)
24892 if (*t == '\n')
24893 *t = 'n';
24894 else if (*t == '\r')
24895 *t = 'r';
24896 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024897 this_var->di_key,
24898 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24899 : ' ',
24900 p,
24901 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24902 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024903 || put_eol(fd) == FAIL)
24904 {
24905 vim_free(p);
24906 return FAIL;
24907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 vim_free(p);
24909 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024910#ifdef FEAT_FLOAT
24911 else if (this_var->di_tv.v_type == VAR_FLOAT
24912 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24913 {
24914 float_T f = this_var->di_tv.vval.v_float;
24915 int sign = ' ';
24916
24917 if (f < 0)
24918 {
24919 f = -f;
24920 sign = '-';
24921 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024922 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024923 this_var->di_key, sign, f) < 0)
24924 || put_eol(fd) == FAIL)
24925 return FAIL;
24926 }
24927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 }
24929 }
24930 return OK;
24931}
24932#endif
24933
Bram Moolenaar661b1822005-07-28 22:36:45 +000024934/*
24935 * Display script name where an item was last set.
24936 * Should only be invoked when 'verbose' is non-zero.
24937 */
24938 void
24939last_set_msg(scriptID)
24940 scid_T scriptID;
24941{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024942 char_u *p;
24943
Bram Moolenaar661b1822005-07-28 22:36:45 +000024944 if (scriptID != 0)
24945 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024946 p = home_replace_save(NULL, get_scriptname(scriptID));
24947 if (p != NULL)
24948 {
24949 verbose_enter();
24950 MSG_PUTS(_("\n\tLast set from "));
24951 MSG_PUTS(p);
24952 vim_free(p);
24953 verbose_leave();
24954 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024955 }
24956}
24957
Bram Moolenaard812df62008-11-09 12:46:09 +000024958/*
24959 * List v:oldfiles in a nice way.
24960 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024961 void
24962ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024963 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024964{
24965 list_T *l = vimvars[VV_OLDFILES].vv_list;
24966 listitem_T *li;
24967 int nr = 0;
24968
24969 if (l == NULL)
24970 msg((char_u *)_("No old files"));
24971 else
24972 {
24973 msg_start();
24974 msg_scroll = TRUE;
24975 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24976 {
24977 msg_outnum((long)++nr);
24978 MSG_PUTS(": ");
24979 msg_outtrans(get_tv_string(&li->li_tv));
24980 msg_putchar('\n');
24981 out_flush(); /* output one line at a time */
24982 ui_breakcheck();
24983 }
24984 /* Assume "got_int" was set to truncate the listing. */
24985 got_int = FALSE;
24986
24987#ifdef FEAT_BROWSE_CMD
24988 if (cmdmod.browse)
24989 {
24990 quit_more = FALSE;
24991 nr = prompt_for_number(FALSE);
24992 msg_starthere();
24993 if (nr > 0)
24994 {
24995 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24996 (long)nr);
24997
24998 if (p != NULL)
24999 {
25000 p = expand_env_save(p);
25001 eap->arg = p;
25002 eap->cmdidx = CMD_edit;
25003 cmdmod.browse = FALSE;
25004 do_exedit(eap, NULL);
25005 vim_free(p);
25006 }
25007 }
25008 }
25009#endif
25010 }
25011}
25012
Bram Moolenaar53744302015-07-17 17:38:22 +020025013/* reset v:option_new, v:option_old and v:option_type */
25014 void
25015reset_v_option_vars()
25016{
25017 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25018 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25019 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25020}
25021
25022
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023#endif /* FEAT_EVAL */
25024
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025026#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027
25028#ifdef WIN3264
25029/*
25030 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25031 */
25032static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25033static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25034static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25035
25036/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025037 * Get the short path (8.3) for the filename in "fnamep".
25038 * Only works for a valid file name.
25039 * When the path gets longer "fnamep" is changed and the allocated buffer
25040 * is put in "bufp".
25041 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25042 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 */
25044 static int
25045get_short_pathname(fnamep, bufp, fnamelen)
25046 char_u **fnamep;
25047 char_u **bufp;
25048 int *fnamelen;
25049{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025050 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051 char_u *newbuf;
25052
25053 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 l = GetShortPathName(*fnamep, *fnamep, len);
25055 if (l > len - 1)
25056 {
25057 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025058 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059 newbuf = vim_strnsave(*fnamep, l);
25060 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062
25063 vim_free(*bufp);
25064 *fnamep = *bufp = newbuf;
25065
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025066 /* Really should always succeed, as the buffer is big enough. */
25067 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 }
25069
25070 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025071 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072}
25073
25074/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025075 * Get the short path (8.3) for the filename in "fname". The converted
25076 * path is returned in "bufp".
25077 *
25078 * Some of the directories specified in "fname" may not exist. This function
25079 * will shorten the existing directories at the beginning of the path and then
25080 * append the remaining non-existing path.
25081 *
25082 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025083 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025084 * bufp - Pointer to an allocated buffer for the filename.
25085 * fnamelen - Length of the filename pointed to by fname
25086 *
25087 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088 */
25089 static int
25090shortpath_for_invalid_fname(fname, bufp, fnamelen)
25091 char_u **fname;
25092 char_u **bufp;
25093 int *fnamelen;
25094{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025095 char_u *short_fname, *save_fname, *pbuf_unused;
25096 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025098 int old_len, len;
25099 int new_len, sfx_len;
25100 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101
25102 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025103 old_len = *fnamelen;
25104 save_fname = vim_strnsave(*fname, old_len);
25105 pbuf_unused = NULL;
25106 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025107
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025108 endp = save_fname + old_len - 1; /* Find the end of the copy */
25109 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025111 /*
25112 * Try shortening the supplied path till it succeeds by removing one
25113 * directory at a time from the tail of the path.
25114 */
25115 len = 0;
25116 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025118 /* go back one path-separator */
25119 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25120 --endp;
25121 if (endp <= save_fname)
25122 break; /* processed the complete path */
25123
25124 /*
25125 * Replace the path separator with a NUL and try to shorten the
25126 * resulting path.
25127 */
25128 ch = *endp;
25129 *endp = 0;
25130 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025131 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025132 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25133 {
25134 retval = FAIL;
25135 goto theend;
25136 }
25137 *endp = ch; /* preserve the string */
25138
25139 if (len > 0)
25140 break; /* successfully shortened the path */
25141
25142 /* failed to shorten the path. Skip the path separator */
25143 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144 }
25145
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025146 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025147 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025148 /*
25149 * Succeeded in shortening the path. Now concatenate the shortened
25150 * path with the remaining path at the tail.
25151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025152
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025153 /* Compute the length of the new path. */
25154 sfx_len = (int)(save_endp - endp) + 1;
25155 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025157 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025158 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025159 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025161 /* There is not enough space in the currently allocated string,
25162 * copy it to a buffer big enough. */
25163 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025165 {
25166 retval = FAIL;
25167 goto theend;
25168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169 }
25170 else
25171 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025172 /* Transfer short_fname to the main buffer (it's big enough),
25173 * unless get_short_pathname() did its work in-place. */
25174 *fname = *bufp = save_fname;
25175 if (short_fname != save_fname)
25176 vim_strncpy(save_fname, short_fname, len);
25177 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025179
25180 /* concat the not-shortened part of the path */
25181 vim_strncpy(*fname + len, endp, sfx_len);
25182 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025184
25185theend:
25186 vim_free(pbuf_unused);
25187 vim_free(save_fname);
25188
25189 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025190}
25191
25192/*
25193 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025194 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025195 */
25196 static int
25197shortpath_for_partial(fnamep, bufp, fnamelen)
25198 char_u **fnamep;
25199 char_u **bufp;
25200 int *fnamelen;
25201{
25202 int sepcount, len, tflen;
25203 char_u *p;
25204 char_u *pbuf, *tfname;
25205 int hasTilde;
25206
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025207 /* Count up the path separators from the RHS.. so we know which part
25208 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025210 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211 if (vim_ispathsep(*p))
25212 ++sepcount;
25213
25214 /* Need full path first (use expand_env() to remove a "~/") */
25215 hasTilde = (**fnamep == '~');
25216 if (hasTilde)
25217 pbuf = tfname = expand_env_save(*fnamep);
25218 else
25219 pbuf = tfname = FullName_save(*fnamep, FALSE);
25220
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025221 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025223 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225
25226 if (len == 0)
25227 {
25228 /* Don't have a valid filename, so shorten the rest of the
25229 * path if we can. This CAN give us invalid 8.3 filenames, but
25230 * there's not a lot of point in guessing what it might be.
25231 */
25232 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025233 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25234 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 }
25236
25237 /* Count the paths backward to find the beginning of the desired string. */
25238 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025239 {
25240#ifdef FEAT_MBYTE
25241 if (has_mbyte)
25242 p -= mb_head_off(tfname, p);
25243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244 if (vim_ispathsep(*p))
25245 {
25246 if (sepcount == 0 || (hasTilde && sepcount == 1))
25247 break;
25248 else
25249 sepcount --;
25250 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 if (hasTilde)
25253 {
25254 --p;
25255 if (p >= tfname)
25256 *p = '~';
25257 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259 }
25260 else
25261 ++p;
25262
25263 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25264 vim_free(*bufp);
25265 *fnamelen = (int)STRLEN(p);
25266 *bufp = pbuf;
25267 *fnamep = p;
25268
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025269 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270}
25271#endif /* WIN3264 */
25272
25273/*
25274 * Adjust a filename, according to a string of modifiers.
25275 * *fnamep must be NUL terminated when called. When returning, the length is
25276 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025277 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025278 * When there is an error, *fnamep is set to NULL.
25279 */
25280 int
25281modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25282 char_u *src; /* string with modifiers */
25283 int *usedlen; /* characters after src that are used */
25284 char_u **fnamep; /* file name so far */
25285 char_u **bufp; /* buffer for allocated file name or NULL */
25286 int *fnamelen; /* length of fnamep */
25287{
25288 int valid = 0;
25289 char_u *tail;
25290 char_u *s, *p, *pbuf;
25291 char_u dirname[MAXPATHL];
25292 int c;
25293 int has_fullname = 0;
25294#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025295 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 int has_shortname = 0;
25297#endif
25298
25299repeat:
25300 /* ":p" - full path/file_name */
25301 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25302 {
25303 has_fullname = 1;
25304
25305 valid |= VALID_PATH;
25306 *usedlen += 2;
25307
25308 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25309 if ((*fnamep)[0] == '~'
25310#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25311 && ((*fnamep)[1] == '/'
25312# ifdef BACKSLASH_IN_FILENAME
25313 || (*fnamep)[1] == '\\'
25314# endif
25315 || (*fnamep)[1] == NUL)
25316
25317#endif
25318 )
25319 {
25320 *fnamep = expand_env_save(*fnamep);
25321 vim_free(*bufp); /* free any allocated file name */
25322 *bufp = *fnamep;
25323 if (*fnamep == NULL)
25324 return -1;
25325 }
25326
25327 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025328 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329 {
25330 if (vim_ispathsep(*p)
25331 && p[1] == '.'
25332 && (p[2] == NUL
25333 || vim_ispathsep(p[2])
25334 || (p[2] == '.'
25335 && (p[3] == NUL || vim_ispathsep(p[3])))))
25336 break;
25337 }
25338
25339 /* FullName_save() is slow, don't use it when not needed. */
25340 if (*p != NUL || !vim_isAbsName(*fnamep))
25341 {
25342 *fnamep = FullName_save(*fnamep, *p != NUL);
25343 vim_free(*bufp); /* free any allocated file name */
25344 *bufp = *fnamep;
25345 if (*fnamep == NULL)
25346 return -1;
25347 }
25348
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025349#ifdef WIN3264
25350# if _WIN32_WINNT >= 0x0500
25351 if (vim_strchr(*fnamep, '~') != NULL)
25352 {
25353 /* Expand 8.3 filename to full path. Needed to make sure the same
25354 * file does not have two different names.
25355 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25356 p = alloc(_MAX_PATH + 1);
25357 if (p != NULL)
25358 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025359 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025360 {
25361 vim_free(*bufp);
25362 *bufp = *fnamep = p;
25363 }
25364 else
25365 vim_free(p);
25366 }
25367 }
25368# endif
25369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 /* Append a path separator to a directory. */
25371 if (mch_isdir(*fnamep))
25372 {
25373 /* Make room for one or two extra characters. */
25374 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25375 vim_free(*bufp); /* free any allocated file name */
25376 *bufp = *fnamep;
25377 if (*fnamep == NULL)
25378 return -1;
25379 add_pathsep(*fnamep);
25380 }
25381 }
25382
25383 /* ":." - path relative to the current directory */
25384 /* ":~" - path relative to the home directory */
25385 /* ":8" - shortname path - postponed till after */
25386 while (src[*usedlen] == ':'
25387 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25388 {
25389 *usedlen += 2;
25390 if (c == '8')
25391 {
25392#ifdef WIN3264
25393 has_shortname = 1; /* Postpone this. */
25394#endif
25395 continue;
25396 }
25397 pbuf = NULL;
25398 /* Need full path first (use expand_env() to remove a "~/") */
25399 if (!has_fullname)
25400 {
25401 if (c == '.' && **fnamep == '~')
25402 p = pbuf = expand_env_save(*fnamep);
25403 else
25404 p = pbuf = FullName_save(*fnamep, FALSE);
25405 }
25406 else
25407 p = *fnamep;
25408
25409 has_fullname = 0;
25410
25411 if (p != NULL)
25412 {
25413 if (c == '.')
25414 {
25415 mch_dirname(dirname, MAXPATHL);
25416 s = shorten_fname(p, dirname);
25417 if (s != NULL)
25418 {
25419 *fnamep = s;
25420 if (pbuf != NULL)
25421 {
25422 vim_free(*bufp); /* free any allocated file name */
25423 *bufp = pbuf;
25424 pbuf = NULL;
25425 }
25426 }
25427 }
25428 else
25429 {
25430 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25431 /* Only replace it when it starts with '~' */
25432 if (*dirname == '~')
25433 {
25434 s = vim_strsave(dirname);
25435 if (s != NULL)
25436 {
25437 *fnamep = s;
25438 vim_free(*bufp);
25439 *bufp = s;
25440 }
25441 }
25442 }
25443 vim_free(pbuf);
25444 }
25445 }
25446
25447 tail = gettail(*fnamep);
25448 *fnamelen = (int)STRLEN(*fnamep);
25449
25450 /* ":h" - head, remove "/file_name", can be repeated */
25451 /* Don't remove the first "/" or "c:\" */
25452 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25453 {
25454 valid |= VALID_HEAD;
25455 *usedlen += 2;
25456 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025457 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025458 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459 *fnamelen = (int)(tail - *fnamep);
25460#ifdef VMS
25461 if (*fnamelen > 0)
25462 *fnamelen += 1; /* the path separator is part of the path */
25463#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025464 if (*fnamelen == 0)
25465 {
25466 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25467 p = vim_strsave((char_u *)".");
25468 if (p == NULL)
25469 return -1;
25470 vim_free(*bufp);
25471 *bufp = *fnamep = tail = p;
25472 *fnamelen = 1;
25473 }
25474 else
25475 {
25476 while (tail > s && !after_pathsep(s, tail))
25477 mb_ptr_back(*fnamep, tail);
25478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025479 }
25480
25481 /* ":8" - shortname */
25482 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25483 {
25484 *usedlen += 2;
25485#ifdef WIN3264
25486 has_shortname = 1;
25487#endif
25488 }
25489
25490#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025491 /*
25492 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 */
25494 if (has_shortname)
25495 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025496 /* Copy the string if it is shortened by :h and when it wasn't copied
25497 * yet, because we are going to change it in place. Avoids changing
25498 * the buffer name for "%:8". */
25499 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 {
25501 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025502 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 return -1;
25504 vim_free(*bufp);
25505 *bufp = *fnamep = p;
25506 }
25507
25508 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025509 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510 if (!has_fullname && !vim_isAbsName(*fnamep))
25511 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025512 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 return -1;
25514 }
25515 else
25516 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025517 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518
Bram Moolenaardc935552011-08-17 15:23:23 +020025519 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025521 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 return -1;
25523
25524 if (l == 0)
25525 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025526 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025528 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529 return -1;
25530 }
25531 *fnamelen = l;
25532 }
25533 }
25534#endif /* WIN3264 */
25535
25536 /* ":t" - tail, just the basename */
25537 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25538 {
25539 *usedlen += 2;
25540 *fnamelen -= (int)(tail - *fnamep);
25541 *fnamep = tail;
25542 }
25543
25544 /* ":e" - extension, can be repeated */
25545 /* ":r" - root, without extension, can be repeated */
25546 while (src[*usedlen] == ':'
25547 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25548 {
25549 /* find a '.' in the tail:
25550 * - for second :e: before the current fname
25551 * - otherwise: The last '.'
25552 */
25553 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25554 s = *fnamep - 2;
25555 else
25556 s = *fnamep + *fnamelen - 1;
25557 for ( ; s > tail; --s)
25558 if (s[0] == '.')
25559 break;
25560 if (src[*usedlen + 1] == 'e') /* :e */
25561 {
25562 if (s > tail)
25563 {
25564 *fnamelen += (int)(*fnamep - (s + 1));
25565 *fnamep = s + 1;
25566#ifdef VMS
25567 /* cut version from the extension */
25568 s = *fnamep + *fnamelen - 1;
25569 for ( ; s > *fnamep; --s)
25570 if (s[0] == ';')
25571 break;
25572 if (s > *fnamep)
25573 *fnamelen = s - *fnamep;
25574#endif
25575 }
25576 else if (*fnamep <= tail)
25577 *fnamelen = 0;
25578 }
25579 else /* :r */
25580 {
25581 if (s > tail) /* remove one extension */
25582 *fnamelen = (int)(s - *fnamep);
25583 }
25584 *usedlen += 2;
25585 }
25586
25587 /* ":s?pat?foo?" - substitute */
25588 /* ":gs?pat?foo?" - global substitute */
25589 if (src[*usedlen] == ':'
25590 && (src[*usedlen + 1] == 's'
25591 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25592 {
25593 char_u *str;
25594 char_u *pat;
25595 char_u *sub;
25596 int sep;
25597 char_u *flags;
25598 int didit = FALSE;
25599
25600 flags = (char_u *)"";
25601 s = src + *usedlen + 2;
25602 if (src[*usedlen + 1] == 'g')
25603 {
25604 flags = (char_u *)"g";
25605 ++s;
25606 }
25607
25608 sep = *s++;
25609 if (sep)
25610 {
25611 /* find end of pattern */
25612 p = vim_strchr(s, sep);
25613 if (p != NULL)
25614 {
25615 pat = vim_strnsave(s, (int)(p - s));
25616 if (pat != NULL)
25617 {
25618 s = p + 1;
25619 /* find end of substitution */
25620 p = vim_strchr(s, sep);
25621 if (p != NULL)
25622 {
25623 sub = vim_strnsave(s, (int)(p - s));
25624 str = vim_strnsave(*fnamep, *fnamelen);
25625 if (sub != NULL && str != NULL)
25626 {
25627 *usedlen = (int)(p + 1 - src);
25628 s = do_string_sub(str, pat, sub, flags);
25629 if (s != NULL)
25630 {
25631 *fnamep = s;
25632 *fnamelen = (int)STRLEN(s);
25633 vim_free(*bufp);
25634 *bufp = s;
25635 didit = TRUE;
25636 }
25637 }
25638 vim_free(sub);
25639 vim_free(str);
25640 }
25641 vim_free(pat);
25642 }
25643 }
25644 /* after using ":s", repeat all the modifiers */
25645 if (didit)
25646 goto repeat;
25647 }
25648 }
25649
Bram Moolenaar26df0922014-02-23 23:39:13 +010025650 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25651 {
25652 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25653 if (p == NULL)
25654 return -1;
25655 vim_free(*bufp);
25656 *bufp = *fnamep = p;
25657 *fnamelen = (int)STRLEN(p);
25658 *usedlen += 2;
25659 }
25660
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 return valid;
25662}
25663
25664/*
25665 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25666 * "flags" can be "g" to do a global substitute.
25667 * Returns an allocated string, NULL for error.
25668 */
25669 char_u *
25670do_string_sub(str, pat, sub, flags)
25671 char_u *str;
25672 char_u *pat;
25673 char_u *sub;
25674 char_u *flags;
25675{
25676 int sublen;
25677 regmatch_T regmatch;
25678 int i;
25679 int do_all;
25680 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025681 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 garray_T ga;
25683 char_u *ret;
25684 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025685 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686
25687 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25688 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025689 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025690
25691 ga_init2(&ga, 1, 200);
25692
25693 do_all = (flags[0] == 'g');
25694
25695 regmatch.rm_ic = p_ic;
25696 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25697 if (regmatch.regprog != NULL)
25698 {
25699 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025700 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25702 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025703 /* Skip empty match except for first match. */
25704 if (regmatch.startp[0] == regmatch.endp[0])
25705 {
25706 if (zero_width == regmatch.startp[0])
25707 {
25708 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025709 i = MB_PTR2LEN(tail);
25710 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25711 (size_t)i);
25712 ga.ga_len += i;
25713 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025714 continue;
25715 }
25716 zero_width = regmatch.startp[0];
25717 }
25718
Bram Moolenaar071d4272004-06-13 20:20:40 +000025719 /*
25720 * Get some space for a temporary buffer to do the substitution
25721 * into. It will contain:
25722 * - The text up to where the match is.
25723 * - The substituted text.
25724 * - The text after the match.
25725 */
25726 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025727 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25729 {
25730 ga_clear(&ga);
25731 break;
25732 }
25733
25734 /* copy the text up to where the match is */
25735 i = (int)(regmatch.startp[0] - tail);
25736 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25737 /* add the substituted text */
25738 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25739 + ga.ga_len + i, TRUE, TRUE, FALSE);
25740 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025741 tail = regmatch.endp[0];
25742 if (*tail == NUL)
25743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744 if (!do_all)
25745 break;
25746 }
25747
25748 if (ga.ga_data != NULL)
25749 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25750
Bram Moolenaar473de612013-06-08 18:19:48 +020025751 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752 }
25753
25754 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25755 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025756 if (p_cpo == empty_option)
25757 p_cpo = save_cpo;
25758 else
25759 /* Darn, evaluating {sub} expression changed the value. */
25760 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761
25762 return ret;
25763}
25764
25765#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */