blob: 6a844e434ce346263874cf364903dc22ae5c2f6b [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 Moolenaar33570922005-01-25 22:26:29 +0000371};
372
373/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000374#define vv_type vv_di.di_tv.v_type
375#define vv_nr vv_di.di_tv.vval.v_number
376#define vv_float vv_di.di_tv.vval.v_float
377#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000378#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200379#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000381
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200382static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000383#define vimvarht vimvardict.dv_hashtab
384
Bram Moolenaara40058a2005-07-11 22:42:07 +0000385static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
386static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
388static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
389static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
391static void list_glob_vars __ARGS((int *first));
392static void list_buf_vars __ARGS((int *first));
393static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000394#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000395static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000397static void list_vim_vars __ARGS((int *first));
398static void list_script_vars __ARGS((int *first));
399static void list_func_vars __ARGS((int *first));
400static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000401static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
402static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100403static 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 +0000404static void clear_lval __ARGS((lval_T *lp));
405static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
406static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
408static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
409static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
410static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
411static void item_lock __ARGS((typval_T *tv, int deep, int lock));
412static int tv_islocked __ARGS((typval_T *tv));
413
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
415static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000420static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
421static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000423static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000428static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100430static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
431static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
432static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000433static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000435static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
437static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100440static 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 +0000441static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000442static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200443static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
445static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000446static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static int string2float __ARGS((char_u *text, float_T *value));
455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
457static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100458static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static 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 +0200460static 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 +0000461static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000462static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100469static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200473static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200478static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100489static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100491static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#ifdef FEAT_FLOAT
494static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
495#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000496static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000499static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000501#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000502static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000503static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
509static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200510static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
515static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200525static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200527#ifdef FEAT_FLOAT
528static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
529#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000532static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#ifdef FEAT_FLOAT
539static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000543static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000552static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000554static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000560static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200561static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000569static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000570static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200571static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000572static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000573static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200576static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000577static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100583static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000586static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000600static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100605static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000607static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000619#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200620static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000621static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200623#ifdef FEAT_LUA
624static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200631static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000632static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000633static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000635static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000639#ifdef vim_mkdir
640static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100643#ifdef FEAT_MZSCHEME
644static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100648static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000649static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000650#ifdef FEAT_FLOAT
651static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000654static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000655static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200656#ifdef FEAT_PYTHON3
657static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
658#endif
659#ifdef FEAT_PYTHON
660static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
661#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000663static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000664static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000676#ifdef FEAT_FLOAT
677static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
678#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200679static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100681static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000684static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000686static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
691static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000693static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000694static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000695static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000696static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200698static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000699static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100701#ifdef FEAT_CRYPT
702static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
703#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000704static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200705static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#ifdef FEAT_FLOAT
708static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200709static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000710#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000712static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000713static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#ifdef FEAT_FLOAT
717static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000720static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200721static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722#ifdef HAVE_STRFTIME
723static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
724#endif
725static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200731static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200732static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000738static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200739static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200741static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000742static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000743static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000744static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000745static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000746static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000748static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200749#ifdef FEAT_FLOAT
750static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
752#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#ifdef FEAT_FLOAT
757static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
758#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200760static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200761static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100762static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100766static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
768static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000773static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000776static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100777static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778
Bram Moolenaar493c1782014-05-28 14:34:46 +0200779static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000780static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static int get_env_len __ARGS((char_u **arg));
782static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000783static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000784static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
785#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
786#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
787 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000788static 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 +0000789static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000790static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200791static 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 +0000792static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static typval_T *alloc_tv __ARGS((void));
794static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static void init_tv __ARGS((typval_T *varp));
796static long get_tv_number __ARGS((typval_T *varp));
797static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000798static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static char_u *get_tv_string __ARGS((typval_T *varp));
800static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000801static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100802static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
803static 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 +0000804static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
805static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
806static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000807static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
808static 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 +0000809static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200810static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
811static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200812static int var_check_func_name __ARGS((char_u *name, int new_var));
813static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200814static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000815static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
817static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
818static int eval_fname_script __ARGS((char_u *p));
819static int eval_fname_sid __ARGS((char_u *p));
820static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static ufunc_T *find_func __ARGS((char_u *name));
822static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200823static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000824#ifdef FEAT_PROFILE
825static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000826static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
827static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_total_cmp __ARGS((const void *s1, const void *s2));
833static int
834# ifdef __BORLANDC__
835 _RTLENTRYF
836# endif
837 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200839static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000840static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000841static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000842static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000843static 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 +0000844static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
845static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000846static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000847static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
848static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000849static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000850static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000851static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200852static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200853static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000854
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200855
856#ifdef EBCDIC
857static int compare_func_name __ARGS((const void *s1, const void *s2));
858static void sortFunctions __ARGS(());
859#endif
860
Bram Moolenaar33570922005-01-25 22:26:29 +0000861/*
862 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000863 */
864 void
865eval_init()
866{
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 int i;
868 struct vimvar *p;
869
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200870 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
871 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200872 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000873 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000874 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875
876 for (i = 0; i < VV_LEN; ++i)
877 {
878 p = &vimvars[i];
879 STRCPY(p->vv_di.di_key, p->vv_name);
880 if (p->vv_flags & VV_RO)
881 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
882 else if (p->vv_flags & VV_RO_SBX)
883 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
884 else
885 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000886
887 /* add to v: scope dict, unless the value is not always available */
888 if (p->vv_type != VAR_UNKNOWN)
889 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000890 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000891 /* add to compat scope dict */
892 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000893 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000894 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100895 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200896 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200897 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200898
899#ifdef EBCDIC
900 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100901 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200902 */
903 sortFunctions();
904#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000905}
906
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907#if defined(EXITFREE) || defined(PROTO)
908 void
909eval_clear()
910{
911 int i;
912 struct vimvar *p;
913
914 for (i = 0; i < VV_LEN; ++i)
915 {
916 p = &vimvars[i];
917 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000919 vim_free(p->vv_str);
920 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000921 }
922 else if (p->vv_di.di_tv.v_type == VAR_LIST)
923 {
924 list_unref(p->vv_list);
925 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000927 }
928 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000929 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930 hash_clear(&compat_hashtab);
931
Bram Moolenaard9fba312005-06-26 22:34:35 +0000932 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100933# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200934 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100935# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936
937 /* global variables */
938 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000939
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000940 /* autoloaded script names */
941 ga_clear_strings(&ga_loaded);
942
Bram Moolenaarcca74132013-09-25 21:00:28 +0200943 /* Script-local variables. First clear all the variables and in a second
944 * loop free the scriptvar_T, because a variable in one script might hold
945 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200946 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200947 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200948 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200949 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200950 ga_clear(&ga_scripts);
951
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000952 /* unreferenced lists and dicts */
953 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000954
955 /* functions */
956 free_all_functions();
957 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000958}
959#endif
960
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 * Return the name of the executed function.
963 */
964 char_u *
965func_name(cookie)
966 void *cookie;
967{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000968 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969}
970
971/*
972 * Return the address holding the next breakpoint line for a funccall cookie.
973 */
974 linenr_T *
975func_breakpoint(cookie)
976 void *cookie;
977{
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980
981/*
982 * Return the address holding the debug tick for a funccall cookie.
983 */
984 int *
985func_dbg_tick(cookie)
986 void *cookie;
987{
Bram Moolenaar33570922005-01-25 22:26:29 +0000988 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991/*
992 * Return the nesting level for a funccall cookie.
993 */
994 int
995func_level(cookie)
996 void *cookie;
997{
Bram Moolenaar33570922005-01-25 22:26:29 +0000998 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001002funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001004/* pointer to list of previously used funccal, still around because some
1005 * item in it is still being used. */
1006funccall_T *previous_funccal = NULL;
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008/*
1009 * Return TRUE when a function was ended by a ":return" command.
1010 */
1011 int
1012current_func_returned()
1013{
1014 return current_funccal->returned;
1015}
1016
1017
1018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 * Set an internal variable to a string value. Creates the variable if it does
1020 * not already exist.
1021 */
1022 void
1023set_internal_string_var(name, value)
1024 char_u *name;
1025 char_u *value;
1026{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001027 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001028 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 val = vim_strsave(value);
1031 if (val != NULL)
1032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001033 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001034 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001036 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001037 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039 }
1040}
1041
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001043static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044static char_u *redir_endp = NULL;
1045static char_u *redir_varname = NULL;
1046
1047/*
1048 * Start recording command output to a variable
1049 * Returns OK if successfully completed the setup. FAIL otherwise.
1050 */
1051 int
1052var_redir_start(name, append)
1053 char_u *name;
1054 int append; /* append to an existing variable */
1055{
1056 int save_emsg;
1057 int err;
1058 typval_T tv;
1059
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001060 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001061 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 {
1063 EMSG(_(e_invarg));
1064 return FAIL;
1065 }
1066
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 redir_varname = vim_strsave(name);
1069 if (redir_varname == NULL)
1070 return FAIL;
1071
1072 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1073 if (redir_lval == NULL)
1074 {
1075 var_redir_stop();
1076 return FAIL;
1077 }
1078
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001079 /* The output is stored in growarray "redir_ga" until redirection ends. */
1080 ga_init2(&redir_ga, (int)sizeof(char), 500);
1081
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001083 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001084 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1086 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001087 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (redir_endp != NULL && *redir_endp != NUL)
1089 /* Trailing characters are present after the variable name */
1090 EMSG(_(e_trailing));
1091 else
1092 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001093 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 var_redir_stop();
1095 return FAIL;
1096 }
1097
1098 /* check if we can write to the variable: set it to or append an empty
1099 * string */
1100 save_emsg = did_emsg;
1101 did_emsg = FALSE;
1102 tv.v_type = VAR_STRING;
1103 tv.vval.v_string = (char_u *)"";
1104 if (append)
1105 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1106 else
1107 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001108 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001110 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 if (err)
1112 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001113 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 var_redir_stop();
1115 return FAIL;
1116 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 return OK;
1119}
1120
1121/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 * Append "value[value_len]" to the variable set by var_redir_start().
1123 * The actual appending is postponed until redirection ends, because the value
1124 * appended may in fact be the string we write to, changing it may cause freed
1125 * memory to be used:
1126 * :redir => foo
1127 * :let foo
1128 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 */
1130 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136
1137 if (redir_lval == NULL)
1138 return;
1139
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001141 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001143 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146 {
1147 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001148 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149 }
1150 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152}
1153
1154/*
1155 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 */
1158 void
1159var_redir_stop()
1160{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001161 typval_T tv;
1162
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (redir_lval != NULL)
1164 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 /* If there was no error: assign the text to the variable. */
1166 if (redir_endp != NULL)
1167 {
1168 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1169 tv.v_type = VAR_STRING;
1170 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001171 /* Call get_lval() again, if it's inside a Dict or List it may
1172 * have changed. */
1173 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001174 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001175 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1176 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1177 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001178 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001179
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 /* free the collected output */
1181 vim_free(redir_ga.ga_data);
1182 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001183
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 vim_free(redir_lval);
1185 redir_lval = NULL;
1186 }
1187 vim_free(redir_varname);
1188 redir_varname = NULL;
1189}
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191# if defined(FEAT_MBYTE) || defined(PROTO)
1192 int
1193eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1194 char_u *enc_from;
1195 char_u *enc_to;
1196 char_u *fname_from;
1197 char_u *fname_to;
1198{
1199 int err = FALSE;
1200
1201 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1202 set_vim_var_string(VV_CC_TO, enc_to, -1);
1203 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1204 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1205 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_CC_FROM, NULL, -1);
1208 set_vim_var_string(VV_CC_TO, NULL, -1);
1209 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1210 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1211
1212 if (err)
1213 return FAIL;
1214 return OK;
1215}
1216# endif
1217
1218# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1219 int
1220eval_printexpr(fname, args)
1221 char_u *fname;
1222 char_u *args;
1223{
1224 int err = FALSE;
1225
1226 set_vim_var_string(VV_FNAME_IN, fname, -1);
1227 set_vim_var_string(VV_CMDARG, args, -1);
1228 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1229 err = TRUE;
1230 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1231 set_vim_var_string(VV_CMDARG, NULL, -1);
1232
1233 if (err)
1234 {
1235 mch_remove(fname);
1236 return FAIL;
1237 }
1238 return OK;
1239}
1240# endif
1241
1242# if defined(FEAT_DIFF) || defined(PROTO)
1243 void
1244eval_diff(origfile, newfile, outfile)
1245 char_u *origfile;
1246 char_u *newfile;
1247 char_u *outfile;
1248{
1249 int err = FALSE;
1250
1251 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1252 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1253 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1254 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1255 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1256 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1257 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1258}
1259
1260 void
1261eval_patch(origfile, difffile, outfile)
1262 char_u *origfile;
1263 char_u *difffile;
1264 char_u *outfile;
1265{
1266 int err;
1267
1268 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1269 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1270 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1271 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1272 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1273 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1274 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1275}
1276# endif
1277
1278/*
1279 * Top level evaluation function, returning a boolean.
1280 * Sets "error" to TRUE if there was an error.
1281 * Return TRUE or FALSE.
1282 */
1283 int
1284eval_to_bool(arg, error, nextcmd, skip)
1285 char_u *arg;
1286 int *error;
1287 char_u **nextcmd;
1288 int skip; /* only parse, don't execute */
1289{
Bram Moolenaar33570922005-01-25 22:26:29 +00001290 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 int retval = FALSE;
1292
1293 if (skip)
1294 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 else
1298 {
1299 *error = FALSE;
1300 if (!skip)
1301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001302 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305 }
1306 if (skip)
1307 --emsg_skip;
1308
1309 return retval;
1310}
1311
1312/*
1313 * Top level evaluation function, returning a string. If "skip" is TRUE,
1314 * only parsing to "nextcmd" is done, without reporting errors. Return
1315 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1316 */
1317 char_u *
1318eval_to_string_skip(arg, nextcmd, skip)
1319 char_u *arg;
1320 char_u **nextcmd;
1321 int skip; /* only parse, don't execute */
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 char_u *retval;
1325
1326 if (skip)
1327 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001328 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 retval = NULL;
1330 else
1331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001332 retval = vim_strsave(get_tv_string(&tv));
1333 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
1335 if (skip)
1336 --emsg_skip;
1337
1338 return retval;
1339}
1340
1341/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001342 * Skip over an expression at "*pp".
1343 * Return FAIL for an error, OK otherwise.
1344 */
1345 int
1346skip_expr(pp)
1347 char_u **pp;
1348{
Bram Moolenaar33570922005-01-25 22:26:29 +00001349 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001350
1351 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001353}
1354
1355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001357 * When "convert" is TRUE convert a List into a sequence of lines and convert
1358 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * Return pointer to allocated memory, or NULL for failure.
1360 */
1361 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 char_u *arg;
1364 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366{
Bram Moolenaar33570922005-01-25 22:26:29 +00001367 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001370#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 retval = NULL;
1376 else
1377 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001378 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001379 {
1380 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001381 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001382 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001383 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001384 if (tv.vval.v_list->lv_len > 0)
1385 ga_append(&ga, NL);
1386 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001387 ga_append(&ga, NUL);
1388 retval = (char_u *)ga.ga_data;
1389 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001390#ifdef FEAT_FLOAT
1391 else if (convert && tv.v_type == VAR_FLOAT)
1392 {
1393 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1394 retval = vim_strsave(numbuf);
1395 }
1396#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 else
1398 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401
1402 return retval;
1403}
1404
1405/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 * Call eval_to_string() without using current local variables and using
1407 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 */
1409 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 char_u *arg;
1412 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415 char_u *retval;
1416 void *save_funccalp;
1417
1418 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001419 if (use_sandbox)
1420 ++sandbox;
1421 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001422 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001423 if (use_sandbox)
1424 --sandbox;
1425 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 restore_funccal(save_funccalp);
1427 return retval;
1428}
1429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430/*
1431 * Top level evaluation function, returning a number.
1432 * Evaluates "expr" silently.
1433 * Returns -1 for an error.
1434 */
1435 int
1436eval_to_number(expr)
1437 char_u *expr;
1438{
Bram Moolenaar33570922005-01-25 22:26:29 +00001439 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001441 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443 ++emsg_off;
1444
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 retval = -1;
1447 else
1448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001449 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 --emsg_off;
1453
1454 return retval;
1455}
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457/*
1458 * Prepare v: variable "idx" to be used.
1459 * Save the current typeval in "save_tv".
1460 * When not used yet add the variable to the v: hashtable.
1461 */
1462 static void
1463prepare_vimvar(idx, save_tv)
1464 int idx;
1465 typval_T *save_tv;
1466{
1467 *save_tv = vimvars[idx].vv_tv;
1468 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1469 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1470}
1471
1472/*
1473 * Restore v: variable "idx" to typeval "save_tv".
1474 * When no longer defined, remove the variable from the v: hashtable.
1475 */
1476 static void
1477restore_vimvar(idx, save_tv)
1478 int idx;
1479 typval_T *save_tv;
1480{
1481 hashitem_T *hi;
1482
Bram Moolenaara40058a2005-07-11 22:42:07 +00001483 vimvars[idx].vv_tv = *save_tv;
1484 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1485 {
1486 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1487 if (HASHITEM_EMPTY(hi))
1488 EMSG2(_(e_intern2), "restore_vimvar()");
1489 else
1490 hash_remove(&vimvarht, hi);
1491 }
1492}
1493
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001494#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001495/*
1496 * Evaluate an expression to a list with suggestions.
1497 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001498 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001499 */
1500 list_T *
1501eval_spell_expr(badword, expr)
1502 char_u *badword;
1503 char_u *expr;
1504{
1505 typval_T save_val;
1506 typval_T rettv;
1507 list_T *list = NULL;
1508 char_u *p = skipwhite(expr);
1509
1510 /* Set "v:val" to the bad word. */
1511 prepare_vimvar(VV_VAL, &save_val);
1512 vimvars[VV_VAL].vv_type = VAR_STRING;
1513 vimvars[VV_VAL].vv_str = badword;
1514 if (p_verbose == 0)
1515 ++emsg_off;
1516
1517 if (eval1(&p, &rettv, TRUE) == OK)
1518 {
1519 if (rettv.v_type != VAR_LIST)
1520 clear_tv(&rettv);
1521 else
1522 list = rettv.vval.v_list;
1523 }
1524
1525 if (p_verbose == 0)
1526 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001527 restore_vimvar(VV_VAL, &save_val);
1528
1529 return list;
1530}
1531
1532/*
1533 * "list" is supposed to contain two items: a word and a number. Return the
1534 * word in "pp" and the number as the return value.
1535 * Return -1 if anything isn't right.
1536 * Used to get the good word and score from the eval_spell_expr() result.
1537 */
1538 int
1539get_spellword(list, pp)
1540 list_T *list;
1541 char_u **pp;
1542{
1543 listitem_T *li;
1544
1545 li = list->lv_first;
1546 if (li == NULL)
1547 return -1;
1548 *pp = get_tv_string(&li->li_tv);
1549
1550 li = li->li_next;
1551 if (li == NULL)
1552 return -1;
1553 return get_tv_number(&li->li_tv);
1554}
1555#endif
1556
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001557/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001558 * Top level evaluation function.
1559 * Returns an allocated typval_T with the result.
1560 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 */
1562 typval_T *
1563eval_expr(arg, nextcmd)
1564 char_u *arg;
1565 char_u **nextcmd;
1566{
1567 typval_T *tv;
1568
1569 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001570 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571 {
1572 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001573 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001574 }
1575
1576 return tv;
1577}
1578
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001581 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582 * Uses argv[argc] for the function arguments. Only Number and String
1583 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001586 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001587call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 char_u *func;
1589 int argc;
1590 char_u **argv;
1591 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001592 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
Bram Moolenaar33570922005-01-25 22:26:29 +00001595 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 long n;
1597 int len;
1598 int i;
1599 int doesrange;
1600 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001603 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001605 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607 for (i = 0; i < argc; i++)
1608 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001609 /* Pass a NULL or empty argument as an empty string */
1610 if (argv[i] == NULL || *argv[i] == NUL)
1611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001612 argvars[i].v_type = VAR_STRING;
1613 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001614 continue;
1615 }
1616
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001617 if (str_arg_only)
1618 len = 0;
1619 else
1620 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001621 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (len != 0 && len == (int)STRLEN(argv[i]))
1623 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001624 argvars[i].v_type = VAR_NUMBER;
1625 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627 else
1628 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001629 argvars[i].v_type = VAR_STRING;
1630 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
1632 }
1633
1634 if (safe)
1635 {
1636 save_funccalp = save_funccal();
1637 ++sandbox;
1638 }
1639
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1641 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (safe)
1645 {
1646 --sandbox;
1647 restore_funccal(save_funccalp);
1648 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 vim_free(argvars);
1650
1651 if (ret == FAIL)
1652 clear_tv(rettv);
1653
1654 return ret;
1655}
1656
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001657/*
1658 * Call vimL function "func" and return the result as a number.
1659 * Returns -1 when calling the function fails.
1660 * Uses argv[argc] for the function arguments.
1661 */
1662 long
1663call_func_retnr(func, argc, argv, safe)
1664 char_u *func;
1665 int argc;
1666 char_u **argv;
1667 int safe; /* use the sandbox */
1668{
1669 typval_T rettv;
1670 long retval;
1671
1672 /* All arguments are passed as strings, no conversion to number. */
1673 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1674 return -1;
1675
1676 retval = get_tv_number_chk(&rettv, NULL);
1677 clear_tv(&rettv);
1678 return retval;
1679}
1680
1681#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1682 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1683
Bram Moolenaar4f688582007-07-24 12:34:30 +00001684# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001686 * Call vimL function "func" and return the result as a string.
1687 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 * Uses argv[argc] for the function arguments.
1689 */
1690 void *
1691call_func_retstr(func, argc, argv, safe)
1692 char_u *func;
1693 int argc;
1694 char_u **argv;
1695 int safe; /* use the sandbox */
1696{
1697 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001698 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001700 /* All arguments are passed as strings, no conversion to number. */
1701 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702 return NULL;
1703
1704 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 return retval;
1707}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001708# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001710/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001711 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001713 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 */
1715 void *
1716call_func_retlist(func, argc, argv, safe)
1717 char_u *func;
1718 int argc;
1719 char_u **argv;
1720 int safe; /* use the sandbox */
1721{
1722 typval_T rettv;
1723
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001724 /* All arguments are passed as strings, no conversion to number. */
1725 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726 return NULL;
1727
1728 if (rettv.v_type != VAR_LIST)
1729 {
1730 clear_tv(&rettv);
1731 return NULL;
1732 }
1733
1734 return rettv.vval.v_list;
1735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#endif
1737
1738/*
1739 * Save the current function call pointer, and set it to NULL.
1740 * Used when executing autocommands and for ":source".
1741 */
1742 void *
1743save_funccal()
1744{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001745 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 current_funccal = NULL;
1748 return (void *)fc;
1749}
1750
1751 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001752restore_funccal(vfc)
1753 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001755 funccall_T *fc = (funccall_T *)vfc;
1756
1757 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758}
1759
Bram Moolenaar05159a02005-02-26 23:04:13 +00001760#if defined(FEAT_PROFILE) || defined(PROTO)
1761/*
1762 * Prepare profiling for entering a child or something else that is not
1763 * counted for the script/function itself.
1764 * Should always be called in pair with prof_child_exit().
1765 */
1766 void
1767prof_child_enter(tm)
1768 proftime_T *tm; /* place to store waittime */
1769{
1770 funccall_T *fc = current_funccal;
1771
1772 if (fc != NULL && fc->func->uf_profiling)
1773 profile_start(&fc->prof_child);
1774 script_prof_save(tm);
1775}
1776
1777/*
1778 * Take care of time spent in a child.
1779 * Should always be called after prof_child_enter().
1780 */
1781 void
1782prof_child_exit(tm)
1783 proftime_T *tm; /* where waittime was stored */
1784{
1785 funccall_T *fc = current_funccal;
1786
1787 if (fc != NULL && fc->func->uf_profiling)
1788 {
1789 profile_end(&fc->prof_child);
1790 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1791 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1792 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1793 }
1794 script_prof_restore(tm);
1795}
1796#endif
1797
1798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#ifdef FEAT_FOLDING
1800/*
1801 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1802 * it in "*cp". Doesn't give error messages.
1803 */
1804 int
1805eval_foldexpr(arg, cp)
1806 char_u *arg;
1807 int *cp;
1808{
Bram Moolenaar33570922005-01-25 22:26:29 +00001809 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 int retval;
1811 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001812 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1813 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001816 if (use_sandbox)
1817 ++sandbox;
1818 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 retval = 0;
1822 else
1823 {
1824 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 if (tv.v_type == VAR_NUMBER)
1826 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001827 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 retval = 0;
1829 else
1830 {
1831 /* If the result is a string, check if there is a non-digit before
1832 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (!VIM_ISDIGIT(*s) && *s != '-')
1835 *cp = *s++;
1836 retval = atol((char *)s);
1837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001841 if (use_sandbox)
1842 --sandbox;
1843 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
1845 return retval;
1846}
1847#endif
1848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let" list all variable values
1851 * ":let var1 var2" list variable values
1852 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001853 * ":let var += expr" assignment command.
1854 * ":let var -= expr" assignment command.
1855 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 */
1858 void
1859ex_let(eap)
1860 exarg_T *eap;
1861{
1862 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001864 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 int var_count = 0;
1867 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001869 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Bram Moolenaardb552d602006-03-23 22:59:57 +00001872 argend = skip_var_list(arg, &var_count, &semicolon);
1873 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001875 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1876 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001877 expr = skipwhite(argend);
1878 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1879 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001881 /*
1882 * ":let" without "=": list variables
1883 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 if (*arg == '[')
1885 EMSG(_(e_invarg));
1886 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001888 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_glob_vars(&first);
1893 list_buf_vars(&first);
1894 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001895#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001896 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001897#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 list_script_vars(&first);
1899 list_func_vars(&first);
1900 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 eap->nextcmd = check_nextcmd(arg);
1903 }
1904 else
1905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 op[0] = '=';
1907 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1911 op[0] = *expr; /* +=, -= or .= */
1912 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001914 else
1915 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (eap->skip)
1918 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (eap->skip)
1921 {
1922 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 --emsg_skip;
1925 }
1926 else if (i != FAIL)
1927 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 }
1932 }
1933}
1934
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935/*
1936 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1937 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 * When "nextchars" is not NULL it points to a string with characters that
1939 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1940 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 * Returns OK or FAIL;
1942 */
1943 static int
1944ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1945 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 int copy; /* copy values from "tv", don't move */
1948 int semicolon; /* from skip_var_list() */
1949 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951{
1952 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 listitem_T *item;
1956 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957
1958 if (*arg != '[')
1959 {
1960 /*
1961 * ":let var = expr" or ":for var in list"
1962 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 return OK;
1966 }
1967
1968 /*
1969 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1970 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001971 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 {
1973 EMSG(_(e_listreq));
1974 return FAIL;
1975 }
1976
1977 i = list_len(l);
1978 if (semicolon == 0 && var_count < i)
1979 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001980 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 return FAIL;
1982 }
1983 if (var_count - semicolon > i)
1984 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001985 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 return FAIL;
1987 }
1988
1989 item = l->lv_first;
1990 while (*arg != ']')
1991 {
1992 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 item = item->li_next;
1995 if (arg == NULL)
1996 return FAIL;
1997
1998 arg = skipwhite(arg);
1999 if (*arg == ';')
2000 {
2001 /* Put the rest of the list (may be empty) in the var after ';'.
2002 * Create a new list for this. */
2003 l = list_alloc();
2004 if (l == NULL)
2005 return FAIL;
2006 while (item != NULL)
2007 {
2008 list_append_tv(l, &item->li_tv);
2009 item = item->li_next;
2010 }
2011
2012 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002013 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 ltv.vval.v_list = l;
2015 l->lv_refcount = 1;
2016
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002017 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2018 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 clear_tv(&ltv);
2020 if (arg == NULL)
2021 return FAIL;
2022 break;
2023 }
2024 else if (*arg != ',' && *arg != ']')
2025 {
2026 EMSG2(_(e_intern2), "ex_let_vars()");
2027 return FAIL;
2028 }
2029 }
2030
2031 return OK;
2032}
2033
2034/*
2035 * Skip over assignable variable "var" or list of variables "[var, var]".
2036 * Used for ":let varvar = expr" and ":for varvar in expr".
2037 * For "[var, var]" increment "*var_count" for each variable.
2038 * for "[var, var; var]" set "semicolon".
2039 * Return NULL for an error.
2040 */
2041 static char_u *
2042skip_var_list(arg, var_count, semicolon)
2043 char_u *arg;
2044 int *var_count;
2045 int *semicolon;
2046{
2047 char_u *p, *s;
2048
2049 if (*arg == '[')
2050 {
2051 /* "[var, var]": find the matching ']'. */
2052 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002053 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 {
2055 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2056 s = skip_var_one(p);
2057 if (s == p)
2058 {
2059 EMSG2(_(e_invarg2), p);
2060 return NULL;
2061 }
2062 ++*var_count;
2063
2064 p = skipwhite(s);
2065 if (*p == ']')
2066 break;
2067 else if (*p == ';')
2068 {
2069 if (*semicolon == 1)
2070 {
2071 EMSG(_("Double ; in list of variables"));
2072 return NULL;
2073 }
2074 *semicolon = 1;
2075 }
2076 else if (*p != ',')
2077 {
2078 EMSG2(_(e_invarg2), p);
2079 return NULL;
2080 }
2081 }
2082 return p + 1;
2083 }
2084 else
2085 return skip_var_one(arg);
2086}
2087
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002089 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002091 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 static char_u *
2093skip_var_one(arg)
2094 char_u *arg;
2095{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002096 if (*arg == '@' && arg[1] != NUL)
2097 return arg + 2;
2098 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2099 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002100}
2101
Bram Moolenaara7043832005-01-21 11:56:39 +00002102/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002103 * List variables for hashtab "ht" with prefix "prefix".
2104 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002108 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112{
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 hashitem_T *hi;
2114 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 int todo;
2116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002117 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2119 {
2120 if (!HASHITEM_EMPTY(hi))
2121 {
2122 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002123 di = HI2DI(hi);
2124 if (empty || di->di_tv.v_type != VAR_STRING
2125 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127 }
2128 }
2129}
2130
2131/*
2132 * List global variables.
2133 */
2134 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135list_glob_vars(first)
2136 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002137{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139}
2140
2141/*
2142 * List buffer variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_buf_vars(first)
2146 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002148 char_u numbuf[NUMBUFLEN];
2149
Bram Moolenaar429fa852013-04-15 12:27:36 +02002150 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002152
2153 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2155 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002156}
2157
2158/*
2159 * List window variables.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_win_vars(first)
2163 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002164{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002165 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002167}
2168
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002169#ifdef FEAT_WINDOWS
2170/*
2171 * List tab page variables.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_tab_vars(first)
2175 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179}
2180#endif
2181
Bram Moolenaara7043832005-01-21 11:56:39 +00002182/*
2183 * List Vim variables.
2184 */
2185 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_vim_vars(first)
2187 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190}
2191
2192/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002193 * List script-local variables, if there is a script.
2194 */
2195 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196list_script_vars(first)
2197 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198{
2199 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2201 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002202}
2203
2204/*
2205 * List function variables, if there is a function.
2206 */
2207 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208list_func_vars(first)
2209 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002210{
2211 if (current_funccal != NULL)
2212 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214}
2215
2216/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 * List variables in "arg".
2218 */
2219 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 exarg_T *eap;
2222 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224{
2225 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 char_u *name_start;
2229 char_u *arg_subsc;
2230 char_u *tofree;
2231 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232
2233 while (!ends_excmd(*arg) && !got_int)
2234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002237 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2239 {
2240 emsg_severe = TRUE;
2241 EMSG(_(e_trailing));
2242 break;
2243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 /* get_name_len() takes care of expanding curly braces */
2248 name_start = name = arg;
2249 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2250 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 /* This is mainly to keep test 49 working: when expanding
2253 * curly braces fails overrule the exception error message. */
2254 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 emsg_severe = TRUE;
2257 EMSG2(_(e_invarg2), arg);
2258 break;
2259 }
2260 error = TRUE;
2261 }
2262 else
2263 {
2264 if (tofree != NULL)
2265 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002266 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
2269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 /* handle d.key, l[idx], f(expr) */
2271 arg_subsc = arg;
2272 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'g': list_glob_vars(first); break;
2281 case 'b': list_buf_vars(first); break;
2282 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002283#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002284 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002286 case 'v': list_vim_vars(first); break;
2287 case 's': list_script_vars(first); break;
2288 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 default:
2290 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 }
2293 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 {
2295 char_u numbuf[NUMBUFLEN];
2296 char_u *tf;
2297 int c;
2298 char_u *s;
2299
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002300 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 c = *arg;
2302 *arg = NUL;
2303 list_one_var_a((char_u *)"",
2304 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 tv.v_type,
2306 s == NULL ? (char_u *)"" : s,
2307 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 *arg = c;
2309 vim_free(tf);
2310 }
2311 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
2314 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315
2316 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318
2319 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
2321
2322 return arg;
2323}
2324
2325/*
2326 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2327 * Returns a pointer to the char just after the var name.
2328 * Returns NULL if there is an error.
2329 */
2330 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002333 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002335 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337{
2338 int c1;
2339 char_u *name;
2340 char_u *p;
2341 char_u *arg_end = NULL;
2342 int len;
2343 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345
2346 /*
2347 * ":let $VAR = expr": Set environment variable.
2348 */
2349 if (*arg == '$')
2350 {
2351 /* Find the end of the name. */
2352 ++arg;
2353 name = arg;
2354 len = get_env_len(&arg);
2355 if (len == 0)
2356 EMSG2(_(e_invarg2), name - 1);
2357 else
2358 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 if (op != NULL && (*op == '+' || *op == '-'))
2360 EMSG2(_(e_letwrong), op);
2361 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002362 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002364 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 {
2366 c1 = name[len];
2367 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 p = get_tv_string_chk(tv);
2369 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 {
2371 int mustfree = FALSE;
2372 char_u *s = vim_getenv(name, &mustfree);
2373
2374 if (s != NULL)
2375 {
2376 p = tofree = concat_str(s, p);
2377 if (mustfree)
2378 vim_free(s);
2379 }
2380 }
2381 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 if (STRICMP(name, "HOME") == 0)
2385 init_homedir();
2386 else if (didset_vim && STRICMP(name, "VIM") == 0)
2387 didset_vim = FALSE;
2388 else if (didset_vimruntime
2389 && STRICMP(name, "VIMRUNTIME") == 0)
2390 didset_vimruntime = FALSE;
2391 arg_end = arg;
2392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 }
2396 }
2397 }
2398
2399 /*
2400 * ":let &option = expr": Set option value.
2401 * ":let &l:option = expr": Set local option value.
2402 * ":let &g:option = expr": Set global option value.
2403 */
2404 else if (*arg == '&')
2405 {
2406 /* Find the end of the name. */
2407 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002408 if (p == NULL || (endchars != NULL
2409 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 EMSG(_(e_letunexp));
2411 else
2412 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 long n;
2414 int opt_type;
2415 long numval;
2416 char_u *stringval = NULL;
2417 char_u *s;
2418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 c1 = *p;
2420 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421
2422 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 s = get_tv_string_chk(tv); /* != NULL if number or string */
2424 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 {
2426 opt_type = get_option_value(arg, &numval,
2427 &stringval, opt_flags);
2428 if ((opt_type == 1 && *op == '.')
2429 || (opt_type == 0 && *op != '.'))
2430 EMSG2(_(e_letwrong), op);
2431 else
2432 {
2433 if (opt_type == 1) /* number */
2434 {
2435 if (*op == '+')
2436 n = numval + n;
2437 else
2438 n = numval - n;
2439 }
2440 else if (opt_type == 0 && stringval != NULL) /* string */
2441 {
2442 s = concat_str(stringval, s);
2443 vim_free(stringval);
2444 stringval = s;
2445 }
2446 }
2447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 if (s != NULL)
2449 {
2450 set_option_value(arg, n, s, opt_flags);
2451 arg_end = p;
2452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457
2458 /*
2459 * ":let @r = expr": Set register contents.
2460 */
2461 else if (*arg == '@')
2462 {
2463 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 if (op != NULL && (*op == '+' || *op == '-'))
2465 EMSG2(_(e_letwrong), op);
2466 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002467 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 EMSG(_(e_letunexp));
2469 else
2470 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002471 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 char_u *s;
2473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 p = get_tv_string_chk(tv);
2475 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002477 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 if (s != NULL)
2479 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002480 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 vim_free(s);
2482 }
2483 }
2484 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002487 arg_end = arg + 1;
2488 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 }
2491 }
2492
2493 /*
2494 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002497 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002499 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002501 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2505 EMSG(_(e_letunexp));
2506 else
2507 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 arg_end = p;
2510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
2514
2515 else
2516 EMSG2(_(e_invarg2), arg);
2517
2518 return arg_end;
2519}
2520
2521/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2523 */
2524 static int
2525check_changedtick(arg)
2526 char_u *arg;
2527{
2528 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2529 {
2530 EMSG2(_(e_readonlyvar), arg);
2531 return TRUE;
2532 }
2533 return FALSE;
2534}
2535
2536/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * Get an lval: variable, Dict item or List item that can be assigned a value
2538 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2539 * "name.key", "name.key[expr]" etc.
2540 * Indexing only works if "name" is an existing List or Dictionary.
2541 * "name" points to the start of the name.
2542 * If "rettv" is not NULL it points to the value to be assigned.
2543 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2544 * wrong; must end in space or cmd separator.
2545 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002546 * flags:
2547 * GLV_QUIET: do not give error messages
2548 * GLV_NO_AUTOLOAD: do not use script autoloading
2549 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002551 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002553 */
2554 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 typval_T *rettv;
2558 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 int unlet;
2560 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002561 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 char_u *expr_start, *expr_end;
2566 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 dictitem_T *v;
2568 typval_T var1;
2569 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002575 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579
2580 if (skip)
2581 {
2582 /* When skipping just find the end of the name. */
2583 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002584 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 }
2586
2587 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002588 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (expr_start != NULL)
2590 {
2591 /* Don't expand the name when we already know there is an error. */
2592 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2593 && *p != '[' && *p != '.')
2594 {
2595 EMSG(_(e_trailing));
2596 return NULL;
2597 }
2598
2599 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2600 if (lp->ll_exp_name == NULL)
2601 {
2602 /* Report an invalid expression in braces, unless the
2603 * expression evaluation has been cancelled due to an
2604 * aborting error, an interrupt, or an exception. */
2605 if (!aborting() && !quiet)
2606 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002607 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 EMSG2(_(e_invarg2), name);
2609 return NULL;
2610 }
2611 }
2612 lp->ll_name = lp->ll_exp_name;
2613 }
2614 else
2615 lp->ll_name = name;
2616
2617 /* Without [idx] or .key we are done. */
2618 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2619 return p;
2620
2621 cc = *p;
2622 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002623 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (v == NULL && !quiet)
2625 EMSG2(_(e_undefvar), lp->ll_name);
2626 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 if (v == NULL)
2628 return NULL;
2629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 /*
2631 * Loop until no more [idx] or .key is following.
2632 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2637 && !(lp->ll_tv->v_type == VAR_DICT
2638 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E689: Can only index a List or Dictionary"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_("E708: [:] must come last"));
2648 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 len = -1;
2652 if (*p == '.')
2653 {
2654 key = p + 1;
2655 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2656 ;
2657 if (len == 0)
2658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_(e_emptykey));
2661 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 }
2663 p = key + len;
2664 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (*p == ':')
2670 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 else
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 empty1 = FALSE;
2674 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 if (get_tv_string_chk(&var1) == NULL)
2677 {
2678 /* not a number or string */
2679 clear_tv(&var1);
2680 return NULL;
2681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683
2684 /* Optionally get the second index [ :expr]. */
2685 if (*p == ':')
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002690 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 if (!empty1)
2692 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (rettv != NULL && (rettv->v_type != VAR_LIST
2696 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704 p = skipwhite(p + 1);
2705 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 else
2708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2711 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 if (get_tv_string_chk(&var2) == NULL)
2717 {
2718 /* not a number or string */
2719 if (!empty1)
2720 clear_tv(&var1);
2721 clear_tv(&var2);
2722 return NULL;
2723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (!quiet)
2733 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 if (!empty1)
2735 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
2740
2741 /* Skip to past ']'. */
2742 ++p;
2743 }
2744
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 {
2747 if (len == -1)
2748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002750 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 if (*key == NUL)
2752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
2758 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 lp->ll_list = NULL;
2760 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002761 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002762
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002763 /* When assigning to a scope dictionary check that a function and
2764 * variable name is valid (only variable name unless it is l: or
2765 * g: dictionary). Disallow overwriting a builtin function. */
2766 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002768 int prevval;
2769 int wrong;
2770
2771 if (len != -1)
2772 {
2773 prevval = key[len];
2774 key[len] = NUL;
2775 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002776 else
2777 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2779 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 || !valid_varname(key);
2782 if (len != -1)
2783 key[len] = prevval;
2784 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785 return NULL;
2786 }
2787
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 /* Can't add "v:" variable. */
2791 if (lp->ll_dict == &vimvardict)
2792 {
2793 EMSG2(_(e_illvar), name);
2794 return NULL;
2795 }
2796
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002797 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002801 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (len == -1)
2803 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 }
2806 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 if (len == -1)
2811 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 p = NULL;
2814 break;
2815 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002817 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 return NULL;
2819
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 else
2825 {
2826 /*
2827 * Get the number and item for the only or first index of the List.
2828 */
2829 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 else
2832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002833 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 clear_tv(&var1);
2835 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_list = lp->ll_tv->vval.v_list;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002841 if (lp->ll_n1 < 0)
2842 {
2843 lp->ll_n1 = 0;
2844 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2845 }
2846 }
2847 if (lp->ll_li == NULL)
2848 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002851 if (!quiet)
2852 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 }
2855
2856 /*
2857 * May need to find the item or absolute index for the second
2858 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 * When no index given: "lp->ll_empty2" is TRUE.
2860 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002864 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 {
2871 if (!quiet)
2872 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002874 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 }
2877
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2879 if (lp->ll_n1 < 0)
2880 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2881 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002882 {
2883 if (!quiet)
2884 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002886 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002887 }
2888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 }
2892
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return p;
2894}
2895
2896/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 */
2899 static void
2900clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902{
2903 vim_free(lp->ll_exp_name);
2904 vim_free(lp->ll_newkey);
2905}
2906
2907/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002908 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 */
2912 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002913set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919{
2920 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 listitem_T *ri;
2922 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923
2924 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 cc = *endp;
2929 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 if (op != NULL && *op != '=')
2931 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002934 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002935 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002936 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002937 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002939 if ((di == NULL
2940 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2941 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2942 FALSE)))
2943 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 set_var(lp->ll_name, &tv, FALSE);
2945 clear_tv(&tv);
2946 }
2947 }
2948 else
2949 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002951 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 else if (tv_check_lock(lp->ll_newkey == NULL
2954 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002955 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 else if (lp->ll_range)
2958 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002959 listitem_T *ll_li = lp->ll_li;
2960 int ll_n1 = lp->ll_n1;
2961
2962 /*
2963 * Check whether any of the list items is locked
2964 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002965 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002966 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002967 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 return;
2969 ri = ri->li_next;
2970 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2971 break;
2972 ll_li = ll_li->li_next;
2973 ++ll_n1;
2974 }
2975
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 /*
2977 * Assign the List values to the list items.
2978 */
2979 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002980 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 if (op != NULL && *op != '=')
2982 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2983 else
2984 {
2985 clear_tv(&lp->ll_li->li_tv);
2986 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 ri = ri->li_next;
2989 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2990 break;
2991 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002994 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 ri = NULL;
2997 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 lp->ll_li = lp->ll_li->li_next;
3001 ++lp->ll_n1;
3002 }
3003 if (ri != NULL)
3004 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 else if (lp->ll_empty2
3006 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 : lp->ll_n1 != lp->ll_n2)
3008 EMSG(_("E711: List value has not enough items"));
3009 }
3010 else
3011 {
3012 /*
3013 * Assign to a List or Dictionary item.
3014 */
3015 if (lp->ll_newkey != NULL)
3016 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 if (op != NULL && *op != '=')
3018 {
3019 EMSG2(_(e_letwrong), op);
3020 return;
3021 }
3022
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003024 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 if (di == NULL)
3026 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003027 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3028 {
3029 vim_free(di);
3030 return;
3031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 else if (op != NULL && *op != '=')
3035 {
3036 tv_op(lp->ll_tv, rettv, op);
3037 return;
3038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /*
3043 * Assign the value to the variable or list item.
3044 */
3045 if (copy)
3046 copy_tv(rettv, lp->ll_tv);
3047 else
3048 {
3049 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003050 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 }
3053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054}
3055
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3058 * Returns OK or FAIL.
3059 */
3060 static int
3061tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003062 typval_T *tv1;
3063 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 char_u *op;
3065{
3066 long n;
3067 char_u numbuf[NUMBUFLEN];
3068 char_u *s;
3069
3070 /* Can't do anything with a Funcref or a Dict on the right. */
3071 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3072 {
3073 switch (tv1->v_type)
3074 {
3075 case VAR_DICT:
3076 case VAR_FUNC:
3077 break;
3078
3079 case VAR_LIST:
3080 if (*op != '+' || tv2->v_type != VAR_LIST)
3081 break;
3082 /* List += List */
3083 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3084 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3085 return OK;
3086
3087 case VAR_NUMBER:
3088 case VAR_STRING:
3089 if (tv2->v_type == VAR_LIST)
3090 break;
3091 if (*op == '+' || *op == '-')
3092 {
3093 /* nr += nr or nr -= nr*/
3094 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003095#ifdef FEAT_FLOAT
3096 if (tv2->v_type == VAR_FLOAT)
3097 {
3098 float_T f = n;
3099
3100 if (*op == '+')
3101 f += tv2->vval.v_float;
3102 else
3103 f -= tv2->vval.v_float;
3104 clear_tv(tv1);
3105 tv1->v_type = VAR_FLOAT;
3106 tv1->vval.v_float = f;
3107 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003108 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003109#endif
3110 {
3111 if (*op == '+')
3112 n += get_tv_number(tv2);
3113 else
3114 n -= get_tv_number(tv2);
3115 clear_tv(tv1);
3116 tv1->v_type = VAR_NUMBER;
3117 tv1->vval.v_number = n;
3118 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 }
3120 else
3121 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122 if (tv2->v_type == VAR_FLOAT)
3123 break;
3124
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003125 /* str .= str */
3126 s = get_tv_string(tv1);
3127 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3128 clear_tv(tv1);
3129 tv1->v_type = VAR_STRING;
3130 tv1->vval.v_string = s;
3131 }
3132 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133
3134#ifdef FEAT_FLOAT
3135 case VAR_FLOAT:
3136 {
3137 float_T f;
3138
3139 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3140 && tv2->v_type != VAR_NUMBER
3141 && tv2->v_type != VAR_STRING))
3142 break;
3143 if (tv2->v_type == VAR_FLOAT)
3144 f = tv2->vval.v_float;
3145 else
3146 f = get_tv_number(tv2);
3147 if (*op == '+')
3148 tv1->vval.v_float += f;
3149 else
3150 tv1->vval.v_float -= f;
3151 }
3152 return OK;
3153#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 }
3155 }
3156
3157 EMSG2(_(e_letwrong), op);
3158 return FAIL;
3159}
3160
3161/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162 * Add a watcher to a list.
3163 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003164 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 list_T *l;
3167 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168{
3169 lw->lw_next = l->lv_watch;
3170 l->lv_watch = lw;
3171}
3172
3173/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003174 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175 * No warning when it isn't found...
3176 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003177 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 list_T *l;
3180 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181{
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183
3184 lwp = &l->lv_watch;
3185 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3186 {
3187 if (lw == lwrem)
3188 {
3189 *lwp = lw->lw_next;
3190 break;
3191 }
3192 lwp = &lw->lw_next;
3193 }
3194}
3195
3196/*
3197 * Just before removing an item from a list: advance watchers to the next
3198 * item.
3199 */
3200 static void
3201list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 list_T *l;
3203 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204{
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206
3207 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3208 if (lw->lw_item == item)
3209 lw->lw_item = item->li_next;
3210}
3211
3212/*
3213 * Evaluate the expression used in a ":for var in expr" command.
3214 * "arg" points to "var".
3215 * Set "*errp" to TRUE for an error, FALSE otherwise;
3216 * Return a pointer that holds the info. Null when there is an error.
3217 */
3218 void *
3219eval_for_line(arg, errp, nextcmdp, skip)
3220 char_u *arg;
3221 int *errp;
3222 char_u **nextcmdp;
3223 int skip;
3224{
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 typval_T tv;
3228 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229
3230 *errp = TRUE; /* default: there is an error */
3231
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 if (fi == NULL)
3234 return NULL;
3235
3236 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3237 if (expr == NULL)
3238 return fi;
3239
3240 expr = skipwhite(expr);
3241 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3242 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003243 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 return fi;
3245 }
3246
3247 if (skip)
3248 ++emsg_skip;
3249 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3250 {
3251 *errp = FALSE;
3252 if (!skip)
3253 {
3254 l = tv.vval.v_list;
3255 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 clear_tv(&tv);
3259 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 else
3261 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003262 /* No need to increment the refcount, it's already set for the
3263 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 fi->fi_list = l;
3265 list_add_watch(l, &fi->fi_lw);
3266 fi->fi_lw.lw_item = l->lv_first;
3267 }
3268 }
3269 }
3270 if (skip)
3271 --emsg_skip;
3272
3273 return fi;
3274}
3275
3276/*
3277 * Use the first item in a ":for" list. Advance to the next.
3278 * Assign the values to the variable (list). "arg" points to the first one.
3279 * Return TRUE when a valid item was found, FALSE when at end of list or
3280 * something wrong.
3281 */
3282 int
3283next_for_item(fi_void, arg)
3284 void *fi_void;
3285 char_u *arg;
3286{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003287 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003289 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290
3291 item = fi->fi_lw.lw_item;
3292 if (item == NULL)
3293 result = FALSE;
3294 else
3295 {
3296 fi->fi_lw.lw_item = item->li_next;
3297 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3298 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3299 }
3300 return result;
3301}
3302
3303/*
3304 * Free the structure used to store info used by ":for".
3305 */
3306 void
3307free_for_info(fi_void)
3308 void *fi_void;
3309{
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003312 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003313 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003315 list_unref(fi->fi_list);
3316 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 vim_free(fi);
3318}
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3321
3322 void
3323set_context_for_expression(xp, arg, cmdidx)
3324 expand_T *xp;
3325 char_u *arg;
3326 cmdidx_T cmdidx;
3327{
3328 int got_eq = FALSE;
3329 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 if (cmdidx == CMD_let)
3333 {
3334 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003335 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 {
3337 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003338 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 {
3340 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 if (vim_iswhite(*p))
3343 break;
3344 }
3345 return;
3346 }
3347 }
3348 else
3349 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3350 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 while ((xp->xp_pattern = vim_strpbrk(arg,
3352 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3353 {
3354 c = *xp->xp_pattern;
3355 if (c == '&')
3356 {
3357 c = xp->xp_pattern[1];
3358 if (c == '&')
3359 {
3360 ++xp->xp_pattern;
3361 xp->xp_context = cmdidx != CMD_let || got_eq
3362 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3363 }
3364 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003367 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3368 xp->xp_pattern += 2;
3369
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
3372 else if (c == '$')
3373 {
3374 /* environment variable */
3375 xp->xp_context = EXPAND_ENV_VARS;
3376 }
3377 else if (c == '=')
3378 {
3379 got_eq = TRUE;
3380 xp->xp_context = EXPAND_EXPRESSION;
3381 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003382 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 && xp->xp_context == EXPAND_FUNCTIONS
3384 && vim_strchr(xp->xp_pattern, '(') == NULL)
3385 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003386 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 break;
3388 }
3389 else if (cmdidx != CMD_let || got_eq)
3390 {
3391 if (c == '"') /* string */
3392 {
3393 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3394 if (c == '\\' && xp->xp_pattern[1] != NUL)
3395 ++xp->xp_pattern;
3396 xp->xp_context = EXPAND_NOTHING;
3397 }
3398 else if (c == '\'') /* literal string */
3399 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003400 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3402 /* skip */ ;
3403 xp->xp_context = EXPAND_NOTHING;
3404 }
3405 else if (c == '|')
3406 {
3407 if (xp->xp_pattern[1] == '|')
3408 {
3409 ++xp->xp_pattern;
3410 xp->xp_context = EXPAND_EXPRESSION;
3411 }
3412 else
3413 xp->xp_context = EXPAND_COMMANDS;
3414 }
3415 else
3416 xp->xp_context = EXPAND_EXPRESSION;
3417 }
3418 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003419 /* Doesn't look like something valid, expand as an expression
3420 * anyway. */
3421 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 arg = xp->xp_pattern;
3423 if (*arg != NUL)
3424 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3425 /* skip */ ;
3426 }
3427 xp->xp_pattern = arg;
3428}
3429
3430#endif /* FEAT_CMDL_COMPL */
3431
3432/*
3433 * ":1,25call func(arg1, arg2)" function call.
3434 */
3435 void
3436ex_call(eap)
3437 exarg_T *eap;
3438{
3439 char_u *arg = eap->arg;
3440 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003442 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 linenr_T lnum;
3446 int doesrange;
3447 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003450 if (eap->skip)
3451 {
3452 /* trans_function_name() doesn't work well when skipping, use eval0()
3453 * instead to skip to any following command, e.g. for:
3454 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003455 ++emsg_skip;
3456 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3457 clear_tv(&rettv);
3458 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003459 return;
3460 }
3461
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003462 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003463 if (fudi.fd_newkey != NULL)
3464 {
3465 /* Still need to give an error message for missing key. */
3466 EMSG2(_(e_dictkey), fudi.fd_newkey);
3467 vim_free(fudi.fd_newkey);
3468 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003469 if (tofree == NULL)
3470 return;
3471
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 /* Increase refcount on dictionary, it could get deleted when evaluating
3473 * the arguments. */
3474 if (fudi.fd_dict != NULL)
3475 ++fudi.fd_dict->dv_refcount;
3476
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003478 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003479 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaar532c7802005-01-27 14:44:31 +00003481 /* Skip white space to allow ":call func ()". Not good, but required for
3482 * backward compatibility. */
3483 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 if (*startarg != '(')
3487 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003488 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 goto end;
3490 }
3491
3492 /*
3493 * When skipping, evaluate the function once, to find the end of the
3494 * arguments.
3495 * When the function takes a range, this is discovered after the first
3496 * call, and the loop is broken.
3497 */
3498 if (eap->skip)
3499 {
3500 ++emsg_skip;
3501 lnum = eap->line2; /* do it once, also with an invalid range */
3502 }
3503 else
3504 lnum = eap->line1;
3505 for ( ; lnum <= eap->line2; ++lnum)
3506 {
3507 if (!eap->skip && eap->addr_count > 0)
3508 {
3509 curwin->w_cursor.lnum = lnum;
3510 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003511#ifdef FEAT_VIRTUALEDIT
3512 curwin->w_cursor.coladd = 0;
3513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003516 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003517 eap->line1, eap->line2, &doesrange,
3518 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 failed = TRUE;
3521 break;
3522 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003523
3524 /* Handle a function returning a Funcref, Dictionary or List. */
3525 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3526 {
3527 failed = TRUE;
3528 break;
3529 }
3530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (doesrange || eap->skip)
3533 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003536 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (aborting())
3540 break;
3541 }
3542 if (eap->skip)
3543 --emsg_skip;
3544
3545 if (!failed)
3546 {
3547 /* Check for trailing illegal characters and a following command. */
3548 if (!ends_excmd(*arg))
3549 {
3550 emsg_severe = TRUE;
3551 EMSG(_(e_trailing));
3552 }
3553 else
3554 eap->nextcmd = check_nextcmd(arg);
3555 }
3556
3557end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003558 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003559 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560}
3561
3562/*
3563 * ":unlet[!] var1 ... " command.
3564 */
3565 void
3566ex_unlet(eap)
3567 exarg_T *eap;
3568{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 ex_unletlock(eap, eap->arg, 0);
3570}
3571
3572/*
3573 * ":lockvar" and ":unlockvar" commands
3574 */
3575 void
3576ex_lockvar(eap)
3577 exarg_T *eap;
3578{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 int deep = 2;
3581
3582 if (eap->forceit)
3583 deep = -1;
3584 else if (vim_isdigit(*arg))
3585 {
3586 deep = getdigits(&arg);
3587 arg = skipwhite(arg);
3588 }
3589
3590 ex_unletlock(eap, arg, deep);
3591}
3592
3593/*
3594 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3595 */
3596 static void
3597ex_unletlock(eap, argstart, deep)
3598 exarg_T *eap;
3599 char_u *argstart;
3600 int deep;
3601{
3602 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
3607 do
3608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003610 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003611 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 if (lv.ll_name == NULL)
3613 error = TRUE; /* error but continue parsing */
3614 if (name_end == NULL || (!vim_iswhite(*name_end)
3615 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 if (name_end != NULL)
3618 {
3619 emsg_severe = TRUE;
3620 EMSG(_(e_trailing));
3621 }
3622 if (!(eap->skip || error))
3623 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 break;
3625 }
3626
3627 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 {
3629 if (eap->cmdidx == CMD_unlet)
3630 {
3631 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3632 error = TRUE;
3633 }
3634 else
3635 {
3636 if (do_lock_var(&lv, name_end, deep,
3637 eap->cmdidx == CMD_lockvar) == FAIL)
3638 error = TRUE;
3639 }
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 if (!eap->skip)
3643 clear_lval(&lv);
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 arg = skipwhite(name_end);
3646 } while (!ends_excmd(*arg));
3647
3648 eap->nextcmd = check_nextcmd(arg);
3649}
3650
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 int forceit;
3656{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 int ret = OK;
3658 int cc;
3659
3660 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 cc = *name_end;
3663 *name_end = NUL;
3664
3665 /* Normal name or expanded name. */
3666 if (check_changedtick(lp->ll_name))
3667 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003672 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003673 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 else if (lp->ll_range)
3678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003680 listitem_T *ll_li = lp->ll_li;
3681 int ll_n1 = lp->ll_n1;
3682
3683 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3684 {
3685 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003686 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003687 return FAIL;
3688 ll_li = li;
3689 ++ll_n1;
3690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691
3692 /* Delete a range of List items. */
3693 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3694 {
3695 li = lp->ll_li->li_next;
3696 listitem_remove(lp->ll_list, lp->ll_li);
3697 lp->ll_li = li;
3698 ++lp->ll_n1;
3699 }
3700 }
3701 else
3702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 /* unlet a List item. */
3705 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003708 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 }
3710
3711 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003712}
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714/*
3715 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003716 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 */
3718 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003719do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003721 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 hashtab_T *ht;
3724 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003725 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003726 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003727 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 ht = find_var_ht(name, &varname);
3730 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 if (ht == &globvarht)
3733 d = &globvardict;
3734 else if (current_funccal != NULL
3735 && ht == &current_funccal->l_vars.dv_hashtab)
3736 d = &current_funccal->l_vars;
3737 else
3738 {
3739 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3740 d = di->di_tv.vval.v_dict;
3741 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 hi = hash_find(ht, varname);
3743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003744 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003745 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003746 if (var_check_fixed(di->di_flags, name, FALSE)
3747 || var_check_ro(di->di_flags, name, FALSE)
3748 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 return FAIL;
3750 delete_var(ht, hi);
3751 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003754 if (forceit)
3755 return OK;
3756 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 return FAIL;
3758}
3759
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760/*
3761 * Lock or unlock variable indicated by "lp".
3762 * "deep" is the levels to go (-1 for unlimited);
3763 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3764 */
3765 static int
3766do_lock_var(lp, name_end, deep, lock)
3767 lval_T *lp;
3768 char_u *name_end;
3769 int deep;
3770 int lock;
3771{
3772 int ret = OK;
3773 int cc;
3774 dictitem_T *di;
3775
3776 if (deep == 0) /* nothing to do */
3777 return OK;
3778
3779 if (lp->ll_tv == NULL)
3780 {
3781 cc = *name_end;
3782 *name_end = NUL;
3783
3784 /* Normal name or expanded name. */
3785 if (check_changedtick(lp->ll_name))
3786 ret = FAIL;
3787 else
3788 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003789 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003790 if (di == NULL)
3791 ret = FAIL;
3792 else
3793 {
3794 if (lock)
3795 di->di_flags |= DI_FLAGS_LOCK;
3796 else
3797 di->di_flags &= ~DI_FLAGS_LOCK;
3798 item_lock(&di->di_tv, deep, lock);
3799 }
3800 }
3801 *name_end = cc;
3802 }
3803 else if (lp->ll_range)
3804 {
3805 listitem_T *li = lp->ll_li;
3806
3807 /* (un)lock a range of List items. */
3808 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3809 {
3810 item_lock(&li->li_tv, deep, lock);
3811 li = li->li_next;
3812 ++lp->ll_n1;
3813 }
3814 }
3815 else if (lp->ll_list != NULL)
3816 /* (un)lock a List item. */
3817 item_lock(&lp->ll_li->li_tv, deep, lock);
3818 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003819 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003820 item_lock(&lp->ll_di->di_tv, deep, lock);
3821
3822 return ret;
3823}
3824
3825/*
3826 * Lock or unlock an item. "deep" is nr of levels to go.
3827 */
3828 static void
3829item_lock(tv, deep, lock)
3830 typval_T *tv;
3831 int deep;
3832 int lock;
3833{
3834 static int recurse = 0;
3835 list_T *l;
3836 listitem_T *li;
3837 dict_T *d;
3838 hashitem_T *hi;
3839 int todo;
3840
3841 if (recurse >= DICT_MAXNEST)
3842 {
3843 EMSG(_("E743: variable nested too deep for (un)lock"));
3844 return;
3845 }
3846 if (deep == 0)
3847 return;
3848 ++recurse;
3849
3850 /* lock/unlock the item itself */
3851 if (lock)
3852 tv->v_lock |= VAR_LOCKED;
3853 else
3854 tv->v_lock &= ~VAR_LOCKED;
3855
3856 switch (tv->v_type)
3857 {
3858 case VAR_LIST:
3859 if ((l = tv->vval.v_list) != NULL)
3860 {
3861 if (lock)
3862 l->lv_lock |= VAR_LOCKED;
3863 else
3864 l->lv_lock &= ~VAR_LOCKED;
3865 if (deep < 0 || deep > 1)
3866 /* recursive: lock/unlock the items the List contains */
3867 for (li = l->lv_first; li != NULL; li = li->li_next)
3868 item_lock(&li->li_tv, deep - 1, lock);
3869 }
3870 break;
3871 case VAR_DICT:
3872 if ((d = tv->vval.v_dict) != NULL)
3873 {
3874 if (lock)
3875 d->dv_lock |= VAR_LOCKED;
3876 else
3877 d->dv_lock &= ~VAR_LOCKED;
3878 if (deep < 0 || deep > 1)
3879 {
3880 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003881 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003882 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3883 {
3884 if (!HASHITEM_EMPTY(hi))
3885 {
3886 --todo;
3887 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3888 }
3889 }
3890 }
3891 }
3892 }
3893 --recurse;
3894}
3895
Bram Moolenaara40058a2005-07-11 22:42:07 +00003896/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003897 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3898 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003899 */
3900 static int
3901tv_islocked(tv)
3902 typval_T *tv;
3903{
3904 return (tv->v_lock & VAR_LOCKED)
3905 || (tv->v_type == VAR_LIST
3906 && tv->vval.v_list != NULL
3907 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3908 || (tv->v_type == VAR_DICT
3909 && tv->vval.v_dict != NULL
3910 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3911}
3912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3914/*
3915 * Delete all "menutrans_" variables.
3916 */
3917 void
3918del_menutrans_vars()
3919{
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
Bram Moolenaar33570922005-01-25 22:26:29 +00003923 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003924 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 {
3927 if (!HASHITEM_EMPTY(hi))
3928 {
3929 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3931 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 }
3933 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935}
3936#endif
3937
3938#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3939
3940/*
3941 * Local string buffer for the next two functions to store a variable name
3942 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3943 * get_user_var_name().
3944 */
3945
3946static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3947
3948static char_u *varnamebuf = NULL;
3949static int varnamebuflen = 0;
3950
3951/*
3952 * Function to concatenate a prefix and a variable name.
3953 */
3954 static char_u *
3955cat_prefix_varname(prefix, name)
3956 int prefix;
3957 char_u *name;
3958{
3959 int len;
3960
3961 len = (int)STRLEN(name) + 3;
3962 if (len > varnamebuflen)
3963 {
3964 vim_free(varnamebuf);
3965 len += 10; /* some additional space */
3966 varnamebuf = alloc(len);
3967 if (varnamebuf == NULL)
3968 {
3969 varnamebuflen = 0;
3970 return NULL;
3971 }
3972 varnamebuflen = len;
3973 }
3974 *varnamebuf = prefix;
3975 varnamebuf[1] = ':';
3976 STRCPY(varnamebuf + 2, name);
3977 return varnamebuf;
3978}
3979
3980/*
3981 * Function given to ExpandGeneric() to obtain the list of user defined
3982 * (global/buffer/window/built-in) variable names.
3983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 char_u *
3985get_user_var_name(xp, idx)
3986 expand_T *xp;
3987 int idx;
3988{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003989 static long_u gdone;
3990 static long_u bdone;
3991 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003992#ifdef FEAT_WINDOWS
3993 static long_u tdone;
3994#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003995 static int vidx;
3996 static hashitem_T *hi;
3997 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004000 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004001 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002#ifdef FEAT_WINDOWS
4003 tdone = 0;
4004#endif
4005 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004006
4007 /* Global variables */
4008 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004010 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004011 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 else
4013 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004014 while (HASHITEM_EMPTY(hi))
4015 ++hi;
4016 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4017 return cat_prefix_varname('g', hi->hi_key);
4018 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020
4021 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004022 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004027 else
4028 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 while (HASHITEM_EMPTY(hi))
4030 ++hi;
4031 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004035 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 return (char_u *)"b:changedtick";
4037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004043 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004052#ifdef FEAT_WINDOWS
4053 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004054 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004055 if (tdone < ht->ht_used)
4056 {
4057 if (tdone++ == 0)
4058 hi = ht->ht_array;
4059 else
4060 ++hi;
4061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 return cat_prefix_varname('t', hi->hi_key);
4064 }
4065#endif
4066
Bram Moolenaar33570922005-01-25 22:26:29 +00004067 /* v: variables */
4068 if (vidx < VV_LEN)
4069 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071 vim_free(varnamebuf);
4072 varnamebuf = NULL;
4073 varnamebuflen = 0;
4074 return NULL;
4075}
4076
4077#endif /* FEAT_CMDL_COMPL */
4078
4079/*
4080 * types for expressions.
4081 */
4082typedef enum
4083{
4084 TYPE_UNKNOWN = 0
4085 , TYPE_EQUAL /* == */
4086 , TYPE_NEQUAL /* != */
4087 , TYPE_GREATER /* > */
4088 , TYPE_GEQUAL /* >= */
4089 , TYPE_SMALLER /* < */
4090 , TYPE_SEQUAL /* <= */
4091 , TYPE_MATCH /* =~ */
4092 , TYPE_NOMATCH /* !~ */
4093} exptype_T;
4094
4095/*
4096 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4099 */
4100
4101/*
4102 * Handle zero level expression.
4103 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004105 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * Return OK or FAIL.
4107 */
4108 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 char_u **nextcmd;
4113 int evaluate;
4114{
4115 int ret;
4116 char_u *p;
4117
4118 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (ret == FAIL || !ends_excmd(*p))
4121 {
4122 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 /*
4125 * Report the invalid expression unless the expression evaluation has
4126 * been cancelled due to an aborting error, an interrupt, or an
4127 * exception.
4128 */
4129 if (!aborting())
4130 EMSG2(_(e_invexpr2), arg);
4131 ret = FAIL;
4132 }
4133 if (nextcmd != NULL)
4134 *nextcmd = check_nextcmd(p);
4135
4136 return ret;
4137}
4138
4139/*
4140 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004141 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 *
4143 * "arg" must point to the first non-white of the expression.
4144 * "arg" is advanced to the next non-white after the recognized expression.
4145 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004146 * Note: "rettv.v_lock" is not set.
4147 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 * Return OK or FAIL.
4149 */
4150 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 int evaluate;
4155{
4156 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004157 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
4159 /*
4160 * Get the first variable.
4161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 return FAIL;
4164
4165 if ((*arg)[0] == '?')
4166 {
4167 result = FALSE;
4168 if (evaluate)
4169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 int error = FALSE;
4171
4172 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 if (error)
4176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178
4179 /*
4180 * Get the second variable.
4181 */
4182 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FAIL;
4185
4186 /*
4187 * Check for the ":".
4188 */
4189 if ((*arg)[0] != ':')
4190 {
4191 EMSG(_("E109: Missing ':' after '?'"));
4192 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return FAIL;
4195 }
4196
4197 /*
4198 * Get the third variable.
4199 */
4200 *arg = skipwhite(*arg + 1);
4201 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4202 {
4203 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206 }
4207 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210
4211 return OK;
4212}
4213
4214/*
4215 * Handle first level expression:
4216 * expr2 || expr2 || expr2 logical OR
4217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
4221 * Return OK or FAIL.
4222 */
4223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int evaluate;
4228{
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 long result;
4231 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 /*
4241 * Repeat until there is no following "||".
4242 */
4243 first = TRUE;
4244 result = FALSE;
4245 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4246 {
4247 if (evaluate && first)
4248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 if (error)
4253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 first = FALSE;
4255 }
4256
4257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(*arg + 2);
4261 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4262 return FAIL;
4263
4264 /*
4265 * Compute the result.
4266 */
4267 if (evaluate && !result)
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (error)
4273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 if (evaluate)
4276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 rettv->v_type = VAR_NUMBER;
4278 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281
4282 return OK;
4283}
4284
4285/*
4286 * Handle second level expression:
4287 * expr3 && expr3 && expr3 logical AND
4288 *
4289 * "arg" must point to the first non-white of the expression.
4290 * "arg" is advanced to the next non-white after the recognized expression.
4291 *
4292 * Return OK or FAIL.
4293 */
4294 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int evaluate;
4299{
Bram Moolenaar33570922005-01-25 22:26:29 +00004300 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 long result;
4302 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
4305 /*
4306 * Get the first variable.
4307 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310
4311 /*
4312 * Repeat until there is no following "&&".
4313 */
4314 first = TRUE;
4315 result = TRUE;
4316 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4317 {
4318 if (evaluate && first)
4319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (error)
4324 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 first = FALSE;
4326 }
4327
4328 /*
4329 * Get the second variable.
4330 */
4331 *arg = skipwhite(*arg + 2);
4332 if (eval4(arg, &var2, evaluate && result) == FAIL)
4333 return FAIL;
4334
4335 /*
4336 * Compute the result.
4337 */
4338 if (evaluate && result)
4339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (error)
4344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346 if (evaluate)
4347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 rettv->v_type = VAR_NUMBER;
4349 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351 }
4352
4353 return OK;
4354}
4355
4356/*
4357 * Handle third level expression:
4358 * var1 == var2
4359 * var1 =~ var2
4360 * var1 != var2
4361 * var1 !~ var2
4362 * var1 > var2
4363 * var1 >= var2
4364 * var1 < var2
4365 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004366 * var1 is var2
4367 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 *
4369 * "arg" must point to the first non-white of the expression.
4370 * "arg" is advanced to the next non-white after the recognized expression.
4371 *
4372 * Return OK or FAIL.
4373 */
4374 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 int evaluate;
4379{
Bram Moolenaar33570922005-01-25 22:26:29 +00004380 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u *p;
4382 int i;
4383 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int len = 2;
4386 long n1, n2;
4387 char_u *s1, *s2;
4388 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4389 regmatch_T regmatch;
4390 int ic;
4391 char_u *save_cpo;
4392
4393 /*
4394 * Get the first variable.
4395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398
4399 p = *arg;
4400 switch (p[0])
4401 {
4402 case '=': if (p[1] == '=')
4403 type = TYPE_EQUAL;
4404 else if (p[1] == '~')
4405 type = TYPE_MATCH;
4406 break;
4407 case '!': if (p[1] == '=')
4408 type = TYPE_NEQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_NOMATCH;
4411 break;
4412 case '>': if (p[1] != '=')
4413 {
4414 type = TYPE_GREATER;
4415 len = 1;
4416 }
4417 else
4418 type = TYPE_GEQUAL;
4419 break;
4420 case '<': if (p[1] != '=')
4421 {
4422 type = TYPE_SMALLER;
4423 len = 1;
4424 }
4425 else
4426 type = TYPE_SEQUAL;
4427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 case 'i': if (p[1] == 's')
4429 {
4430 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4431 len = 5;
4432 if (!vim_isIDc(p[len]))
4433 {
4434 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4435 type_is = TRUE;
4436 }
4437 }
4438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440
4441 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004442 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 */
4444 if (type != TYPE_UNKNOWN)
4445 {
4446 /* extra question mark appended: ignore case */
4447 if (p[len] == '?')
4448 {
4449 ic = TRUE;
4450 ++len;
4451 }
4452 /* extra '#' appended: match case */
4453 else if (p[len] == '#')
4454 {
4455 ic = FALSE;
4456 ++len;
4457 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004458 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 else
4460 ic = p_ic;
4461
4462 /*
4463 * Get the second variable.
4464 */
4465 *arg = skipwhite(p + len);
4466 if (eval5(arg, &var2, evaluate) == FAIL)
4467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004468 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return FAIL;
4470 }
4471
4472 if (evaluate)
4473 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004474 if (type_is && rettv->v_type != var2.v_type)
4475 {
4476 /* For "is" a different type always means FALSE, for "notis"
4477 * it means TRUE. */
4478 n1 = (type == TYPE_NEQUAL);
4479 }
4480 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4481 {
4482 if (type_is)
4483 {
4484 n1 = (rettv->v_type == var2.v_type
4485 && rettv->vval.v_list == var2.vval.v_list);
4486 if (type == TYPE_NEQUAL)
4487 n1 = !n1;
4488 }
4489 else if (rettv->v_type != var2.v_type
4490 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4491 {
4492 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004493 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004494 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004495 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004496 clear_tv(rettv);
4497 clear_tv(&var2);
4498 return FAIL;
4499 }
4500 else
4501 {
4502 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004503 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4504 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 }
4509
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004510 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4511 {
4512 if (type_is)
4513 {
4514 n1 = (rettv->v_type == var2.v_type
4515 && rettv->vval.v_dict == var2.vval.v_dict);
4516 if (type == TYPE_NEQUAL)
4517 n1 = !n1;
4518 }
4519 else if (rettv->v_type != var2.v_type
4520 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4521 {
4522 if (rettv->v_type != var2.v_type)
4523 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4524 else
4525 EMSG(_("E736: Invalid operation for Dictionary"));
4526 clear_tv(rettv);
4527 clear_tv(&var2);
4528 return FAIL;
4529 }
4530 else
4531 {
4532 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004533 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4534 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004535 if (type == TYPE_NEQUAL)
4536 n1 = !n1;
4537 }
4538 }
4539
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004540 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4541 {
4542 if (rettv->v_type != var2.v_type
4543 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4544 {
4545 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004546 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004547 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004548 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004549 clear_tv(rettv);
4550 clear_tv(&var2);
4551 return FAIL;
4552 }
4553 else
4554 {
4555 /* Compare two Funcrefs for being equal or unequal. */
4556 if (rettv->vval.v_string == NULL
4557 || var2.vval.v_string == NULL)
4558 n1 = FALSE;
4559 else
4560 n1 = STRCMP(rettv->vval.v_string,
4561 var2.vval.v_string) == 0;
4562 if (type == TYPE_NEQUAL)
4563 n1 = !n1;
4564 }
4565 }
4566
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004567#ifdef FEAT_FLOAT
4568 /*
4569 * If one of the two variables is a float, compare as a float.
4570 * When using "=~" or "!~", always compare as string.
4571 */
4572 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4573 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4574 {
4575 float_T f1, f2;
4576
4577 if (rettv->v_type == VAR_FLOAT)
4578 f1 = rettv->vval.v_float;
4579 else
4580 f1 = get_tv_number(rettv);
4581 if (var2.v_type == VAR_FLOAT)
4582 f2 = var2.vval.v_float;
4583 else
4584 f2 = get_tv_number(&var2);
4585 n1 = FALSE;
4586 switch (type)
4587 {
4588 case TYPE_EQUAL: n1 = (f1 == f2); break;
4589 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4590 case TYPE_GREATER: n1 = (f1 > f2); break;
4591 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4592 case TYPE_SMALLER: n1 = (f1 < f2); break;
4593 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4594 case TYPE_UNKNOWN:
4595 case TYPE_MATCH:
4596 case TYPE_NOMATCH: break; /* avoid gcc warning */
4597 }
4598 }
4599#endif
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 /*
4602 * If one of the two variables is a number, compare as a number.
4603 * When using "=~" or "!~", always compare as string.
4604 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004605 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 n1 = get_tv_number(rettv);
4609 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 switch (type)
4611 {
4612 case TYPE_EQUAL: n1 = (n1 == n2); break;
4613 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4614 case TYPE_GREATER: n1 = (n1 > n2); break;
4615 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4616 case TYPE_SMALLER: n1 = (n1 < n2); break;
4617 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4618 case TYPE_UNKNOWN:
4619 case TYPE_MATCH:
4620 case TYPE_NOMATCH: break; /* avoid gcc warning */
4621 }
4622 }
4623 else
4624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 s1 = get_tv_string_buf(rettv, buf1);
4626 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4628 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4629 else
4630 i = 0;
4631 n1 = FALSE;
4632 switch (type)
4633 {
4634 case TYPE_EQUAL: n1 = (i == 0); break;
4635 case TYPE_NEQUAL: n1 = (i != 0); break;
4636 case TYPE_GREATER: n1 = (i > 0); break;
4637 case TYPE_GEQUAL: n1 = (i >= 0); break;
4638 case TYPE_SMALLER: n1 = (i < 0); break;
4639 case TYPE_SEQUAL: n1 = (i <= 0); break;
4640
4641 case TYPE_MATCH:
4642 case TYPE_NOMATCH:
4643 /* avoid 'l' flag in 'cpoptions' */
4644 save_cpo = p_cpo;
4645 p_cpo = (char_u *)"";
4646 regmatch.regprog = vim_regcomp(s2,
4647 RE_MAGIC + RE_STRING);
4648 regmatch.rm_ic = ic;
4649 if (regmatch.regprog != NULL)
4650 {
4651 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004652 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (type == TYPE_NOMATCH)
4654 n1 = !n1;
4655 }
4656 p_cpo = save_cpo;
4657 break;
4658
4659 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4660 }
4661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004662 clear_tv(rettv);
4663 clear_tv(&var2);
4664 rettv->v_type = VAR_NUMBER;
4665 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667 }
4668
4669 return OK;
4670}
4671
4672/*
4673 * Handle fourth level expression:
4674 * + number addition
4675 * - number subtraction
4676 * . string concatenation
4677 *
4678 * "arg" must point to the first non-white of the expression.
4679 * "arg" is advanced to the next non-white after the recognized expression.
4680 *
4681 * Return OK or FAIL.
4682 */
4683 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 int evaluate;
4688{
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 typval_T var2;
4690 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int op;
4692 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693#ifdef FEAT_FLOAT
4694 float_T f1 = 0, f2 = 0;
4695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *s1, *s2;
4697 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4698 char_u *p;
4699
4700 /*
4701 * Get the first variable.
4702 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004703 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return FAIL;
4705
4706 /*
4707 * Repeat computing, until no '+', '-' or '.' is following.
4708 */
4709 for (;;)
4710 {
4711 op = **arg;
4712 if (op != '+' && op != '-' && op != '.')
4713 break;
4714
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 if ((op != '+' || rettv->v_type != VAR_LIST)
4716#ifdef FEAT_FLOAT
4717 && (op == '.' || rettv->v_type != VAR_FLOAT)
4718#endif
4719 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
4721 /* For "list + ...", an illegal use of the first operand as
4722 * a number cannot be determined before evaluating the 2nd
4723 * operand: if this is also a list, all is ok.
4724 * For "something . ...", "something - ..." or "non-list + ...",
4725 * we know that the first operand needs to be a string or number
4726 * without evaluating the 2nd operand. So check before to avoid
4727 * side effects after an error. */
4728 if (evaluate && get_tv_string_chk(rettv) == NULL)
4729 {
4730 clear_tv(rettv);
4731 return FAIL;
4732 }
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
4736 * Get the second variable.
4737 */
4738 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004739 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743 }
4744
4745 if (evaluate)
4746 {
4747 /*
4748 * Compute the result.
4749 */
4750 if (op == '.')
4751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004752 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4753 s2 = get_tv_string_buf_chk(&var2, buf2);
4754 if (s2 == NULL) /* type error ? */
4755 {
4756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 return FAIL;
4759 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004760 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 clear_tv(rettv);
4762 rettv->v_type = VAR_STRING;
4763 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004765 else if (op == '+' && rettv->v_type == VAR_LIST
4766 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004767 {
4768 /* concatenate Lists */
4769 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4770 &var3) == FAIL)
4771 {
4772 clear_tv(rettv);
4773 clear_tv(&var2);
4774 return FAIL;
4775 }
4776 clear_tv(rettv);
4777 *rettv = var3;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 int error = FALSE;
4782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 f1 = rettv->vval.v_float;
4787 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 else
4790#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 n1 = get_tv_number_chk(rettv, &error);
4793 if (error)
4794 {
4795 /* This can only happen for "list + non-list". For
4796 * "non-list + ..." or "something - ...", we returned
4797 * before evaluating the 2nd operand. */
4798 clear_tv(rettv);
4799 return FAIL;
4800 }
4801#ifdef FEAT_FLOAT
4802 if (var2.v_type == VAR_FLOAT)
4803 f1 = n1;
4804#endif
4805 }
4806#ifdef FEAT_FLOAT
4807 if (var2.v_type == VAR_FLOAT)
4808 {
4809 f2 = var2.vval.v_float;
4810 n2 = 0;
4811 }
4812 else
4813#endif
4814 {
4815 n2 = get_tv_number_chk(&var2, &error);
4816 if (error)
4817 {
4818 clear_tv(rettv);
4819 clear_tv(&var2);
4820 return FAIL;
4821 }
4822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
4824 f2 = n2;
4825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828
4829#ifdef FEAT_FLOAT
4830 /* If there is a float on either side the result is a float. */
4831 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4832 {
4833 if (op == '+')
4834 f1 = f1 + f2;
4835 else
4836 f1 = f1 - f2;
4837 rettv->v_type = VAR_FLOAT;
4838 rettv->vval.v_float = f1;
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#endif
4842 {
4843 if (op == '+')
4844 n1 = n1 + n2;
4845 else
4846 n1 = n1 - n2;
4847 rettv->v_type = VAR_NUMBER;
4848 rettv->vval.v_number = n1;
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 }
4854 return OK;
4855}
4856
4857/*
4858 * Handle fifth level expression:
4859 * * number multiplication
4860 * / number division
4861 * % number modulo
4862 *
4863 * "arg" must point to the first non-white of the expression.
4864 * "arg" is advanced to the next non-white after the recognized expression.
4865 *
4866 * Return OK or FAIL.
4867 */
4868 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004869eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004873 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int op;
4877 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#ifdef FEAT_FLOAT
4879 int use_float = FALSE;
4880 float_T f1 = 0, f2;
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * Get the first variable.
4886 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004887 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return FAIL;
4889
4890 /*
4891 * Repeat computing, until no '*', '/' or '%' is following.
4892 */
4893 for (;;)
4894 {
4895 op = **arg;
4896 if (op != '*' && op != '/' && op != '%')
4897 break;
4898
4899 if (evaluate)
4900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (rettv->v_type == VAR_FLOAT)
4903 {
4904 f1 = rettv->vval.v_float;
4905 use_float = TRUE;
4906 n1 = 0;
4907 }
4908 else
4909#endif
4910 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 if (error)
4913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 else
4916 n1 = 0;
4917
4918 /*
4919 * Get the second variable.
4920 */
4921 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004922 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924
4925 if (evaluate)
4926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#ifdef FEAT_FLOAT
4928 if (var2.v_type == VAR_FLOAT)
4929 {
4930 if (!use_float)
4931 {
4932 f1 = n1;
4933 use_float = TRUE;
4934 }
4935 f2 = var2.vval.v_float;
4936 n2 = 0;
4937 }
4938 else
4939#endif
4940 {
4941 n2 = get_tv_number_chk(&var2, &error);
4942 clear_tv(&var2);
4943 if (error)
4944 return FAIL;
4945#ifdef FEAT_FLOAT
4946 if (use_float)
4947 f2 = n2;
4948#endif
4949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 /*
4952 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 if (op == '*')
4959 f1 = f1 * f2;
4960 else if (op == '/')
4961 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962# ifdef VMS
4963 /* VMS crashes on divide by zero, work around it */
4964 if (f2 == 0.0)
4965 {
4966 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 }
4973 else
4974 f1 = f1 / f2;
4975# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 /* We rely on the floating point library to handle divide
4977 * by zero to result in "inf" and not a crash. */
4978 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004983 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 return FAIL;
4985 }
4986 rettv->v_type = VAR_FLOAT;
4987 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 if (op == '*')
4993 n1 = n1 * n2;
4994 else if (op == '/')
4995 {
4996 if (n2 == 0) /* give an error message? */
4997 {
4998 if (n1 == 0)
4999 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5000 else if (n1 < 0)
5001 n1 = -0x7fffffffL;
5002 else
5003 n1 = 0x7fffffffL;
5004 }
5005 else
5006 n1 = n1 / n2;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 {
5010 if (n2 == 0) /* give an error message? */
5011 n1 = 0;
5012 else
5013 n1 = n1 % n2;
5014 }
5015 rettv->v_type = VAR_NUMBER;
5016 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 }
5020
5021 return OK;
5022}
5023
5024/*
5025 * Handle sixth level expression:
5026 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005027 * "string" string constant
5028 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 * &option-name option value
5030 * @r register contents
5031 * identifier variable value
5032 * function() function call
5033 * $VAR environment variable
5034 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005035 * [expr, expr] List
5036 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 *
5038 * Also handle:
5039 * ! in front logical NOT
5040 * - in front unary minus
5041 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * trailing [] subscript in String or List
5043 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *
5045 * "arg" must point to the first non-white of the expression.
5046 * "arg" is advanced to the next non-white after the recognized expression.
5047 *
5048 * Return OK or FAIL.
5049 */
5050 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005051eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005055 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 long n;
5058 int len;
5059 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *start_leader, *end_leader;
5061 int ret = OK;
5062 char_u *alias;
5063
5064 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * Skip '!' and '-' characters. They are handled later.
5072 */
5073 start_leader = *arg;
5074 while (**arg == '!' || **arg == '-' || **arg == '+')
5075 *arg = skipwhite(*arg + 1);
5076 end_leader = *arg;
5077
5078 switch (**arg)
5079 {
5080 /*
5081 * Number constant.
5082 */
5083 case '0':
5084 case '1':
5085 case '2':
5086 case '3':
5087 case '4':
5088 case '5':
5089 case '6':
5090 case '7':
5091 case '8':
5092 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 {
5094#ifdef FEAT_FLOAT
5095 char_u *p = skipdigits(*arg + 1);
5096 int get_float = FALSE;
5097
5098 /* We accept a float when the format matches
5099 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005100 * strict to avoid backwards compatibility problems.
5101 * Don't look for a float after the "." operator, so that
5102 * ":let vers = 1.2.3" doesn't fail. */
5103 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 get_float = TRUE;
5106 p = skipdigits(p + 2);
5107 if (*p == 'e' || *p == 'E')
5108 {
5109 ++p;
5110 if (*p == '-' || *p == '+')
5111 ++p;
5112 if (!vim_isdigit(*p))
5113 get_float = FALSE;
5114 else
5115 p = skipdigits(p + 1);
5116 }
5117 if (ASCII_ISALPHA(*p) || *p == '.')
5118 get_float = FALSE;
5119 }
5120 if (get_float)
5121 {
5122 float_T f;
5123
5124 *arg += string2float(*arg, &f);
5125 if (evaluate)
5126 {
5127 rettv->v_type = VAR_FLOAT;
5128 rettv->vval.v_float = f;
5129 }
5130 }
5131 else
5132#endif
5133 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005134 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005135 *arg += len;
5136 if (evaluate)
5137 {
5138 rettv->v_type = VAR_NUMBER;
5139 rettv->vval.v_number = n;
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * String constant: "string".
5147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150
5151 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 break;
5156
5157 /*
5158 * List: [expr, expr]
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Dictionary: {key: val, key: val}
5165 */
5166 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5167 break;
5168
5169 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Environment variable: $VAR.
5177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180
5181 /*
5182 * Register contents: @r.
5183 */
5184 case '@': ++*arg;
5185 if (evaluate)
5186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005188 rettv->vval.v_string = get_reg_contents(**arg,
5189 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 if (**arg != NUL)
5192 ++*arg;
5193 break;
5194
5195 /*
5196 * nested expression: (expression).
5197 */
5198 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (**arg == ')')
5201 ++*arg;
5202 else if (ret == OK)
5203 {
5204 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ret = FAIL;
5207 }
5208 break;
5209
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213
5214 if (ret == NOTDONE)
5215 {
5216 /*
5217 * Must be a variable or function name.
5218 * Can also be a curly-braces kind of name: {expr}.
5219 */
5220 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 if (alias != NULL)
5223 s = alias;
5224
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 ret = FAIL;
5227 else
5228 {
5229 if (**arg == '(') /* recursive! */
5230 {
5231 /* If "s" is the name of a variable of type VAR_FUNC
5232 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005233 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234
5235 /* Invoke the function. */
5236 ret = get_func_tv(s, len, rettv, arg,
5237 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005238 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005239
5240 /* If evaluate is FALSE rettv->v_type was not set in
5241 * get_func_tv, but it's needed in handle_subscript() to parse
5242 * what follows. So set it here. */
5243 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5244 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005245 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005246 rettv->v_type = VAR_FUNC;
5247 }
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 /* Stop the expression evaluation when immediately
5250 * aborting on error, or when an interrupt occurred or
5251 * an exception was thrown but not caught. */
5252 if (aborting())
5253 {
5254 if (ret == OK)
5255 clear_tv(rettv);
5256 ret = FAIL;
5257 }
5258 }
5259 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005260 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005261 else
5262 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005264 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
5266
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 *arg = skipwhite(*arg);
5268
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5270 * expr(expr). */
5271 if (ret == OK)
5272 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
5274 /*
5275 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5276 */
5277 if (ret == OK && evaluate && end_leader > start_leader)
5278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005280 int val = 0;
5281#ifdef FEAT_FLOAT
5282 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 if (rettv->v_type == VAR_FLOAT)
5285 f = rettv->vval.v_float;
5286 else
5287#endif
5288 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 clear_tv(rettv);
5292 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 else
5295 {
5296 while (end_leader > start_leader)
5297 {
5298 --end_leader;
5299 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300 {
5301#ifdef FEAT_FLOAT
5302 if (rettv->v_type == VAR_FLOAT)
5303 f = !f;
5304 else
5305#endif
5306 val = !val;
5307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 {
5310#ifdef FEAT_FLOAT
5311 if (rettv->v_type == VAR_FLOAT)
5312 f = -f;
5313 else
5314#endif
5315 val = -val;
5316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318#ifdef FEAT_FLOAT
5319 if (rettv->v_type == VAR_FLOAT)
5320 {
5321 clear_tv(rettv);
5322 rettv->vval.v_float = f;
5323 }
5324 else
5325#endif
5326 {
5327 clear_tv(rettv);
5328 rettv->v_type = VAR_NUMBER;
5329 rettv->vval.v_number = val;
5330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333
5334 return ret;
5335}
5336
5337/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005338 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5339 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5341 */
5342 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005343eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005345 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005347 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348{
5349 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 long len = -1;
5353 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005357 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005359 if (verbose)
5360 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 return FAIL;
5362 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005363#ifdef FEAT_FLOAT
5364 else if (rettv->v_type == VAR_FLOAT)
5365 {
5366 if (verbose)
5367 EMSG(_(e_float_as_string));
5368 return FAIL;
5369 }
5370#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 /*
5375 * dict.name
5376 */
5377 key = *arg + 1;
5378 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5379 ;
5380 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 }
5384 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 /*
5387 * something[idx]
5388 *
5389 * Get the (first) variable from inside the [].
5390 */
5391 *arg = skipwhite(*arg + 1);
5392 if (**arg == ':')
5393 empty1 = TRUE;
5394 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5395 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005396 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5397 {
5398 /* not a number or string */
5399 clear_tv(&var1);
5400 return FAIL;
5401 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402
5403 /*
5404 * Get the second variable from inside the [:].
5405 */
5406 if (**arg == ':')
5407 {
5408 range = TRUE;
5409 *arg = skipwhite(*arg + 1);
5410 if (**arg == ']')
5411 empty2 = TRUE;
5412 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005414 if (!empty1)
5415 clear_tv(&var1);
5416 return FAIL;
5417 }
5418 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5419 {
5420 /* not a number or string */
5421 if (!empty1)
5422 clear_tv(&var1);
5423 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 return FAIL;
5425 }
5426 }
5427
5428 /* Check for the ']'. */
5429 if (**arg != ']')
5430 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005431 if (verbose)
5432 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 clear_tv(&var1);
5434 if (range)
5435 clear_tv(&var2);
5436 return FAIL;
5437 }
5438 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 }
5440
5441 if (evaluate)
5442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 n1 = 0;
5444 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 n1 = get_tv_number(&var1);
5447 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 }
5449 if (range)
5450 {
5451 if (empty2)
5452 n2 = -1;
5453 else
5454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005455 n2 = get_tv_number(&var2);
5456 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 }
5459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 {
5462 case VAR_NUMBER:
5463 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 len = (long)STRLEN(s);
5466 if (range)
5467 {
5468 /* The resulting variable is a substring. If the indexes
5469 * are out of range the result is empty. */
5470 if (n1 < 0)
5471 {
5472 n1 = len + n1;
5473 if (n1 < 0)
5474 n1 = 0;
5475 }
5476 if (n2 < 0)
5477 n2 = len + n2;
5478 else if (n2 >= len)
5479 n2 = len;
5480 if (n1 >= len || n2 < 0 || n1 > n2)
5481 s = NULL;
5482 else
5483 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5484 }
5485 else
5486 {
5487 /* The resulting variable is a string of a single
5488 * character. If the index is too big or negative the
5489 * result is empty. */
5490 if (n1 >= len || n1 < 0)
5491 s = NULL;
5492 else
5493 s = vim_strnsave(s + n1, 1);
5494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 clear_tv(rettv);
5496 rettv->v_type = VAR_STRING;
5497 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 break;
5499
5500 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005501 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 if (n1 < 0)
5503 n1 = len + n1;
5504 if (!empty1 && (n1 < 0 || n1 >= len))
5505 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005506 /* For a range we allow invalid values and return an empty
5507 * list. A list index out of range is an error. */
5508 if (!range)
5509 {
5510 if (verbose)
5511 EMSGN(_(e_listidx), n1);
5512 return FAIL;
5513 }
5514 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 }
5516 if (range)
5517 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005518 list_T *l;
5519 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520
5521 if (n2 < 0)
5522 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005523 else if (n2 >= len)
5524 n2 = len - 1;
5525 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005526 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 l = list_alloc();
5528 if (l == NULL)
5529 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 n1 <= n2; ++n1)
5532 {
5533 if (list_append_tv(l, &item->li_tv) == FAIL)
5534 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005535 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 return FAIL;
5537 }
5538 item = item->li_next;
5539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 clear_tv(rettv);
5541 rettv->v_type = VAR_LIST;
5542 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005543 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 else
5546 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 clear_tv(rettv);
5549 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 }
5551 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552
5553 case VAR_DICT:
5554 if (range)
5555 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005556 if (verbose)
5557 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558 if (len == -1)
5559 clear_tv(&var1);
5560 return FAIL;
5561 }
5562 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005563 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564
5565 if (len == -1)
5566 {
5567 key = get_tv_string(&var1);
5568 if (*key == NUL)
5569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005570 if (verbose)
5571 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572 clear_tv(&var1);
5573 return FAIL;
5574 }
5575 }
5576
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005577 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005579 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005580 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 if (len == -1)
5582 clear_tv(&var1);
5583 if (item == NULL)
5584 return FAIL;
5585
5586 copy_tv(&item->di_tv, &var1);
5587 clear_tv(rettv);
5588 *rettv = var1;
5589 }
5590 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 }
5592 }
5593
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 return OK;
5595}
5596
5597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 * Get an option value.
5599 * "arg" points to the '&' or '+' before the option name.
5600 * "arg" is advanced to character after the option name.
5601 * Return OK or FAIL.
5602 */
5603 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005606 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 int evaluate;
5608{
5609 char_u *option_end;
5610 long numval;
5611 char_u *stringval;
5612 int opt_type;
5613 int c;
5614 int working = (**arg == '+'); /* has("+option") */
5615 int ret = OK;
5616 int opt_flags;
5617
5618 /*
5619 * Isolate the option name and find its value.
5620 */
5621 option_end = find_option_end(arg, &opt_flags);
5622 if (option_end == NULL)
5623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 EMSG2(_("E112: Option name missing: %s"), *arg);
5626 return FAIL;
5627 }
5628
5629 if (!evaluate)
5630 {
5631 *arg = option_end;
5632 return OK;
5633 }
5634
5635 c = *option_end;
5636 *option_end = NUL;
5637 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 if (opt_type == -3) /* invalid name */
5641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005642 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 EMSG2(_("E113: Unknown option: %s"), *arg);
5644 ret = FAIL;
5645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
5648 if (opt_type == -2) /* hidden string option */
5649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650 rettv->v_type = VAR_STRING;
5651 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
5653 else if (opt_type == -1) /* hidden number option */
5654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 rettv->v_type = VAR_NUMBER;
5656 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 else if (opt_type == 1) /* number option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_NUMBER;
5661 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else /* string option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_STRING;
5666 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 }
5669 else if (working && (opt_type == -2 || opt_type == -1))
5670 ret = FAIL;
5671
5672 *option_end = c; /* put back for error messages */
5673 *arg = option_end;
5674
5675 return ret;
5676}
5677
5678/*
5679 * Allocate a variable for a string constant.
5680 * Return OK or FAIL.
5681 */
5682 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 int evaluate;
5687{
5688 char_u *p;
5689 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 int extra = 0;
5691
5692 /*
5693 * Find the end of the string, skipping backslashed characters.
5694 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005695 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
5697 if (*p == '\\' && p[1] != NUL)
5698 {
5699 ++p;
5700 /* A "\<x>" form occupies at least 4 characters, and produces up
5701 * to 6 characters: reserve space for 2 extra */
5702 if (*p == '<')
5703 extra += 2;
5704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706
5707 if (*p != '"')
5708 {
5709 EMSG2(_("E114: Missing quote: %s"), *arg);
5710 return FAIL;
5711 }
5712
5713 /* If only parsing, set *arg and return here */
5714 if (!evaluate)
5715 {
5716 *arg = p + 1;
5717 return OK;
5718 }
5719
5720 /*
5721 * Copy the string into allocated memory, handling backslashed
5722 * characters.
5723 */
5724 name = alloc((unsigned)(p - *arg + extra));
5725 if (name == NULL)
5726 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 rettv->v_type = VAR_STRING;
5728 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
5732 if (*p == '\\')
5733 {
5734 switch (*++p)
5735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 case 'b': *name++ = BS; ++p; break;
5737 case 'e': *name++ = ESC; ++p; break;
5738 case 'f': *name++ = FF; ++p; break;
5739 case 'n': *name++ = NL; ++p; break;
5740 case 'r': *name++ = CAR; ++p; break;
5741 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
5743 case 'X': /* hex: "\x1", "\x12" */
5744 case 'x':
5745 case 'u': /* Unicode: "\u0023" */
5746 case 'U':
5747 if (vim_isxdigit(p[1]))
5748 {
5749 int n, nr;
5750 int c = toupper(*p);
5751
5752 if (c == 'X')
5753 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005754 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005756 else
5757 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 nr = 0;
5759 while (--n >= 0 && vim_isxdigit(p[1]))
5760 {
5761 ++p;
5762 nr = (nr << 4) + hex2nr(*p);
5763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765#ifdef FEAT_MBYTE
5766 /* For "\u" store the number according to
5767 * 'encoding'. */
5768 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 else
5771#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 break;
5775
5776 /* octal: "\1", "\12", "\123" */
5777 case '0':
5778 case '1':
5779 case '2':
5780 case '3':
5781 case '4':
5782 case '5':
5783 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 case '7': *name = *p++ - '0';
5785 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 *name = (*name << 3) + *p++ - '0';
5788 if (*p >= '0' && *p <= '7')
5789 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 break;
5793
5794 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 if (extra != 0)
5797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800 }
5801 /* FALLTHROUGH */
5802
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 break;
5805 }
5806 }
5807 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 *arg = p + 1;
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 return OK;
5815}
5816
5817/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 * Return OK or FAIL.
5820 */
5821 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005822get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 int evaluate;
5826{
5827 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005828 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829 int reduce = 0;
5830
5831 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 */
5834 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5835 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 break;
5840 ++reduce;
5841 ++p;
5842 }
5843 }
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 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 return FAIL;
5849 }
5850
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 if (!evaluate)
5853 {
5854 *arg = p + 1;
5855 return OK;
5856 }
5857
5858 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 */
5861 str = alloc((unsigned)((p - *arg) - reduce));
5862 if (str == NULL)
5863 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 rettv->v_type = VAR_STRING;
5865 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 break;
5873 ++p;
5874 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 *arg = p + 1;
5879
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 return OK;
5881}
5882
5883/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 * Allocate a variable for a List and fill it from "*arg".
5885 * Return OK or FAIL.
5886 */
5887 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005888get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 int evaluate;
5892{
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 list_T *l = NULL;
5894 typval_T tv;
5895 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896
5897 if (evaluate)
5898 {
5899 l = list_alloc();
5900 if (l == NULL)
5901 return FAIL;
5902 }
5903
5904 *arg = skipwhite(*arg + 1);
5905 while (**arg != ']' && **arg != NUL)
5906 {
5907 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5908 goto failret;
5909 if (evaluate)
5910 {
5911 item = listitem_alloc();
5912 if (item != NULL)
5913 {
5914 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005915 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 list_append(l, item);
5917 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005918 else
5919 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920 }
5921
5922 if (**arg == ']')
5923 break;
5924 if (**arg != ',')
5925 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 goto failret;
5928 }
5929 *arg = skipwhite(*arg + 1);
5930 }
5931
5932 if (**arg != ']')
5933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935failret:
5936 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005937 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 return FAIL;
5939 }
5940
5941 *arg = skipwhite(*arg + 1);
5942 if (evaluate)
5943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005944 rettv->v_type = VAR_LIST;
5945 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 ++l->lv_refcount;
5947 }
5948
5949 return OK;
5950}
5951
5952/*
5953 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005954 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005956 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957list_alloc()
5958{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 list_T *l;
5960
5961 l = (list_T *)alloc_clear(sizeof(list_T));
5962 if (l != NULL)
5963 {
5964 /* Prepend the list to the list of lists for garbage collection. */
5965 if (first_list != NULL)
5966 first_list->lv_used_prev = l;
5967 l->lv_used_prev = NULL;
5968 l->lv_used_next = first_list;
5969 first_list = l;
5970 }
5971 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972}
5973
5974/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005975 * Allocate an empty list for a return value.
5976 * Returns OK or FAIL.
5977 */
5978 static int
5979rettv_list_alloc(rettv)
5980 typval_T *rettv;
5981{
5982 list_T *l = list_alloc();
5983
5984 if (l == NULL)
5985 return FAIL;
5986
5987 rettv->vval.v_list = l;
5988 rettv->v_type = VAR_LIST;
5989 ++l->lv_refcount;
5990 return OK;
5991}
5992
5993/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 * Unreference a list: decrement the reference count and free it when it
5995 * becomes zero.
5996 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005997 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006001 if (l != NULL && --l->lv_refcount <= 0)
6002 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003}
6004
6005/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006006 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Ignores the reference count.
6008 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006009 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006010list_free(l, recurse)
6011 list_T *l;
6012 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006016 /* Remove the list from the list of lists for garbage collection. */
6017 if (l->lv_used_prev == NULL)
6018 first_list = l->lv_used_next;
6019 else
6020 l->lv_used_prev->lv_used_next = l->lv_used_next;
6021 if (l->lv_used_next != NULL)
6022 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6023
Bram Moolenaard9fba312005-06-26 22:34:35 +00006024 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006026 /* Remove the item before deleting it. */
6027 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006028 if (recurse || (item->li_tv.v_type != VAR_LIST
6029 && item->li_tv.v_type != VAR_DICT))
6030 clear_tv(&item->li_tv);
6031 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032 }
6033 vim_free(l);
6034}
6035
6036/*
6037 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006038 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006040 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041listitem_alloc()
6042{
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044}
6045
6046/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006047 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006049 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006053 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 vim_free(item);
6055}
6056
6057/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006058 * Remove a list item from a List and free it. Also clears the value.
6059 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006060 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006061listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 list_T *l;
6063 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006064{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006065 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006066 listitem_free(item);
6067}
6068
6069/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 * Get the number of items in a list.
6071 */
6072 static long
6073list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 if (l == NULL)
6077 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006078 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079}
6080
6081/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082 * Return TRUE when two lists have exactly the same values.
6083 */
6084 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006085list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 list_T *l1;
6087 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090{
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006093 if (l1 == NULL || l2 == NULL)
6094 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006095 if (l1 == l2)
6096 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006097 if (list_len(l1) != list_len(l2))
6098 return FALSE;
6099
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 for (item1 = l1->lv_first, item2 = l2->lv_first;
6101 item1 != NULL && item2 != NULL;
6102 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104 return FALSE;
6105 return item1 == NULL && item2 == NULL;
6106}
6107
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006108#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6109 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006110/*
6111 * Return the dictitem that an entry in a hashtable points to.
6112 */
6113 dictitem_T *
6114dict_lookup(hi)
6115 hashitem_T *hi;
6116{
6117 return HI2DI(hi);
6118}
6119#endif
6120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006122 * Return TRUE when two dictionaries have exactly the same key/values.
6123 */
6124 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006125dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 dict_T *d1;
6127 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 hashitem_T *hi;
6132 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006133 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006135 if (d1 == NULL || d2 == NULL)
6136 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006137 if (d1 == d2)
6138 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139 if (dict_len(d1) != dict_len(d2))
6140 return FALSE;
6141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006142 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006145 if (!HASHITEM_EMPTY(hi))
6146 {
6147 item2 = dict_find(d2, hi->hi_key, -1);
6148 if (item2 == NULL)
6149 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006151 return FALSE;
6152 --todo;
6153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 }
6155 return TRUE;
6156}
6157
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006158static int tv_equal_recurse_limit;
6159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006163 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 */
6165 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 typval_T *tv1;
6168 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006169 int ic; /* ignore case */
6170 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171{
6172 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006173 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006175 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006177 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006178 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006180 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181 * recursiveness to a limit. We guess they are equal then.
6182 * A fixed limit has the problem of still taking an awful long time.
6183 * Reduce the limit every time running into it. That should work fine for
6184 * deeply linked structures that are not recursively linked and catch
6185 * recursiveness quickly. */
6186 if (!recursive)
6187 tv_equal_recurse_limit = 1000;
6188 if (recursive_cnt >= tv_equal_recurse_limit)
6189 {
6190 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006191 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006192 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193
6194 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006195 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006196 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006197 ++recursive_cnt;
6198 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6199 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006200 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201
6202 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 ++recursive_cnt;
6204 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6205 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006206 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207
6208 case VAR_FUNC:
6209 return (tv1->vval.v_string != NULL
6210 && tv2->vval.v_string != NULL
6211 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6212
6213 case VAR_NUMBER:
6214 return tv1->vval.v_number == tv2->vval.v_number;
6215
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006216#ifdef FEAT_FLOAT
6217 case VAR_FLOAT:
6218 return tv1->vval.v_float == tv2->vval.v_float;
6219#endif
6220
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006221 case VAR_STRING:
6222 s1 = get_tv_string_buf(tv1, buf1);
6223 s2 = get_tv_string_buf(tv2, buf2);
6224 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006226
6227 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006228 return TRUE;
6229}
6230
6231/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006232 * Locate item with index "n" in list "l" and return it.
6233 * A negative index is counted from the end; -1 is the last item.
6234 * Returns NULL when "n" is out of range.
6235 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006236 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006238 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239 long n;
6240{
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 long idx;
6243
6244 if (l == NULL)
6245 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006246
6247 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006249 n = l->lv_len + n;
6250
6251 /* Check for index out of range. */
6252 if (n < 0 || n >= l->lv_len)
6253 return NULL;
6254
6255 /* When there is a cached index may start search from there. */
6256 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006258 if (n < l->lv_idx / 2)
6259 {
6260 /* closest to the start of the list */
6261 item = l->lv_first;
6262 idx = 0;
6263 }
6264 else if (n > (l->lv_idx + l->lv_len) / 2)
6265 {
6266 /* closest to the end of the list */
6267 item = l->lv_last;
6268 idx = l->lv_len - 1;
6269 }
6270 else
6271 {
6272 /* closest to the cached index */
6273 item = l->lv_idx_item;
6274 idx = l->lv_idx;
6275 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 }
6277 else
6278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_len / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006292
6293 while (n > idx)
6294 {
6295 /* search forward */
6296 item = item->li_next;
6297 ++idx;
6298 }
6299 while (n < idx)
6300 {
6301 /* search backward */
6302 item = item->li_prev;
6303 --idx;
6304 }
6305
6306 /* cache the used index */
6307 l->lv_idx = idx;
6308 l->lv_idx_item = item;
6309
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 return item;
6311}
6312
6313/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006314 * Get list item "l[idx]" as a number.
6315 */
6316 static long
6317list_find_nr(l, idx, errorp)
6318 list_T *l;
6319 long idx;
6320 int *errorp; /* set to TRUE when something wrong */
6321{
6322 listitem_T *li;
6323
6324 li = list_find(l, idx);
6325 if (li == NULL)
6326 {
6327 if (errorp != NULL)
6328 *errorp = TRUE;
6329 return -1L;
6330 }
6331 return get_tv_number_chk(&li->li_tv, errorp);
6332}
6333
6334/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006335 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6336 */
6337 char_u *
6338list_find_str(l, idx)
6339 list_T *l;
6340 long idx;
6341{
6342 listitem_T *li;
6343
6344 li = list_find(l, idx - 1);
6345 if (li == NULL)
6346 {
6347 EMSGN(_(e_listidx), idx);
6348 return NULL;
6349 }
6350 return get_tv_string(&li->li_tv);
6351}
6352
6353/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006354 * Locate "item" list "l" and return its index.
6355 * Returns -1 when "item" is not in the list.
6356 */
6357 static long
6358list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 list_T *l;
6360 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006361{
6362 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006364
6365 if (l == NULL)
6366 return -1;
6367 idx = 0;
6368 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6369 ++idx;
6370 if (li == NULL)
6371 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006372 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373}
6374
6375/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376 * Append item "item" to the end of list "l".
6377 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006378 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 list_T *l;
6381 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382{
6383 if (l->lv_last == NULL)
6384 {
6385 /* empty list */
6386 l->lv_first = item;
6387 l->lv_last = item;
6388 item->li_prev = NULL;
6389 }
6390 else
6391 {
6392 l->lv_last->li_next = item;
6393 item->li_prev = l->lv_last;
6394 l->lv_last = item;
6395 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006396 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 item->li_next = NULL;
6398}
6399
6400/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006404 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 list_T *l;
6407 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006409 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410
Bram Moolenaar05159a02005-02-26 23:04:13 +00006411 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006413 copy_tv(tv, &li->li_tv);
6414 list_append(l, li);
6415 return OK;
6416}
6417
6418/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006419 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006420 * Return FAIL when out of memory.
6421 */
6422 int
6423list_append_dict(list, dict)
6424 list_T *list;
6425 dict_T *dict;
6426{
6427 listitem_T *li = listitem_alloc();
6428
6429 if (li == NULL)
6430 return FAIL;
6431 li->li_tv.v_type = VAR_DICT;
6432 li->li_tv.v_lock = 0;
6433 li->li_tv.vval.v_dict = dict;
6434 list_append(list, li);
6435 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 return OK;
6437}
6438
6439/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006440 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006441 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006442 * Returns FAIL when out of memory.
6443 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006444 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006445list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446 list_T *l;
6447 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006448 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006449{
6450 listitem_T *li = listitem_alloc();
6451
6452 if (li == NULL)
6453 return FAIL;
6454 list_append(l, li);
6455 li->li_tv.v_type = VAR_STRING;
6456 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006457 if (str == NULL)
6458 li->li_tv.vval.v_string = NULL;
6459 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006460 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006461 return FAIL;
6462 return OK;
6463}
6464
6465/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006466 * Append "n" to list "l".
6467 * Returns FAIL when out of memory.
6468 */
6469 static int
6470list_append_number(l, n)
6471 list_T *l;
6472 varnumber_T n;
6473{
6474 listitem_T *li;
6475
6476 li = listitem_alloc();
6477 if (li == NULL)
6478 return FAIL;
6479 li->li_tv.v_type = VAR_NUMBER;
6480 li->li_tv.v_lock = 0;
6481 li->li_tv.vval.v_number = n;
6482 list_append(l, li);
6483 return OK;
6484}
6485
6486/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 * If "item" is NULL append at the end.
6489 * Return FAIL when out of memory.
6490 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006491 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *l;
6494 typval_T *tv;
6495 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496{
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498
6499 if (ni == NULL)
6500 return FAIL;
6501 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006502 list_insert(l, ni, item);
6503 return OK;
6504}
6505
6506 void
6507list_insert(l, ni, item)
6508 list_T *l;
6509 listitem_T *ni;
6510 listitem_T *item;
6511{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 if (item == NULL)
6513 /* Append new item at end of list. */
6514 list_append(l, ni);
6515 else
6516 {
6517 /* Insert new item before existing item. */
6518 ni->li_prev = item->li_prev;
6519 ni->li_next = item;
6520 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 ++l->lv_idx;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 l->lv_idx_item = NULL;
6529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533}
6534
6535/*
6536 * Extend "l1" with "l2".
6537 * If "bef" is NULL append at the end, otherwise insert before this item.
6538 * Returns FAIL when out of memory.
6539 */
6540 static int
6541list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 list_T *l1;
6543 list_T *l2;
6544 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545{
Bram Moolenaar33570922005-01-25 22:26:29 +00006546 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006547 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006549 /* We also quit the loop when we have inserted the original item count of
6550 * the list, avoid a hang when we extend a list with itself. */
6551 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6553 return FAIL;
6554 return OK;
6555}
6556
6557/*
6558 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6559 * Return FAIL when out of memory.
6560 */
6561 static int
6562list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 list_T *l1;
6564 list_T *l2;
6565 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006569 if (l1 == NULL || l2 == NULL)
6570 return FAIL;
6571
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 if (l == NULL)
6575 return FAIL;
6576 tv->v_type = VAR_LIST;
6577 tv->vval.v_list = l;
6578
6579 /* append all items from the second list */
6580 return list_extend(l, l2, NULL);
6581}
6582
6583/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 * Returns NULL when out of memory.
6588 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594{
Bram Moolenaar33570922005-01-25 22:26:29 +00006595 list_T *copy;
6596 listitem_T *item;
6597 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598
6599 if (orig == NULL)
6600 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601
6602 copy = list_alloc();
6603 if (copy != NULL)
6604 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605 if (copyID != 0)
6606 {
6607 /* Do this before adding the items, because one of the items may
6608 * refer back to this list. */
6609 orig->lv_copyID = copyID;
6610 orig->lv_copylist = copy;
6611 }
6612 for (item = orig->lv_first; item != NULL && !got_int;
6613 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 {
6615 ni = listitem_alloc();
6616 if (ni == NULL)
6617 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006618 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 {
6620 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6621 {
6622 vim_free(ni);
6623 break;
6624 }
6625 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006627 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 list_append(copy, ni);
6629 }
6630 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006631 if (item != NULL)
6632 {
6633 list_unref(copy);
6634 copy = NULL;
6635 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 }
6637
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 return copy;
6639}
6640
6641/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006643 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006644 * This used to be called list_remove, but that conflicts with a Sun header
6645 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006647 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006648vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006649 list_T *l;
6650 listitem_T *item;
6651 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652{
Bram Moolenaar33570922005-01-25 22:26:29 +00006653 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006655 /* notify watchers */
6656 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006658 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006659 list_fix_watch(l, ip);
6660 if (ip == item2)
6661 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663
6664 if (item2->li_next == NULL)
6665 l->lv_last = item->li_prev;
6666 else
6667 item2->li_next->li_prev = item->li_prev;
6668 if (item->li_prev == NULL)
6669 l->lv_first = item2->li_next;
6670 else
6671 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006672 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673}
6674
6675/*
6676 * Return an allocated string with the string representation of a list.
6677 * May return NULL.
6678 */
6679 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006680list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683{
6684 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685
6686 if (tv->vval.v_list == NULL)
6687 return NULL;
6688 ga_init2(&ga, (int)sizeof(char), 80);
6689 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 {
6692 vim_free(ga.ga_data);
6693 return NULL;
6694 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695 ga_append(&ga, ']');
6696 ga_append(&ga, NUL);
6697 return (char_u *)ga.ga_data;
6698}
6699
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700typedef struct join_S {
6701 char_u *s;
6702 char_u *tofree;
6703} join_T;
6704
6705 static int
6706list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6707 garray_T *gap; /* to store the result in */
6708 list_T *l;
6709 char_u *sep;
6710 int echo_style;
6711 int copyID;
6712 garray_T *join_gap; /* to keep each list item string */
6713{
6714 int i;
6715 join_T *p;
6716 int len;
6717 int sumlen = 0;
6718 int first = TRUE;
6719 char_u *tofree;
6720 char_u numbuf[NUMBUFLEN];
6721 listitem_T *item;
6722 char_u *s;
6723
6724 /* Stringify each item in the list. */
6725 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6726 {
6727 if (echo_style)
6728 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6729 else
6730 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6731 if (s == NULL)
6732 return FAIL;
6733
6734 len = (int)STRLEN(s);
6735 sumlen += len;
6736
6737 ga_grow(join_gap, 1);
6738 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6739 if (tofree != NULL || s != numbuf)
6740 {
6741 p->s = s;
6742 p->tofree = tofree;
6743 }
6744 else
6745 {
6746 p->s = vim_strnsave(s, len);
6747 p->tofree = p->s;
6748 }
6749
6750 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006751 if (did_echo_string_emsg) /* recursion error, bail out */
6752 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006753 }
6754
6755 /* Allocate result buffer with its total size, avoid re-allocation and
6756 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6757 if (join_gap->ga_len >= 2)
6758 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6759 if (ga_grow(gap, sumlen + 2) == FAIL)
6760 return FAIL;
6761
6762 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6763 {
6764 if (first)
6765 first = FALSE;
6766 else
6767 ga_concat(gap, sep);
6768 p = ((join_T *)join_gap->ga_data) + i;
6769
6770 if (p->s != NULL)
6771 ga_concat(gap, p->s);
6772 line_breakcheck();
6773 }
6774
6775 return OK;
6776}
6777
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006780 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006781 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006784list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006785 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006786 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006788 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006789 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 garray_T join_ga;
6792 int retval;
6793 join_T *p;
6794 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795
Bram Moolenaard39a7512015-04-16 22:51:22 +02006796 if (l->lv_len < 1)
6797 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6799 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6800
6801 /* Dispose each item in join_ga. */
6802 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006804 p = (join_T *)join_ga.ga_data;
6805 for (i = 0; i < join_ga.ga_len; ++i)
6806 {
6807 vim_free(p->tofree);
6808 ++p;
6809 }
6810 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812
6813 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814}
6815
6816/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006817 * Garbage collection for lists and dictionaries.
6818 *
6819 * We use reference counts to be able to free most items right away when they
6820 * are no longer used. But for composite items it's possible that it becomes
6821 * unused while the reference count is > 0: When there is a recursive
6822 * reference. Example:
6823 * :let l = [1, 2, 3]
6824 * :let d = {9: l}
6825 * :let l[1] = d
6826 *
6827 * Since this is quite unusual we handle this with garbage collection: every
6828 * once in a while find out which lists and dicts are not referenced from any
6829 * variable.
6830 *
6831 * Here is a good reference text about garbage collection (refers to Python
6832 * but it applies to all reference-counting mechanisms):
6833 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006835
6836/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 * Do garbage collection for lists and dicts.
6838 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 int
6841garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006843 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006844 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 buf_T *buf;
6846 win_T *wp;
6847 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006848 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006849 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006850 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006851#ifdef FEAT_WINDOWS
6852 tabpage_T *tp;
6853#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006855 /* Only do this once. */
6856 want_garbage_collect = FALSE;
6857 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006858 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006859
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 /* We advance by two because we add one for items referenced through
6861 * previous_funccal. */
6862 current_copyID += COPYID_INC;
6863 copyID = current_copyID;
6864
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 /*
6866 * 1. Go through all accessible variables and mark all lists and dicts
6867 * with copyID.
6868 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869
6870 /* Don't free variables in the previous_funccal list unless they are only
6871 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006872 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6874 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006875 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6876 NULL);
6877 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6878 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879 }
6880
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 /* script-local variables */
6882 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884
6885 /* buffer-local variables */
6886 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6888 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889
6890 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006891 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006894#ifdef FEAT_AUTOCMD
6895 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6897 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006898#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006900#ifdef FEAT_WINDOWS
6901 /* tabpage-local variables */
6902 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6904 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905#endif
6906
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
6910 /* function-local variables */
6911 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6912 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6914 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 }
6916
Bram Moolenaard812df62008-11-09 12:46:09 +00006917 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006919
Bram Moolenaar1dced572012-04-05 16:54:08 +02006920#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006922#endif
6923
Bram Moolenaardb913952012-06-29 12:54:53 +02006924#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#endif
6927
6928#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006930#endif
6931
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 /*
6935 * 2. Free lists and dictionaries that are not referenced.
6936 */
6937 did_free = free_unref_items(copyID);
6938
6939 /*
6940 * 3. Check if any funccal can be freed now.
6941 */
6942 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 if (can_free_funccal(*pfc, copyID))
6945 {
6946 fc = *pfc;
6947 *pfc = fc->caller;
6948 free_funccal(fc, TRUE);
6949 did_free = TRUE;
6950 did_free_funccal = TRUE;
6951 }
6952 else
6953 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 if (did_free_funccal)
6956 /* When a funccal was freed some more items might be garbage
6957 * collected, so run again. */
6958 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 else if (p_verbose > 0)
6961 {
6962 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6963 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964
6965 return did_free;
6966}
6967
6968/*
6969 * Free lists and dictionaries that are no longer referenced.
6970 */
6971 static int
6972free_unref_items(copyID)
6973 int copyID;
6974{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 dict_T *dd, *dd_next;
6976 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 int did_free = FALSE;
6978
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 */
6982 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006983 {
6984 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006987 /* Free the Dictionary and ordinary items it contains, but don't
6988 * recurse into Lists and Dictionaries, they will be in the list
6989 * of dicts or list of lists. */
6990 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006993 dd = dd_next;
6994 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995
6996 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 * Go through the list of lists and free items without the copyID.
6998 * But don't free a list that has a watcher (used in a for loop), these
6999 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 */
7001 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007002 {
7003 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7005 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007007 /* Free the List and ordinary items it contains, but don't recurse
7008 * into Lists and Dictionaries, they will be in the list of dicts
7009 * or list of lists. */
7010 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 ll = ll_next;
7014 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 return did_free;
7016}
7017
7018/*
7019 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 * "list_stack" is used to add lists to be marked. Can be NULL.
7021 *
7022 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 int
7025set_ref_in_ht(ht, copyID, list_stack)
7026 hashtab_T *ht;
7027 int copyID;
7028 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029{
7030 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 hashtab_T *cur_ht;
7034 ht_stack_T *ht_stack = NULL;
7035 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 cur_ht = ht;
7038 for (;;)
7039 {
7040 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 /* Mark each item in the hashtab. If the item contains a hashtab
7043 * it is added to ht_stack, if it contains a list it is added to
7044 * list_stack. */
7045 todo = (int)cur_ht->ht_used;
7046 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7047 if (!HASHITEM_EMPTY(hi))
7048 {
7049 --todo;
7050 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7051 &ht_stack, list_stack);
7052 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054
7055 if (ht_stack == NULL)
7056 break;
7057
7058 /* take an item from the stack */
7059 cur_ht = ht_stack->ht;
7060 tempitem = ht_stack;
7061 ht_stack = ht_stack->prev;
7062 free(tempitem);
7063 }
7064
7065 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066}
7067
7068/*
7069 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7071 *
7072 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 int
7075set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 list_T *l;
7077 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 listitem_T *li;
7081 int abort = FALSE;
7082 list_T *cur_l;
7083 list_stack_T *list_stack = NULL;
7084 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007086 cur_l = l;
7087 for (;;)
7088 {
7089 if (!abort)
7090 /* Mark each item in the list. If the item contains a hashtab
7091 * it is added to ht_stack, if it contains a list it is added to
7092 * list_stack. */
7093 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7094 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7095 ht_stack, &list_stack);
7096 if (list_stack == NULL)
7097 break;
7098
7099 /* take an item from the stack */
7100 cur_l = list_stack->list;
7101 tempitem = list_stack;
7102 list_stack = list_stack->prev;
7103 free(tempitem);
7104 }
7105
7106 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107}
7108
7109/*
7110 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 * "list_stack" is used to add lists to be marked. Can be NULL.
7112 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7113 *
7114 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 int
7117set_ref_in_item(tv, copyID, ht_stack, list_stack)
7118 typval_T *tv;
7119 int copyID;
7120 ht_stack_T **ht_stack;
7121 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122{
7123 dict_T *dd;
7124 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126
7127 switch (tv->v_type)
7128 {
7129 case VAR_DICT:
7130 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007131 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007132 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007133 /* Didn't see this dict yet. */
7134 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 if (ht_stack == NULL)
7136 {
7137 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7138 }
7139 else
7140 {
7141 ht_stack_T *newitem = (ht_stack_T*)malloc(
7142 sizeof(ht_stack_T));
7143 if (newitem == NULL)
7144 abort = TRUE;
7145 else
7146 {
7147 newitem->ht = &dd->dv_hashtab;
7148 newitem->prev = *ht_stack;
7149 *ht_stack = newitem;
7150 }
7151 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007153 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154
7155 case VAR_LIST:
7156 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007157 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007158 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007159 /* Didn't see this list yet. */
7160 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 if (list_stack == NULL)
7162 {
7163 abort = set_ref_in_list(ll, copyID, ht_stack);
7164 }
7165 else
7166 {
7167 list_stack_T *newitem = (list_stack_T*)malloc(
7168 sizeof(list_stack_T));
7169 if (newitem == NULL)
7170 abort = TRUE;
7171 else
7172 {
7173 newitem->list = ll;
7174 newitem->prev = *list_stack;
7175 *list_stack = newitem;
7176 }
7177 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007178 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007179 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007180 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007182}
7183
7184/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185 * Allocate an empty header for a dictionary.
7186 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007187 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188dict_alloc()
7189{
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007195 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007196 if (first_dict != NULL)
7197 first_dict->dv_used_prev = d;
7198 d->dv_used_next = first_dict;
7199 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007200 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007203 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007204 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007205 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007206 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007207 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209}
7210
7211/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007212 * Allocate an empty dict for a return value.
7213 * Returns OK or FAIL.
7214 */
7215 static int
7216rettv_dict_alloc(rettv)
7217 typval_T *rettv;
7218{
7219 dict_T *d = dict_alloc();
7220
7221 if (d == NULL)
7222 return FAIL;
7223
7224 rettv->vval.v_dict = d;
7225 rettv->v_type = VAR_DICT;
7226 ++d->dv_refcount;
7227 return OK;
7228}
7229
7230
7231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 * Unreference a Dictionary: decrement the reference count and free it when it
7233 * becomes zero.
7234 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007235 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007239 if (d != NULL && --d->dv_refcount <= 0)
7240 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241}
7242
7243/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007244 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 * Ignores the reference count.
7246 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007247 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007248dict_free(d, recurse)
7249 dict_T *d;
7250 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007256 /* Remove the dict from the list of dicts for garbage collection. */
7257 if (d->dv_used_prev == NULL)
7258 first_dict = d->dv_used_next;
7259 else
7260 d->dv_used_prev->dv_used_next = d->dv_used_next;
7261 if (d->dv_used_next != NULL)
7262 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7263
7264 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007265 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007266 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 if (!HASHITEM_EMPTY(hi))
7270 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007271 /* Remove the item before deleting it, just in case there is
7272 * something recursive causing trouble. */
7273 di = HI2DI(hi);
7274 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007275 if (recurse || (di->di_tv.v_type != VAR_LIST
7276 && di->di_tv.v_type != VAR_DICT))
7277 clear_tv(&di->di_tv);
7278 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 --todo;
7280 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 vim_free(d);
7284}
7285
7286/*
7287 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 * The "key" is copied to the new item.
7289 * Note that the value of the item "di_tv" still needs to be initialized!
7290 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007292 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293dictitem_alloc(key)
7294 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007302 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305}
7306
7307/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308 * Make a copy of a Dictionary item.
7309 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313{
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007316 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7317 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 if (di != NULL)
7319 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007321 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 copy_tv(&org->di_tv, &di->di_tv);
7323 }
7324 return di;
7325}
7326
7327/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 * Remove item "item" from Dictionary "dict" and free it.
7329 */
7330 static void
7331dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 dict_T *dict;
7333 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 if (HASHITEM_EMPTY(hi))
7339 EMSG2(_(e_intern2), "dictitem_remove()");
7340 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 dictitem_free(item);
7343}
7344
7345/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 * Free a dict item. Also clears the value.
7347 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007348 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007353 if (item->di_flags & DI_FLAGS_ALLOC)
7354 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355}
7356
7357/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7359 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007360 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 * Returns NULL when out of memory.
7362 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007364dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368{
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 dict_T *copy;
7370 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007372 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373
7374 if (orig == NULL)
7375 return NULL;
7376
7377 copy = dict_alloc();
7378 if (copy != NULL)
7379 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007380 if (copyID != 0)
7381 {
7382 orig->dv_copyID = copyID;
7383 orig->dv_copydict = copy;
7384 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007385 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007386 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 --todo;
7391
7392 di = dictitem_alloc(hi->hi_key);
7393 if (di == NULL)
7394 break;
7395 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 {
7397 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7398 copyID) == FAIL)
7399 {
7400 vim_free(di);
7401 break;
7402 }
7403 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 else
7405 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7406 if (dict_add(copy, di) == FAIL)
7407 {
7408 dictitem_free(di);
7409 break;
7410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007413
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007415 if (todo > 0)
7416 {
7417 dict_unref(copy);
7418 copy = NULL;
7419 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007420 }
7421
7422 return copy;
7423}
7424
7425/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007427 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007429 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 dict_T *d;
7432 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433{
Bram Moolenaar33570922005-01-25 22:26:29 +00007434 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435}
7436
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007438 * Add a number or string entry to dictionary "d".
7439 * When "str" is NULL use number "nr", otherwise use "str".
7440 * Returns FAIL when out of memory and when key already exists.
7441 */
7442 int
7443dict_add_nr_str(d, key, nr, str)
7444 dict_T *d;
7445 char *key;
7446 long nr;
7447 char_u *str;
7448{
7449 dictitem_T *item;
7450
7451 item = dictitem_alloc((char_u *)key);
7452 if (item == NULL)
7453 return FAIL;
7454 item->di_tv.v_lock = 0;
7455 if (str == NULL)
7456 {
7457 item->di_tv.v_type = VAR_NUMBER;
7458 item->di_tv.vval.v_number = nr;
7459 }
7460 else
7461 {
7462 item->di_tv.v_type = VAR_STRING;
7463 item->di_tv.vval.v_string = vim_strsave(str);
7464 }
7465 if (dict_add(d, item) == FAIL)
7466 {
7467 dictitem_free(item);
7468 return FAIL;
7469 }
7470 return OK;
7471}
7472
7473/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007474 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007475 * Returns FAIL when out of memory and when key already exists.
7476 */
7477 int
7478dict_add_list(d, key, list)
7479 dict_T *d;
7480 char *key;
7481 list_T *list;
7482{
7483 dictitem_T *item;
7484
7485 item = dictitem_alloc((char_u *)key);
7486 if (item == NULL)
7487 return FAIL;
7488 item->di_tv.v_lock = 0;
7489 item->di_tv.v_type = VAR_LIST;
7490 item->di_tv.vval.v_list = list;
7491 if (dict_add(d, item) == FAIL)
7492 {
7493 dictitem_free(item);
7494 return FAIL;
7495 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007496 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007497 return OK;
7498}
7499
7500/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 * Get the number of items in a Dictionary.
7502 */
7503 static long
7504dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507 if (d == NULL)
7508 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007509 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007510}
7511
7512/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 * Find item "key[len]" in Dictionary "d".
7514 * If "len" is negative use strlen(key).
7515 * Returns NULL when not found.
7516 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007517 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 char_u *key;
7521 int len;
7522{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523#define AKEYLEN 200
7524 char_u buf[AKEYLEN];
7525 char_u *akey;
7526 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 if (len < 0)
7530 akey = key;
7531 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007532 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 tofree = akey = vim_strnsave(key, len);
7534 if (akey == NULL)
7535 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007536 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 else
7538 {
7539 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007540 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541 akey = buf;
7542 }
7543
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 vim_free(tofree);
7546 if (HASHITEM_EMPTY(hi))
7547 return NULL;
7548 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549}
7550
7551/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007552 * Get a string item from a dictionary.
7553 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007554 * Returns NULL if the entry doesn't exist or out of memory.
7555 */
7556 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007557get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007558 dict_T *d;
7559 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007560 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007561{
7562 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007563 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007564
7565 di = dict_find(d, key, -1);
7566 if (di == NULL)
7567 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007568 s = get_tv_string(&di->di_tv);
7569 if (save && s != NULL)
7570 s = vim_strsave(s);
7571 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007572}
7573
7574/*
7575 * Get a number item from a dictionary.
7576 * Returns 0 if the entry doesn't exist or out of memory.
7577 */
7578 long
7579get_dict_number(d, key)
7580 dict_T *d;
7581 char_u *key;
7582{
7583 dictitem_T *di;
7584
7585 di = dict_find(d, key, -1);
7586 if (di == NULL)
7587 return 0;
7588 return get_tv_number(&di->di_tv);
7589}
7590
7591/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 * Return an allocated string with the string representation of a Dictionary.
7593 * May return NULL.
7594 */
7595 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007596dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599{
7600 garray_T ga;
7601 int first = TRUE;
7602 char_u *tofree;
7603 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007607 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007609 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 return NULL;
7611 ga_init2(&ga, (int)sizeof(char), 80);
7612 ga_append(&ga, '{');
7613
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007614 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007615 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 --todo;
7620
7621 if (first)
7622 first = FALSE;
7623 else
7624 ga_concat(&ga, (char_u *)", ");
7625
7626 tofree = string_quote(hi->hi_key, FALSE);
7627 if (tofree != NULL)
7628 {
7629 ga_concat(&ga, tofree);
7630 vim_free(tofree);
7631 }
7632 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007633 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 if (s != NULL)
7635 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007637 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007638 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007639 line_breakcheck();
7640
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007643 if (todo > 0)
7644 {
7645 vim_free(ga.ga_data);
7646 return NULL;
7647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648
7649 ga_append(&ga, '}');
7650 ga_append(&ga, NUL);
7651 return (char_u *)ga.ga_data;
7652}
7653
7654/*
7655 * Allocate a variable for a Dictionary and fill it from "*arg".
7656 * Return OK or FAIL. Returns NOTDONE for {expr}.
7657 */
7658 static int
7659get_dict_tv(arg, rettv, evaluate)
7660 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 int evaluate;
7663{
Bram Moolenaar33570922005-01-25 22:26:29 +00007664 dict_T *d = NULL;
7665 typval_T tvkey;
7666 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007667 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007670 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671
7672 /*
7673 * First check if it's not a curly-braces thing: {expr}.
7674 * Must do this without evaluating, otherwise a function may be called
7675 * twice. Unfortunately this means we need to call eval1() twice for the
7676 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007679 if (*start != '}')
7680 {
7681 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7682 return FAIL;
7683 if (*start == '}')
7684 return NOTDONE;
7685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686
7687 if (evaluate)
7688 {
7689 d = dict_alloc();
7690 if (d == NULL)
7691 return FAIL;
7692 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693 tvkey.v_type = VAR_UNKNOWN;
7694 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695
7696 *arg = skipwhite(*arg + 1);
7697 while (**arg != '}' && **arg != NUL)
7698 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 goto failret;
7701 if (**arg != ':')
7702 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007703 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007704 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 goto failret;
7706 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007707 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007709 key = get_tv_string_buf_chk(&tvkey, buf);
7710 if (key == NULL || *key == NUL)
7711 {
7712 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7713 if (key != NULL)
7714 EMSG(_(e_emptykey));
7715 clear_tv(&tvkey);
7716 goto failret;
7717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719
7720 *arg = skipwhite(*arg + 1);
7721 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7722 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007723 if (evaluate)
7724 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 goto failret;
7726 }
7727 if (evaluate)
7728 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007729 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 if (item != NULL)
7731 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007732 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007733 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734 clear_tv(&tv);
7735 goto failret;
7736 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007737 item = dictitem_alloc(key);
7738 clear_tv(&tvkey);
7739 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007742 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 if (dict_add(d, item) == FAIL)
7744 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 }
7746 }
7747
7748 if (**arg == '}')
7749 break;
7750 if (**arg != ',')
7751 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007752 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 goto failret;
7754 }
7755 *arg = skipwhite(*arg + 1);
7756 }
7757
7758 if (**arg != '}')
7759 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007760 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761failret:
7762 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007763 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 return FAIL;
7765 }
7766
7767 *arg = skipwhite(*arg + 1);
7768 if (evaluate)
7769 {
7770 rettv->v_type = VAR_DICT;
7771 rettv->vval.v_dict = d;
7772 ++d->dv_refcount;
7773 }
7774
7775 return OK;
7776}
7777
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 * Return a string with the string representation of a variable.
7780 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007781 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007782 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007783 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007784 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007785 */
7786 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007787echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007790 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007791 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007792{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007793 static int recurse = 0;
7794 char_u *r = NULL;
7795
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007797 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007798 if (!did_echo_string_emsg)
7799 {
7800 /* Only give this message once for a recursive call to avoid
7801 * flooding the user with errors. And stop iterating over lists
7802 * and dicts. */
7803 did_echo_string_emsg = TRUE;
7804 EMSG(_("E724: variable nested too deep for displaying"));
7805 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007807 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 }
7809 ++recurse;
7810
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 switch (tv->v_type)
7812 {
7813 case VAR_FUNC:
7814 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 r = tv->vval.v_string;
7816 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007817
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007818 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007819 if (tv->vval.v_list == NULL)
7820 {
7821 *tofree = NULL;
7822 r = NULL;
7823 }
7824 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7825 {
7826 *tofree = NULL;
7827 r = (char_u *)"[...]";
7828 }
7829 else
7830 {
7831 tv->vval.v_list->lv_copyID = copyID;
7832 *tofree = list2string(tv, copyID);
7833 r = *tofree;
7834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007836
Bram Moolenaar8c711452005-01-14 21:53:12 +00007837 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838 if (tv->vval.v_dict == NULL)
7839 {
7840 *tofree = NULL;
7841 r = NULL;
7842 }
7843 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7844 {
7845 *tofree = NULL;
7846 r = (char_u *)"{...}";
7847 }
7848 else
7849 {
7850 tv->vval.v_dict->dv_copyID = copyID;
7851 *tofree = dict2string(tv, copyID);
7852 r = *tofree;
7853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856 case VAR_STRING:
7857 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007858 *tofree = NULL;
7859 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007861
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007862#ifdef FEAT_FLOAT
7863 case VAR_FLOAT:
7864 *tofree = NULL;
7865 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7866 r = numbuf;
7867 break;
7868#endif
7869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007871 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007872 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874
Bram Moolenaar8502c702014-06-17 12:51:16 +02007875 if (--recurse == 0)
7876 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878}
7879
7880/*
7881 * Return a string with the string representation of a variable.
7882 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7883 * "numbuf" is used for a number.
7884 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007885 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 */
7887 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007888tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 char_u **tofree;
7891 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007892 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893{
7894 switch (tv->v_type)
7895 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 case VAR_FUNC:
7897 *tofree = string_quote(tv->vval.v_string, TRUE);
7898 return *tofree;
7899 case VAR_STRING:
7900 *tofree = string_quote(tv->vval.v_string, FALSE);
7901 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#ifdef FEAT_FLOAT
7903 case VAR_FLOAT:
7904 *tofree = NULL;
7905 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7906 return numbuf;
7907#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007910 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007911 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007912 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007915 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007916}
7917
7918/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 * Return string "str" in ' quotes, doubling ' characters.
7920 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007922 */
7923 static char_u *
7924string_quote(str, function)
7925 char_u *str;
7926 int function;
7927{
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 char_u *p, *r, *s;
7930
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 len = (function ? 13 : 3);
7932 if (str != NULL)
7933 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007934 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007935 for (p = str; *p != NUL; mb_ptr_adv(p))
7936 if (*p == '\'')
7937 ++len;
7938 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 s = r = alloc(len);
7940 if (r != NULL)
7941 {
7942 if (function)
7943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007945 r += 10;
7946 }
7947 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 if (str != NULL)
7950 for (p = str; *p != NUL; )
7951 {
7952 if (*p == '\'')
7953 *r++ = '\'';
7954 MB_COPY_CHAR(p, r);
7955 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 if (function)
7958 *r++ = ')';
7959 *r++ = NUL;
7960 }
7961 return s;
7962}
7963
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964#ifdef FEAT_FLOAT
7965/*
7966 * Convert the string "text" to a floating point number.
7967 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7968 * this always uses a decimal point.
7969 * Returns the length of the text that was consumed.
7970 */
7971 static int
7972string2float(text, value)
7973 char_u *text;
7974 float_T *value; /* result stored here */
7975{
7976 char *s = (char *)text;
7977 float_T f;
7978
7979 f = strtod(s, &s);
7980 *value = f;
7981 return (int)((char_u *)s - text);
7982}
7983#endif
7984
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 * Get the value of an environment variable.
7987 * "arg" is pointing to the '$'. It is advanced to after the name.
7988 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007989 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 */
7991 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 int evaluate;
7996{
7997 char_u *string = NULL;
7998 int len;
7999 int cc;
8000 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008001 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
8003 ++*arg;
8004 name = *arg;
8005 len = get_env_len(arg);
8006 if (evaluate)
8007 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008008 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008009 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008010
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008011 cc = name[len];
8012 name[len] = NUL;
8013 /* first try vim_getenv(), fast for normal environment vars */
8014 string = vim_getenv(name, &mustfree);
8015 if (string != NULL && *string != NUL)
8016 {
8017 if (!mustfree)
8018 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008020 else
8021 {
8022 if (mustfree)
8023 vim_free(string);
8024
8025 /* next try expanding things like $VIM and ${HOME} */
8026 string = expand_env_save(name - 1);
8027 if (string != NULL && *string == '$')
8028 {
8029 vim_free(string);
8030 string = NULL;
8031 }
8032 }
8033 name[len] = cc;
8034
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008035 rettv->v_type = VAR_STRING;
8036 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
8038
8039 return OK;
8040}
8041
8042/*
8043 * Array with names and number of arguments of all internal functions
8044 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8045 */
8046static struct fst
8047{
8048 char *f_name; /* function name */
8049 char f_min_argc; /* minimal number of arguments */
8050 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008052 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053} functions[] =
8054{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#ifdef FEAT_FLOAT
8056 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008057 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008058#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008059 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008060 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"append", 2, 2, f_append},
8062 {"argc", 0, 0, f_argc},
8063 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008064 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008065 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008067 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008069 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008072 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"bufexists", 1, 1, f_bufexists},
8074 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8075 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8076 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8077 {"buflisted", 1, 1, f_buflisted},
8078 {"bufloaded", 1, 1, f_bufloaded},
8079 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008080 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {"bufwinnr", 1, 1, f_bufwinnr},
8082 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008083 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008084 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008085 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"ceil", 1, 1, f_ceil},
8088#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008089 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008090 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008092 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008094#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008095 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008096 {"complete_add", 1, 1, f_complete_add},
8097 {"complete_check", 0, 0, f_complete_check},
8098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008100 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#ifdef FEAT_FLOAT
8102 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008103 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008104#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008105 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008107 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008108 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"delete", 1, 1, f_delete},
8110 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008111 {"diff_filler", 1, 1, f_diff_filler},
8112 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008113 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008115 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"eventhandler", 0, 0, f_eventhandler},
8117 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008118 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008120#ifdef FEAT_FLOAT
8121 {"exp", 1, 1, f_exp},
8122#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008123 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008124 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008125 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8127 {"filereadable", 1, 1, f_filereadable},
8128 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008129 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008130 {"finddir", 1, 3, f_finddir},
8131 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132#ifdef FEAT_FLOAT
8133 {"float2nr", 1, 1, f_float2nr},
8134 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008135 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008137 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"fnamemodify", 2, 2, f_fnamemodify},
8139 {"foldclosed", 1, 1, f_foldclosed},
8140 {"foldclosedend", 1, 1, f_foldclosedend},
8141 {"foldlevel", 1, 1, f_foldlevel},
8142 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008143 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008146 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008147 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008148 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008149 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"getchar", 0, 1, f_getchar},
8151 {"getcharmod", 0, 0, f_getcharmod},
8152 {"getcmdline", 0, 0, f_getcmdline},
8153 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008154 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008155 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008156 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008158 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008159 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"getfsize", 1, 1, f_getfsize},
8161 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008162 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008163 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008164 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008165 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008166 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008167 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008168 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008169 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008171 {"gettabvar", 2, 3, f_gettabvar},
8172 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"getwinposx", 0, 0, f_getwinposx},
8174 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008175 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008176 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008177 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008178 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008180 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008181 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008182 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8184 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8185 {"histadd", 2, 2, f_histadd},
8186 {"histdel", 1, 2, f_histdel},
8187 {"histget", 1, 2, f_histget},
8188 {"histnr", 1, 1, f_histnr},
8189 {"hlID", 1, 1, f_hlID},
8190 {"hlexists", 1, 1, f_hlexists},
8191 {"hostname", 0, 0, f_hostname},
8192 {"iconv", 3, 3, f_iconv},
8193 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008194 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008195 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008197 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"inputrestore", 0, 0, f_inputrestore},
8199 {"inputsave", 0, 0, f_inputsave},
8200 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008201 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008202 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008204 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008205 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008206 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008207 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008209 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"libcall", 3, 3, f_libcall},
8211 {"libcallnr", 3, 3, f_libcallnr},
8212 {"line", 1, 1, f_line},
8213 {"line2byte", 1, 1, f_line2byte},
8214 {"lispindent", 1, 1, f_lispindent},
8215 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008216#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008217 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008218 {"log10", 1, 1, f_log10},
8219#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008220#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008221 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008222#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008223 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008224 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008225 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008226 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008227 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008228 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008229 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008230 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008231 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008232 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008233 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008234 {"max", 1, 1, f_max},
8235 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008236#ifdef vim_mkdir
8237 {"mkdir", 1, 3, f_mkdir},
8238#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008240#ifdef FEAT_MZSCHEME
8241 {"mzeval", 1, 1, f_mzeval},
8242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008244 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008245 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008246 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247#ifdef FEAT_FLOAT
8248 {"pow", 2, 2, f_pow},
8249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008251 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008252 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008253#ifdef FEAT_PYTHON3
8254 {"py3eval", 1, 1, f_py3eval},
8255#endif
8256#ifdef FEAT_PYTHON
8257 {"pyeval", 1, 1, f_pyeval},
8258#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008259 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008260 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008261 {"reltime", 0, 2, f_reltime},
8262 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"remote_expr", 2, 3, f_remote_expr},
8264 {"remote_foreground", 1, 1, f_remote_foreground},
8265 {"remote_peek", 1, 2, f_remote_peek},
8266 {"remote_read", 1, 1, f_remote_read},
8267 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008268 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008270 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#ifdef FEAT_FLOAT
8274 {"round", 1, 1, f_round},
8275#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008276 {"screenattr", 2, 2, f_screenattr},
8277 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008278 {"screencol", 0, 0, f_screencol},
8279 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008280 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008281 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008282 {"searchpair", 3, 7, f_searchpair},
8283 {"searchpairpos", 3, 7, f_searchpairpos},
8284 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"server2client", 2, 2, f_server2client},
8286 {"serverlist", 0, 0, f_serverlist},
8287 {"setbufvar", 3, 3, f_setbufvar},
8288 {"setcmdpos", 1, 1, f_setcmdpos},
8289 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008290 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008291 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008292 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008293 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008295 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008296 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008298#ifdef FEAT_CRYPT
8299 {"sha256", 1, 1, f_sha256},
8300#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008301 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008302 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008304#ifdef FEAT_FLOAT
8305 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008306 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008307#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008308 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008309 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008310 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008311 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008312 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008313#ifdef FEAT_FLOAT
8314 {"sqrt", 1, 1, f_sqrt},
8315 {"str2float", 1, 1, f_str2float},
8316#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008317 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008318 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008319 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320#ifdef HAVE_STRFTIME
8321 {"strftime", 1, 2, f_strftime},
8322#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008323 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008324 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"strlen", 1, 1, f_strlen},
8326 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008327 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008329 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008330 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"substitute", 4, 4, f_substitute},
8332 {"synID", 3, 3, f_synID},
8333 {"synIDattr", 2, 3, f_synIDattr},
8334 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008335 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008336 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008337 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008338 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008339 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008340 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008341 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008342 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008343 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008344#ifdef FEAT_FLOAT
8345 {"tan", 1, 1, f_tan},
8346 {"tanh", 1, 1, f_tanh},
8347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008349 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"tolower", 1, 1, f_tolower},
8351 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008352 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008353#ifdef FEAT_FLOAT
8354 {"trunc", 1, 1, f_trunc},
8355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008357 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008358 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008359 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008360 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"virtcol", 1, 1, f_virtcol},
8362 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008363 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"winbufnr", 1, 1, f_winbufnr},
8365 {"wincol", 0, 0, f_wincol},
8366 {"winheight", 1, 1, f_winheight},
8367 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008368 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008370 {"winrestview", 1, 1, f_winrestview},
8371 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008373 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008374 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375};
8376
8377#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8378
8379/*
8380 * Function given to ExpandGeneric() to obtain the list of internal
8381 * or user defined function names.
8382 */
8383 char_u *
8384get_function_name(xp, idx)
8385 expand_T *xp;
8386 int idx;
8387{
8388 static int intidx = -1;
8389 char_u *name;
8390
8391 if (idx == 0)
8392 intidx = -1;
8393 if (intidx < 0)
8394 {
8395 name = get_user_func_name(xp, idx);
8396 if (name != NULL)
8397 return name;
8398 }
8399 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8400 {
8401 STRCPY(IObuff, functions[intidx].f_name);
8402 STRCAT(IObuff, "(");
8403 if (functions[intidx].f_max_argc == 0)
8404 STRCAT(IObuff, ")");
8405 return IObuff;
8406 }
8407
8408 return NULL;
8409}
8410
8411/*
8412 * Function given to ExpandGeneric() to obtain the list of internal or
8413 * user defined variable or function names.
8414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 char_u *
8416get_expr_name(xp, idx)
8417 expand_T *xp;
8418 int idx;
8419{
8420 static int intidx = -1;
8421 char_u *name;
8422
8423 if (idx == 0)
8424 intidx = -1;
8425 if (intidx < 0)
8426 {
8427 name = get_function_name(xp, idx);
8428 if (name != NULL)
8429 return name;
8430 }
8431 return get_user_var_name(xp, ++intidx);
8432}
8433
8434#endif /* FEAT_CMDL_COMPL */
8435
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008436#if defined(EBCDIC) || defined(PROTO)
8437/*
8438 * Compare struct fst by function name.
8439 */
8440 static int
8441compare_func_name(s1, s2)
8442 const void *s1;
8443 const void *s2;
8444{
8445 struct fst *p1 = (struct fst *)s1;
8446 struct fst *p2 = (struct fst *)s2;
8447
8448 return STRCMP(p1->f_name, p2->f_name);
8449}
8450
8451/*
8452 * Sort the function table by function name.
8453 * The sorting of the table above is ASCII dependant.
8454 * On machines using EBCDIC we have to sort it.
8455 */
8456 static void
8457sortFunctions()
8458{
8459 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8460
8461 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8462}
8463#endif
8464
8465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466/*
8467 * Find internal function in table above.
8468 * Return index, or -1 if not found
8469 */
8470 static int
8471find_internal_func(name)
8472 char_u *name; /* name of the function */
8473{
8474 int first = 0;
8475 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8476 int cmp;
8477 int x;
8478
8479 /*
8480 * Find the function name in the table. Binary search.
8481 */
8482 while (first <= last)
8483 {
8484 x = first + ((unsigned)(last - first) >> 1);
8485 cmp = STRCMP(name, functions[x].f_name);
8486 if (cmp < 0)
8487 last = x - 1;
8488 else if (cmp > 0)
8489 first = x + 1;
8490 else
8491 return x;
8492 }
8493 return -1;
8494}
8495
8496/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008497 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8498 * name it contains, otherwise return "name".
8499 */
8500 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008501deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008502 char_u *name;
8503 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008504 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008505{
Bram Moolenaar33570922005-01-25 22:26:29 +00008506 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 int cc;
8508
8509 cc = name[*lenp];
8510 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008511 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008512 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 {
8517 *lenp = 0;
8518 return (char_u *)""; /* just in case */
8519 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008520 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 }
8523
8524 return name;
8525}
8526
8527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 * Allocate a variable for the result of a function.
8529 * Return OK or FAIL.
8530 */
8531 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8533 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 char_u *name; /* name of the function */
8535 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 char_u **arg; /* argument, pointing to the '(' */
8538 linenr_T firstline; /* first line of range */
8539 linenr_T lastline; /* last line of range */
8540 int *doesrange; /* return: function handled range */
8541 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 char_u *argp;
8545 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008546 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 int argcount = 0; /* number of arguments found */
8548
8549 /*
8550 * Get the arguments.
8551 */
8552 argp = *arg;
8553 while (argcount < MAX_FUNC_ARGS)
8554 {
8555 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8556 if (*argp == ')' || *argp == ',' || *argp == NUL)
8557 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8559 {
8560 ret = FAIL;
8561 break;
8562 }
8563 ++argcount;
8564 if (*argp != ',')
8565 break;
8566 }
8567 if (*argp == ')')
8568 ++argp;
8569 else
8570 ret = FAIL;
8571
8572 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 {
8577 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008578 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008579 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008580 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
8583 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
8586 *arg = skipwhite(argp);
8587 return ret;
8588}
8589
8590
8591/*
8592 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008593 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008594 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 */
8596 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008597call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008598 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008599 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008603 typval_T *argvars; /* vars for arguments, must have "argcount"
8604 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 linenr_T firstline; /* first line of range */
8606 linenr_T lastline; /* last line of range */
8607 int *doesrange; /* return: function handled range */
8608 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008609 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612#define ERROR_UNKNOWN 0
8613#define ERROR_TOOMANY 1
8614#define ERROR_TOOFEW 2
8615#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008616#define ERROR_DICT 4
8617#define ERROR_NONE 5
8618#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 int error = ERROR_NONE;
8620 int i;
8621 int llen;
8622 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623#define FLEN_FIXED 40
8624 char_u fname_buf[FLEN_FIXED + 1];
8625 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008626 char_u *name;
8627
8628 /* Make a copy of the name, if it comes from a funcref variable it could
8629 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008630 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008631 if (name == NULL)
8632 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 /*
8635 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8636 * Change <SNR>123_name() to K_SNR 123_name().
8637 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 llen = eval_fname_script(name);
8640 if (llen > 0)
8641 {
8642 fname_buf[0] = K_SPECIAL;
8643 fname_buf[1] = KS_EXTRA;
8644 fname_buf[2] = (int)KE_SNR;
8645 i = 3;
8646 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8647 {
8648 if (current_SID <= 0)
8649 error = ERROR_SCRIPT;
8650 else
8651 {
8652 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8653 i = (int)STRLEN(fname_buf);
8654 }
8655 }
8656 if (i + STRLEN(name + llen) < FLEN_FIXED)
8657 {
8658 STRCPY(fname_buf + i, name + llen);
8659 fname = fname_buf;
8660 }
8661 else
8662 {
8663 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8664 if (fname == NULL)
8665 error = ERROR_OTHER;
8666 else
8667 {
8668 mch_memmove(fname, fname_buf, (size_t)i);
8669 STRCPY(fname + i, name + llen);
8670 }
8671 }
8672 }
8673 else
8674 fname = name;
8675
8676 *doesrange = FALSE;
8677
8678
8679 /* execute the function if no errors detected and executing */
8680 if (evaluate && error == ERROR_NONE)
8681 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008682 char_u *rfname = fname;
8683
8684 /* Ignore "g:" before a function name. */
8685 if (fname[0] == 'g' && fname[1] == ':')
8686 rfname = fname + 2;
8687
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008688 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8689 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 error = ERROR_UNKNOWN;
8691
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008692 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
8694 /*
8695 * User defined function.
8696 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008697 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008700 /* Trigger FuncUndefined event, may load the function. */
8701 if (fp == NULL
8702 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008703 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008706 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008707 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008710 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008712 {
8713 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008714 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008715 }
8716
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 if (fp != NULL)
8718 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008719 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008721 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008723 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008725 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008726 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 else
8728 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008729 int did_save_redo = FALSE;
8730
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 /*
8732 * Call the user function.
8733 * Save and restore search patterns, script variables and
8734 * redo buffer.
8735 */
8736 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008737 if (!ins_compl_active())
8738 {
8739 saveRedobuff();
8740 did_save_redo = TRUE;
8741 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008742 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008744 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008745 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8746 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8747 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008748 /* Function was unreferenced while being used, free it
8749 * now. */
8750 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008751 if (did_save_redo)
8752 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 restore_search_patterns();
8754 error = ERROR_NONE;
8755 }
8756 }
8757 }
8758 else
8759 {
8760 /*
8761 * Find the function name in the table, call its implementation.
8762 */
8763 i = find_internal_func(fname);
8764 if (i >= 0)
8765 {
8766 if (argcount < functions[i].f_min_argc)
8767 error = ERROR_TOOFEW;
8768 else if (argcount > functions[i].f_max_argc)
8769 error = ERROR_TOOMANY;
8770 else
8771 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008772 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008773 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 error = ERROR_NONE;
8775 }
8776 }
8777 }
8778 /*
8779 * The function call (or "FuncUndefined" autocommand sequence) might
8780 * have been aborted by an error, an interrupt, or an explicitly thrown
8781 * exception that has not been caught so far. This situation can be
8782 * tested for by calling aborting(). For an error in an internal
8783 * function or for the "E132" error in call_user_func(), however, the
8784 * throw point at which the "force_abort" flag (temporarily reset by
8785 * emsg()) is normally updated has not been reached yet. We need to
8786 * update that flag first to make aborting() reliable.
8787 */
8788 update_force_abort();
8789 }
8790 if (error == ERROR_NONE)
8791 ret = OK;
8792
8793 /*
8794 * Report an error unless the argument evaluation or function call has been
8795 * cancelled due to an aborting error, an interrupt, or an exception.
8796 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008797 if (!aborting())
8798 {
8799 switch (error)
8800 {
8801 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008802 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008803 break;
8804 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008805 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008806 break;
8807 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008808 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008809 name);
8810 break;
8811 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008812 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 name);
8814 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008815 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008816 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008817 name);
8818 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008819 }
8820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 if (fname != name && fname != fname_buf)
8823 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008824 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826 return ret;
8827}
8828
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829/*
8830 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008831 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008832 */
8833 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008834emsg_funcname(ermsg, name)
8835 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836 char_u *name;
8837{
8838 char_u *p;
8839
8840 if (*name == K_SPECIAL)
8841 p = concat_str((char_u *)"<SNR>", name + 3);
8842 else
8843 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008844 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008845 if (p != name)
8846 vim_free(p);
8847}
8848
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008849/*
8850 * Return TRUE for a non-zero Number and a non-empty String.
8851 */
8852 static int
8853non_zero_arg(argvars)
8854 typval_T *argvars;
8855{
8856 return ((argvars[0].v_type == VAR_NUMBER
8857 && argvars[0].vval.v_number != 0)
8858 || (argvars[0].v_type == VAR_STRING
8859 && argvars[0].vval.v_string != NULL
8860 && *argvars[0].vval.v_string != NUL));
8861}
8862
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863/*********************************************
8864 * Implementation of the built-in functions
8865 */
8866
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008867#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008868static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8869
8870/*
8871 * Get the float value of "argvars[0]" into "f".
8872 * Returns FAIL when the argument is not a Number or Float.
8873 */
8874 static int
8875get_float_arg(argvars, f)
8876 typval_T *argvars;
8877 float_T *f;
8878{
8879 if (argvars[0].v_type == VAR_FLOAT)
8880 {
8881 *f = argvars[0].vval.v_float;
8882 return OK;
8883 }
8884 if (argvars[0].v_type == VAR_NUMBER)
8885 {
8886 *f = (float_T)argvars[0].vval.v_number;
8887 return OK;
8888 }
8889 EMSG(_("E808: Number or Float required"));
8890 return FAIL;
8891}
8892
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893/*
8894 * "abs(expr)" function
8895 */
8896 static void
8897f_abs(argvars, rettv)
8898 typval_T *argvars;
8899 typval_T *rettv;
8900{
8901 if (argvars[0].v_type == VAR_FLOAT)
8902 {
8903 rettv->v_type = VAR_FLOAT;
8904 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8905 }
8906 else
8907 {
8908 varnumber_T n;
8909 int error = FALSE;
8910
8911 n = get_tv_number_chk(&argvars[0], &error);
8912 if (error)
8913 rettv->vval.v_number = -1;
8914 else if (n > 0)
8915 rettv->vval.v_number = n;
8916 else
8917 rettv->vval.v_number = -n;
8918 }
8919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008920
8921/*
8922 * "acos()" function
8923 */
8924 static void
8925f_acos(argvars, rettv)
8926 typval_T *argvars;
8927 typval_T *rettv;
8928{
8929 float_T f;
8930
8931 rettv->v_type = VAR_FLOAT;
8932 if (get_float_arg(argvars, &f) == OK)
8933 rettv->vval.v_float = acos(f);
8934 else
8935 rettv->vval.v_float = 0.0;
8936}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008937#endif
8938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008940 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 */
8942 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008943f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 typval_T *argvars;
8945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
Bram Moolenaar33570922005-01-25 22:26:29 +00008947 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008950 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008952 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008953 && !tv_check_lock(l->lv_lock,
8954 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008955 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008957 }
8958 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959 EMSG(_(e_listreq));
8960}
8961
8962/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008963 * "and(expr, expr)" function
8964 */
8965 static void
8966f_and(argvars, rettv)
8967 typval_T *argvars;
8968 typval_T *rettv;
8969{
8970 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8971 & get_tv_number_chk(&argvars[1], NULL);
8972}
8973
8974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008975 * "append(lnum, string/list)" function
8976 */
8977 static void
8978f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 typval_T *argvars;
8980 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008981{
8982 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 list_T *l = NULL;
8985 listitem_T *li = NULL;
8986 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008987 long added = 0;
8988
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008989 /* When coming here from Insert mode, sync undo, so that this can be
8990 * undone separately from what was previously inserted. */
8991 if (u_sync_once == 2)
8992 {
8993 u_sync_once = 1; /* notify that u_sync() was called */
8994 u_sync(TRUE);
8995 }
8996
Bram Moolenaar0d660222005-01-07 21:51:51 +00008997 lnum = get_tv_lnum(argvars);
8998 if (lnum >= 0
8999 && lnum <= curbuf->b_ml.ml_line_count
9000 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009001 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004 l = argvars[1].vval.v_list;
9005 if (l == NULL)
9006 return;
9007 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009009 for (;;)
9010 {
9011 if (l == NULL)
9012 tv = &argvars[1]; /* append a string */
9013 else if (li == NULL)
9014 break; /* end of list */
9015 else
9016 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009017 line = get_tv_string_chk(tv);
9018 if (line == NULL) /* type error */
9019 {
9020 rettv->vval.v_number = 1; /* Failed */
9021 break;
9022 }
9023 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009024 ++added;
9025 if (l == NULL)
9026 break;
9027 li = li->li_next;
9028 }
9029
9030 appended_lines_mark(lnum, added);
9031 if (curwin->w_cursor.lnum > lnum)
9032 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 else
9035 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036}
9037
9038/*
9039 * "argc()" function
9040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009043 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047}
9048
9049/*
9050 * "argidx()" function
9051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
9060/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009061 * "arglistid()" function
9062 */
9063 static void
9064f_arglistid(argvars, rettv)
9065 typval_T *argvars UNUSED;
9066 typval_T *rettv;
9067{
9068 win_T *wp;
9069 tabpage_T *tp = NULL;
9070 long n;
9071
9072 rettv->vval.v_number = -1;
9073 if (argvars[0].v_type != VAR_UNKNOWN)
9074 {
9075 if (argvars[1].v_type != VAR_UNKNOWN)
9076 {
9077 n = get_tv_number(&argvars[1]);
9078 if (n >= 0)
9079 tp = find_tabpage(n);
9080 }
9081 else
9082 tp = curtab;
9083
9084 if (tp != NULL)
9085 {
9086 wp = find_win_by_nr(&argvars[0], tp);
9087 if (wp != NULL)
9088 rettv->vval.v_number = wp->w_alist->id;
9089 }
9090 }
9091 else
9092 rettv->vval.v_number = curwin->w_alist->id;
9093}
9094
9095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 * "argv(nr)" function
9097 */
9098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 typval_T *argvars;
9101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
9103 int idx;
9104
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009105 if (argvars[0].v_type != VAR_UNKNOWN)
9106 {
9107 idx = get_tv_number_chk(&argvars[0], NULL);
9108 if (idx >= 0 && idx < ARGCOUNT)
9109 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9110 else
9111 rettv->vval.v_string = NULL;
9112 rettv->v_type = VAR_STRING;
9113 }
9114 else if (rettv_list_alloc(rettv) == OK)
9115 for (idx = 0; idx < ARGCOUNT; ++idx)
9116 list_append_string(rettv->vval.v_list,
9117 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118}
9119
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009120#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009121/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009122 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009123 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009124 static void
9125f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009126 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009127 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009128{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009129 float_T f;
9130
9131 rettv->v_type = VAR_FLOAT;
9132 if (get_float_arg(argvars, &f) == OK)
9133 rettv->vval.v_float = asin(f);
9134 else
9135 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009136}
9137
9138/*
9139 * "atan()" function
9140 */
9141 static void
9142f_atan(argvars, rettv)
9143 typval_T *argvars;
9144 typval_T *rettv;
9145{
9146 float_T f;
9147
9148 rettv->v_type = VAR_FLOAT;
9149 if (get_float_arg(argvars, &f) == OK)
9150 rettv->vval.v_float = atan(f);
9151 else
9152 rettv->vval.v_float = 0.0;
9153}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009154
9155/*
9156 * "atan2()" function
9157 */
9158 static void
9159f_atan2(argvars, rettv)
9160 typval_T *argvars;
9161 typval_T *rettv;
9162{
9163 float_T fx, fy;
9164
9165 rettv->v_type = VAR_FLOAT;
9166 if (get_float_arg(argvars, &fx) == OK
9167 && get_float_arg(&argvars[1], &fy) == OK)
9168 rettv->vval.v_float = atan2(fx, fy);
9169 else
9170 rettv->vval.v_float = 0.0;
9171}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009172#endif
9173
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174/*
9175 * "browse(save, title, initdir, default)" function
9176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009179 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182#ifdef FEAT_BROWSE
9183 int save;
9184 char_u *title;
9185 char_u *initdir;
9186 char_u *defname;
9187 char_u buf[NUMBUFLEN];
9188 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009191 save = get_tv_number_chk(&argvars[0], &error);
9192 title = get_tv_string_chk(&argvars[1]);
9193 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9194 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 if (error || title == NULL || initdir == NULL || defname == NULL)
9197 rettv->vval.v_string = NULL;
9198 else
9199 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009200 do_browse(save ? BROWSE_SAVE : 0,
9201 title, defname, NULL, initdir, NULL, curbuf);
9202#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009204#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009206}
9207
9208/*
9209 * "browsedir(title, initdir)" function
9210 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009213 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009214 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009215{
9216#ifdef FEAT_BROWSE
9217 char_u *title;
9218 char_u *initdir;
9219 char_u buf[NUMBUFLEN];
9220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 title = get_tv_string_chk(&argvars[0]);
9222 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009224 if (title == NULL || initdir == NULL)
9225 rettv->vval.v_string = NULL;
9226 else
9227 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009228 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233}
9234
Bram Moolenaar33570922005-01-25 22:26:29 +00009235static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009236
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237/*
9238 * Find a buffer by number or exact name.
9239 */
9240 static buf_T *
9241find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009242 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
9244 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009246 if (avar->v_type == VAR_NUMBER)
9247 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009248 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009250 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009251 if (buf == NULL)
9252 {
9253 /* No full path name match, try a match with a URL or a "nofile"
9254 * buffer, these don't use the full path. */
9255 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9256 if (buf->b_fname != NULL
9257 && (path_with_url(buf->b_fname)
9258#ifdef FEAT_QUICKFIX
9259 || bt_nofile(buf)
9260#endif
9261 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009262 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009263 break;
9264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 }
9266 return buf;
9267}
9268
9269/*
9270 * "bufexists(expr)" function
9271 */
9272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009273f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009274 typval_T *argvars;
9275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278}
9279
9280/*
9281 * "buflisted(expr)" function
9282 */
9283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 typval_T *argvars;
9286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288 buf_T *buf;
9289
9290 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292}
9293
9294/*
9295 * "bufloaded(expr)" function
9296 */
9297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009298f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009299 typval_T *argvars;
9300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 buf_T *buf;
9303
9304 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306}
9307
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009308static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310/*
9311 * Get buffer by number or pattern.
9312 */
9313 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009314get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009316 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 int save_magic;
9320 char_u *save_cpo;
9321 buf_T *buf;
9322
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 if (tv->v_type == VAR_NUMBER)
9324 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009325 if (tv->v_type != VAR_STRING)
9326 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 if (name == NULL || *name == NUL)
9328 return curbuf;
9329 if (name[0] == '$' && name[1] == NUL)
9330 return lastbuf;
9331
9332 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9333 save_magic = p_magic;
9334 p_magic = TRUE;
9335 save_cpo = p_cpo;
9336 p_cpo = (char_u *)"";
9337
9338 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009339 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
9341 p_magic = save_magic;
9342 p_cpo = save_cpo;
9343
9344 /* If not found, try expanding the name, like done for bufexists(). */
9345 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348 return buf;
9349}
9350
9351/*
9352 * "bufname(expr)" function
9353 */
9354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009356 typval_T *argvars;
9357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358{
9359 buf_T *buf;
9360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009361 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009363 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 --emsg_off;
9370}
9371
9372/*
9373 * "bufnr(expr)" function
9374 */
9375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009377 typval_T *argvars;
9378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009381 int error = FALSE;
9382 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009386 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009387 --emsg_off;
9388
9389 /* If the buffer isn't found and the second argument is not zero create a
9390 * new buffer. */
9391 if (buf == NULL
9392 && argvars[1].v_type != VAR_UNKNOWN
9393 && get_tv_number_chk(&argvars[1], &error) != 0
9394 && !error
9395 && (name = get_tv_string_chk(&argvars[0])) != NULL
9396 && !error)
9397 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9398
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403}
9404
9405/*
9406 * "bufwinnr(nr)" function
9407 */
9408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009410 typval_T *argvars;
9411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
9413#ifdef FEAT_WINDOWS
9414 win_T *wp;
9415 int winnr = 0;
9416#endif
9417 buf_T *buf;
9418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009421 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422#ifdef FEAT_WINDOWS
9423 for (wp = firstwin; wp; wp = wp->w_next)
9424 {
9425 ++winnr;
9426 if (wp->w_buffer == buf)
9427 break;
9428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432#endif
9433 --emsg_off;
9434}
9435
9436/*
9437 * "byte2line(byte)" function
9438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009441 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443{
9444#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446#else
9447 long boff = 0;
9448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 (linenr_T)0, &boff);
9455#endif
9456}
9457
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009458 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009459byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009462 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009463{
9464#ifdef FEAT_MBYTE
9465 char_u *t;
9466#endif
9467 char_u *str;
9468 long idx;
9469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 str = get_tv_string_chk(&argvars[0]);
9471 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009473 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009474 return;
9475
9476#ifdef FEAT_MBYTE
9477 t = str;
9478 for ( ; idx > 0; idx--)
9479 {
9480 if (*t == NUL) /* EOL reached */
9481 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009482 if (enc_utf8 && comp)
9483 t += utf_ptr2len(t);
9484 else
9485 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009486 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009487 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009488#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009489 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009491#endif
9492}
9493
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009494/*
9495 * "byteidx()" function
9496 */
9497 static void
9498f_byteidx(argvars, rettv)
9499 typval_T *argvars;
9500 typval_T *rettv;
9501{
9502 byteidx(argvars, rettv, FALSE);
9503}
9504
9505/*
9506 * "byteidxcomp()" function
9507 */
9508 static void
9509f_byteidxcomp(argvars, rettv)
9510 typval_T *argvars;
9511 typval_T *rettv;
9512{
9513 byteidx(argvars, rettv, TRUE);
9514}
9515
Bram Moolenaardb913952012-06-29 12:54:53 +02009516 int
9517func_call(name, args, selfdict, rettv)
9518 char_u *name;
9519 typval_T *args;
9520 dict_T *selfdict;
9521 typval_T *rettv;
9522{
9523 listitem_T *item;
9524 typval_T argv[MAX_FUNC_ARGS + 1];
9525 int argc = 0;
9526 int dummy;
9527 int r = 0;
9528
9529 for (item = args->vval.v_list->lv_first; item != NULL;
9530 item = item->li_next)
9531 {
9532 if (argc == MAX_FUNC_ARGS)
9533 {
9534 EMSG(_("E699: Too many arguments"));
9535 break;
9536 }
9537 /* Make a copy of each argument. This is needed to be able to set
9538 * v_lock to VAR_FIXED in the copy without changing the original list.
9539 */
9540 copy_tv(&item->li_tv, &argv[argc++]);
9541 }
9542
9543 if (item == NULL)
9544 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9545 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9546 &dummy, TRUE, selfdict);
9547
9548 /* Free the arguments. */
9549 while (argc > 0)
9550 clear_tv(&argv[--argc]);
9551
9552 return r;
9553}
9554
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009555/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009556 * "call(func, arglist)" function
9557 */
9558 static void
9559f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009560 typval_T *argvars;
9561 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009562{
9563 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009564 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009565
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009566 if (argvars[1].v_type != VAR_LIST)
9567 {
9568 EMSG(_(e_listreq));
9569 return;
9570 }
9571 if (argvars[1].vval.v_list == NULL)
9572 return;
9573
9574 if (argvars[0].v_type == VAR_FUNC)
9575 func = argvars[0].vval.v_string;
9576 else
9577 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009578 if (*func == NUL)
9579 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009580
Bram Moolenaare9a41262005-01-15 22:18:47 +00009581 if (argvars[2].v_type != VAR_UNKNOWN)
9582 {
9583 if (argvars[2].v_type != VAR_DICT)
9584 {
9585 EMSG(_(e_dictreq));
9586 return;
9587 }
9588 selfdict = argvars[2].vval.v_dict;
9589 }
9590
Bram Moolenaardb913952012-06-29 12:54:53 +02009591 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009592}
9593
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009594#ifdef FEAT_FLOAT
9595/*
9596 * "ceil({float})" function
9597 */
9598 static void
9599f_ceil(argvars, rettv)
9600 typval_T *argvars;
9601 typval_T *rettv;
9602{
9603 float_T f;
9604
9605 rettv->v_type = VAR_FLOAT;
9606 if (get_float_arg(argvars, &f) == OK)
9607 rettv->vval.v_float = ceil(f);
9608 else
9609 rettv->vval.v_float = 0.0;
9610}
9611#endif
9612
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009613/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009614 * "changenr()" function
9615 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009616 static void
9617f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009618 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009619 typval_T *rettv;
9620{
9621 rettv->vval.v_number = curbuf->b_u_seq_cur;
9622}
9623
9624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 * "char2nr(string)" function
9626 */
9627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 typval_T *argvars;
9630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
9632#ifdef FEAT_MBYTE
9633 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009634 {
9635 int utf8 = 0;
9636
9637 if (argvars[1].v_type != VAR_UNKNOWN)
9638 utf8 = get_tv_number_chk(&argvars[1], NULL);
9639
9640 if (utf8)
9641 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9642 else
9643 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 else
9646#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648}
9649
9650/*
9651 * "cindent(lnum)" function
9652 */
9653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009655 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658#ifdef FEAT_CINDENT
9659 pos_T pos;
9660 linenr_T lnum;
9661
9662 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9665 {
9666 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009667 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 curwin->w_cursor = pos;
9669 }
9670 else
9671#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673}
9674
9675/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009676 * "clearmatches()" function
9677 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009678 static void
9679f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009680 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009681 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009682{
9683#ifdef FEAT_SEARCH_EXTRA
9684 clear_matches(curwin);
9685#endif
9686}
9687
9688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 * "col(string)" function
9690 */
9691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009693 typval_T *argvars;
9694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696 colnr_T col = 0;
9697 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009698 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009700 fp = var2fpos(&argvars[0], FALSE, &fnum);
9701 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 {
9703 if (fp->col == MAXCOL)
9704 {
9705 /* '> can be MAXCOL, get the length of the line then */
9706 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009707 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 else
9709 col = MAXCOL;
9710 }
9711 else
9712 {
9713 col = fp->col + 1;
9714#ifdef FEAT_VIRTUALEDIT
9715 /* col(".") when the cursor is on the NUL at the end of the line
9716 * because of "coladd" can be seen as an extra column. */
9717 if (virtual_active() && fp == &curwin->w_cursor)
9718 {
9719 char_u *p = ml_get_cursor();
9720
9721 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9722 curwin->w_virtcol - curwin->w_cursor.coladd))
9723 {
9724# ifdef FEAT_MBYTE
9725 int l;
9726
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009727 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 col += l;
9729# else
9730 if (*p != NUL && p[1] == NUL)
9731 ++col;
9732# endif
9733 }
9734 }
9735#endif
9736 }
9737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739}
9740
Bram Moolenaar572cb562005-08-05 21:35:02 +00009741#if defined(FEAT_INS_EXPAND)
9742/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009743 * "complete()" function
9744 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009745 static void
9746f_complete(argvars, rettv)
9747 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009748 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009749{
9750 int startcol;
9751
9752 if ((State & INSERT) == 0)
9753 {
9754 EMSG(_("E785: complete() can only be used in Insert mode"));
9755 return;
9756 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009757
9758 /* Check for undo allowed here, because if something was already inserted
9759 * the line was already saved for undo and this check isn't done. */
9760 if (!undo_allowed())
9761 return;
9762
Bram Moolenaarade00832006-03-10 21:46:58 +00009763 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9764 {
9765 EMSG(_(e_invarg));
9766 return;
9767 }
9768
9769 startcol = get_tv_number_chk(&argvars[0], NULL);
9770 if (startcol <= 0)
9771 return;
9772
9773 set_completion(startcol - 1, argvars[1].vval.v_list);
9774}
9775
9776/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009777 * "complete_add()" function
9778 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009779 static void
9780f_complete_add(argvars, rettv)
9781 typval_T *argvars;
9782 typval_T *rettv;
9783{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009784 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009785}
9786
9787/*
9788 * "complete_check()" function
9789 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009790 static void
9791f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009792 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009793 typval_T *rettv;
9794{
9795 int saved = RedrawingDisabled;
9796
9797 RedrawingDisabled = 0;
9798 ins_compl_check_keys(0);
9799 rettv->vval.v_number = compl_interrupted;
9800 RedrawingDisabled = saved;
9801}
9802#endif
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804/*
9805 * "confirm(message, buttons[, default [, type]])" function
9806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009809 typval_T *argvars UNUSED;
9810 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9813 char_u *message;
9814 char_u *buttons = NULL;
9815 char_u buf[NUMBUFLEN];
9816 char_u buf2[NUMBUFLEN];
9817 int def = 1;
9818 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009819 char_u *typestr;
9820 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 message = get_tv_string_chk(&argvars[0]);
9823 if (message == NULL)
9824 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009825 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009827 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9828 if (buttons == NULL)
9829 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009833 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009835 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9836 if (typestr == NULL)
9837 error = TRUE;
9838 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 switch (TOUPPER_ASC(*typestr))
9841 {
9842 case 'E': type = VIM_ERROR; break;
9843 case 'Q': type = VIM_QUESTION; break;
9844 case 'I': type = VIM_INFO; break;
9845 case 'W': type = VIM_WARNING; break;
9846 case 'G': type = VIM_GENERIC; break;
9847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 }
9849 }
9850 }
9851 }
9852
9853 if (buttons == NULL || *buttons == NUL)
9854 buttons = (char_u *)_("&Ok");
9855
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009856 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009858 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859#endif
9860}
9861
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009862/*
9863 * "copy()" function
9864 */
9865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009867 typval_T *argvars;
9868 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009869{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009870 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009871}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009873#ifdef FEAT_FLOAT
9874/*
9875 * "cos()" function
9876 */
9877 static void
9878f_cos(argvars, rettv)
9879 typval_T *argvars;
9880 typval_T *rettv;
9881{
9882 float_T f;
9883
9884 rettv->v_type = VAR_FLOAT;
9885 if (get_float_arg(argvars, &f) == OK)
9886 rettv->vval.v_float = cos(f);
9887 else
9888 rettv->vval.v_float = 0.0;
9889}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009890
9891/*
9892 * "cosh()" function
9893 */
9894 static void
9895f_cosh(argvars, rettv)
9896 typval_T *argvars;
9897 typval_T *rettv;
9898{
9899 float_T f;
9900
9901 rettv->v_type = VAR_FLOAT;
9902 if (get_float_arg(argvars, &f) == OK)
9903 rettv->vval.v_float = cosh(f);
9904 else
9905 rettv->vval.v_float = 0.0;
9906}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009907#endif
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009910 * "count()" function
9911 */
9912 static void
9913f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 typval_T *argvars;
9915 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009916{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009917 long n = 0;
9918 int ic = FALSE;
9919
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009921 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 listitem_T *li;
9923 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009925
Bram Moolenaare9a41262005-01-15 22:18:47 +00009926 if ((l = argvars[0].vval.v_list) != NULL)
9927 {
9928 li = l->lv_first;
9929 if (argvars[2].v_type != VAR_UNKNOWN)
9930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 int error = FALSE;
9932
9933 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009934 if (argvars[3].v_type != VAR_UNKNOWN)
9935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 idx = get_tv_number_chk(&argvars[3], &error);
9937 if (!error)
9938 {
9939 li = list_find(l, idx);
9940 if (li == NULL)
9941 EMSGN(_(e_listidx), idx);
9942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009943 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 if (error)
9945 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009946 }
9947
9948 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009949 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 ++n;
9951 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009953 else if (argvars[0].v_type == VAR_DICT)
9954 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009955 int todo;
9956 dict_T *d;
9957 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009958
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009959 if ((d = argvars[0].vval.v_dict) != NULL)
9960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009961 int error = FALSE;
9962
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 if (argvars[2].v_type != VAR_UNKNOWN)
9964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009966 if (argvars[3].v_type != VAR_UNKNOWN)
9967 EMSG(_(e_invarg));
9968 }
9969
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009970 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009971 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009972 {
9973 if (!HASHITEM_EMPTY(hi))
9974 {
9975 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009976 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009977 ++n;
9978 }
9979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009980 }
9981 }
9982 else
9983 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009984 rettv->vval.v_number = n;
9985}
9986
9987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9989 *
9990 * Checks the existence of a cscope connection.
9991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009994 typval_T *argvars UNUSED;
9995 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996{
9997#ifdef FEAT_CSCOPE
9998 int num = 0;
9999 char_u *dbpath = NULL;
10000 char_u *prepend = NULL;
10001 char_u buf[NUMBUFLEN];
10002
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010003 if (argvars[0].v_type != VAR_UNKNOWN
10004 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 num = (int)get_tv_number(&argvars[0]);
10007 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010008 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013#endif
10014}
10015
10016/*
10017 * "cursor(lnum, col)" function
10018 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010019 * Moves the cursor to the specified line and column.
10020 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010024 typval_T *argvars;
10025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010028#ifdef FEAT_VIRTUALEDIT
10029 long coladd = 0;
10030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010032 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010033 if (argvars[1].v_type == VAR_UNKNOWN)
10034 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010035 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010036 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010037
Bram Moolenaar493c1782014-05-28 14:34:46 +020010038 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010039 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010040 line = pos.lnum;
10041 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010042#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010043 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010044#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010045 if (curswant >= 0)
10046 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010047 }
10048 else
10049 {
10050 line = get_tv_lnum(argvars);
10051 col = get_tv_number_chk(&argvars[1], NULL);
10052#ifdef FEAT_VIRTUALEDIT
10053 if (argvars[2].v_type != VAR_UNKNOWN)
10054 coladd = get_tv_number_chk(&argvars[2], NULL);
10055#endif
10056 }
10057 if (line < 0 || col < 0
10058#ifdef FEAT_VIRTUALEDIT
10059 || coladd < 0
10060#endif
10061 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010062 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (line > 0)
10064 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 if (col > 0)
10066 curwin->w_cursor.col = col - 1;
10067#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010068 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069#endif
10070
10071 /* Make sure the cursor is in a valid position. */
10072 check_cursor();
10073#ifdef FEAT_MBYTE
10074 /* Correct cursor for multi-byte character. */
10075 if (has_mbyte)
10076 mb_adjust_cursor();
10077#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010078
10079 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010080 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081}
10082
10083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010084 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 */
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010091 int noref = 0;
10092
10093 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010095 if (noref < 0 || noref > 1)
10096 EMSG(_(e_invarg));
10097 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010098 {
10099 current_copyID += COPYID_INC;
10100 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102}
10103
10104/*
10105 * "delete()" function
10106 */
10107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010109 typval_T *argvars;
10110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116}
10117
10118/*
10119 * "did_filetype()" function
10120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010123 typval_T *argvars UNUSED;
10124 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125{
10126#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128#endif
10129}
10130
10131/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010132 * "diff_filler()" function
10133 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010136 typval_T *argvars UNUSED;
10137 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010138{
10139#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010141#endif
10142}
10143
10144/*
10145 * "diff_hlID()" function
10146 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010149 typval_T *argvars UNUSED;
10150 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010151{
10152#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010154 static linenr_T prev_lnum = 0;
10155 static int changedtick = 0;
10156 static int fnum = 0;
10157 static int change_start = 0;
10158 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010159 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010160 int filler_lines;
10161 int col;
10162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010163 if (lnum < 0) /* ignore type error in {lnum} arg */
10164 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010165 if (lnum != prev_lnum
10166 || changedtick != curbuf->b_changedtick
10167 || fnum != curbuf->b_fnum)
10168 {
10169 /* New line, buffer, change: need to get the values. */
10170 filler_lines = diff_check(curwin, lnum);
10171 if (filler_lines < 0)
10172 {
10173 if (filler_lines == -1)
10174 {
10175 change_start = MAXCOL;
10176 change_end = -1;
10177 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10178 hlID = HLF_ADD; /* added line */
10179 else
10180 hlID = HLF_CHD; /* changed line */
10181 }
10182 else
10183 hlID = HLF_ADD; /* added line */
10184 }
10185 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010186 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010187 prev_lnum = lnum;
10188 changedtick = curbuf->b_changedtick;
10189 fnum = curbuf->b_fnum;
10190 }
10191
10192 if (hlID == HLF_CHD || hlID == HLF_TXD)
10193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010195 if (col >= change_start && col <= change_end)
10196 hlID = HLF_TXD; /* changed text */
10197 else
10198 hlID = HLF_CHD; /* changed line */
10199 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010200 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010201#endif
10202}
10203
10204/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010205 * "empty({expr})" function
10206 */
10207 static void
10208f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010209 typval_T *argvars;
10210 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010211{
10212 int n;
10213
10214 switch (argvars[0].v_type)
10215 {
10216 case VAR_STRING:
10217 case VAR_FUNC:
10218 n = argvars[0].vval.v_string == NULL
10219 || *argvars[0].vval.v_string == NUL;
10220 break;
10221 case VAR_NUMBER:
10222 n = argvars[0].vval.v_number == 0;
10223 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010224#ifdef FEAT_FLOAT
10225 case VAR_FLOAT:
10226 n = argvars[0].vval.v_float == 0.0;
10227 break;
10228#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010229 case VAR_LIST:
10230 n = argvars[0].vval.v_list == NULL
10231 || argvars[0].vval.v_list->lv_first == NULL;
10232 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 case VAR_DICT:
10234 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010235 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010237 default:
10238 EMSG2(_(e_intern2), "f_empty()");
10239 n = 0;
10240 }
10241
10242 rettv->vval.v_number = n;
10243}
10244
10245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 * "escape({string}, {chars})" function
10247 */
10248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010250 typval_T *argvars;
10251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252{
10253 char_u buf[NUMBUFLEN];
10254
Bram Moolenaar758711c2005-02-02 23:11:38 +000010255 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10256 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258}
10259
10260/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010261 * "eval()" function
10262 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010263 static void
10264f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010265 typval_T *argvars;
10266 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010267{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010268 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010270 s = get_tv_string_chk(&argvars[0]);
10271 if (s != NULL)
10272 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010273
Bram Moolenaar615b9972015-01-14 17:15:05 +010010274 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010275 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10276 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010277 if (p != NULL && !aborting())
10278 EMSG2(_(e_invexpr2), p);
10279 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010280 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010281 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010282 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010283 else if (*s != NUL)
10284 EMSG(_(e_trailing));
10285}
10286
10287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * "eventhandler()" function
10289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010291f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296}
10297
10298/*
10299 * "executable()" function
10300 */
10301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010303 typval_T *argvars;
10304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010306 char_u *name = get_tv_string(&argvars[0]);
10307
10308 /* Check in $PATH and also check directly if there is a directory name. */
10309 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10310 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010311}
10312
10313/*
10314 * "exepath()" function
10315 */
10316 static void
10317f_exepath(argvars, rettv)
10318 typval_T *argvars;
10319 typval_T *rettv;
10320{
10321 char_u *p = NULL;
10322
Bram Moolenaarb5971142015-03-21 17:32:19 +010010323 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010324 rettv->v_type = VAR_STRING;
10325 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326}
10327
10328/*
10329 * "exists()" function
10330 */
10331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010333 typval_T *argvars;
10334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335{
10336 char_u *p;
10337 char_u *name;
10338 int n = FALSE;
10339 int len = 0;
10340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 if (*p == '$') /* environment variable */
10343 {
10344 /* first try "normal" environment variables (fast) */
10345 if (mch_getenv(p + 1) != NULL)
10346 n = TRUE;
10347 else
10348 {
10349 /* try expanding things like $VIM and ${HOME} */
10350 p = expand_env_save(p);
10351 if (p != NULL && *p != '$')
10352 n = TRUE;
10353 vim_free(p);
10354 }
10355 }
10356 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010359 if (*skipwhite(p) != NUL)
10360 n = FALSE; /* trailing garbage */
10361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 else if (*p == '*') /* internal or user defined function */
10363 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010364 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 }
10366 else if (*p == ':')
10367 {
10368 n = cmd_exists(p + 1);
10369 }
10370 else if (*p == '#')
10371 {
10372#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010373 if (p[1] == '#')
10374 n = autocmd_supported(p + 2);
10375 else
10376 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377#endif
10378 }
10379 else /* internal variable */
10380 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010381 char_u *tofree;
10382 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010384 /* get_name_len() takes care of expanding curly braces */
10385 name = p;
10386 len = get_name_len(&p, &tofree, TRUE, FALSE);
10387 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010389 if (tofree != NULL)
10390 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010391 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010392 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010394 /* handle d.key, l[idx], f(expr) */
10395 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10396 if (n)
10397 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 }
10399 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010400 if (*p != NUL)
10401 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010403 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 }
10405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407}
10408
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010409#ifdef FEAT_FLOAT
10410/*
10411 * "exp()" function
10412 */
10413 static void
10414f_exp(argvars, rettv)
10415 typval_T *argvars;
10416 typval_T *rettv;
10417{
10418 float_T f;
10419
10420 rettv->v_type = VAR_FLOAT;
10421 if (get_float_arg(argvars, &f) == OK)
10422 rettv->vval.v_float = exp(f);
10423 else
10424 rettv->vval.v_float = 0.0;
10425}
10426#endif
10427
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428/*
10429 * "expand()" function
10430 */
10431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010433 typval_T *argvars;
10434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435{
10436 char_u *s;
10437 int len;
10438 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010439 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010442 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010445 if (argvars[1].v_type != VAR_UNKNOWN
10446 && argvars[2].v_type != VAR_UNKNOWN
10447 && get_tv_number_chk(&argvars[2], &error)
10448 && !error)
10449 {
10450 rettv->v_type = VAR_LIST;
10451 rettv->vval.v_list = NULL;
10452 }
10453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 if (*s == '%' || *s == '#' || *s == '<')
10456 {
10457 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010458 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010460 if (rettv->v_type == VAR_LIST)
10461 {
10462 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10463 list_append_string(rettv->vval.v_list, result, -1);
10464 else
10465 vim_free(result);
10466 }
10467 else
10468 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 }
10470 else
10471 {
10472 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010473 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 if (argvars[1].v_type != VAR_UNKNOWN
10475 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010476 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010477 if (!error)
10478 {
10479 ExpandInit(&xpc);
10480 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010481 if (p_wic)
10482 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010483 if (rettv->v_type == VAR_STRING)
10484 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10485 options, WILD_ALL);
10486 else if (rettv_list_alloc(rettv) != FAIL)
10487 {
10488 int i;
10489
10490 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10491 for (i = 0; i < xpc.xp_numfiles; i++)
10492 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10493 ExpandCleanup(&xpc);
10494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 }
10496 else
10497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 }
10499}
10500
10501/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010502 * Go over all entries in "d2" and add them to "d1".
10503 * When "action" is "error" then a duplicate key is an error.
10504 * When "action" is "force" then a duplicate key is overwritten.
10505 * Otherwise duplicate keys are ignored ("action" is "keep").
10506 */
10507 void
10508dict_extend(d1, d2, action)
10509 dict_T *d1;
10510 dict_T *d2;
10511 char_u *action;
10512{
10513 dictitem_T *di1;
10514 hashitem_T *hi2;
10515 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010516 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010517
10518 todo = (int)d2->dv_hashtab.ht_used;
10519 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10520 {
10521 if (!HASHITEM_EMPTY(hi2))
10522 {
10523 --todo;
10524 di1 = dict_find(d1, hi2->hi_key, -1);
10525 if (d1->dv_scope != 0)
10526 {
10527 /* Disallow replacing a builtin function in l: and g:.
10528 * Check the key to be valid when adding to any
10529 * scope. */
10530 if (d1->dv_scope == VAR_DEF_SCOPE
10531 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10532 && var_check_func_name(hi2->hi_key,
10533 di1 == NULL))
10534 break;
10535 if (!valid_varname(hi2->hi_key))
10536 break;
10537 }
10538 if (di1 == NULL)
10539 {
10540 di1 = dictitem_copy(HI2DI(hi2));
10541 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10542 dictitem_free(di1);
10543 }
10544 else if (*action == 'e')
10545 {
10546 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10547 break;
10548 }
10549 else if (*action == 'f' && HI2DI(hi2) != di1)
10550 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010551 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10552 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010553 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010554 clear_tv(&di1->di_tv);
10555 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10556 }
10557 }
10558 }
10559}
10560
10561/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010562 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010564 */
10565 static void
10566f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010567 typval_T *argvars;
10568 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010569{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010570 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010571
Bram Moolenaare9a41262005-01-15 22:18:47 +000010572 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010573 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 list_T *l1, *l2;
10575 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010578
Bram Moolenaare9a41262005-01-15 22:18:47 +000010579 l1 = argvars[0].vval.v_list;
10580 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010581 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010582 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 {
10584 if (argvars[2].v_type != VAR_UNKNOWN)
10585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 before = get_tv_number_chk(&argvars[2], &error);
10587 if (error)
10588 return; /* type error; errmsg already given */
10589
Bram Moolenaar758711c2005-02-02 23:11:38 +000010590 if (before == l1->lv_len)
10591 item = NULL;
10592 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010594 item = list_find(l1, before);
10595 if (item == NULL)
10596 {
10597 EMSGN(_(e_listidx), before);
10598 return;
10599 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 }
10601 }
10602 else
10603 item = NULL;
10604 list_extend(l1, l2, item);
10605
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 copy_tv(&argvars[0], rettv);
10607 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010608 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010609 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10610 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010611 dict_T *d1, *d2;
10612 char_u *action;
10613 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010614
10615 d1 = argvars[0].vval.v_dict;
10616 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010617 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010618 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010619 {
10620 /* Check the third argument. */
10621 if (argvars[2].v_type != VAR_UNKNOWN)
10622 {
10623 static char *(av[]) = {"keep", "force", "error"};
10624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 action = get_tv_string_chk(&argvars[2]);
10626 if (action == NULL)
10627 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628 for (i = 0; i < 3; ++i)
10629 if (STRCMP(action, av[i]) == 0)
10630 break;
10631 if (i == 3)
10632 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010633 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 return;
10635 }
10636 }
10637 else
10638 action = (char_u *)"force";
10639
Bram Moolenaara9922d62013-05-30 13:01:18 +020010640 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010641
Bram Moolenaare9a41262005-01-15 22:18:47 +000010642 copy_tv(&argvars[0], rettv);
10643 }
10644 }
10645 else
10646 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010647}
10648
10649/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010650 * "feedkeys()" function
10651 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010652 static void
10653f_feedkeys(argvars, rettv)
10654 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010655 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010656{
10657 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010658 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010659 char_u *keys, *flags;
10660 char_u nbuf[NUMBUFLEN];
10661 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010662 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010663
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010664 /* This is not allowed in the sandbox. If the commands would still be
10665 * executed in the sandbox it would be OK, but it probably happens later,
10666 * when "sandbox" is no longer set. */
10667 if (check_secure())
10668 return;
10669
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010670 keys = get_tv_string(&argvars[0]);
10671 if (*keys != NUL)
10672 {
10673 if (argvars[1].v_type != VAR_UNKNOWN)
10674 {
10675 flags = get_tv_string_buf(&argvars[1], nbuf);
10676 for ( ; *flags != NUL; ++flags)
10677 {
10678 switch (*flags)
10679 {
10680 case 'n': remap = FALSE; break;
10681 case 'm': remap = TRUE; break;
10682 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010683 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010684 }
10685 }
10686 }
10687
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010688 /* Need to escape K_SPECIAL and CSI before putting the string in the
10689 * typeahead buffer. */
10690 keys_esc = vim_strsave_escape_csi(keys);
10691 if (keys_esc != NULL)
10692 {
10693 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010694 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010695 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010696 if (vgetc_busy)
10697 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010698 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010699 }
10700}
10701
10702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 * "filereadable()" function
10704 */
10705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010707 typval_T *argvars;
10708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010710 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 char_u *p;
10712 int n;
10713
Bram Moolenaarc236c162008-07-13 17:41:49 +000010714#ifndef O_NONBLOCK
10715# define O_NONBLOCK 0
10716#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010718 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10719 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 {
10721 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010722 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 }
10724 else
10725 n = FALSE;
10726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728}
10729
10730/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010731 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 * rights to write into.
10733 */
10734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010736 typval_T *argvars;
10737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010739 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740}
10741
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010742static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010743
10744 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010745findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010746 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010747 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010748 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010749{
10750#ifdef FEAT_SEARCHPATH
10751 char_u *fname;
10752 char_u *fresult = NULL;
10753 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10754 char_u *p;
10755 char_u pathbuf[NUMBUFLEN];
10756 int count = 1;
10757 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010758 int error = FALSE;
10759#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010760
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010761 rettv->vval.v_string = NULL;
10762 rettv->v_type = VAR_STRING;
10763
10764#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010766
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010767 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10770 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010771 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010772 else
10773 {
10774 if (*p != NUL)
10775 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010778 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010780 }
10781
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010782 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10783 error = TRUE;
10784
10785 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010787 do
10788 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010789 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010790 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 fresult = find_file_in_path_option(first ? fname : NULL,
10792 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010793 0, first, path,
10794 find_what,
10795 curbuf->b_ffname,
10796 find_what == FINDFILE_DIR
10797 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010799
10800 if (fresult != NULL && rettv->v_type == VAR_LIST)
10801 list_append_string(rettv->vval.v_list, fresult, -1);
10802
10803 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010804 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010805
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010806 if (rettv->v_type == VAR_STRING)
10807 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010808#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010809}
10810
Bram Moolenaar33570922005-01-25 22:26:29 +000010811static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10812static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010813
10814/*
10815 * Implementation of map() and filter().
10816 */
10817 static void
10818filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010819 typval_T *argvars;
10820 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010821 int map;
10822{
10823 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010824 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010825 listitem_T *li, *nli;
10826 list_T *l = NULL;
10827 dictitem_T *di;
10828 hashtab_T *ht;
10829 hashitem_T *hi;
10830 dict_T *d = NULL;
10831 typval_T save_val;
10832 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010833 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010834 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010835 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010836 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010837 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010838 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010839 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010840
Bram Moolenaare9a41262005-01-15 22:18:47 +000010841 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010843 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010844 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 return;
10846 }
10847 else if (argvars[0].v_type == VAR_DICT)
10848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010849 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010850 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 return;
10852 }
10853 else
10854 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010855 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 return;
10857 }
10858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010859 expr = get_tv_string_buf_chk(&argvars[1], buf);
10860 /* On type errors, the preceding call has already displayed an error
10861 * message. Avoid a misleading error message for an empty string that
10862 * was not passed as argument. */
10863 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 prepare_vimvar(VV_VAL, &save_val);
10866 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010867
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010868 /* We reset "did_emsg" to be able to detect whether an error
10869 * occurred during evaluation of the expression. */
10870 save_did_emsg = did_emsg;
10871 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010872
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010873 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 vimvars[VV_KEY].vv_type = VAR_STRING;
10877
10878 ht = &d->dv_hashtab;
10879 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010880 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 if (!HASHITEM_EMPTY(hi))
10884 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010885 int r;
10886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887 --todo;
10888 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010889 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010890 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10891 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 break;
10893 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010894 r = filter_map_one(&di->di_tv, expr, map, &rem);
10895 clear_tv(&vimvars[VV_KEY].vv_tv);
10896 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 break;
10898 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010899 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010900 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10901 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010902 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010904 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905 }
10906 }
10907 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908 }
10909 else
10910 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010911 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010913 for (li = l->lv_first; li != NULL; li = nli)
10914 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010915 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010916 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010918 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010919 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010920 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010921 break;
10922 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010924 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010925 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010926 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010927
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010928 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010930
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010931 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010933
10934 copy_tv(&argvars[0], rettv);
10935}
10936
10937 static int
10938filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010939 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 char_u *expr;
10941 int map;
10942 int *remp;
10943{
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010945 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010946 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 s = expr;
10950 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010951 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 if (*s != NUL) /* check for trailing chars after expr */
10953 {
10954 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010955 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010956 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010957 }
10958 if (map)
10959 {
10960 /* map(): replace the list item value */
10961 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010962 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 *tv = rettv;
10964 }
10965 else
10966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 int error = FALSE;
10968
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010971 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 /* On type error, nothing has been removed; return FAIL to stop the
10973 * loop. The error message was given by get_tv_number_chk(). */
10974 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010975 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010977 retval = OK;
10978theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010979 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010980 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010981}
10982
10983/*
10984 * "filter()" function
10985 */
10986 static void
10987f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *argvars;
10989 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010990{
10991 filter_map(argvars, rettv, FALSE);
10992}
10993
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010994/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010995 * "finddir({fname}[, {path}[, {count}]])" function
10996 */
10997 static void
10998f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010999 typval_T *argvars;
11000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011002 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011003}
11004
11005/*
11006 * "findfile({fname}[, {path}[, {count}]])" function
11007 */
11008 static void
11009f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 typval_T *argvars;
11011 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011012{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011013 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011014}
11015
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011016#ifdef FEAT_FLOAT
11017/*
11018 * "float2nr({float})" function
11019 */
11020 static void
11021f_float2nr(argvars, rettv)
11022 typval_T *argvars;
11023 typval_T *rettv;
11024{
11025 float_T f;
11026
11027 if (get_float_arg(argvars, &f) == OK)
11028 {
11029 if (f < -0x7fffffff)
11030 rettv->vval.v_number = -0x7fffffff;
11031 else if (f > 0x7fffffff)
11032 rettv->vval.v_number = 0x7fffffff;
11033 else
11034 rettv->vval.v_number = (varnumber_T)f;
11035 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011036}
11037
11038/*
11039 * "floor({float})" function
11040 */
11041 static void
11042f_floor(argvars, rettv)
11043 typval_T *argvars;
11044 typval_T *rettv;
11045{
11046 float_T f;
11047
11048 rettv->v_type = VAR_FLOAT;
11049 if (get_float_arg(argvars, &f) == OK)
11050 rettv->vval.v_float = floor(f);
11051 else
11052 rettv->vval.v_float = 0.0;
11053}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011054
11055/*
11056 * "fmod()" function
11057 */
11058 static void
11059f_fmod(argvars, rettv)
11060 typval_T *argvars;
11061 typval_T *rettv;
11062{
11063 float_T fx, fy;
11064
11065 rettv->v_type = VAR_FLOAT;
11066 if (get_float_arg(argvars, &fx) == OK
11067 && get_float_arg(&argvars[1], &fy) == OK)
11068 rettv->vval.v_float = fmod(fx, fy);
11069 else
11070 rettv->vval.v_float = 0.0;
11071}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011072#endif
11073
Bram Moolenaar0d660222005-01-07 21:51:51 +000011074/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011075 * "fnameescape({string})" function
11076 */
11077 static void
11078f_fnameescape(argvars, rettv)
11079 typval_T *argvars;
11080 typval_T *rettv;
11081{
11082 rettv->vval.v_string = vim_strsave_fnameescape(
11083 get_tv_string(&argvars[0]), FALSE);
11084 rettv->v_type = VAR_STRING;
11085}
11086
11087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 * "fnamemodify({fname}, {mods})" function
11089 */
11090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011092 typval_T *argvars;
11093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094{
11095 char_u *fname;
11096 char_u *mods;
11097 int usedlen = 0;
11098 int len;
11099 char_u *fbuf = NULL;
11100 char_u buf[NUMBUFLEN];
11101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 fname = get_tv_string_chk(&argvars[0]);
11103 mods = get_tv_string_buf_chk(&argvars[1], buf);
11104 if (fname == NULL || mods == NULL)
11105 fname = NULL;
11106 else
11107 {
11108 len = (int)STRLEN(fname);
11109 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 vim_free(fbuf);
11118}
11119
Bram Moolenaar33570922005-01-25 22:26:29 +000011120static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
11122/*
11123 * "foldclosed()" function
11124 */
11125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011127 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011129 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
11131#ifdef FEAT_FOLDING
11132 linenr_T lnum;
11133 linenr_T first, last;
11134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11137 {
11138 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11139 {
11140 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 return;
11145 }
11146 }
11147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149}
11150
11151/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011152 * "foldclosed()" function
11153 */
11154 static void
11155f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158{
11159 foldclosed_both(argvars, rettv, FALSE);
11160}
11161
11162/*
11163 * "foldclosedend()" function
11164 */
11165 static void
11166f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 typval_T *argvars;
11168 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011169{
11170 foldclosed_both(argvars, rettv, TRUE);
11171}
11172
11173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 * "foldlevel()" function
11175 */
11176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011178 typval_T *argvars UNUSED;
11179 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180{
11181#ifdef FEAT_FOLDING
11182 linenr_T lnum;
11183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188}
11189
11190/*
11191 * "foldtext()" function
11192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011195 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197{
11198#ifdef FEAT_FOLDING
11199 linenr_T lnum;
11200 char_u *s;
11201 char_u *r;
11202 int len;
11203 char *txt;
11204#endif
11205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->v_type = VAR_STRING;
11207 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11210 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11211 <= curbuf->b_ml.ml_line_count
11212 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 {
11214 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011215 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11216 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 {
11218 if (!linewhite(lnum))
11219 break;
11220 ++lnum;
11221 }
11222
11223 /* Find interesting text in this line. */
11224 s = skipwhite(ml_get(lnum));
11225 /* skip C comment-start */
11226 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011229 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011230 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011231 {
11232 s = skipwhite(ml_get(lnum + 1));
11233 if (*s == '*')
11234 s = skipwhite(s + 1);
11235 }
11236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 txt = _("+-%s%3ld lines: ");
11238 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 + 20 /* for %3ld */
11241 + STRLEN(s))); /* concatenated */
11242 if (r != NULL)
11243 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011244 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11245 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11246 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 len = (int)STRLEN(r);
11248 STRCAT(r, s);
11249 /* remove 'foldmarker' and 'commentstring' */
11250 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 }
11253 }
11254#endif
11255}
11256
11257/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011258 * "foldtextresult(lnum)" function
11259 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011264{
11265#ifdef FEAT_FOLDING
11266 linenr_T lnum;
11267 char_u *text;
11268 char_u buf[51];
11269 foldinfo_T foldinfo;
11270 int fold_count;
11271#endif
11272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = VAR_STRING;
11274 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011275#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 /* treat illegal types and illegal string values for {lnum} the same */
11278 if (lnum < 0)
11279 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011280 fold_count = foldedCount(curwin, lnum, &foldinfo);
11281 if (fold_count > 0)
11282 {
11283 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11284 &foldinfo, buf);
11285 if (text == buf)
11286 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011288 }
11289#endif
11290}
11291
11292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 * "foreground()" function
11294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011297 typval_T *argvars UNUSED;
11298 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300#ifdef FEAT_GUI
11301 if (gui.in_use)
11302 gui_mch_set_foreground();
11303#else
11304# ifdef WIN32
11305 win32_set_foreground();
11306# endif
11307#endif
11308}
11309
11310/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011311 * "function()" function
11312 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *argvars;
11316 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011317{
11318 char_u *s;
11319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011321 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011322 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011323 /* Don't check an autoload name for existence here. */
11324 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011325 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011326 else
11327 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011328 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011329 {
11330 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011331 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011332
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011333 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11334 * also be called from another script. Using trans_function_name()
11335 * would also work, but some plugins depend on the name being
11336 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011337 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011338 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011339 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011340 if (rettv->vval.v_string != NULL)
11341 {
11342 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011343 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011344 }
11345 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011346 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011347 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011349 }
11350}
11351
11352/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011353 * "garbagecollect()" function
11354 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011355 static void
11356f_garbagecollect(argvars, rettv)
11357 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011358 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011359{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011360 /* This is postponed until we are back at the toplevel, because we may be
11361 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11362 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011363
11364 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11365 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011366}
11367
11368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011369 * "get()" function
11370 */
11371 static void
11372f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *argvars;
11374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011375{
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 listitem_T *li;
11377 list_T *l;
11378 dictitem_T *di;
11379 dict_T *d;
11380 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011381
Bram Moolenaare9a41262005-01-15 22:18:47 +000011382 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011383 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011384 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 int error = FALSE;
11387
11388 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11389 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011390 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011391 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011393 else if (argvars[0].v_type == VAR_DICT)
11394 {
11395 if ((d = argvars[0].vval.v_dict) != NULL)
11396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011397 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011398 if (di != NULL)
11399 tv = &di->di_tv;
11400 }
11401 }
11402 else
11403 EMSG2(_(e_listdictarg), "get()");
11404
11405 if (tv == NULL)
11406 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011408 copy_tv(&argvars[2], rettv);
11409 }
11410 else
11411 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011412}
11413
Bram Moolenaar342337a2005-07-21 21:11:17 +000011414static 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 +000011415
11416/*
11417 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011418 * Return a range (from start to end) of lines in rettv from the specified
11419 * buffer.
11420 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011421 */
11422 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011423get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011424 buf_T *buf;
11425 linenr_T start;
11426 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011427 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011428 typval_T *rettv;
11429{
11430 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011431
Bram Moolenaar959a1432013-12-14 12:17:38 +010011432 rettv->v_type = VAR_STRING;
11433 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011434 if (retlist && rettv_list_alloc(rettv) == FAIL)
11435 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011436
11437 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11438 return;
11439
11440 if (!retlist)
11441 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011442 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11443 p = ml_get_buf(buf, start, FALSE);
11444 else
11445 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011446 rettv->vval.v_string = vim_strsave(p);
11447 }
11448 else
11449 {
11450 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011451 return;
11452
11453 if (start < 1)
11454 start = 1;
11455 if (end > buf->b_ml.ml_line_count)
11456 end = buf->b_ml.ml_line_count;
11457 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011458 if (list_append_string(rettv->vval.v_list,
11459 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011460 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011461 }
11462}
11463
11464/*
11465 * "getbufline()" function
11466 */
11467 static void
11468f_getbufline(argvars, rettv)
11469 typval_T *argvars;
11470 typval_T *rettv;
11471{
11472 linenr_T lnum;
11473 linenr_T end;
11474 buf_T *buf;
11475
11476 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11477 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011478 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011479 --emsg_off;
11480
Bram Moolenaar661b1822005-07-28 22:36:45 +000011481 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011482 if (argvars[2].v_type == VAR_UNKNOWN)
11483 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011484 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011485 end = get_tv_lnum_buf(&argvars[2], buf);
11486
Bram Moolenaar342337a2005-07-21 21:11:17 +000011487 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011488}
11489
Bram Moolenaar0d660222005-01-07 21:51:51 +000011490/*
11491 * "getbufvar()" function
11492 */
11493 static void
11494f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 typval_T *argvars;
11496 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497{
11498 buf_T *buf;
11499 buf_T *save_curbuf;
11500 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011502 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11505 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011506 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011507 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011508
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011509 rettv->v_type = VAR_STRING;
11510 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011511
11512 if (buf != NULL && varname != NULL)
11513 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011514 /* set curbuf to be our buf, temporarily */
11515 save_curbuf = curbuf;
11516 curbuf = buf;
11517
Bram Moolenaar0d660222005-01-07 21:51:51 +000011518 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011519 {
11520 if (get_option_tv(&varname, rettv, TRUE) == OK)
11521 done = TRUE;
11522 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011523 else if (STRCMP(varname, "changedtick") == 0)
11524 {
11525 rettv->v_type = VAR_NUMBER;
11526 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011527 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011528 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011529 else
11530 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011531 /* Look up the variable. */
11532 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11533 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11534 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011535 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011536 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011537 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011538 done = TRUE;
11539 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011541
11542 /* restore previous notion of curbuf */
11543 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544 }
11545
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011546 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11547 /* use the default value */
11548 copy_tv(&argvars[2], rettv);
11549
Bram Moolenaar0d660222005-01-07 21:51:51 +000011550 --emsg_off;
11551}
11552
11553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 * "getchar()" function
11555 */
11556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011558 typval_T *argvars;
11559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560{
11561 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011562 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011564 /* Position the cursor. Needed after a message that ends in a space. */
11565 windgoto(msg_row, msg_col);
11566
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 ++no_mapping;
11568 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011569 for (;;)
11570 {
11571 if (argvars[0].v_type == VAR_UNKNOWN)
11572 /* getchar(): blocking wait. */
11573 n = safe_vgetc();
11574 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11575 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011576 n = vpeekc_any();
11577 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011578 /* illegal argument or getchar(0) and no char avail: return zero */
11579 n = 0;
11580 else
11581 /* getchar(0) and char avail: return char */
11582 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011583
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011584 if (n == K_IGNORE)
11585 continue;
11586 break;
11587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 --no_mapping;
11589 --allow_keys;
11590
Bram Moolenaar219b8702006-11-01 14:32:36 +000011591 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11592 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11593 vimvars[VV_MOUSE_COL].vv_nr = 0;
11594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 if (IS_SPECIAL(n) || mod_mask != 0)
11597 {
11598 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11599 int i = 0;
11600
11601 /* Turn a special key into three bytes, plus modifier. */
11602 if (mod_mask != 0)
11603 {
11604 temp[i++] = K_SPECIAL;
11605 temp[i++] = KS_MODIFIER;
11606 temp[i++] = mod_mask;
11607 }
11608 if (IS_SPECIAL(n))
11609 {
11610 temp[i++] = K_SPECIAL;
11611 temp[i++] = K_SECOND(n);
11612 temp[i++] = K_THIRD(n);
11613 }
11614#ifdef FEAT_MBYTE
11615 else if (has_mbyte)
11616 i += (*mb_char2bytes)(n, temp + i);
11617#endif
11618 else
11619 temp[i++] = n;
11620 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->v_type = VAR_STRING;
11622 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011623
11624#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011625 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011626 {
11627 int row = mouse_row;
11628 int col = mouse_col;
11629 win_T *win;
11630 linenr_T lnum;
11631# ifdef FEAT_WINDOWS
11632 win_T *wp;
11633# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011634 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011635
11636 if (row >= 0 && col >= 0)
11637 {
11638 /* Find the window at the mouse coordinates and compute the
11639 * text position. */
11640 win = mouse_find_win(&row, &col);
11641 (void)mouse_comp_pos(win, &row, &col, &lnum);
11642# ifdef FEAT_WINDOWS
11643 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011644 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011645# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011646 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011647 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11648 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11649 }
11650 }
11651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 }
11653}
11654
11655/*
11656 * "getcharmod()" function
11657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664}
11665
11666/*
11667 * "getcmdline()" function
11668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011671 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674 rettv->v_type = VAR_STRING;
11675 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676}
11677
11678/*
11679 * "getcmdpos()" function
11680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011686 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687}
11688
11689/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011690 * "getcmdtype()" function
11691 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011692 static void
11693f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011694 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011695 typval_T *rettv;
11696{
11697 rettv->v_type = VAR_STRING;
11698 rettv->vval.v_string = alloc(2);
11699 if (rettv->vval.v_string != NULL)
11700 {
11701 rettv->vval.v_string[0] = get_cmdline_type();
11702 rettv->vval.v_string[1] = NUL;
11703 }
11704}
11705
11706/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011707 * "getcmdwintype()" function
11708 */
11709 static void
11710f_getcmdwintype(argvars, rettv)
11711 typval_T *argvars UNUSED;
11712 typval_T *rettv;
11713{
11714 rettv->v_type = VAR_STRING;
11715 rettv->vval.v_string = NULL;
11716#ifdef FEAT_CMDWIN
11717 rettv->vval.v_string = alloc(2);
11718 if (rettv->vval.v_string != NULL)
11719 {
11720 rettv->vval.v_string[0] = cmdwin_type;
11721 rettv->vval.v_string[1] = NUL;
11722 }
11723#endif
11724}
11725
11726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 * "getcwd()" function
11728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011731 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011734 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011737 rettv->vval.v_string = NULL;
11738 cwd = alloc(MAXPATHL);
11739 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011741 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11742 {
11743 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011745 if (rettv->vval.v_string != NULL)
11746 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011748 }
11749 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 }
11751}
11752
11753/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011754 * "getfontname()" function
11755 */
11756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011759 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011760{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->v_type = VAR_STRING;
11762 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011763#ifdef FEAT_GUI
11764 if (gui.in_use)
11765 {
11766 GuiFont font;
11767 char_u *name = NULL;
11768
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011769 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011770 {
11771 /* Get the "Normal" font. Either the name saved by
11772 * hl_set_font_name() or from the font ID. */
11773 font = gui.norm_font;
11774 name = hl_get_font_name();
11775 }
11776 else
11777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011779 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11780 return;
11781 font = gui_mch_get_font(name, FALSE);
11782 if (font == NOFONT)
11783 return; /* Invalid font name, return empty string. */
11784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011786 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011787 gui_mch_free_font(font);
11788 }
11789#endif
11790}
11791
11792/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011793 * "getfperm({fname})" function
11794 */
11795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011796f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011797 typval_T *argvars;
11798 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011799{
11800 char_u *fname;
11801 struct stat st;
11802 char_u *perm = NULL;
11803 char_u flags[] = "rwx";
11804 int i;
11805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011809 if (mch_stat((char *)fname, &st) >= 0)
11810 {
11811 perm = vim_strsave((char_u *)"---------");
11812 if (perm != NULL)
11813 {
11814 for (i = 0; i < 9; i++)
11815 {
11816 if (st.st_mode & (1 << (8 - i)))
11817 perm[i] = flags[i % 3];
11818 }
11819 }
11820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011822}
11823
11824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 * "getfsize({fname})" function
11826 */
11827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011829 typval_T *argvars;
11830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831{
11832 char_u *fname;
11833 struct stat st;
11834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838
11839 if (mch_stat((char *)fname, &st) >= 0)
11840 {
11841 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011846
11847 /* non-perfect check for overflow */
11848 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11849 rettv->vval.v_number = -2;
11850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 }
11852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854}
11855
11856/*
11857 * "getftime({fname})" function
11858 */
11859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011861 typval_T *argvars;
11862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863{
11864 char_u *fname;
11865 struct stat st;
11866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868
11869 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873}
11874
11875/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011876 * "getftype({fname})" function
11877 */
11878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011880 typval_T *argvars;
11881 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011882{
11883 char_u *fname;
11884 struct stat st;
11885 char_u *type = NULL;
11886 char *t;
11887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011891 if (mch_lstat((char *)fname, &st) >= 0)
11892 {
11893#ifdef S_ISREG
11894 if (S_ISREG(st.st_mode))
11895 t = "file";
11896 else if (S_ISDIR(st.st_mode))
11897 t = "dir";
11898# ifdef S_ISLNK
11899 else if (S_ISLNK(st.st_mode))
11900 t = "link";
11901# endif
11902# ifdef S_ISBLK
11903 else if (S_ISBLK(st.st_mode))
11904 t = "bdev";
11905# endif
11906# ifdef S_ISCHR
11907 else if (S_ISCHR(st.st_mode))
11908 t = "cdev";
11909# endif
11910# ifdef S_ISFIFO
11911 else if (S_ISFIFO(st.st_mode))
11912 t = "fifo";
11913# endif
11914# ifdef S_ISSOCK
11915 else if (S_ISSOCK(st.st_mode))
11916 t = "fifo";
11917# endif
11918 else
11919 t = "other";
11920#else
11921# ifdef S_IFMT
11922 switch (st.st_mode & S_IFMT)
11923 {
11924 case S_IFREG: t = "file"; break;
11925 case S_IFDIR: t = "dir"; break;
11926# ifdef S_IFLNK
11927 case S_IFLNK: t = "link"; break;
11928# endif
11929# ifdef S_IFBLK
11930 case S_IFBLK: t = "bdev"; break;
11931# endif
11932# ifdef S_IFCHR
11933 case S_IFCHR: t = "cdev"; break;
11934# endif
11935# ifdef S_IFIFO
11936 case S_IFIFO: t = "fifo"; break;
11937# endif
11938# ifdef S_IFSOCK
11939 case S_IFSOCK: t = "socket"; break;
11940# endif
11941 default: t = "other";
11942 }
11943# else
11944 if (mch_isdir(fname))
11945 t = "dir";
11946 else
11947 t = "file";
11948# endif
11949#endif
11950 type = vim_strsave((char_u *)t);
11951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011953}
11954
11955/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011956 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011957 */
11958 static void
11959f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011960 typval_T *argvars;
11961 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011962{
11963 linenr_T lnum;
11964 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011965 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011966
11967 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011968 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011969 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011970 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011971 retlist = FALSE;
11972 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011973 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011974 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011975 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011976 retlist = TRUE;
11977 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011978
Bram Moolenaar342337a2005-07-21 21:11:17 +000011979 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011980}
11981
11982/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011983 * "getmatches()" function
11984 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011985 static void
11986f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011987 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011988 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011989{
11990#ifdef FEAT_SEARCH_EXTRA
11991 dict_T *dict;
11992 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011993 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011994
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011995 if (rettv_list_alloc(rettv) == OK)
11996 {
11997 while (cur != NULL)
11998 {
11999 dict = dict_alloc();
12000 if (dict == NULL)
12001 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012002 if (cur->match.regprog == NULL)
12003 {
12004 /* match added with matchaddpos() */
12005 for (i = 0; i < MAXPOSMATCH; ++i)
12006 {
12007 llpos_T *llpos;
12008 char buf[6];
12009 list_T *l;
12010
12011 llpos = &cur->pos.pos[i];
12012 if (llpos->lnum == 0)
12013 break;
12014 l = list_alloc();
12015 if (l == NULL)
12016 break;
12017 list_append_number(l, (varnumber_T)llpos->lnum);
12018 if (llpos->col > 0)
12019 {
12020 list_append_number(l, (varnumber_T)llpos->col);
12021 list_append_number(l, (varnumber_T)llpos->len);
12022 }
12023 sprintf(buf, "pos%d", i + 1);
12024 dict_add_list(dict, buf, l);
12025 }
12026 }
12027 else
12028 {
12029 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12030 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012031 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012032 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12033 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
12034 list_append_dict(rettv->vval.v_list, dict);
12035 cur = cur->next;
12036 }
12037 }
12038#endif
12039}
12040
12041/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012042 * "getpid()" function
12043 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012044 static void
12045f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012046 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012047 typval_T *rettv;
12048{
12049 rettv->vval.v_number = mch_get_pid();
12050}
12051
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012052static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12053
12054/*
12055 * "getcurpos()" function
12056 */
12057 static void
12058f_getcurpos(argvars, rettv)
12059 typval_T *argvars;
12060 typval_T *rettv;
12061{
12062 getpos_both(argvars, rettv, TRUE);
12063}
12064
Bram Moolenaar18081e32008-02-20 19:11:07 +000012065/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012066 * "getpos(string)" function
12067 */
12068 static void
12069f_getpos(argvars, rettv)
12070 typval_T *argvars;
12071 typval_T *rettv;
12072{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012073 getpos_both(argvars, rettv, FALSE);
12074}
12075
12076 static void
12077getpos_both(argvars, rettv, getcurpos)
12078 typval_T *argvars;
12079 typval_T *rettv;
12080 int getcurpos;
12081{
Bram Moolenaara5525202006-03-02 22:52:09 +000012082 pos_T *fp;
12083 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012084 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012085
12086 if (rettv_list_alloc(rettv) == OK)
12087 {
12088 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012089 if (getcurpos)
12090 fp = &curwin->w_cursor;
12091 else
12092 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012093 if (fnum != -1)
12094 list_append_number(l, (varnumber_T)fnum);
12095 else
12096 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012097 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12098 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012099 list_append_number(l, (fp != NULL)
12100 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012101 : (varnumber_T)0);
12102 list_append_number(l,
12103#ifdef FEAT_VIRTUALEDIT
12104 (fp != NULL) ? (varnumber_T)fp->coladd :
12105#endif
12106 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012107 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012108 list_append_number(l, curwin->w_curswant == MAXCOL ?
12109 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012110 }
12111 else
12112 rettv->vval.v_number = FALSE;
12113}
12114
12115/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012116 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012117 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012118 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012119f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012120 typval_T *argvars UNUSED;
12121 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012122{
12123#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012124 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012125#endif
12126
Bram Moolenaar2641f772005-03-25 21:58:17 +000012127#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012128 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012129 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012130 wp = NULL;
12131 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12132 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012133 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012134 if (wp == NULL)
12135 return;
12136 }
12137
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012138 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012139 }
12140#endif
12141}
12142
12143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 * "getreg()" function
12145 */
12146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012148 typval_T *argvars;
12149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150{
12151 char_u *strregname;
12152 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012153 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012154 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012155 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012157 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012159 strregname = get_tv_string_chk(&argvars[0]);
12160 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012161 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012163 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012164 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12165 return_list = get_tv_number_chk(&argvars[2], &error);
12166 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012169 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012170
12171 if (error)
12172 return;
12173
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 regname = (strregname == NULL ? '"' : *strregname);
12175 if (regname == 0)
12176 regname = '"';
12177
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012178 if (return_list)
12179 {
12180 rettv->v_type = VAR_LIST;
12181 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12182 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012183 if (rettv->vval.v_list != NULL)
12184 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012185 }
12186 else
12187 {
12188 rettv->v_type = VAR_STRING;
12189 rettv->vval.v_string = get_reg_contents(regname,
12190 arg2 ? GREG_EXPR_SRC : 0);
12191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192}
12193
12194/*
12195 * "getregtype()" function
12196 */
12197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012199 typval_T *argvars;
12200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201{
12202 char_u *strregname;
12203 int regname;
12204 char_u buf[NUMBUFLEN + 2];
12205 long reglen = 0;
12206
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012207 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012208 {
12209 strregname = get_tv_string_chk(&argvars[0]);
12210 if (strregname == NULL) /* type error; errmsg already given */
12211 {
12212 rettv->v_type = VAR_STRING;
12213 rettv->vval.v_string = NULL;
12214 return;
12215 }
12216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 else
12218 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012219 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220
12221 regname = (strregname == NULL ? '"' : *strregname);
12222 if (regname == 0)
12223 regname = '"';
12224
12225 buf[0] = NUL;
12226 buf[1] = NUL;
12227 switch (get_reg_type(regname, &reglen))
12228 {
12229 case MLINE: buf[0] = 'V'; break;
12230 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 case MBLOCK:
12232 buf[0] = Ctrl_V;
12233 sprintf((char *)buf + 1, "%ld", reglen + 1);
12234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012236 rettv->v_type = VAR_STRING;
12237 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238}
12239
12240/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012241 * "gettabvar()" function
12242 */
12243 static void
12244f_gettabvar(argvars, rettv)
12245 typval_T *argvars;
12246 typval_T *rettv;
12247{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012248 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012249 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012250 dictitem_T *v;
12251 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012252 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012253
12254 rettv->v_type = VAR_STRING;
12255 rettv->vval.v_string = NULL;
12256
12257 varname = get_tv_string_chk(&argvars[1]);
12258 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12259 if (tp != NULL && varname != NULL)
12260 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012261 /* Set tp to be our tabpage, temporarily. Also set the window to the
12262 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012263 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12264 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012265 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012266 /* look up the variable */
12267 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12268 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12269 if (v != NULL)
12270 {
12271 copy_tv(&v->di_tv, rettv);
12272 done = TRUE;
12273 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012274 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012275
12276 /* restore previous notion of curwin */
12277 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012278 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012279
12280 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012281 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012282}
12283
12284/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012285 * "gettabwinvar()" function
12286 */
12287 static void
12288f_gettabwinvar(argvars, rettv)
12289 typval_T *argvars;
12290 typval_T *rettv;
12291{
12292 getwinvar(argvars, rettv, 1);
12293}
12294
12295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 * "getwinposx()" function
12297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012300 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304#ifdef FEAT_GUI
12305 if (gui.in_use)
12306 {
12307 int x, y;
12308
12309 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 }
12312#endif
12313}
12314
12315/*
12316 * "getwinposy()" function
12317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324#ifdef FEAT_GUI
12325 if (gui.in_use)
12326 {
12327 int x, y;
12328
12329 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 }
12332#endif
12333}
12334
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012335/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012336 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012337 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012338 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012339find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012340 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012341 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012342{
12343#ifdef FEAT_WINDOWS
12344 win_T *wp;
12345#endif
12346 int nr;
12347
12348 nr = get_tv_number_chk(vp, NULL);
12349
12350#ifdef FEAT_WINDOWS
12351 if (nr < 0)
12352 return NULL;
12353 if (nr == 0)
12354 return curwin;
12355
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012356 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12357 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012358 if (--nr <= 0)
12359 break;
12360 return wp;
12361#else
12362 if (nr == 0 || nr == 1)
12363 return curwin;
12364 return NULL;
12365#endif
12366}
12367
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368/*
12369 * "getwinvar()" function
12370 */
12371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012373 typval_T *argvars;
12374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012376 getwinvar(argvars, rettv, 0);
12377}
12378
12379/*
12380 * getwinvar() and gettabwinvar()
12381 */
12382 static void
12383getwinvar(argvars, rettv, off)
12384 typval_T *argvars;
12385 typval_T *rettv;
12386 int off; /* 1 for gettabwinvar() */
12387{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 win_T *win, *oldcurwin;
12389 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012390 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012391 tabpage_T *tp = NULL;
12392 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012393 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012395#ifdef FEAT_WINDOWS
12396 if (off == 1)
12397 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12398 else
12399 tp = curtab;
12400#endif
12401 win = find_win_by_nr(&argvars[off], tp);
12402 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012405 rettv->v_type = VAR_STRING;
12406 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407
12408 if (win != NULL && varname != NULL)
12409 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012410 /* Set curwin to be our win, temporarily. Also set the tabpage,
12411 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012412 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012413 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012414 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012415 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012416 if (get_option_tv(&varname, rettv, 1) == OK)
12417 done = TRUE;
12418 }
12419 else
12420 {
12421 /* Look up the variable. */
12422 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12423 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12424 varname, FALSE);
12425 if (v != NULL)
12426 {
12427 copy_tv(&v->di_tv, rettv);
12428 done = TRUE;
12429 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012432
12433 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012434 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 }
12436
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012437 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12438 /* use the default return value */
12439 copy_tv(&argvars[off + 2], rettv);
12440
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 --emsg_off;
12442}
12443
12444/*
12445 * "glob()" function
12446 */
12447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012448f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 typval_T *argvars;
12450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012452 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012454 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012456 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012457 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012459 if (argvars[1].v_type != VAR_UNKNOWN)
12460 {
12461 if (get_tv_number_chk(&argvars[1], &error))
12462 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012463 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012464 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012465 if (get_tv_number_chk(&argvars[2], &error))
12466 {
12467 rettv->v_type = VAR_LIST;
12468 rettv->vval.v_list = NULL;
12469 }
12470 if (argvars[3].v_type != VAR_UNKNOWN
12471 && get_tv_number_chk(&argvars[3], &error))
12472 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012473 }
12474 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012475 if (!error)
12476 {
12477 ExpandInit(&xpc);
12478 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012479 if (p_wic)
12480 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012481 if (rettv->v_type == VAR_STRING)
12482 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012483 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012484 else if (rettv_list_alloc(rettv) != FAIL)
12485 {
12486 int i;
12487
12488 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12489 NULL, options, WILD_ALL_KEEP);
12490 for (i = 0; i < xpc.xp_numfiles; i++)
12491 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12492
12493 ExpandCleanup(&xpc);
12494 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012495 }
12496 else
12497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498}
12499
12500/*
12501 * "globpath()" function
12502 */
12503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012505 typval_T *argvars;
12506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012508 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012511 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012512 garray_T ga;
12513 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012515 /* When the optional second argument is non-zero, don't remove matches
12516 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012518 if (argvars[2].v_type != VAR_UNKNOWN)
12519 {
12520 if (get_tv_number_chk(&argvars[2], &error))
12521 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012522 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012523 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012524 if (get_tv_number_chk(&argvars[3], &error))
12525 {
12526 rettv->v_type = VAR_LIST;
12527 rettv->vval.v_list = NULL;
12528 }
12529 if (argvars[4].v_type != VAR_UNKNOWN
12530 && get_tv_number_chk(&argvars[4], &error))
12531 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012532 }
12533 }
12534 if (file != NULL && !error)
12535 {
12536 ga_init2(&ga, (int)sizeof(char_u *), 10);
12537 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12538 if (rettv->v_type == VAR_STRING)
12539 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12540 else if (rettv_list_alloc(rettv) != FAIL)
12541 for (i = 0; i < ga.ga_len; ++i)
12542 list_append_string(rettv->vval.v_list,
12543 ((char_u **)(ga.ga_data))[i], -1);
12544 ga_clear_strings(&ga);
12545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012546 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012547 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548}
12549
12550/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012551 * "glob2regpat()" function
12552 */
12553 static void
12554f_glob2regpat(argvars, rettv)
12555 typval_T *argvars;
12556 typval_T *rettv;
12557{
12558 char_u *pat = get_tv_string_chk(&argvars[0]);
12559
12560 rettv->v_type = VAR_STRING;
12561 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12562}
12563
12564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 * "has()" function
12566 */
12567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 typval_T *argvars;
12570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571{
12572 int i;
12573 char_u *name;
12574 int n = FALSE;
12575 static char *(has_list[]) =
12576 {
12577#ifdef AMIGA
12578 "amiga",
12579# ifdef FEAT_ARP
12580 "arp",
12581# endif
12582#endif
12583#ifdef __BEOS__
12584 "beos",
12585#endif
12586#ifdef MSDOS
12587# ifdef DJGPP
12588 "dos32",
12589# else
12590 "dos16",
12591# endif
12592#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012593#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594 "mac",
12595#endif
12596#if defined(MACOS_X_UNIX)
12597 "macunix",
12598#endif
12599#ifdef OS2
12600 "os2",
12601#endif
12602#ifdef __QNX__
12603 "qnx",
12604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605#ifdef UNIX
12606 "unix",
12607#endif
12608#ifdef VMS
12609 "vms",
12610#endif
12611#ifdef WIN16
12612 "win16",
12613#endif
12614#ifdef WIN32
12615 "win32",
12616#endif
12617#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12618 "win32unix",
12619#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012620#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 "win64",
12622#endif
12623#ifdef EBCDIC
12624 "ebcdic",
12625#endif
12626#ifndef CASE_INSENSITIVE_FILENAME
12627 "fname_case",
12628#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012629#ifdef HAVE_ACL
12630 "acl",
12631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632#ifdef FEAT_ARABIC
12633 "arabic",
12634#endif
12635#ifdef FEAT_AUTOCMD
12636 "autocmd",
12637#endif
12638#ifdef FEAT_BEVAL
12639 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012640# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12641 "balloon_multiline",
12642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643#endif
12644#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12645 "builtin_terms",
12646# ifdef ALL_BUILTIN_TCAPS
12647 "all_builtin_terms",
12648# endif
12649#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012650#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12651 || defined(FEAT_GUI_W32) \
12652 || defined(FEAT_GUI_MOTIF))
12653 "browsefilter",
12654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655#ifdef FEAT_BYTEOFF
12656 "byte_offset",
12657#endif
12658#ifdef FEAT_CINDENT
12659 "cindent",
12660#endif
12661#ifdef FEAT_CLIENTSERVER
12662 "clientserver",
12663#endif
12664#ifdef FEAT_CLIPBOARD
12665 "clipboard",
12666#endif
12667#ifdef FEAT_CMDL_COMPL
12668 "cmdline_compl",
12669#endif
12670#ifdef FEAT_CMDHIST
12671 "cmdline_hist",
12672#endif
12673#ifdef FEAT_COMMENTS
12674 "comments",
12675#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012676#ifdef FEAT_CONCEAL
12677 "conceal",
12678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679#ifdef FEAT_CRYPT
12680 "cryptv",
12681#endif
12682#ifdef FEAT_CSCOPE
12683 "cscope",
12684#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012685#ifdef FEAT_CURSORBIND
12686 "cursorbind",
12687#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012688#ifdef CURSOR_SHAPE
12689 "cursorshape",
12690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691#ifdef DEBUG
12692 "debug",
12693#endif
12694#ifdef FEAT_CON_DIALOG
12695 "dialog_con",
12696#endif
12697#ifdef FEAT_GUI_DIALOG
12698 "dialog_gui",
12699#endif
12700#ifdef FEAT_DIFF
12701 "diff",
12702#endif
12703#ifdef FEAT_DIGRAPHS
12704 "digraphs",
12705#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012706#ifdef FEAT_DIRECTX
12707 "directx",
12708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709#ifdef FEAT_DND
12710 "dnd",
12711#endif
12712#ifdef FEAT_EMACS_TAGS
12713 "emacs_tags",
12714#endif
12715 "eval", /* always present, of course! */
12716#ifdef FEAT_EX_EXTRA
12717 "ex_extra",
12718#endif
12719#ifdef FEAT_SEARCH_EXTRA
12720 "extra_search",
12721#endif
12722#ifdef FEAT_FKMAP
12723 "farsi",
12724#endif
12725#ifdef FEAT_SEARCHPATH
12726 "file_in_path",
12727#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012728#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012729 "filterpipe",
12730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731#ifdef FEAT_FIND_ID
12732 "find_in_path",
12733#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012734#ifdef FEAT_FLOAT
12735 "float",
12736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737#ifdef FEAT_FOLDING
12738 "folding",
12739#endif
12740#ifdef FEAT_FOOTER
12741 "footer",
12742#endif
12743#if !defined(USE_SYSTEM) && defined(UNIX)
12744 "fork",
12745#endif
12746#ifdef FEAT_GETTEXT
12747 "gettext",
12748#endif
12749#ifdef FEAT_GUI
12750 "gui",
12751#endif
12752#ifdef FEAT_GUI_ATHENA
12753# ifdef FEAT_GUI_NEXTAW
12754 "gui_neXtaw",
12755# else
12756 "gui_athena",
12757# endif
12758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759#ifdef FEAT_GUI_GTK
12760 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012763#ifdef FEAT_GUI_GNOME
12764 "gui_gnome",
12765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766#ifdef FEAT_GUI_MAC
12767 "gui_mac",
12768#endif
12769#ifdef FEAT_GUI_MOTIF
12770 "gui_motif",
12771#endif
12772#ifdef FEAT_GUI_PHOTON
12773 "gui_photon",
12774#endif
12775#ifdef FEAT_GUI_W16
12776 "gui_win16",
12777#endif
12778#ifdef FEAT_GUI_W32
12779 "gui_win32",
12780#endif
12781#ifdef FEAT_HANGULIN
12782 "hangul_input",
12783#endif
12784#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12785 "iconv",
12786#endif
12787#ifdef FEAT_INS_EXPAND
12788 "insert_expand",
12789#endif
12790#ifdef FEAT_JUMPLIST
12791 "jumplist",
12792#endif
12793#ifdef FEAT_KEYMAP
12794 "keymap",
12795#endif
12796#ifdef FEAT_LANGMAP
12797 "langmap",
12798#endif
12799#ifdef FEAT_LIBCALL
12800 "libcall",
12801#endif
12802#ifdef FEAT_LINEBREAK
12803 "linebreak",
12804#endif
12805#ifdef FEAT_LISP
12806 "lispindent",
12807#endif
12808#ifdef FEAT_LISTCMDS
12809 "listcmds",
12810#endif
12811#ifdef FEAT_LOCALMAP
12812 "localmap",
12813#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012814#ifdef FEAT_LUA
12815# ifndef DYNAMIC_LUA
12816 "lua",
12817# endif
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819#ifdef FEAT_MENU
12820 "menu",
12821#endif
12822#ifdef FEAT_SESSION
12823 "mksession",
12824#endif
12825#ifdef FEAT_MODIFY_FNAME
12826 "modify_fname",
12827#endif
12828#ifdef FEAT_MOUSE
12829 "mouse",
12830#endif
12831#ifdef FEAT_MOUSESHAPE
12832 "mouseshape",
12833#endif
12834#if defined(UNIX) || defined(VMS)
12835# ifdef FEAT_MOUSE_DEC
12836 "mouse_dec",
12837# endif
12838# ifdef FEAT_MOUSE_GPM
12839 "mouse_gpm",
12840# endif
12841# ifdef FEAT_MOUSE_JSB
12842 "mouse_jsbterm",
12843# endif
12844# ifdef FEAT_MOUSE_NET
12845 "mouse_netterm",
12846# endif
12847# ifdef FEAT_MOUSE_PTERM
12848 "mouse_pterm",
12849# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012850# ifdef FEAT_MOUSE_SGR
12851 "mouse_sgr",
12852# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012853# ifdef FEAT_SYSMOUSE
12854 "mouse_sysmouse",
12855# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012856# ifdef FEAT_MOUSE_URXVT
12857 "mouse_urxvt",
12858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859# ifdef FEAT_MOUSE_XTERM
12860 "mouse_xterm",
12861# endif
12862#endif
12863#ifdef FEAT_MBYTE
12864 "multi_byte",
12865#endif
12866#ifdef FEAT_MBYTE_IME
12867 "multi_byte_ime",
12868#endif
12869#ifdef FEAT_MULTI_LANG
12870 "multi_lang",
12871#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012872#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012873#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012874 "mzscheme",
12875#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877#ifdef FEAT_OLE
12878 "ole",
12879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880#ifdef FEAT_PATH_EXTRA
12881 "path_extra",
12882#endif
12883#ifdef FEAT_PERL
12884#ifndef DYNAMIC_PERL
12885 "perl",
12886#endif
12887#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012888#ifdef FEAT_PERSISTENT_UNDO
12889 "persistent_undo",
12890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891#ifdef FEAT_PYTHON
12892#ifndef DYNAMIC_PYTHON
12893 "python",
12894#endif
12895#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012896#ifdef FEAT_PYTHON3
12897#ifndef DYNAMIC_PYTHON3
12898 "python3",
12899#endif
12900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901#ifdef FEAT_POSTSCRIPT
12902 "postscript",
12903#endif
12904#ifdef FEAT_PRINTER
12905 "printer",
12906#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012907#ifdef FEAT_PROFILE
12908 "profile",
12909#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012910#ifdef FEAT_RELTIME
12911 "reltime",
12912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913#ifdef FEAT_QUICKFIX
12914 "quickfix",
12915#endif
12916#ifdef FEAT_RIGHTLEFT
12917 "rightleft",
12918#endif
12919#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12920 "ruby",
12921#endif
12922#ifdef FEAT_SCROLLBIND
12923 "scrollbind",
12924#endif
12925#ifdef FEAT_CMDL_INFO
12926 "showcmd",
12927 "cmdline_info",
12928#endif
12929#ifdef FEAT_SIGNS
12930 "signs",
12931#endif
12932#ifdef FEAT_SMARTINDENT
12933 "smartindent",
12934#endif
12935#ifdef FEAT_SNIFF
12936 "sniff",
12937#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012938#ifdef STARTUPTIME
12939 "startuptime",
12940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941#ifdef FEAT_STL_OPT
12942 "statusline",
12943#endif
12944#ifdef FEAT_SUN_WORKSHOP
12945 "sun_workshop",
12946#endif
12947#ifdef FEAT_NETBEANS_INTG
12948 "netbeans_intg",
12949#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012950#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012951 "spell",
12952#endif
12953#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 "syntax",
12955#endif
12956#if defined(USE_SYSTEM) || !defined(UNIX)
12957 "system",
12958#endif
12959#ifdef FEAT_TAG_BINS
12960 "tag_binary",
12961#endif
12962#ifdef FEAT_TAG_OLDSTATIC
12963 "tag_old_static",
12964#endif
12965#ifdef FEAT_TAG_ANYWHITE
12966 "tag_any_white",
12967#endif
12968#ifdef FEAT_TCL
12969# ifndef DYNAMIC_TCL
12970 "tcl",
12971# endif
12972#endif
12973#ifdef TERMINFO
12974 "terminfo",
12975#endif
12976#ifdef FEAT_TERMRESPONSE
12977 "termresponse",
12978#endif
12979#ifdef FEAT_TEXTOBJ
12980 "textobjects",
12981#endif
12982#ifdef HAVE_TGETENT
12983 "tgetent",
12984#endif
12985#ifdef FEAT_TITLE
12986 "title",
12987#endif
12988#ifdef FEAT_TOOLBAR
12989 "toolbar",
12990#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012991#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12992 "unnamedplus",
12993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994#ifdef FEAT_USR_CMDS
12995 "user-commands", /* was accidentally included in 5.4 */
12996 "user_commands",
12997#endif
12998#ifdef FEAT_VIMINFO
12999 "viminfo",
13000#endif
13001#ifdef FEAT_VERTSPLIT
13002 "vertsplit",
13003#endif
13004#ifdef FEAT_VIRTUALEDIT
13005 "virtualedit",
13006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008#ifdef FEAT_VISUALEXTRA
13009 "visualextra",
13010#endif
13011#ifdef FEAT_VREPLACE
13012 "vreplace",
13013#endif
13014#ifdef FEAT_WILDIGN
13015 "wildignore",
13016#endif
13017#ifdef FEAT_WILDMENU
13018 "wildmenu",
13019#endif
13020#ifdef FEAT_WINDOWS
13021 "windows",
13022#endif
13023#ifdef FEAT_WAK
13024 "winaltkeys",
13025#endif
13026#ifdef FEAT_WRITEBACKUP
13027 "writebackup",
13028#endif
13029#ifdef FEAT_XIM
13030 "xim",
13031#endif
13032#ifdef FEAT_XFONTSET
13033 "xfontset",
13034#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013035#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013036 "xpm",
13037 "xpm_w32", /* for backward compatibility */
13038#else
13039# if defined(HAVE_XPM)
13040 "xpm",
13041# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043#ifdef USE_XSMP
13044 "xsmp",
13045#endif
13046#ifdef USE_XSMP_INTERACT
13047 "xsmp_interact",
13048#endif
13049#ifdef FEAT_XCLIPBOARD
13050 "xterm_clipboard",
13051#endif
13052#ifdef FEAT_XTERM_SAVE
13053 "xterm_save",
13054#endif
13055#if defined(UNIX) && defined(FEAT_X11)
13056 "X11",
13057#endif
13058 NULL
13059 };
13060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 for (i = 0; has_list[i] != NULL; ++i)
13063 if (STRICMP(name, has_list[i]) == 0)
13064 {
13065 n = TRUE;
13066 break;
13067 }
13068
13069 if (n == FALSE)
13070 {
13071 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013072 {
13073 if (name[5] == '-'
13074 && STRLEN(name) > 11
13075 && vim_isdigit(name[6])
13076 && vim_isdigit(name[8])
13077 && vim_isdigit(name[10]))
13078 {
13079 int major = atoi((char *)name + 6);
13080 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013081
13082 /* Expect "patch-9.9.01234". */
13083 n = (major < VIM_VERSION_MAJOR
13084 || (major == VIM_VERSION_MAJOR
13085 && (minor < VIM_VERSION_MINOR
13086 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013087 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013088 }
13089 else
13090 n = has_patch(atoi((char *)name + 5));
13091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 else if (STRICMP(name, "vim_starting") == 0)
13093 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013094#ifdef FEAT_MBYTE
13095 else if (STRICMP(name, "multi_byte_encoding") == 0)
13096 n = has_mbyte;
13097#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013098#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13099 else if (STRICMP(name, "balloon_multiline") == 0)
13100 n = multiline_balloon_available();
13101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102#ifdef DYNAMIC_TCL
13103 else if (STRICMP(name, "tcl") == 0)
13104 n = tcl_enabled(FALSE);
13105#endif
13106#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13107 else if (STRICMP(name, "iconv") == 0)
13108 n = iconv_enabled(FALSE);
13109#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013110#ifdef DYNAMIC_LUA
13111 else if (STRICMP(name, "lua") == 0)
13112 n = lua_enabled(FALSE);
13113#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013114#ifdef DYNAMIC_MZSCHEME
13115 else if (STRICMP(name, "mzscheme") == 0)
13116 n = mzscheme_enabled(FALSE);
13117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118#ifdef DYNAMIC_RUBY
13119 else if (STRICMP(name, "ruby") == 0)
13120 n = ruby_enabled(FALSE);
13121#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013122#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123#ifdef DYNAMIC_PYTHON
13124 else if (STRICMP(name, "python") == 0)
13125 n = python_enabled(FALSE);
13126#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013127#endif
13128#ifdef FEAT_PYTHON3
13129#ifdef DYNAMIC_PYTHON3
13130 else if (STRICMP(name, "python3") == 0)
13131 n = python3_enabled(FALSE);
13132#endif
13133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#ifdef DYNAMIC_PERL
13135 else if (STRICMP(name, "perl") == 0)
13136 n = perl_enabled(FALSE);
13137#endif
13138#ifdef FEAT_GUI
13139 else if (STRICMP(name, "gui_running") == 0)
13140 n = (gui.in_use || gui.starting);
13141# ifdef FEAT_GUI_W32
13142 else if (STRICMP(name, "gui_win32s") == 0)
13143 n = gui_is_win32s();
13144# endif
13145# ifdef FEAT_BROWSE
13146 else if (STRICMP(name, "browse") == 0)
13147 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13148# endif
13149#endif
13150#ifdef FEAT_SYN_HL
13151 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013152 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153#endif
13154#if defined(WIN3264)
13155 else if (STRICMP(name, "win95") == 0)
13156 n = mch_windows95();
13157#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013158#ifdef FEAT_NETBEANS_INTG
13159 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013160 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 }
13163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165}
13166
13167/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013168 * "has_key()" function
13169 */
13170 static void
13171f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013172 typval_T *argvars;
13173 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013174{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013175 if (argvars[0].v_type != VAR_DICT)
13176 {
13177 EMSG(_(e_dictreq));
13178 return;
13179 }
13180 if (argvars[0].vval.v_dict == NULL)
13181 return;
13182
13183 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013184 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013185}
13186
13187/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013188 * "haslocaldir()" function
13189 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013190 static void
13191f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013192 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013193 typval_T *rettv;
13194{
13195 rettv->vval.v_number = (curwin->w_localdir != NULL);
13196}
13197
13198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 * "hasmapto()" function
13200 */
13201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
13206 char_u *name;
13207 char_u *mode;
13208 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013209 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013212 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213 mode = (char_u *)"nvo";
13214 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013217 if (argvars[2].v_type != VAR_UNKNOWN)
13218 abbr = get_tv_number(&argvars[2]);
13219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220
Bram Moolenaar2c932302006-03-18 21:42:09 +000013221 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013222 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013224 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225}
13226
13227/*
13228 * "histadd()" function
13229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013232 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234{
13235#ifdef FEAT_CMDHIST
13236 int histype;
13237 char_u *str;
13238 char_u buf[NUMBUFLEN];
13239#endif
13240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 if (check_restricted() || check_secure())
13243 return;
13244#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013245 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13246 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 if (histype >= 0)
13248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 if (*str != NUL)
13251 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013252 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 return;
13256 }
13257 }
13258#endif
13259}
13260
13261/*
13262 * "histdel()" function
13263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013266 typval_T *argvars UNUSED;
13267 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268{
13269#ifdef FEAT_CMDHIST
13270 int n;
13271 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013272 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013274 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13275 if (str == NULL)
13276 n = 0;
13277 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013279 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013280 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013283 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 else
13285 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013286 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287 get_tv_string_buf(&argvars[1], buf));
13288 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289#endif
13290}
13291
13292/*
13293 * "histget()" function
13294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013297 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299{
13300#ifdef FEAT_CMDHIST
13301 int type;
13302 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013303 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13306 if (str == NULL)
13307 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013309 {
13310 type = get_histtype(str);
13311 if (argvars[1].v_type == VAR_UNKNOWN)
13312 idx = get_history_idx(type);
13313 else
13314 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13315 /* -1 on type error */
13316 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013319 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322}
13323
13324/*
13325 * "histnr()" function
13326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331{
13332 int i;
13333
13334#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013335 char_u *history = get_tv_string_chk(&argvars[0]);
13336
13337 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 if (i >= HIST_CMD && i < HIST_COUNT)
13339 i = get_history_idx(i);
13340 else
13341#endif
13342 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344}
13345
13346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 * "highlightID(name)" function
13348 */
13349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013351 typval_T *argvars;
13352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355}
13356
13357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013358 * "highlight_exists()" function
13359 */
13360 static void
13361f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013364{
13365 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13366}
13367
13368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 * "hostname()" function
13370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013372f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013373 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375{
13376 char_u hostname[256];
13377
13378 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013379 rettv->v_type = VAR_STRING;
13380 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381}
13382
13383/*
13384 * iconv() function
13385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013387f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013388 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390{
13391#ifdef FEAT_MBYTE
13392 char_u buf1[NUMBUFLEN];
13393 char_u buf2[NUMBUFLEN];
13394 char_u *from, *to, *str;
13395 vimconv_T vimconv;
13396#endif
13397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013398 rettv->v_type = VAR_STRING;
13399 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400
13401#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013402 str = get_tv_string(&argvars[0]);
13403 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13404 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 vimconv.vc_type = CONV_NONE;
13406 convert_setup(&vimconv, from, to);
13407
13408 /* If the encodings are equal, no conversion needed. */
13409 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013412 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413
13414 convert_setup(&vimconv, NULL, NULL);
13415 vim_free(from);
13416 vim_free(to);
13417#endif
13418}
13419
13420/*
13421 * "indent()" function
13422 */
13423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013424f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013425 typval_T *argvars;
13426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427{
13428 linenr_T lnum;
13429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013430 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013432 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013437/*
13438 * "index()" function
13439 */
13440 static void
13441f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 typval_T *argvars;
13443 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013444{
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 list_T *l;
13446 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013447 long idx = 0;
13448 int ic = FALSE;
13449
13450 rettv->vval.v_number = -1;
13451 if (argvars[0].v_type != VAR_LIST)
13452 {
13453 EMSG(_(e_listreq));
13454 return;
13455 }
13456 l = argvars[0].vval.v_list;
13457 if (l != NULL)
13458 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013459 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013460 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013462 int error = FALSE;
13463
Bram Moolenaar758711c2005-02-02 23:11:38 +000013464 /* Start at specified item. Use the cached index that list_find()
13465 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013467 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013468 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 ic = get_tv_number_chk(&argvars[3], &error);
13470 if (error)
13471 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013472 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013473
Bram Moolenaar758711c2005-02-02 23:11:38 +000013474 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013475 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013476 {
13477 rettv->vval.v_number = idx;
13478 break;
13479 }
13480 }
13481}
13482
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483static int inputsecret_flag = 0;
13484
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013485static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13486
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013488 * This function is used by f_input() and f_inputdialog() functions. The third
13489 * argument to f_input() specifies the type of completion to use at the
13490 * prompt. The third argument to f_inputdialog() specifies the value to return
13491 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 */
13493 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013494get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013495 typval_T *argvars;
13496 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013497 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 char_u *p = NULL;
13501 int c;
13502 char_u buf[NUMBUFLEN];
13503 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013504 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013505 int xp_type = EXPAND_NOTHING;
13506 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013508 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510
13511#ifdef NO_CONSOLE_INPUT
13512 /* While starting up, there is no place to enter text. */
13513 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515#endif
13516
13517 cmd_silent = FALSE; /* Want to see the prompt. */
13518 if (prompt != NULL)
13519 {
13520 /* Only the part of the message after the last NL is considered as
13521 * prompt for the command line */
13522 p = vim_strrchr(prompt, '\n');
13523 if (p == NULL)
13524 p = prompt;
13525 else
13526 {
13527 ++p;
13528 c = *p;
13529 *p = NUL;
13530 msg_start();
13531 msg_clr_eos();
13532 msg_puts_attr(prompt, echo_attr);
13533 msg_didout = FALSE;
13534 msg_starthere();
13535 *p = c;
13536 }
13537 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013539 if (argvars[1].v_type != VAR_UNKNOWN)
13540 {
13541 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13542 if (defstr != NULL)
13543 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013545 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013546 {
13547 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013548 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013549 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013550
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013551 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013552 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013553
Bram Moolenaar4463f292005-09-25 22:20:24 +000013554 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13555 if (xp_name == NULL)
13556 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013557
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013558 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013559
Bram Moolenaar4463f292005-09-25 22:20:24 +000013560 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13561 &xp_arg) == FAIL)
13562 return;
13563 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013564 }
13565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013567 {
13568# ifdef FEAT_EX_EXTRA
13569 int save_ex_normal_busy = ex_normal_busy;
13570 ex_normal_busy = 0;
13571# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013572 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013573 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13574 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013575# ifdef FEAT_EX_EXTRA
13576 ex_normal_busy = save_ex_normal_busy;
13577# endif
13578 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013579 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013580 && argvars[1].v_type != VAR_UNKNOWN
13581 && argvars[2].v_type != VAR_UNKNOWN)
13582 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13583 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013584
13585 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013587 /* since the user typed this, no need to wait for return */
13588 need_wait_return = FALSE;
13589 msg_didout = FALSE;
13590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 cmd_silent = cmd_silent_save;
13592}
13593
13594/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013595 * "input()" function
13596 * Also handles inputsecret() when inputsecret is set.
13597 */
13598 static void
13599f_input(argvars, rettv)
13600 typval_T *argvars;
13601 typval_T *rettv;
13602{
13603 get_user_input(argvars, rettv, FALSE);
13604}
13605
13606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 * "inputdialog()" function
13608 */
13609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013611 typval_T *argvars;
13612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613{
13614#if defined(FEAT_GUI_TEXTDIALOG)
13615 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13616 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13617 {
13618 char_u *message;
13619 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013620 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 message = get_tv_string_chk(&argvars[0]);
13623 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013624 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013625 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 else
13627 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 if (message != NULL && defstr != NULL
13629 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013630 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 else
13633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634 if (message != NULL && defstr != NULL
13635 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013636 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_string = vim_strsave(
13638 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 }
13644 else
13645#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013646 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647}
13648
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013649/*
13650 * "inputlist()" function
13651 */
13652 static void
13653f_inputlist(argvars, rettv)
13654 typval_T *argvars;
13655 typval_T *rettv;
13656{
13657 listitem_T *li;
13658 int selected;
13659 int mouse_used;
13660
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013661#ifdef NO_CONSOLE_INPUT
13662 /* While starting up, there is no place to enter text. */
13663 if (no_console_input())
13664 return;
13665#endif
13666 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13667 {
13668 EMSG2(_(e_listarg), "inputlist()");
13669 return;
13670 }
13671
13672 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013673 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013674 lines_left = Rows; /* avoid more prompt */
13675 msg_scroll = TRUE;
13676 msg_clr_eos();
13677
13678 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13679 {
13680 msg_puts(get_tv_string(&li->li_tv));
13681 msg_putchar('\n');
13682 }
13683
13684 /* Ask for choice. */
13685 selected = prompt_for_number(&mouse_used);
13686 if (mouse_used)
13687 selected -= lines_left;
13688
13689 rettv->vval.v_number = selected;
13690}
13691
13692
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13694
13695/*
13696 * "inputrestore()" function
13697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013699f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013700 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702{
13703 if (ga_userinput.ga_len > 0)
13704 {
13705 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13707 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013708 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 }
13710 else if (p_verbose > 1)
13711 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013712 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 }
13715}
13716
13717/*
13718 * "inputsave()" function
13719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013722 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013725 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 if (ga_grow(&ga_userinput, 1) == OK)
13727 {
13728 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13729 + ga_userinput.ga_len);
13730 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013731 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 }
13733 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013734 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735}
13736
13737/*
13738 * "inputsecret()" function
13739 */
13740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013742 typval_T *argvars;
13743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744{
13745 ++cmdline_star;
13746 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 --cmdline_star;
13749 --inputsecret_flag;
13750}
13751
13752/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013753 * "insert()" function
13754 */
13755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013756f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013757 typval_T *argvars;
13758 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013759{
13760 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013761 listitem_T *item;
13762 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013764
13765 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013766 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013767 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013768 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013769 {
13770 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 before = get_tv_number_chk(&argvars[2], &error);
13772 if (error)
13773 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013774
Bram Moolenaar758711c2005-02-02 23:11:38 +000013775 if (before == l->lv_len)
13776 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013777 else
13778 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013779 item = list_find(l, before);
13780 if (item == NULL)
13781 {
13782 EMSGN(_(e_listidx), before);
13783 l = NULL;
13784 }
13785 }
13786 if (l != NULL)
13787 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013788 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013789 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013790 }
13791 }
13792}
13793
13794/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013795 * "invert(expr)" function
13796 */
13797 static void
13798f_invert(argvars, rettv)
13799 typval_T *argvars;
13800 typval_T *rettv;
13801{
13802 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13803}
13804
13805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 * "isdirectory()" function
13807 */
13808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013809f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013810 typval_T *argvars;
13811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013813 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814}
13815
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013816/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013817 * "islocked()" function
13818 */
13819 static void
13820f_islocked(argvars, rettv)
13821 typval_T *argvars;
13822 typval_T *rettv;
13823{
13824 lval_T lv;
13825 char_u *end;
13826 dictitem_T *di;
13827
13828 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013829 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13830 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013831 if (end != NULL && lv.ll_name != NULL)
13832 {
13833 if (*end != NUL)
13834 EMSG(_(e_trailing));
13835 else
13836 {
13837 if (lv.ll_tv == NULL)
13838 {
13839 if (check_changedtick(lv.ll_name))
13840 rettv->vval.v_number = 1; /* always locked */
13841 else
13842 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013843 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013844 if (di != NULL)
13845 {
13846 /* Consider a variable locked when:
13847 * 1. the variable itself is locked
13848 * 2. the value of the variable is locked.
13849 * 3. the List or Dict value is locked.
13850 */
13851 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13852 || tv_islocked(&di->di_tv));
13853 }
13854 }
13855 }
13856 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013857 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013858 else if (lv.ll_newkey != NULL)
13859 EMSG2(_(e_dictkey), lv.ll_newkey);
13860 else if (lv.ll_list != NULL)
13861 /* List item. */
13862 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13863 else
13864 /* Dictionary item. */
13865 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13866 }
13867 }
13868
13869 clear_lval(&lv);
13870}
13871
Bram Moolenaar33570922005-01-25 22:26:29 +000013872static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013873
13874/*
13875 * Turn a dict into a list:
13876 * "what" == 0: list of keys
13877 * "what" == 1: list of values
13878 * "what" == 2: list of items
13879 */
13880 static void
13881dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013882 typval_T *argvars;
13883 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013884 int what;
13885{
Bram Moolenaar33570922005-01-25 22:26:29 +000013886 list_T *l2;
13887 dictitem_T *di;
13888 hashitem_T *hi;
13889 listitem_T *li;
13890 listitem_T *li2;
13891 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013892 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013893
Bram Moolenaar8c711452005-01-14 21:53:12 +000013894 if (argvars[0].v_type != VAR_DICT)
13895 {
13896 EMSG(_(e_dictreq));
13897 return;
13898 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013899 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013900 return;
13901
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013902 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013903 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013904
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013905 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013906 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013907 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013908 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013909 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013910 --todo;
13911 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013912
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013913 li = listitem_alloc();
13914 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013915 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013916 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013917
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013918 if (what == 0)
13919 {
13920 /* keys() */
13921 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013922 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013923 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13924 }
13925 else if (what == 1)
13926 {
13927 /* values() */
13928 copy_tv(&di->di_tv, &li->li_tv);
13929 }
13930 else
13931 {
13932 /* items() */
13933 l2 = list_alloc();
13934 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013935 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013936 li->li_tv.vval.v_list = l2;
13937 if (l2 == NULL)
13938 break;
13939 ++l2->lv_refcount;
13940
13941 li2 = listitem_alloc();
13942 if (li2 == NULL)
13943 break;
13944 list_append(l2, li2);
13945 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013946 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013947 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13948
13949 li2 = listitem_alloc();
13950 if (li2 == NULL)
13951 break;
13952 list_append(l2, li2);
13953 copy_tv(&di->di_tv, &li2->li_tv);
13954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013955 }
13956 }
13957}
13958
13959/*
13960 * "items(dict)" function
13961 */
13962 static void
13963f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013964 typval_T *argvars;
13965 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013966{
13967 dict_list(argvars, rettv, 2);
13968}
13969
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013971 * "join()" function
13972 */
13973 static void
13974f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013975 typval_T *argvars;
13976 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013977{
13978 garray_T ga;
13979 char_u *sep;
13980
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013981 if (argvars[0].v_type != VAR_LIST)
13982 {
13983 EMSG(_(e_listreq));
13984 return;
13985 }
13986 if (argvars[0].vval.v_list == NULL)
13987 return;
13988 if (argvars[1].v_type == VAR_UNKNOWN)
13989 sep = (char_u *)" ";
13990 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013992
13993 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994
13995 if (sep != NULL)
13996 {
13997 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013998 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013999 ga_append(&ga, NUL);
14000 rettv->vval.v_string = (char_u *)ga.ga_data;
14001 }
14002 else
14003 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014004}
14005
14006/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014007 * "keys()" function
14008 */
14009 static void
14010f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014011 typval_T *argvars;
14012 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014013{
14014 dict_list(argvars, rettv, 0);
14015}
14016
14017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 * "last_buffer_nr()" function.
14019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014022 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024{
14025 int n = 0;
14026 buf_T *buf;
14027
14028 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14029 if (n < buf->b_fnum)
14030 n = buf->b_fnum;
14031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014033}
14034
14035/*
14036 * "len()" function
14037 */
14038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014040 typval_T *argvars;
14041 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014042{
14043 switch (argvars[0].v_type)
14044 {
14045 case VAR_STRING:
14046 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_number = (varnumber_T)STRLEN(
14048 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014049 break;
14050 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014051 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014052 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014053 case VAR_DICT:
14054 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14055 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014056 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014057 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014058 break;
14059 }
14060}
14061
Bram Moolenaar33570922005-01-25 22:26:29 +000014062static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014063
14064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014066 typval_T *argvars;
14067 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014068 int type;
14069{
14070#ifdef FEAT_LIBCALL
14071 char_u *string_in;
14072 char_u **string_result;
14073 int nr_result;
14074#endif
14075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014076 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014077 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014079
14080 if (check_restricted() || check_secure())
14081 return;
14082
14083#ifdef FEAT_LIBCALL
14084 /* The first two args must be strings, otherwise its meaningless */
14085 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14086 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014087 string_in = NULL;
14088 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014089 string_in = argvars[2].vval.v_string;
14090 if (type == VAR_NUMBER)
14091 string_result = NULL;
14092 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014093 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014094 if (mch_libcall(argvars[0].vval.v_string,
14095 argvars[1].vval.v_string,
14096 string_in,
14097 argvars[2].vval.v_number,
14098 string_result,
14099 &nr_result) == OK
14100 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014101 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014102 }
14103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104}
14105
14106/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107 * "libcall()" function
14108 */
14109 static void
14110f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 typval_T *argvars;
14112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014113{
14114 libcall_common(argvars, rettv, VAR_STRING);
14115}
14116
14117/*
14118 * "libcallnr()" function
14119 */
14120 static void
14121f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 typval_T *argvars;
14123 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014124{
14125 libcall_common(argvars, rettv, VAR_NUMBER);
14126}
14127
14128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 * "line(string)" function
14130 */
14131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014133 typval_T *argvars;
14134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135{
14136 linenr_T lnum = 0;
14137 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014138 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014140 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 if (fp != NULL)
14142 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014143 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144}
14145
14146/*
14147 * "line2byte(lnum)" function
14148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014151 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153{
14154#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156#else
14157 linenr_T lnum;
14158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14164 if (rettv->vval.v_number >= 0)
14165 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166#endif
14167}
14168
14169/*
14170 * "lispindent(lnum)" function
14171 */
14172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014173f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014174 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176{
14177#ifdef FEAT_LISP
14178 pos_T pos;
14179 linenr_T lnum;
14180
14181 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14184 {
14185 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014186 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 curwin->w_cursor = pos;
14188 }
14189 else
14190#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192}
14193
14194/*
14195 * "localtime()" function
14196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014198f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014202 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203}
14204
Bram Moolenaar33570922005-01-25 22:26:29 +000014205static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206
14207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014209 typval_T *argvars;
14210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 int exact;
14212{
14213 char_u *keys;
14214 char_u *which;
14215 char_u buf[NUMBUFLEN];
14216 char_u *keys_buf = NULL;
14217 char_u *rhs;
14218 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014219 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014220 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014221 mapblock_T *mp;
14222 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223
14224 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014225 rettv->v_type = VAR_STRING;
14226 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 if (*keys == NUL)
14230 return;
14231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014232 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014235 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014236 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014237 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014238 if (argvars[3].v_type != VAR_UNKNOWN)
14239 get_dict = get_tv_number(&argvars[3]);
14240 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 else
14243 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014244 if (which == NULL)
14245 return;
14246
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 mode = get_map_mode(&which, 0);
14248
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014249 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014250 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014252
14253 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014255 /* Return a string. */
14256 if (rhs != NULL)
14257 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258
Bram Moolenaarbd743252010-10-20 21:23:33 +020014259 }
14260 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14261 {
14262 /* Return a dictionary. */
14263 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14264 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14265 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266
Bram Moolenaarbd743252010-10-20 21:23:33 +020014267 dict_add_nr_str(dict, "lhs", 0L, lhs);
14268 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14269 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14270 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14271 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14272 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14273 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014274 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014275 dict_add_nr_str(dict, "mode", 0L, mapmode);
14276
14277 vim_free(lhs);
14278 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 }
14280}
14281
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014282#ifdef FEAT_FLOAT
14283/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014284 * "log()" function
14285 */
14286 static void
14287f_log(argvars, rettv)
14288 typval_T *argvars;
14289 typval_T *rettv;
14290{
14291 float_T f;
14292
14293 rettv->v_type = VAR_FLOAT;
14294 if (get_float_arg(argvars, &f) == OK)
14295 rettv->vval.v_float = log(f);
14296 else
14297 rettv->vval.v_float = 0.0;
14298}
14299
14300/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014301 * "log10()" function
14302 */
14303 static void
14304f_log10(argvars, rettv)
14305 typval_T *argvars;
14306 typval_T *rettv;
14307{
14308 float_T f;
14309
14310 rettv->v_type = VAR_FLOAT;
14311 if (get_float_arg(argvars, &f) == OK)
14312 rettv->vval.v_float = log10(f);
14313 else
14314 rettv->vval.v_float = 0.0;
14315}
14316#endif
14317
Bram Moolenaar1dced572012-04-05 16:54:08 +020014318#ifdef FEAT_LUA
14319/*
14320 * "luaeval()" function
14321 */
14322 static void
14323f_luaeval(argvars, rettv)
14324 typval_T *argvars;
14325 typval_T *rettv;
14326{
14327 char_u *str;
14328 char_u buf[NUMBUFLEN];
14329
14330 str = get_tv_string_buf(&argvars[0], buf);
14331 do_luaeval(str, argvars + 1, rettv);
14332}
14333#endif
14334
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014336 * "map()" function
14337 */
14338 static void
14339f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014340 typval_T *argvars;
14341 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014342{
14343 filter_map(argvars, rettv, TRUE);
14344}
14345
14346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 */
14349 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014350f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014351 typval_T *argvars;
14352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014354 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355}
14356
14357/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014358 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 */
14360 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014361f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014362 typval_T *argvars;
14363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014365 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366}
14367
Bram Moolenaar33570922005-01-25 22:26:29 +000014368static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369
14370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014372 typval_T *argvars;
14373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 int type;
14375{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014376 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014377 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014378 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379 char_u *pat;
14380 regmatch_T regmatch;
14381 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014382 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 char_u *save_cpo;
14384 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014385 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014386 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014387 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 list_T *l = NULL;
14389 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014390 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014391 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392
14393 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14394 save_cpo = p_cpo;
14395 p_cpo = (char_u *)"";
14396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014397 rettv->vval.v_number = -1;
14398 if (type == 3)
14399 {
14400 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014401 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014402 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014403 }
14404 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406 rettv->v_type = VAR_STRING;
14407 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014410 if (argvars[0].v_type == VAR_LIST)
14411 {
14412 if ((l = argvars[0].vval.v_list) == NULL)
14413 goto theend;
14414 li = l->lv_first;
14415 }
14416 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014417 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014418 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014419 len = (long)STRLEN(str);
14420 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14423 if (pat == NULL)
14424 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014425
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014426 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014428 int error = FALSE;
14429
14430 start = get_tv_number_chk(&argvars[2], &error);
14431 if (error)
14432 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014433 if (l != NULL)
14434 {
14435 li = list_find(l, start);
14436 if (li == NULL)
14437 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014438 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014439 }
14440 else
14441 {
14442 if (start < 0)
14443 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014444 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014445 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014446 /* When "count" argument is there ignore matches before "start",
14447 * otherwise skip part of the string. Differs when pattern is "^"
14448 * or "\<". */
14449 if (argvars[3].v_type != VAR_UNKNOWN)
14450 startcol = start;
14451 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014452 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014453 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014454 len -= start;
14455 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014456 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014457
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014458 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459 nth = get_tv_number_chk(&argvars[3], &error);
14460 if (error)
14461 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 }
14463
14464 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14465 if (regmatch.regprog != NULL)
14466 {
14467 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014468
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014469 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014470 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014471 if (l != NULL)
14472 {
14473 if (li == NULL)
14474 {
14475 match = FALSE;
14476 break;
14477 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014478 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014479 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014480 if (str == NULL)
14481 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014482 }
14483
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014484 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014485
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014486 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014487 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014488 if (l == NULL && !match)
14489 break;
14490
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014491 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014492 if (l != NULL)
14493 {
14494 li = li->li_next;
14495 ++idx;
14496 }
14497 else
14498 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014499#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014500 startcol = (colnr_T)(regmatch.startp[0]
14501 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014502#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014503 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014504#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014505 if (startcol > (colnr_T)len
14506 || str + startcol <= regmatch.startp[0])
14507 {
14508 match = FALSE;
14509 break;
14510 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014511 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014512 }
14513
14514 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014516 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014518 int i;
14519
14520 /* return list with matched string and submatches */
14521 for (i = 0; i < NSUBEXP; ++i)
14522 {
14523 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014524 {
14525 if (list_append_string(rettv->vval.v_list,
14526 (char_u *)"", 0) == FAIL)
14527 break;
14528 }
14529 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014530 regmatch.startp[i],
14531 (int)(regmatch.endp[i] - regmatch.startp[i]))
14532 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014533 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014534 }
14535 }
14536 else if (type == 2)
14537 {
14538 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014539 if (l != NULL)
14540 copy_tv(&li->li_tv, rettv);
14541 else
14542 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014544 }
14545 else if (l != NULL)
14546 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 else
14548 {
14549 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 (varnumber_T)(regmatch.startp[0] - str);
14552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014553 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014555 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 }
14557 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014558 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 }
14560
14561theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014562 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 p_cpo = save_cpo;
14564}
14565
14566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 * "match()" function
14568 */
14569 static void
14570f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014571 typval_T *argvars;
14572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573{
14574 find_some_match(argvars, rettv, 1);
14575}
14576
14577/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014578 * "matchadd()" function
14579 */
14580 static void
14581f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014582 typval_T *argvars UNUSED;
14583 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014584{
14585#ifdef FEAT_SEARCH_EXTRA
14586 char_u buf[NUMBUFLEN];
14587 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14588 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14589 int prio = 10; /* default priority */
14590 int id = -1;
14591 int error = FALSE;
14592
14593 rettv->vval.v_number = -1;
14594
14595 if (grp == NULL || pat == NULL)
14596 return;
14597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014598 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014599 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014600 if (argvars[3].v_type != VAR_UNKNOWN)
14601 id = get_tv_number_chk(&argvars[3], &error);
14602 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014603 if (error == TRUE)
14604 return;
14605 if (id >= 1 && id <= 3)
14606 {
14607 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14608 return;
14609 }
14610
Bram Moolenaarb3414592014-06-17 17:48:32 +020014611 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14612#endif
14613}
14614
14615/*
14616 * "matchaddpos()" function
14617 */
14618 static void
14619f_matchaddpos(argvars, rettv)
14620 typval_T *argvars UNUSED;
14621 typval_T *rettv UNUSED;
14622{
14623#ifdef FEAT_SEARCH_EXTRA
14624 char_u buf[NUMBUFLEN];
14625 char_u *group;
14626 int prio = 10;
14627 int id = -1;
14628 int error = FALSE;
14629 list_T *l;
14630
14631 rettv->vval.v_number = -1;
14632
14633 group = get_tv_string_buf_chk(&argvars[0], buf);
14634 if (group == NULL)
14635 return;
14636
14637 if (argvars[1].v_type != VAR_LIST)
14638 {
14639 EMSG2(_(e_listarg), "matchaddpos()");
14640 return;
14641 }
14642 l = argvars[1].vval.v_list;
14643 if (l == NULL)
14644 return;
14645
14646 if (argvars[2].v_type != VAR_UNKNOWN)
14647 {
14648 prio = get_tv_number_chk(&argvars[2], &error);
14649 if (argvars[3].v_type != VAR_UNKNOWN)
14650 id = get_tv_number_chk(&argvars[3], &error);
14651 }
14652 if (error == TRUE)
14653 return;
14654
14655 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14656 if (id == 1 || id == 2)
14657 {
14658 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14659 return;
14660 }
14661
14662 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014663#endif
14664}
14665
14666/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014667 * "matcharg()" function
14668 */
14669 static void
14670f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014671 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014672 typval_T *rettv;
14673{
14674 if (rettv_list_alloc(rettv) == OK)
14675 {
14676#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014677 int id = get_tv_number(&argvars[0]);
14678 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014679
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014680 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014681 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014682 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14683 {
14684 list_append_string(rettv->vval.v_list,
14685 syn_id2name(m->hlg_id), -1);
14686 list_append_string(rettv->vval.v_list, m->pattern, -1);
14687 }
14688 else
14689 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014690 list_append_string(rettv->vval.v_list, NULL, -1);
14691 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014692 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014693 }
14694#endif
14695 }
14696}
14697
14698/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014699 * "matchdelete()" function
14700 */
14701 static void
14702f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014703 typval_T *argvars UNUSED;
14704 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014705{
14706#ifdef FEAT_SEARCH_EXTRA
14707 rettv->vval.v_number = match_delete(curwin,
14708 (int)get_tv_number(&argvars[0]), TRUE);
14709#endif
14710}
14711
14712/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 * "matchend()" function
14714 */
14715 static void
14716f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014717 typval_T *argvars;
14718 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014719{
14720 find_some_match(argvars, rettv, 0);
14721}
14722
14723/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014724 * "matchlist()" function
14725 */
14726 static void
14727f_matchlist(argvars, rettv)
14728 typval_T *argvars;
14729 typval_T *rettv;
14730{
14731 find_some_match(argvars, rettv, 3);
14732}
14733
14734/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014735 * "matchstr()" function
14736 */
14737 static void
14738f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014739 typval_T *argvars;
14740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014741{
14742 find_some_match(argvars, rettv, 2);
14743}
14744
Bram Moolenaar33570922005-01-25 22:26:29 +000014745static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014746
14747 static void
14748max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014749 typval_T *argvars;
14750 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014751 int domax;
14752{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014753 long n = 0;
14754 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014755 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014756
14757 if (argvars[0].v_type == VAR_LIST)
14758 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014759 list_T *l;
14760 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014761
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014762 l = argvars[0].vval.v_list;
14763 if (l != NULL)
14764 {
14765 li = l->lv_first;
14766 if (li != NULL)
14767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014768 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014769 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014770 {
14771 li = li->li_next;
14772 if (li == NULL)
14773 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014775 if (domax ? i > n : i < n)
14776 n = i;
14777 }
14778 }
14779 }
14780 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014781 else if (argvars[0].v_type == VAR_DICT)
14782 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014783 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014784 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014785 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014786 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014787
14788 d = argvars[0].vval.v_dict;
14789 if (d != NULL)
14790 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014791 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014792 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014793 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014794 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014795 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014796 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014797 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014798 if (first)
14799 {
14800 n = i;
14801 first = FALSE;
14802 }
14803 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014804 n = i;
14805 }
14806 }
14807 }
14808 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014809 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014810 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014811 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014812}
14813
14814/*
14815 * "max()" function
14816 */
14817 static void
14818f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014819 typval_T *argvars;
14820 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014821{
14822 max_min(argvars, rettv, TRUE);
14823}
14824
14825/*
14826 * "min()" function
14827 */
14828 static void
14829f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014830 typval_T *argvars;
14831 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014832{
14833 max_min(argvars, rettv, FALSE);
14834}
14835
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014836static int mkdir_recurse __ARGS((char_u *dir, int prot));
14837
14838/*
14839 * Create the directory in which "dir" is located, and higher levels when
14840 * needed.
14841 */
14842 static int
14843mkdir_recurse(dir, prot)
14844 char_u *dir;
14845 int prot;
14846{
14847 char_u *p;
14848 char_u *updir;
14849 int r = FAIL;
14850
14851 /* Get end of directory name in "dir".
14852 * We're done when it's "/" or "c:/". */
14853 p = gettail_sep(dir);
14854 if (p <= get_past_head(dir))
14855 return OK;
14856
14857 /* If the directory exists we're done. Otherwise: create it.*/
14858 updir = vim_strnsave(dir, (int)(p - dir));
14859 if (updir == NULL)
14860 return FAIL;
14861 if (mch_isdir(updir))
14862 r = OK;
14863 else if (mkdir_recurse(updir, prot) == OK)
14864 r = vim_mkdir_emsg(updir, prot);
14865 vim_free(updir);
14866 return r;
14867}
14868
14869#ifdef vim_mkdir
14870/*
14871 * "mkdir()" function
14872 */
14873 static void
14874f_mkdir(argvars, rettv)
14875 typval_T *argvars;
14876 typval_T *rettv;
14877{
14878 char_u *dir;
14879 char_u buf[NUMBUFLEN];
14880 int prot = 0755;
14881
14882 rettv->vval.v_number = FAIL;
14883 if (check_restricted() || check_secure())
14884 return;
14885
14886 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014887 if (*dir == NUL)
14888 rettv->vval.v_number = FAIL;
14889 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014890 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014891 if (*gettail(dir) == NUL)
14892 /* remove trailing slashes */
14893 *gettail_sep(dir) = NUL;
14894
14895 if (argvars[1].v_type != VAR_UNKNOWN)
14896 {
14897 if (argvars[2].v_type != VAR_UNKNOWN)
14898 prot = get_tv_number_chk(&argvars[2], NULL);
14899 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14900 mkdir_recurse(dir, prot);
14901 }
14902 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014903 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014904}
14905#endif
14906
Bram Moolenaar0d660222005-01-07 21:51:51 +000014907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 * "mode()" function
14909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014911f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014912 typval_T *argvars;
14913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014915 char_u buf[3];
14916
14917 buf[1] = NUL;
14918 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920 if (VIsual_active)
14921 {
14922 if (VIsual_select)
14923 buf[0] = VIsual_mode + 's' - 'v';
14924 else
14925 buf[0] = VIsual_mode;
14926 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014927 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014928 || State == CONFIRM)
14929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014931 if (State == ASKMORE)
14932 buf[1] = 'm';
14933 else if (State == CONFIRM)
14934 buf[1] = '?';
14935 }
14936 else if (State == EXTERNCMD)
14937 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938 else if (State & INSERT)
14939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014940#ifdef FEAT_VREPLACE
14941 if (State & VREPLACE_FLAG)
14942 {
14943 buf[0] = 'R';
14944 buf[1] = 'v';
14945 }
14946 else
14947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948 if (State & REPLACE_FLAG)
14949 buf[0] = 'R';
14950 else
14951 buf[0] = 'i';
14952 }
14953 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014956 if (exmode_active)
14957 buf[1] = 'v';
14958 }
14959 else if (exmode_active)
14960 {
14961 buf[0] = 'c';
14962 buf[1] = 'e';
14963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014967 if (finish_op)
14968 buf[1] = 'o';
14969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014971 /* Clear out the minor mode when the argument is not a non-zero number or
14972 * non-empty string. */
14973 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014974 buf[1] = NUL;
14975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014976 rettv->vval.v_string = vim_strsave(buf);
14977 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978}
14979
Bram Moolenaar429fa852013-04-15 12:27:36 +020014980#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014981/*
14982 * "mzeval()" function
14983 */
14984 static void
14985f_mzeval(argvars, rettv)
14986 typval_T *argvars;
14987 typval_T *rettv;
14988{
14989 char_u *str;
14990 char_u buf[NUMBUFLEN];
14991
14992 str = get_tv_string_buf(&argvars[0], buf);
14993 do_mzeval(str, rettv);
14994}
Bram Moolenaar75676462013-01-30 14:55:42 +010014995
14996 void
14997mzscheme_call_vim(name, args, rettv)
14998 char_u *name;
14999 typval_T *args;
15000 typval_T *rettv;
15001{
15002 typval_T argvars[3];
15003
15004 argvars[0].v_type = VAR_STRING;
15005 argvars[0].vval.v_string = name;
15006 copy_tv(args, &argvars[1]);
15007 argvars[2].v_type = VAR_UNKNOWN;
15008 f_call(argvars, rettv);
15009 clear_tv(&argvars[1]);
15010}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015011#endif
15012
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015014 * "nextnonblank()" function
15015 */
15016 static void
15017f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015018 typval_T *argvars;
15019 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015020{
15021 linenr_T lnum;
15022
15023 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015025 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015026 {
15027 lnum = 0;
15028 break;
15029 }
15030 if (*skipwhite(ml_get(lnum)) != NUL)
15031 break;
15032 }
15033 rettv->vval.v_number = lnum;
15034}
15035
15036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 * "nr2char()" function
15038 */
15039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015041 typval_T *argvars;
15042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043{
15044 char_u buf[NUMBUFLEN];
15045
15046#ifdef FEAT_MBYTE
15047 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015048 {
15049 int utf8 = 0;
15050
15051 if (argvars[1].v_type != VAR_UNKNOWN)
15052 utf8 = get_tv_number_chk(&argvars[1], NULL);
15053 if (utf8)
15054 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15055 else
15056 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 else
15059#endif
15060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015061 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062 buf[1] = NUL;
15063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015064 rettv->v_type = VAR_STRING;
15065 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015066}
15067
15068/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015069 * "or(expr, expr)" function
15070 */
15071 static void
15072f_or(argvars, rettv)
15073 typval_T *argvars;
15074 typval_T *rettv;
15075{
15076 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15077 | get_tv_number_chk(&argvars[1], NULL);
15078}
15079
15080/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015081 * "pathshorten()" function
15082 */
15083 static void
15084f_pathshorten(argvars, rettv)
15085 typval_T *argvars;
15086 typval_T *rettv;
15087{
15088 char_u *p;
15089
15090 rettv->v_type = VAR_STRING;
15091 p = get_tv_string_chk(&argvars[0]);
15092 if (p == NULL)
15093 rettv->vval.v_string = NULL;
15094 else
15095 {
15096 p = vim_strsave(p);
15097 rettv->vval.v_string = p;
15098 if (p != NULL)
15099 shorten_dir(p);
15100 }
15101}
15102
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015103#ifdef FEAT_FLOAT
15104/*
15105 * "pow()" function
15106 */
15107 static void
15108f_pow(argvars, rettv)
15109 typval_T *argvars;
15110 typval_T *rettv;
15111{
15112 float_T fx, fy;
15113
15114 rettv->v_type = VAR_FLOAT;
15115 if (get_float_arg(argvars, &fx) == OK
15116 && get_float_arg(&argvars[1], &fy) == OK)
15117 rettv->vval.v_float = pow(fx, fy);
15118 else
15119 rettv->vval.v_float = 0.0;
15120}
15121#endif
15122
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015123/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124 * "prevnonblank()" function
15125 */
15126 static void
15127f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015128 typval_T *argvars;
15129 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130{
15131 linenr_T lnum;
15132
15133 lnum = get_tv_lnum(argvars);
15134 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15135 lnum = 0;
15136 else
15137 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15138 --lnum;
15139 rettv->vval.v_number = lnum;
15140}
15141
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015142#ifdef HAVE_STDARG_H
15143/* This dummy va_list is here because:
15144 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15145 * - locally in the function results in a "used before set" warning
15146 * - using va_start() to initialize it gives "function with fixed args" error */
15147static va_list ap;
15148#endif
15149
Bram Moolenaar8c711452005-01-14 21:53:12 +000015150/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015151 * "printf()" function
15152 */
15153 static void
15154f_printf(argvars, rettv)
15155 typval_T *argvars;
15156 typval_T *rettv;
15157{
15158 rettv->v_type = VAR_STRING;
15159 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015160#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015161 {
15162 char_u buf[NUMBUFLEN];
15163 int len;
15164 char_u *s;
15165 int saved_did_emsg = did_emsg;
15166 char *fmt;
15167
15168 /* Get the required length, allocate the buffer and do it for real. */
15169 did_emsg = FALSE;
15170 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015171 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015172 if (!did_emsg)
15173 {
15174 s = alloc(len + 1);
15175 if (s != NULL)
15176 {
15177 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015178 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015179 }
15180 }
15181 did_emsg |= saved_did_emsg;
15182 }
15183#endif
15184}
15185
15186/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015187 * "pumvisible()" function
15188 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015189 static void
15190f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015191 typval_T *argvars UNUSED;
15192 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015193{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015194#ifdef FEAT_INS_EXPAND
15195 if (pum_visible())
15196 rettv->vval.v_number = 1;
15197#endif
15198}
15199
Bram Moolenaardb913952012-06-29 12:54:53 +020015200#ifdef FEAT_PYTHON3
15201/*
15202 * "py3eval()" function
15203 */
15204 static void
15205f_py3eval(argvars, rettv)
15206 typval_T *argvars;
15207 typval_T *rettv;
15208{
15209 char_u *str;
15210 char_u buf[NUMBUFLEN];
15211
15212 str = get_tv_string_buf(&argvars[0], buf);
15213 do_py3eval(str, rettv);
15214}
15215#endif
15216
15217#ifdef FEAT_PYTHON
15218/*
15219 * "pyeval()" function
15220 */
15221 static void
15222f_pyeval(argvars, rettv)
15223 typval_T *argvars;
15224 typval_T *rettv;
15225{
15226 char_u *str;
15227 char_u buf[NUMBUFLEN];
15228
15229 str = get_tv_string_buf(&argvars[0], buf);
15230 do_pyeval(str, rettv);
15231}
15232#endif
15233
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015234/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015235 * "range()" function
15236 */
15237 static void
15238f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015239 typval_T *argvars;
15240 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015241{
15242 long start;
15243 long end;
15244 long stride = 1;
15245 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015246 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015248 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015249 if (argvars[1].v_type == VAR_UNKNOWN)
15250 {
15251 end = start - 1;
15252 start = 0;
15253 }
15254 else
15255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015256 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015257 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015258 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015259 }
15260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 if (error)
15262 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015263 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015264 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015265 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015266 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015267 else
15268 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015269 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015270 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015271 if (list_append_number(rettv->vval.v_list,
15272 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015273 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015274 }
15275}
15276
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015277/*
15278 * "readfile()" function
15279 */
15280 static void
15281f_readfile(argvars, rettv)
15282 typval_T *argvars;
15283 typval_T *rettv;
15284{
15285 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015286 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015287 char_u *fname;
15288 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015289 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15290 int io_size = sizeof(buf);
15291 int readlen; /* size of last fread() */
15292 char_u *prev = NULL; /* previously read bytes, if any */
15293 long prevlen = 0; /* length of data in prev */
15294 long prevsize = 0; /* size of prev buffer */
15295 long maxline = MAXLNUM;
15296 long cnt = 0;
15297 char_u *p; /* position in buf */
15298 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015299
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015300 if (argvars[1].v_type != VAR_UNKNOWN)
15301 {
15302 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15303 binary = TRUE;
15304 if (argvars[2].v_type != VAR_UNKNOWN)
15305 maxline = get_tv_number(&argvars[2]);
15306 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015308 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015309 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015310
15311 /* Always open the file in binary mode, library functions have a mind of
15312 * their own about CR-LF conversion. */
15313 fname = get_tv_string(&argvars[0]);
15314 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15315 {
15316 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15317 return;
15318 }
15319
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015320 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015321 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015322 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015323
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015324 /* This for loop processes what was read, but is also entered at end
15325 * of file so that either:
15326 * - an incomplete line gets written
15327 * - a "binary" file gets an empty line at the end if it ends in a
15328 * newline. */
15329 for (p = buf, start = buf;
15330 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15331 ++p)
15332 {
15333 if (*p == '\n' || readlen <= 0)
15334 {
15335 listitem_T *li;
15336 char_u *s = NULL;
15337 long_u len = p - start;
15338
15339 /* Finished a line. Remove CRs before NL. */
15340 if (readlen > 0 && !binary)
15341 {
15342 while (len > 0 && start[len - 1] == '\r')
15343 --len;
15344 /* removal may cross back to the "prev" string */
15345 if (len == 0)
15346 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15347 --prevlen;
15348 }
15349 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015350 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 else
15352 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015353 /* Change "prev" buffer to be the right size. This way
15354 * the bytes are only copied once, and very long lines are
15355 * allocated only once. */
15356 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015357 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015358 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015359 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015360 prev = NULL; /* the list will own the string */
15361 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015362 }
15363 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015364 if (s == NULL)
15365 {
15366 do_outofmem_msg((long_u) prevlen + len + 1);
15367 failed = TRUE;
15368 break;
15369 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015370
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015371 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015372 {
15373 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015374 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015375 break;
15376 }
15377 li->li_tv.v_type = VAR_STRING;
15378 li->li_tv.v_lock = 0;
15379 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015380 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015381
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015382 start = p + 1; /* step over newline */
15383 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015384 break;
15385 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015386 else if (*p == NUL)
15387 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015388#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015389 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15390 * when finding the BF and check the previous two bytes. */
15391 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015392 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015393 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15394 * + 1, these may be in the "prev" string. */
15395 char_u back1 = p >= buf + 1 ? p[-1]
15396 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15397 char_u back2 = p >= buf + 2 ? p[-2]
15398 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15399 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015400
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015401 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015402 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015403 char_u *dest = p - 2;
15404
15405 /* Usually a BOM is at the beginning of a file, and so at
15406 * the beginning of a line; then we can just step over it.
15407 */
15408 if (start == dest)
15409 start = p + 1;
15410 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015411 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015412 /* have to shuffle buf to close gap */
15413 int adjust_prevlen = 0;
15414
15415 if (dest < buf)
15416 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015417 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015418 dest = buf;
15419 }
15420 if (readlen > p - buf + 1)
15421 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15422 readlen -= 3 - adjust_prevlen;
15423 prevlen -= adjust_prevlen;
15424 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015425 }
15426 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015427 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015428#endif
15429 } /* for */
15430
15431 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15432 break;
15433 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015434 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015435 /* There's part of a line in buf, store it in "prev". */
15436 if (p - start + prevlen >= prevsize)
15437 {
15438 /* need bigger "prev" buffer */
15439 char_u *newprev;
15440
15441 /* A common use case is ordinary text files and "prev" gets a
15442 * fragment of a line, so the first allocation is made
15443 * small, to avoid repeatedly 'allocing' large and
15444 * 'reallocing' small. */
15445 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015446 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015447 else
15448 {
15449 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015450 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015451 prevsize = grow50pc > growmin ? grow50pc : growmin;
15452 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015453 newprev = prev == NULL ? alloc(prevsize)
15454 : vim_realloc(prev, prevsize);
15455 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 {
15457 do_outofmem_msg((long_u)prevsize);
15458 failed = TRUE;
15459 break;
15460 }
15461 prev = newprev;
15462 }
15463 /* Add the line part to end of "prev". */
15464 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015465 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015466 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015467 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015468
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015469 /*
15470 * For a negative line count use only the lines at the end of the file,
15471 * free the rest.
15472 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015473 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015474 while (cnt > -maxline)
15475 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015476 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015477 --cnt;
15478 }
15479
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015480 if (failed)
15481 {
15482 list_free(rettv->vval.v_list, TRUE);
15483 /* readfile doc says an empty list is returned on error */
15484 rettv->vval.v_list = list_alloc();
15485 }
15486
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015487 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015488 fclose(fd);
15489}
15490
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015491#if defined(FEAT_RELTIME)
15492static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15493
15494/*
15495 * Convert a List to proftime_T.
15496 * Return FAIL when there is something wrong.
15497 */
15498 static int
15499list2proftime(arg, tm)
15500 typval_T *arg;
15501 proftime_T *tm;
15502{
15503 long n1, n2;
15504 int error = FALSE;
15505
15506 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15507 || arg->vval.v_list->lv_len != 2)
15508 return FAIL;
15509 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15510 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15511# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015512 tm->HighPart = n1;
15513 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015514# else
15515 tm->tv_sec = n1;
15516 tm->tv_usec = n2;
15517# endif
15518 return error ? FAIL : OK;
15519}
15520#endif /* FEAT_RELTIME */
15521
15522/*
15523 * "reltime()" function
15524 */
15525 static void
15526f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015527 typval_T *argvars UNUSED;
15528 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015529{
15530#ifdef FEAT_RELTIME
15531 proftime_T res;
15532 proftime_T start;
15533
15534 if (argvars[0].v_type == VAR_UNKNOWN)
15535 {
15536 /* No arguments: get current time. */
15537 profile_start(&res);
15538 }
15539 else if (argvars[1].v_type == VAR_UNKNOWN)
15540 {
15541 if (list2proftime(&argvars[0], &res) == FAIL)
15542 return;
15543 profile_end(&res);
15544 }
15545 else
15546 {
15547 /* Two arguments: compute the difference. */
15548 if (list2proftime(&argvars[0], &start) == FAIL
15549 || list2proftime(&argvars[1], &res) == FAIL)
15550 return;
15551 profile_sub(&res, &start);
15552 }
15553
15554 if (rettv_list_alloc(rettv) == OK)
15555 {
15556 long n1, n2;
15557
15558# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015559 n1 = res.HighPart;
15560 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015561# else
15562 n1 = res.tv_sec;
15563 n2 = res.tv_usec;
15564# endif
15565 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15566 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15567 }
15568#endif
15569}
15570
15571/*
15572 * "reltimestr()" function
15573 */
15574 static void
15575f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015576 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015577 typval_T *rettv;
15578{
15579#ifdef FEAT_RELTIME
15580 proftime_T tm;
15581#endif
15582
15583 rettv->v_type = VAR_STRING;
15584 rettv->vval.v_string = NULL;
15585#ifdef FEAT_RELTIME
15586 if (list2proftime(&argvars[0], &tm) == OK)
15587 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15588#endif
15589}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015590
Bram Moolenaar0d660222005-01-07 21:51:51 +000015591#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15592static void make_connection __ARGS((void));
15593static int check_connection __ARGS((void));
15594
15595 static void
15596make_connection()
15597{
15598 if (X_DISPLAY == NULL
15599# ifdef FEAT_GUI
15600 && !gui.in_use
15601# endif
15602 )
15603 {
15604 x_force_connect = TRUE;
15605 setup_term_clip();
15606 x_force_connect = FALSE;
15607 }
15608}
15609
15610 static int
15611check_connection()
15612{
15613 make_connection();
15614 if (X_DISPLAY == NULL)
15615 {
15616 EMSG(_("E240: No connection to Vim server"));
15617 return FAIL;
15618 }
15619 return OK;
15620}
15621#endif
15622
15623#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015624static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015625
15626 static void
15627remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015628 typval_T *argvars;
15629 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015630 int expr;
15631{
15632 char_u *server_name;
15633 char_u *keys;
15634 char_u *r = NULL;
15635 char_u buf[NUMBUFLEN];
15636# ifdef WIN32
15637 HWND w;
15638# else
15639 Window w;
15640# endif
15641
15642 if (check_restricted() || check_secure())
15643 return;
15644
15645# ifdef FEAT_X11
15646 if (check_connection() == FAIL)
15647 return;
15648# endif
15649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015650 server_name = get_tv_string_chk(&argvars[0]);
15651 if (server_name == NULL)
15652 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015653 keys = get_tv_string_buf(&argvars[1], buf);
15654# ifdef WIN32
15655 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15656# else
15657 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15658 < 0)
15659# endif
15660 {
15661 if (r != NULL)
15662 EMSG(r); /* sending worked but evaluation failed */
15663 else
15664 EMSG2(_("E241: Unable to send to %s"), server_name);
15665 return;
15666 }
15667
15668 rettv->vval.v_string = r;
15669
15670 if (argvars[2].v_type != VAR_UNKNOWN)
15671 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015672 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015673 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015674 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015675
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015676 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015677 v.di_tv.v_type = VAR_STRING;
15678 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015679 idvar = get_tv_string_chk(&argvars[2]);
15680 if (idvar != NULL)
15681 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015682 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015683 }
15684}
15685#endif
15686
15687/*
15688 * "remote_expr()" function
15689 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690 static void
15691f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015692 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694{
15695 rettv->v_type = VAR_STRING;
15696 rettv->vval.v_string = NULL;
15697#ifdef FEAT_CLIENTSERVER
15698 remote_common(argvars, rettv, TRUE);
15699#endif
15700}
15701
15702/*
15703 * "remote_foreground()" function
15704 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015705 static void
15706f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015707 typval_T *argvars UNUSED;
15708 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015709{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015710#ifdef FEAT_CLIENTSERVER
15711# ifdef WIN32
15712 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015713 {
15714 char_u *server_name = get_tv_string_chk(&argvars[0]);
15715
15716 if (server_name != NULL)
15717 serverForeground(server_name);
15718 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015719# else
15720 /* Send a foreground() expression to the server. */
15721 argvars[1].v_type = VAR_STRING;
15722 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15723 argvars[2].v_type = VAR_UNKNOWN;
15724 remote_common(argvars, rettv, TRUE);
15725 vim_free(argvars[1].vval.v_string);
15726# endif
15727#endif
15728}
15729
Bram Moolenaar0d660222005-01-07 21:51:51 +000015730 static void
15731f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015733 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734{
15735#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015736 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015737 char_u *s = NULL;
15738# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015739 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015740# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015741 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015742
15743 if (check_restricted() || check_secure())
15744 {
15745 rettv->vval.v_number = -1;
15746 return;
15747 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015748 serverid = get_tv_string_chk(&argvars[0]);
15749 if (serverid == NULL)
15750 {
15751 rettv->vval.v_number = -1;
15752 return; /* type error; errmsg already given */
15753 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015755 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756 if (n == 0)
15757 rettv->vval.v_number = -1;
15758 else
15759 {
15760 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15761 rettv->vval.v_number = (s != NULL);
15762 }
15763# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015764 if (check_connection() == FAIL)
15765 return;
15766
15767 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015768 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015769# endif
15770
15771 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015773 char_u *retvar;
15774
Bram Moolenaar33570922005-01-25 22:26:29 +000015775 v.di_tv.v_type = VAR_STRING;
15776 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015777 retvar = get_tv_string_chk(&argvars[1]);
15778 if (retvar != NULL)
15779 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015780 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015781 }
15782#else
15783 rettv->vval.v_number = -1;
15784#endif
15785}
15786
Bram Moolenaar0d660222005-01-07 21:51:51 +000015787 static void
15788f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015789 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015790 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015791{
15792 char_u *r = NULL;
15793
15794#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015795 char_u *serverid = get_tv_string_chk(&argvars[0]);
15796
15797 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798 {
15799# ifdef WIN32
15800 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015801 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015802
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015803 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 if (n != 0)
15805 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15806 if (r == NULL)
15807# else
15808 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015809 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015810# endif
15811 EMSG(_("E277: Unable to read a server reply"));
15812 }
15813#endif
15814 rettv->v_type = VAR_STRING;
15815 rettv->vval.v_string = r;
15816}
15817
15818/*
15819 * "remote_send()" function
15820 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821 static void
15822f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015823 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015824 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015825{
15826 rettv->v_type = VAR_STRING;
15827 rettv->vval.v_string = NULL;
15828#ifdef FEAT_CLIENTSERVER
15829 remote_common(argvars, rettv, FALSE);
15830#endif
15831}
15832
15833/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015834 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015835 */
15836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015837f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015838 typval_T *argvars;
15839 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015840{
Bram Moolenaar33570922005-01-25 22:26:29 +000015841 list_T *l;
15842 listitem_T *item, *item2;
15843 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015844 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015845 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015846 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 dict_T *d;
15848 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015849 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015850
Bram Moolenaar8c711452005-01-14 21:53:12 +000015851 if (argvars[0].v_type == VAR_DICT)
15852 {
15853 if (argvars[2].v_type != VAR_UNKNOWN)
15854 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015855 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015856 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 key = get_tv_string_chk(&argvars[1]);
15859 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015861 di = dict_find(d, key, -1);
15862 if (di == NULL)
15863 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015864 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15865 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015866 {
15867 *rettv = di->di_tv;
15868 init_tv(&di->di_tv);
15869 dictitem_remove(d, di);
15870 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015872 }
15873 }
15874 else if (argvars[0].v_type != VAR_LIST)
15875 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015876 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015877 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015879 int error = FALSE;
15880
15881 idx = get_tv_number_chk(&argvars[1], &error);
15882 if (error)
15883 ; /* type error: do nothing, errmsg already given */
15884 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015885 EMSGN(_(e_listidx), idx);
15886 else
15887 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015888 if (argvars[2].v_type == VAR_UNKNOWN)
15889 {
15890 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015891 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015892 *rettv = item->li_tv;
15893 vim_free(item);
15894 }
15895 else
15896 {
15897 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015898 end = get_tv_number_chk(&argvars[2], &error);
15899 if (error)
15900 ; /* type error: do nothing */
15901 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015902 EMSGN(_(e_listidx), end);
15903 else
15904 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015905 int cnt = 0;
15906
15907 for (li = item; li != NULL; li = li->li_next)
15908 {
15909 ++cnt;
15910 if (li == item2)
15911 break;
15912 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015913 if (li == NULL) /* didn't find "item2" after "item" */
15914 EMSG(_(e_invrange));
15915 else
15916 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015917 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015918 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015919 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015920 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015921 l->lv_first = item;
15922 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015923 item->li_prev = NULL;
15924 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015925 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015926 }
15927 }
15928 }
15929 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930 }
15931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932}
15933
15934/*
15935 * "rename({from}, {to})" function
15936 */
15937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015938f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015939 typval_T *argvars;
15940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941{
15942 char_u buf[NUMBUFLEN];
15943
15944 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015945 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015947 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15948 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949}
15950
15951/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015952 * "repeat()" function
15953 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015954 static void
15955f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015956 typval_T *argvars;
15957 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015958{
15959 char_u *p;
15960 int n;
15961 int slen;
15962 int len;
15963 char_u *r;
15964 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015965
15966 n = get_tv_number(&argvars[1]);
15967 if (argvars[0].v_type == VAR_LIST)
15968 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015969 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015970 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015971 if (list_extend(rettv->vval.v_list,
15972 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015973 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015974 }
15975 else
15976 {
15977 p = get_tv_string(&argvars[0]);
15978 rettv->v_type = VAR_STRING;
15979 rettv->vval.v_string = NULL;
15980
15981 slen = (int)STRLEN(p);
15982 len = slen * n;
15983 if (len <= 0)
15984 return;
15985
15986 r = alloc(len + 1);
15987 if (r != NULL)
15988 {
15989 for (i = 0; i < n; i++)
15990 mch_memmove(r + i * slen, p, (size_t)slen);
15991 r[len] = NUL;
15992 }
15993
15994 rettv->vval.v_string = r;
15995 }
15996}
15997
15998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 * "resolve()" function
16000 */
16001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016002f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016003 typval_T *argvars;
16004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005{
16006 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016007#ifdef HAVE_READLINK
16008 char_u *buf = NULL;
16009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016011 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012#ifdef FEAT_SHORTCUT
16013 {
16014 char_u *v = NULL;
16015
16016 v = mch_resolve_shortcut(p);
16017 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016018 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016020 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 }
16022#else
16023# ifdef HAVE_READLINK
16024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 char_u *cpy;
16026 int len;
16027 char_u *remain = NULL;
16028 char_u *q;
16029 int is_relative_to_current = FALSE;
16030 int has_trailing_pathsep = FALSE;
16031 int limit = 100;
16032
16033 p = vim_strsave(p);
16034
16035 if (p[0] == '.' && (vim_ispathsep(p[1])
16036 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16037 is_relative_to_current = TRUE;
16038
16039 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016040 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016043 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045
16046 q = getnextcomp(p);
16047 if (*q != NUL)
16048 {
16049 /* Separate the first path component in "p", and keep the
16050 * remainder (beginning with the path separator). */
16051 remain = vim_strsave(q - 1);
16052 q[-1] = NUL;
16053 }
16054
Bram Moolenaard9462e32011-04-11 21:35:11 +020016055 buf = alloc(MAXPATHL + 1);
16056 if (buf == NULL)
16057 goto fail;
16058
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 for (;;)
16060 {
16061 for (;;)
16062 {
16063 len = readlink((char *)p, (char *)buf, MAXPATHL);
16064 if (len <= 0)
16065 break;
16066 buf[len] = NUL;
16067
16068 if (limit-- == 0)
16069 {
16070 vim_free(p);
16071 vim_free(remain);
16072 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 goto fail;
16075 }
16076
16077 /* Ensure that the result will have a trailing path separator
16078 * if the argument has one. */
16079 if (remain == NULL && has_trailing_pathsep)
16080 add_pathsep(buf);
16081
16082 /* Separate the first path component in the link value and
16083 * concatenate the remainders. */
16084 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16085 if (*q != NUL)
16086 {
16087 if (remain == NULL)
16088 remain = vim_strsave(q - 1);
16089 else
16090 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016091 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092 if (cpy != NULL)
16093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094 vim_free(remain);
16095 remain = cpy;
16096 }
16097 }
16098 q[-1] = NUL;
16099 }
16100
16101 q = gettail(p);
16102 if (q > p && *q == NUL)
16103 {
16104 /* Ignore trailing path separator. */
16105 q[-1] = NUL;
16106 q = gettail(p);
16107 }
16108 if (q > p && !mch_isFullName(buf))
16109 {
16110 /* symlink is relative to directory of argument */
16111 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16112 if (cpy != NULL)
16113 {
16114 STRCPY(cpy, p);
16115 STRCPY(gettail(cpy), buf);
16116 vim_free(p);
16117 p = cpy;
16118 }
16119 }
16120 else
16121 {
16122 vim_free(p);
16123 p = vim_strsave(buf);
16124 }
16125 }
16126
16127 if (remain == NULL)
16128 break;
16129
16130 /* Append the first path component of "remain" to "p". */
16131 q = getnextcomp(remain + 1);
16132 len = q - remain - (*q != NUL);
16133 cpy = vim_strnsave(p, STRLEN(p) + len);
16134 if (cpy != NULL)
16135 {
16136 STRNCAT(cpy, remain, len);
16137 vim_free(p);
16138 p = cpy;
16139 }
16140 /* Shorten "remain". */
16141 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016142 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 else
16144 {
16145 vim_free(remain);
16146 remain = NULL;
16147 }
16148 }
16149
16150 /* If the result is a relative path name, make it explicitly relative to
16151 * the current directory if and only if the argument had this form. */
16152 if (!vim_ispathsep(*p))
16153 {
16154 if (is_relative_to_current
16155 && *p != NUL
16156 && !(p[0] == '.'
16157 && (p[1] == NUL
16158 || vim_ispathsep(p[1])
16159 || (p[1] == '.'
16160 && (p[2] == NUL
16161 || vim_ispathsep(p[2]))))))
16162 {
16163 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016164 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 if (cpy != NULL)
16166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 vim_free(p);
16168 p = cpy;
16169 }
16170 }
16171 else if (!is_relative_to_current)
16172 {
16173 /* Strip leading "./". */
16174 q = p;
16175 while (q[0] == '.' && vim_ispathsep(q[1]))
16176 q += 2;
16177 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016178 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 }
16180 }
16181
16182 /* Ensure that the result will have no trailing path separator
16183 * if the argument had none. But keep "/" or "//". */
16184 if (!has_trailing_pathsep)
16185 {
16186 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016187 if (after_pathsep(p, q))
16188 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 }
16190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016191 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 }
16193# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016194 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195# endif
16196#endif
16197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016198 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199
16200#ifdef HAVE_READLINK
16201fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016202 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016204 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205}
16206
16207/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209 */
16210 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 typval_T *argvars;
16213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214{
Bram Moolenaar33570922005-01-25 22:26:29 +000016215 list_T *l;
16216 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218 if (argvars[0].v_type != VAR_LIST)
16219 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016220 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016221 && !tv_check_lock(l->lv_lock,
16222 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016223 {
16224 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016225 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016226 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 while (li != NULL)
16228 {
16229 ni = li->li_prev;
16230 list_append(l, li);
16231 li = ni;
16232 }
16233 rettv->vval.v_list = l;
16234 rettv->v_type = VAR_LIST;
16235 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016236 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238}
16239
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016240#define SP_NOMOVE 0x01 /* don't move cursor */
16241#define SP_REPEAT 0x02 /* repeat to find outer pair */
16242#define SP_RETCOUNT 0x04 /* return matchcount */
16243#define SP_SETPCMARK 0x08 /* set previous context mark */
16244#define SP_START 0x10 /* accept match at start position */
16245#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16246#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016247
Bram Moolenaar33570922005-01-25 22:26:29 +000016248static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249
16250/*
16251 * Get flags for a search function.
16252 * Possibly sets "p_ws".
16253 * Returns BACKWARD, FORWARD or zero (for an error).
16254 */
16255 static int
16256get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016257 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016258 int *flagsp;
16259{
16260 int dir = FORWARD;
16261 char_u *flags;
16262 char_u nbuf[NUMBUFLEN];
16263 int mask;
16264
16265 if (varp->v_type != VAR_UNKNOWN)
16266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016267 flags = get_tv_string_buf_chk(varp, nbuf);
16268 if (flags == NULL)
16269 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016270 while (*flags != NUL)
16271 {
16272 switch (*flags)
16273 {
16274 case 'b': dir = BACKWARD; break;
16275 case 'w': p_ws = TRUE; break;
16276 case 'W': p_ws = FALSE; break;
16277 default: mask = 0;
16278 if (flagsp != NULL)
16279 switch (*flags)
16280 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016281 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016282 case 'e': mask = SP_END; break;
16283 case 'm': mask = SP_RETCOUNT; break;
16284 case 'n': mask = SP_NOMOVE; break;
16285 case 'p': mask = SP_SUBPAT; break;
16286 case 'r': mask = SP_REPEAT; break;
16287 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016288 }
16289 if (mask == 0)
16290 {
16291 EMSG2(_(e_invarg2), flags);
16292 dir = 0;
16293 }
16294 else
16295 *flagsp |= mask;
16296 }
16297 if (dir == 0)
16298 break;
16299 ++flags;
16300 }
16301 }
16302 return dir;
16303}
16304
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016306 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016308 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016309search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016310 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016311 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016312 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016314 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 char_u *pat;
16316 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016317 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318 int save_p_ws = p_ws;
16319 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016320 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016321 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016322 proftime_T tm;
16323#ifdef FEAT_RELTIME
16324 long time_limit = 0;
16325#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016326 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016327 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016329 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016330 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016331 if (dir == 0)
16332 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016333 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016334 if (flags & SP_START)
16335 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016336 if (flags & SP_END)
16337 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016338
Bram Moolenaar76929292008-01-06 19:07:36 +000016339 /* Optional arguments: line number to stop searching and timeout. */
16340 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016341 {
16342 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16343 if (lnum_stop < 0)
16344 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016345#ifdef FEAT_RELTIME
16346 if (argvars[3].v_type != VAR_UNKNOWN)
16347 {
16348 time_limit = get_tv_number_chk(&argvars[3], NULL);
16349 if (time_limit < 0)
16350 goto theend;
16351 }
16352#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016353 }
16354
Bram Moolenaar76929292008-01-06 19:07:36 +000016355#ifdef FEAT_RELTIME
16356 /* Set the time limit, if there is one. */
16357 profile_setlimit(time_limit, &tm);
16358#endif
16359
Bram Moolenaar231334e2005-07-25 20:46:57 +000016360 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016361 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016362 * Check to make sure only those flags are set.
16363 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16364 * flags cannot be set. Check for that condition also.
16365 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016366 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016367 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016369 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016370 goto theend;
16371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016373 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016374 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016375 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016376 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016378 if (flags & SP_SUBPAT)
16379 retval = subpatnum;
16380 else
16381 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016382 if (flags & SP_SETPCMARK)
16383 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016385 if (match_pos != NULL)
16386 {
16387 /* Store the match cursor position */
16388 match_pos->lnum = pos.lnum;
16389 match_pos->col = pos.col + 1;
16390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 /* "/$" will put the cursor after the end of the line, may need to
16392 * correct that here */
16393 check_cursor();
16394 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016395
16396 /* If 'n' flag is used: restore cursor position. */
16397 if (flags & SP_NOMOVE)
16398 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016399 else
16400 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016401theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016403
16404 return retval;
16405}
16406
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016407#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016408
16409/*
16410 * round() is not in C90, use ceil() or floor() instead.
16411 */
16412 float_T
16413vim_round(f)
16414 float_T f;
16415{
16416 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16417}
16418
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016419/*
16420 * "round({float})" function
16421 */
16422 static void
16423f_round(argvars, rettv)
16424 typval_T *argvars;
16425 typval_T *rettv;
16426{
16427 float_T f;
16428
16429 rettv->v_type = VAR_FLOAT;
16430 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016431 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016432 else
16433 rettv->vval.v_float = 0.0;
16434}
16435#endif
16436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016437/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016438 * "screenattr()" function
16439 */
16440 static void
16441f_screenattr(argvars, rettv)
16442 typval_T *argvars UNUSED;
16443 typval_T *rettv;
16444{
16445 int row;
16446 int col;
16447 int c;
16448
16449 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16450 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16451 if (row < 0 || row >= screen_Rows
16452 || col < 0 || col >= screen_Columns)
16453 c = -1;
16454 else
16455 c = ScreenAttrs[LineOffset[row] + col];
16456 rettv->vval.v_number = c;
16457}
16458
16459/*
16460 * "screenchar()" function
16461 */
16462 static void
16463f_screenchar(argvars, rettv)
16464 typval_T *argvars UNUSED;
16465 typval_T *rettv;
16466{
16467 int row;
16468 int col;
16469 int off;
16470 int c;
16471
16472 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16473 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16474 if (row < 0 || row >= screen_Rows
16475 || col < 0 || col >= screen_Columns)
16476 c = -1;
16477 else
16478 {
16479 off = LineOffset[row] + col;
16480#ifdef FEAT_MBYTE
16481 if (enc_utf8 && ScreenLinesUC[off] != 0)
16482 c = ScreenLinesUC[off];
16483 else
16484#endif
16485 c = ScreenLines[off];
16486 }
16487 rettv->vval.v_number = c;
16488}
16489
16490/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016491 * "screencol()" function
16492 *
16493 * First column is 1 to be consistent with virtcol().
16494 */
16495 static void
16496f_screencol(argvars, rettv)
16497 typval_T *argvars UNUSED;
16498 typval_T *rettv;
16499{
16500 rettv->vval.v_number = screen_screencol() + 1;
16501}
16502
16503/*
16504 * "screenrow()" function
16505 */
16506 static void
16507f_screenrow(argvars, rettv)
16508 typval_T *argvars UNUSED;
16509 typval_T *rettv;
16510{
16511 rettv->vval.v_number = screen_screenrow() + 1;
16512}
16513
16514/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016515 * "search()" function
16516 */
16517 static void
16518f_search(argvars, rettv)
16519 typval_T *argvars;
16520 typval_T *rettv;
16521{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016522 int flags = 0;
16523
16524 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525}
16526
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016528 * "searchdecl()" function
16529 */
16530 static void
16531f_searchdecl(argvars, rettv)
16532 typval_T *argvars;
16533 typval_T *rettv;
16534{
16535 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016536 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016537 int error = FALSE;
16538 char_u *name;
16539
16540 rettv->vval.v_number = 1; /* default: FAIL */
16541
16542 name = get_tv_string_chk(&argvars[0]);
16543 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016544 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016545 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016546 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16547 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16548 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016549 if (!error && name != NULL)
16550 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016551 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016552}
16553
16554/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016555 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016557 static int
16558searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016559 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016560 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561{
16562 char_u *spat, *mpat, *epat;
16563 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 int dir;
16566 int flags = 0;
16567 char_u nbuf1[NUMBUFLEN];
16568 char_u nbuf2[NUMBUFLEN];
16569 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016570 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016571 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016572 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016575 spat = get_tv_string_chk(&argvars[0]);
16576 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16577 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16578 if (spat == NULL || mpat == NULL || epat == NULL)
16579 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 /* Handle the optional fourth argument: flags */
16582 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016583 if (dir == 0)
16584 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016585
16586 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016587 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16588 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016589 if ((flags & (SP_END | SP_SUBPAT)) != 0
16590 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016591 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016592 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016593 goto theend;
16594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016596 /* Using 'r' implies 'W', otherwise it doesn't work. */
16597 if (flags & SP_REPEAT)
16598 p_ws = FALSE;
16599
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016600 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016601 if (argvars[3].v_type == VAR_UNKNOWN
16602 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 skip = (char_u *)"";
16604 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016606 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016607 if (argvars[5].v_type != VAR_UNKNOWN)
16608 {
16609 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16610 if (lnum_stop < 0)
16611 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016612#ifdef FEAT_RELTIME
16613 if (argvars[6].v_type != VAR_UNKNOWN)
16614 {
16615 time_limit = get_tv_number_chk(&argvars[6], NULL);
16616 if (time_limit < 0)
16617 goto theend;
16618 }
16619#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016620 }
16621 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016622 if (skip == NULL)
16623 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016625 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016626 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016627
16628theend:
16629 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016630
16631 return retval;
16632}
16633
16634/*
16635 * "searchpair()" function
16636 */
16637 static void
16638f_searchpair(argvars, rettv)
16639 typval_T *argvars;
16640 typval_T *rettv;
16641{
16642 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16643}
16644
16645/*
16646 * "searchpairpos()" function
16647 */
16648 static void
16649f_searchpairpos(argvars, rettv)
16650 typval_T *argvars;
16651 typval_T *rettv;
16652{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016653 pos_T match_pos;
16654 int lnum = 0;
16655 int col = 0;
16656
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016657 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016658 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016659
16660 if (searchpair_cmn(argvars, &match_pos) > 0)
16661 {
16662 lnum = match_pos.lnum;
16663 col = match_pos.col;
16664 }
16665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016666 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16667 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016668}
16669
16670/*
16671 * Search for a start/middle/end thing.
16672 * Used by searchpair(), see its documentation for the details.
16673 * Returns 0 or -1 for no match,
16674 */
16675 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016676do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16677 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016678 char_u *spat; /* start pattern */
16679 char_u *mpat; /* middle pattern */
16680 char_u *epat; /* end pattern */
16681 int dir; /* BACKWARD or FORWARD */
16682 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016683 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016684 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016685 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016686 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016687{
16688 char_u *save_cpo;
16689 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16690 long retval = 0;
16691 pos_T pos;
16692 pos_T firstpos;
16693 pos_T foundpos;
16694 pos_T save_cursor;
16695 pos_T save_pos;
16696 int n;
16697 int r;
16698 int nest = 1;
16699 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016700 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016701 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016702
16703 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16704 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016705 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016706
Bram Moolenaar76929292008-01-06 19:07:36 +000016707#ifdef FEAT_RELTIME
16708 /* Set the time limit, if there is one. */
16709 profile_setlimit(time_limit, &tm);
16710#endif
16711
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016712 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16713 * start/middle/end (pat3, for the top pair). */
16714 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16715 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16716 if (pat2 == NULL || pat3 == NULL)
16717 goto theend;
16718 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16719 if (*mpat == NUL)
16720 STRCPY(pat3, pat2);
16721 else
16722 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16723 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016724 if (flags & SP_START)
16725 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016726
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727 save_cursor = curwin->w_cursor;
16728 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016729 clearpos(&firstpos);
16730 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 pat = pat3;
16732 for (;;)
16733 {
16734 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016735 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16737 /* didn't find it or found the first match again: FAIL */
16738 break;
16739
16740 if (firstpos.lnum == 0)
16741 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016742 if (equalpos(pos, foundpos))
16743 {
16744 /* Found the same position again. Can happen with a pattern that
16745 * has "\zs" at the end and searching backwards. Advance one
16746 * character and try again. */
16747 if (dir == BACKWARD)
16748 decl(&pos);
16749 else
16750 incl(&pos);
16751 }
16752 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016754 /* clear the start flag to avoid getting stuck here */
16755 options &= ~SEARCH_START;
16756
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757 /* If the skip pattern matches, ignore this match. */
16758 if (*skip != NUL)
16759 {
16760 save_pos = curwin->w_cursor;
16761 curwin->w_cursor = pos;
16762 r = eval_to_bool(skip, &err, NULL, FALSE);
16763 curwin->w_cursor = save_pos;
16764 if (err)
16765 {
16766 /* Evaluating {skip} caused an error, break here. */
16767 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016768 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 break;
16770 }
16771 if (r)
16772 continue;
16773 }
16774
16775 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16776 {
16777 /* Found end when searching backwards or start when searching
16778 * forward: nested pair. */
16779 ++nest;
16780 pat = pat2; /* nested, don't search for middle */
16781 }
16782 else
16783 {
16784 /* Found end when searching forward or start when searching
16785 * backward: end of (nested) pair; or found middle in outer pair. */
16786 if (--nest == 1)
16787 pat = pat3; /* outer level, search for middle */
16788 }
16789
16790 if (nest == 0)
16791 {
16792 /* Found the match: return matchcount or line number. */
16793 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016794 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016796 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016797 if (flags & SP_SETPCMARK)
16798 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 curwin->w_cursor = pos;
16800 if (!(flags & SP_REPEAT))
16801 break;
16802 nest = 1; /* search for next unmatched */
16803 }
16804 }
16805
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016806 if (match_pos != NULL)
16807 {
16808 /* Store the match cursor position */
16809 match_pos->lnum = curwin->w_cursor.lnum;
16810 match_pos->col = curwin->w_cursor.col + 1;
16811 }
16812
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016814 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 curwin->w_cursor = save_cursor;
16816
16817theend:
16818 vim_free(pat2);
16819 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016820 if (p_cpo == empty_option)
16821 p_cpo = save_cpo;
16822 else
16823 /* Darn, evaluating the {skip} expression changed the value. */
16824 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016825
16826 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827}
16828
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016829/*
16830 * "searchpos()" function
16831 */
16832 static void
16833f_searchpos(argvars, rettv)
16834 typval_T *argvars;
16835 typval_T *rettv;
16836{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016837 pos_T match_pos;
16838 int lnum = 0;
16839 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016840 int n;
16841 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016842
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016843 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016844 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016845
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016846 n = search_cmn(argvars, &match_pos, &flags);
16847 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016848 {
16849 lnum = match_pos.lnum;
16850 col = match_pos.col;
16851 }
16852
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016853 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16854 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016855 if (flags & SP_SUBPAT)
16856 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016857}
16858
16859
Bram Moolenaar0d660222005-01-07 21:51:51 +000016860 static void
16861f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016862 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865#ifdef FEAT_CLIENTSERVER
16866 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 char_u *server = get_tv_string_chk(&argvars[0]);
16868 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869
Bram Moolenaar0d660222005-01-07 21:51:51 +000016870 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016871 if (server == NULL || reply == NULL)
16872 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873 if (check_restricted() || check_secure())
16874 return;
16875# ifdef FEAT_X11
16876 if (check_connection() == FAIL)
16877 return;
16878# endif
16879
16880 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882 EMSG(_("E258: Unable to send to client"));
16883 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885 rettv->vval.v_number = 0;
16886#else
16887 rettv->vval.v_number = -1;
16888#endif
16889}
16890
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 static void
16892f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016893 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895{
16896 char_u *r = NULL;
16897
16898#ifdef FEAT_CLIENTSERVER
16899# ifdef WIN32
16900 r = serverGetVimNames();
16901# else
16902 make_connection();
16903 if (X_DISPLAY != NULL)
16904 r = serverGetVimNames(X_DISPLAY);
16905# endif
16906#endif
16907 rettv->v_type = VAR_STRING;
16908 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909}
16910
16911/*
16912 * "setbufvar()" function
16913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016915f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016917 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918{
16919 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016922 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 char_u nbuf[NUMBUFLEN];
16924
16925 if (check_restricted() || check_secure())
16926 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016927 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16928 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016929 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 varp = &argvars[2];
16931
16932 if (buf != NULL && varname != NULL && varp != NULL)
16933 {
16934 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936
16937 if (*varname == '&')
16938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016939 long numval;
16940 char_u *strval;
16941 int error = FALSE;
16942
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016944 numval = get_tv_number_chk(varp, &error);
16945 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946 if (!error && strval != NULL)
16947 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 }
16949 else
16950 {
16951 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16952 if (bufvarname != NULL)
16953 {
16954 STRCPY(bufvarname, "b:");
16955 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016956 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 vim_free(bufvarname);
16958 }
16959 }
16960
16961 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964}
16965
16966/*
16967 * "setcmdpos()" function
16968 */
16969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016970f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016971 typval_T *argvars;
16972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016974 int pos = (int)get_tv_number(&argvars[0]) - 1;
16975
16976 if (pos >= 0)
16977 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978}
16979
16980/*
16981 * "setline()" function
16982 */
16983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016984f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016985 typval_T *argvars;
16986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987{
16988 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016989 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016990 list_T *l = NULL;
16991 listitem_T *li = NULL;
16992 long added = 0;
16993 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016995 lnum = get_tv_lnum(&argvars[0]);
16996 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016998 l = argvars[1].vval.v_list;
16999 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017001 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017002 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017003
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017004 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017005 for (;;)
17006 {
17007 if (l != NULL)
17008 {
17009 /* list argument, get next string */
17010 if (li == NULL)
17011 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017012 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017013 li = li->li_next;
17014 }
17015
17016 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017017 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017018 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017019
17020 /* When coming here from Insert mode, sync undo, so that this can be
17021 * undone separately from what was previously inserted. */
17022 if (u_sync_once == 2)
17023 {
17024 u_sync_once = 1; /* notify that u_sync() was called */
17025 u_sync(TRUE);
17026 }
17027
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017028 if (lnum <= curbuf->b_ml.ml_line_count)
17029 {
17030 /* existing line, replace it */
17031 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17032 {
17033 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017034 if (lnum == curwin->w_cursor.lnum)
17035 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017036 rettv->vval.v_number = 0; /* OK */
17037 }
17038 }
17039 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17040 {
17041 /* lnum is one past the last line, append the line */
17042 ++added;
17043 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17044 rettv->vval.v_number = 0; /* OK */
17045 }
17046
17047 if (l == NULL) /* only one string argument */
17048 break;
17049 ++lnum;
17050 }
17051
17052 if (added > 0)
17053 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054}
17055
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017056static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17057
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017059 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017060 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017061 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017062set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017063 win_T *wp UNUSED;
17064 typval_T *list_arg UNUSED;
17065 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017066 typval_T *rettv;
17067{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017068#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017069 char_u *act;
17070 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017071#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017072
Bram Moolenaar2641f772005-03-25 21:58:17 +000017073 rettv->vval.v_number = -1;
17074
17075#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017076 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017077 EMSG(_(e_listreq));
17078 else
17079 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017080 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017081
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017082 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017083 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017084 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017085 if (act == NULL)
17086 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017087 if (*act == 'a' || *act == 'r')
17088 action = *act;
17089 }
17090
Bram Moolenaar81484f42012-12-05 15:16:47 +010017091 if (l != NULL && set_errorlist(wp, l, action,
17092 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017093 rettv->vval.v_number = 0;
17094 }
17095#endif
17096}
17097
17098/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017099 * "setloclist()" function
17100 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017101 static void
17102f_setloclist(argvars, rettv)
17103 typval_T *argvars;
17104 typval_T *rettv;
17105{
17106 win_T *win;
17107
17108 rettv->vval.v_number = -1;
17109
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017110 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017111 if (win != NULL)
17112 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17113}
17114
17115/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017116 * "setmatches()" function
17117 */
17118 static void
17119f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017120 typval_T *argvars UNUSED;
17121 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017122{
17123#ifdef FEAT_SEARCH_EXTRA
17124 list_T *l;
17125 listitem_T *li;
17126 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017127 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017128
17129 rettv->vval.v_number = -1;
17130 if (argvars[0].v_type != VAR_LIST)
17131 {
17132 EMSG(_(e_listreq));
17133 return;
17134 }
17135 if ((l = argvars[0].vval.v_list) != NULL)
17136 {
17137
17138 /* To some extent make sure that we are dealing with a list from
17139 * "getmatches()". */
17140 li = l->lv_first;
17141 while (li != NULL)
17142 {
17143 if (li->li_tv.v_type != VAR_DICT
17144 || (d = li->li_tv.vval.v_dict) == NULL)
17145 {
17146 EMSG(_(e_invarg));
17147 return;
17148 }
17149 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017150 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17151 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017152 && dict_find(d, (char_u *)"priority", -1) != NULL
17153 && dict_find(d, (char_u *)"id", -1) != NULL))
17154 {
17155 EMSG(_(e_invarg));
17156 return;
17157 }
17158 li = li->li_next;
17159 }
17160
17161 clear_matches(curwin);
17162 li = l->lv_first;
17163 while (li != NULL)
17164 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017165 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017166 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017167 dictitem_T *di;
17168
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017169 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017170
17171 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17172 {
17173 if (s == NULL)
17174 {
17175 s = list_alloc();
17176 if (s == NULL)
17177 return;
17178 }
17179
17180 /* match from matchaddpos() */
17181 for (i = 1; i < 9; i++)
17182 {
17183 sprintf((char *)buf, (char *)"pos%d", i);
17184 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17185 {
17186 if (di->di_tv.v_type != VAR_LIST)
17187 return;
17188
17189 list_append_tv(s, &di->di_tv);
17190 s->lv_refcount++;
17191 }
17192 else
17193 break;
17194 }
17195 }
17196 if (i == 0)
17197 {
17198 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017199 get_dict_string(d, (char_u *)"pattern", FALSE),
17200 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017201 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017202 }
17203 else
17204 {
17205 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17206 NULL, (int)get_dict_number(d, (char_u *)"priority"),
17207 (int)get_dict_number(d, (char_u *)"id"), s);
17208 list_unref(s);
17209 s = NULL;
17210 }
17211
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017212 li = li->li_next;
17213 }
17214 rettv->vval.v_number = 0;
17215 }
17216#endif
17217}
17218
17219/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017220 * "setpos()" function
17221 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017222 static void
17223f_setpos(argvars, rettv)
17224 typval_T *argvars;
17225 typval_T *rettv;
17226{
17227 pos_T pos;
17228 int fnum;
17229 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017230 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017231
Bram Moolenaar08250432008-02-13 11:42:46 +000017232 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017233 name = get_tv_string_chk(argvars);
17234 if (name != NULL)
17235 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017236 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017237 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017238 if (--pos.col < 0)
17239 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017240 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017241 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017242 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017243 if (fnum == curbuf->b_fnum)
17244 {
17245 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017246 if (curswant >= 0)
17247 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017248 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017249 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017250 }
17251 else
17252 EMSG(_(e_invarg));
17253 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017254 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17255 {
17256 /* set mark */
17257 if (setmark_pos(name[1], &pos, fnum) == OK)
17258 rettv->vval.v_number = 0;
17259 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017260 else
17261 EMSG(_(e_invarg));
17262 }
17263 }
17264}
17265
17266/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017267 * "setqflist()" function
17268 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017269 static void
17270f_setqflist(argvars, rettv)
17271 typval_T *argvars;
17272 typval_T *rettv;
17273{
17274 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17275}
17276
17277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 * "setreg()" function
17279 */
17280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017281f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017282 typval_T *argvars;
17283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284{
17285 int regname;
17286 char_u *strregname;
17287 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017288 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 int append;
17290 char_u yank_type;
17291 long block_len;
17292
17293 block_len = -1;
17294 yank_type = MAUTO;
17295 append = FALSE;
17296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017297 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017298 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 if (strregname == NULL)
17301 return; /* type error; errmsg already given */
17302 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 if (regname == 0 || regname == '@')
17304 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017306 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017308 stropt = get_tv_string_chk(&argvars[2]);
17309 if (stropt == NULL)
17310 return; /* type error */
17311 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 switch (*stropt)
17313 {
17314 case 'a': case 'A': /* append */
17315 append = TRUE;
17316 break;
17317 case 'v': case 'c': /* character-wise selection */
17318 yank_type = MCHAR;
17319 break;
17320 case 'V': case 'l': /* line-wise selection */
17321 yank_type = MLINE;
17322 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 case 'b': case Ctrl_V: /* block-wise selection */
17324 yank_type = MBLOCK;
17325 if (VIM_ISDIGIT(stropt[1]))
17326 {
17327 ++stropt;
17328 block_len = getdigits(&stropt) - 1;
17329 --stropt;
17330 }
17331 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 }
17333 }
17334
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017335 if (argvars[1].v_type == VAR_LIST)
17336 {
17337 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017338 char_u **allocval;
17339 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017340 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017341 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017342 int len = argvars[1].vval.v_list->lv_len;
17343 listitem_T *li;
17344
Bram Moolenaar7d647822014-04-05 21:28:56 +020017345 /* First half: use for pointers to result lines; second half: use for
17346 * pointers to allocated copies. */
17347 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017348 if (lstval == NULL)
17349 return;
17350 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017351 allocval = lstval + len + 2;
17352 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017353
17354 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17355 li = li->li_next)
17356 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017357 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017358 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017359 goto free_lstval;
17360 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017361 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017362 /* Need to make a copy, next get_tv_string_buf_chk() will
17363 * overwrite the string. */
17364 strval = vim_strsave(buf);
17365 if (strval == NULL)
17366 goto free_lstval;
17367 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017368 }
17369 *curval++ = strval;
17370 }
17371 *curval++ = NULL;
17372
17373 write_reg_contents_lst(regname, lstval, -1,
17374 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017375free_lstval:
17376 while (curallocval > allocval)
17377 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017378 vim_free(lstval);
17379 }
17380 else
17381 {
17382 strval = get_tv_string_chk(&argvars[1]);
17383 if (strval == NULL)
17384 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017385 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389}
17390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017391/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017392 * "settabvar()" function
17393 */
17394 static void
17395f_settabvar(argvars, rettv)
17396 typval_T *argvars;
17397 typval_T *rettv;
17398{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017399#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017400 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017401 tabpage_T *tp;
17402#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017403 char_u *varname, *tabvarname;
17404 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017405
17406 rettv->vval.v_number = 0;
17407
17408 if (check_restricted() || check_secure())
17409 return;
17410
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017411#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017412 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017413#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017414 varname = get_tv_string_chk(&argvars[1]);
17415 varp = &argvars[2];
17416
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017417 if (varname != NULL && varp != NULL
17418#ifdef FEAT_WINDOWS
17419 && tp != NULL
17420#endif
17421 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017422 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017423#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017424 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017425 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017426#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017427
17428 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17429 if (tabvarname != NULL)
17430 {
17431 STRCPY(tabvarname, "t:");
17432 STRCPY(tabvarname + 2, varname);
17433 set_var(tabvarname, varp, TRUE);
17434 vim_free(tabvarname);
17435 }
17436
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017437#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017438 /* Restore current tabpage */
17439 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017440 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017441#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017442 }
17443}
17444
17445/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017446 * "settabwinvar()" function
17447 */
17448 static void
17449f_settabwinvar(argvars, rettv)
17450 typval_T *argvars;
17451 typval_T *rettv;
17452{
17453 setwinvar(argvars, rettv, 1);
17454}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455
17456/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017457 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017460f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 typval_T *argvars;
17462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017464 setwinvar(argvars, rettv, 0);
17465}
17466
17467/*
17468 * "setwinvar()" and "settabwinvar()" functions
17469 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017470
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017471 static void
17472setwinvar(argvars, rettv, off)
17473 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017474 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017475 int off;
17476{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 win_T *win;
17478#ifdef FEAT_WINDOWS
17479 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017480 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481#endif
17482 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017483 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017485 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486
17487 if (check_restricted() || check_secure())
17488 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017489
17490#ifdef FEAT_WINDOWS
17491 if (off == 1)
17492 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17493 else
17494 tp = curtab;
17495#endif
17496 win = find_win_by_nr(&argvars[off], tp);
17497 varname = get_tv_string_chk(&argvars[off + 1]);
17498 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499
17500 if (win != NULL && varname != NULL && varp != NULL)
17501 {
17502#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017503 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017506 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017508 long numval;
17509 char_u *strval;
17510 int error = FALSE;
17511
17512 ++varname;
17513 numval = get_tv_number_chk(varp, &error);
17514 strval = get_tv_string_buf_chk(varp, nbuf);
17515 if (!error && strval != NULL)
17516 set_option_value(varname, numval, strval, OPT_LOCAL);
17517 }
17518 else
17519 {
17520 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17521 if (winvarname != NULL)
17522 {
17523 STRCPY(winvarname, "w:");
17524 STRCPY(winvarname + 2, varname);
17525 set_var(winvarname, varp, TRUE);
17526 vim_free(winvarname);
17527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 }
17529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017531 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532#endif
17533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534}
17535
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017536#ifdef FEAT_CRYPT
17537/*
17538 * "sha256({string})" function
17539 */
17540 static void
17541f_sha256(argvars, rettv)
17542 typval_T *argvars;
17543 typval_T *rettv;
17544{
17545 char_u *p;
17546
17547 p = get_tv_string(&argvars[0]);
17548 rettv->vval.v_string = vim_strsave(
17549 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17550 rettv->v_type = VAR_STRING;
17551}
17552#endif /* FEAT_CRYPT */
17553
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017555 * "shellescape({string})" function
17556 */
17557 static void
17558f_shellescape(argvars, rettv)
17559 typval_T *argvars;
17560 typval_T *rettv;
17561{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017562 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017563 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017564 rettv->v_type = VAR_STRING;
17565}
17566
17567/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017568 * shiftwidth() function
17569 */
17570 static void
17571f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017572 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017573 typval_T *rettv;
17574{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017575 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017576}
17577
17578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 */
17581 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017582f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 typval_T *argvars;
17584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017586 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588 p = get_tv_string(&argvars[0]);
17589 rettv->vval.v_string = vim_strsave(p);
17590 simplify_filename(rettv->vval.v_string); /* simplify in place */
17591 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592}
17593
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017594#ifdef FEAT_FLOAT
17595/*
17596 * "sin()" function
17597 */
17598 static void
17599f_sin(argvars, rettv)
17600 typval_T *argvars;
17601 typval_T *rettv;
17602{
17603 float_T f;
17604
17605 rettv->v_type = VAR_FLOAT;
17606 if (get_float_arg(argvars, &f) == OK)
17607 rettv->vval.v_float = sin(f);
17608 else
17609 rettv->vval.v_float = 0.0;
17610}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017611
17612/*
17613 * "sinh()" function
17614 */
17615 static void
17616f_sinh(argvars, rettv)
17617 typval_T *argvars;
17618 typval_T *rettv;
17619{
17620 float_T f;
17621
17622 rettv->v_type = VAR_FLOAT;
17623 if (get_float_arg(argvars, &f) == OK)
17624 rettv->vval.v_float = sinh(f);
17625 else
17626 rettv->vval.v_float = 0.0;
17627}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017628#endif
17629
Bram Moolenaar0d660222005-01-07 21:51:51 +000017630static int
17631#ifdef __BORLANDC__
17632 _RTLENTRYF
17633#endif
17634 item_compare __ARGS((const void *s1, const void *s2));
17635static int
17636#ifdef __BORLANDC__
17637 _RTLENTRYF
17638#endif
17639 item_compare2 __ARGS((const void *s1, const void *s2));
17640
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017641/* struct used in the array that's given to qsort() */
17642typedef struct
17643{
17644 listitem_T *item;
17645 int idx;
17646} sortItem_T;
17647
Bram Moolenaar0d660222005-01-07 21:51:51 +000017648static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017649static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017650static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017651static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017652static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017653static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017654static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017655#define ITEM_COMPARE_FAIL 999
17656
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017658 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017660 static int
17661#ifdef __BORLANDC__
17662_RTLENTRYF
17663#endif
17664item_compare(s1, s2)
17665 const void *s1;
17666 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017668 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017669 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017670 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017671 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017672 int res;
17673 char_u numbuf1[NUMBUFLEN];
17674 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017676 si1 = (sortItem_T *)s1;
17677 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017678 tv1 = &si1->item->li_tv;
17679 tv2 = &si2->item->li_tv;
17680 /* tv2string() puts quotes around a string and allocates memory. Don't do
17681 * that for string variables. Use a single quote when comparing with a
17682 * non-string to do what the docs promise. */
17683 if (tv1->v_type == VAR_STRING)
17684 {
17685 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17686 p1 = (char_u *)"'";
17687 else
17688 p1 = tv1->vval.v_string;
17689 }
17690 else
17691 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17692 if (tv2->v_type == VAR_STRING)
17693 {
17694 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17695 p2 = (char_u *)"'";
17696 else
17697 p2 = tv2->vval.v_string;
17698 }
17699 else
17700 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017701 if (p1 == NULL)
17702 p1 = (char_u *)"";
17703 if (p2 == NULL)
17704 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017705 if (!item_compare_numeric)
17706 {
17707 if (item_compare_ic)
17708 res = STRICMP(p1, p2);
17709 else
17710 res = STRCMP(p1, p2);
17711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017713 {
17714 double n1, n2;
17715 n1 = strtod((char *)p1, (char **)&p1);
17716 n2 = strtod((char *)p2, (char **)&p2);
17717 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17718 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017719
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017720 /* When the result would be zero, compare the item indexes. Makes the
17721 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017722 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017723 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017724
Bram Moolenaar0d660222005-01-07 21:51:51 +000017725 vim_free(tofree1);
17726 vim_free(tofree2);
17727 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728}
17729
17730 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017731#ifdef __BORLANDC__
17732_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017734item_compare2(s1, s2)
17735 const void *s1;
17736 const void *s2;
17737{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017738 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017740 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017741 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017744 /* shortcut after failure in previous call; compare all items equal */
17745 if (item_compare_func_err)
17746 return 0;
17747
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017748 si1 = (sortItem_T *)s1;
17749 si2 = (sortItem_T *)s2;
17750
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017751 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017752 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017753 copy_tv(&si1->item->li_tv, &argv[0]);
17754 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017755
17756 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017757 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017758 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17759 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017760 clear_tv(&argv[0]);
17761 clear_tv(&argv[1]);
17762
17763 if (res == FAIL)
17764 res = ITEM_COMPARE_FAIL;
17765 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017766 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17767 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017768 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017769 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017770
17771 /* When the result would be zero, compare the pointers themselves. Makes
17772 * the sort stable. */
17773 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017774 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017775
Bram Moolenaar0d660222005-01-07 21:51:51 +000017776 return res;
17777}
17778
17779/*
17780 * "sort({list})" function
17781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017783do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017784 typval_T *argvars;
17785 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017786 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787{
Bram Moolenaar33570922005-01-25 22:26:29 +000017788 list_T *l;
17789 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017790 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017791 long len;
17792 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793
Bram Moolenaar0d660222005-01-07 21:51:51 +000017794 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017795 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 else
17797 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017798 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017799 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017800 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17801 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017802 return;
17803 rettv->vval.v_list = l;
17804 rettv->v_type = VAR_LIST;
17805 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806
Bram Moolenaar0d660222005-01-07 21:51:51 +000017807 len = list_len(l);
17808 if (len <= 1)
17809 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810
Bram Moolenaar0d660222005-01-07 21:51:51 +000017811 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017812 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017813 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017814 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017815 if (argvars[1].v_type != VAR_UNKNOWN)
17816 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017817 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017818 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017819 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017820 else
17821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017822 int error = FALSE;
17823
17824 i = get_tv_number_chk(&argvars[1], &error);
17825 if (error)
17826 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017827 if (i == 1)
17828 item_compare_ic = TRUE;
17829 else
17830 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017831 if (item_compare_func != NULL)
17832 {
17833 if (STRCMP(item_compare_func, "n") == 0)
17834 {
17835 item_compare_func = NULL;
17836 item_compare_numeric = TRUE;
17837 }
17838 else if (STRCMP(item_compare_func, "i") == 0)
17839 {
17840 item_compare_func = NULL;
17841 item_compare_ic = TRUE;
17842 }
17843 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017845
17846 if (argvars[2].v_type != VAR_UNKNOWN)
17847 {
17848 /* optional third argument: {dict} */
17849 if (argvars[2].v_type != VAR_DICT)
17850 {
17851 EMSG(_(e_dictreq));
17852 return;
17853 }
17854 item_compare_selfdict = argvars[2].vval.v_dict;
17855 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857
Bram Moolenaar0d660222005-01-07 21:51:51 +000017858 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017859 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860 if (ptrs == NULL)
17861 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862
Bram Moolenaar327aa022014-03-25 18:24:23 +010017863 i = 0;
17864 if (sort)
17865 {
17866 /* sort(): ptrs will be the list to sort */
17867 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017868 {
17869 ptrs[i].item = li;
17870 ptrs[i].idx = i;
17871 ++i;
17872 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017873
17874 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017875 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017876 /* test the compare function */
17877 if (item_compare_func != NULL
17878 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017879 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017880 EMSG(_("E702: Sort compare function failed"));
17881 else
17882 {
17883 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017884 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017885 item_compare_func == NULL ? item_compare : item_compare2);
17886
17887 if (!item_compare_func_err)
17888 {
17889 /* Clear the List and append the items in sorted order. */
17890 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17891 l->lv_len = 0;
17892 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017893 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017894 }
17895 }
17896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017898 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017899 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17900
17901 /* f_uniq(): ptrs will be a stack of items to remove */
17902 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017903 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017904 item_compare_func_ptr = item_compare_func
17905 ? item_compare2 : item_compare;
17906
17907 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17908 li = li->li_next)
17909 {
17910 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17911 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017912 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017913 if (item_compare_func_err)
17914 {
17915 EMSG(_("E882: Uniq compare function failed"));
17916 break;
17917 }
17918 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017920 if (!item_compare_func_err)
17921 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017922 while (--i >= 0)
17923 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017924 li = ptrs[i].item->li_next;
17925 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017926 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017927 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017928 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017929 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017930 list_fix_watch(l, li);
17931 listitem_free(li);
17932 l->lv_len--;
17933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017934 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017935 }
17936
17937 vim_free(ptrs);
17938 }
17939}
17940
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017941/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017942 * "sort({list})" function
17943 */
17944 static void
17945f_sort(argvars, rettv)
17946 typval_T *argvars;
17947 typval_T *rettv;
17948{
17949 do_sort_uniq(argvars, rettv, TRUE);
17950}
17951
17952/*
17953 * "uniq({list})" function
17954 */
17955 static void
17956f_uniq(argvars, rettv)
17957 typval_T *argvars;
17958 typval_T *rettv;
17959{
17960 do_sort_uniq(argvars, rettv, FALSE);
17961}
17962
17963/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017964 * "soundfold({word})" function
17965 */
17966 static void
17967f_soundfold(argvars, rettv)
17968 typval_T *argvars;
17969 typval_T *rettv;
17970{
17971 char_u *s;
17972
17973 rettv->v_type = VAR_STRING;
17974 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017975#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017976 rettv->vval.v_string = eval_soundfold(s);
17977#else
17978 rettv->vval.v_string = vim_strsave(s);
17979#endif
17980}
17981
17982/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017983 * "spellbadword()" function
17984 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017985 static void
17986f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017987 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017988 typval_T *rettv;
17989{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017990 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017991 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017992 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017993
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017994 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017995 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017996
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017997#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017998 if (argvars[0].v_type == VAR_UNKNOWN)
17999 {
18000 /* Find the start and length of the badly spelled word. */
18001 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18002 if (len != 0)
18003 word = ml_get_cursor();
18004 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018005 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018006 {
18007 char_u *str = get_tv_string_chk(&argvars[0]);
18008 int capcol = -1;
18009
18010 if (str != NULL)
18011 {
18012 /* Check the argument for spelling. */
18013 while (*str != NUL)
18014 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018015 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018016 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018017 {
18018 word = str;
18019 break;
18020 }
18021 str += len;
18022 }
18023 }
18024 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018025#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018026
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018027 list_append_string(rettv->vval.v_list, word, len);
18028 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018029 attr == HLF_SPB ? "bad" :
18030 attr == HLF_SPR ? "rare" :
18031 attr == HLF_SPL ? "local" :
18032 attr == HLF_SPC ? "caps" :
18033 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018034}
18035
18036/*
18037 * "spellsuggest()" function
18038 */
18039 static void
18040f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018041 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018042 typval_T *rettv;
18043{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018044#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018045 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018046 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018047 int maxcount;
18048 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018049 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018050 listitem_T *li;
18051 int need_capital = FALSE;
18052#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018053
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018054 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018055 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018056
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018057#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018058 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018059 {
18060 str = get_tv_string(&argvars[0]);
18061 if (argvars[1].v_type != VAR_UNKNOWN)
18062 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018063 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018064 if (maxcount <= 0)
18065 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018066 if (argvars[2].v_type != VAR_UNKNOWN)
18067 {
18068 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18069 if (typeerr)
18070 return;
18071 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018072 }
18073 else
18074 maxcount = 25;
18075
Bram Moolenaar4770d092006-01-12 23:22:24 +000018076 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018077
18078 for (i = 0; i < ga.ga_len; ++i)
18079 {
18080 str = ((char_u **)ga.ga_data)[i];
18081
18082 li = listitem_alloc();
18083 if (li == NULL)
18084 vim_free(str);
18085 else
18086 {
18087 li->li_tv.v_type = VAR_STRING;
18088 li->li_tv.v_lock = 0;
18089 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018090 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018091 }
18092 }
18093 ga_clear(&ga);
18094 }
18095#endif
18096}
18097
Bram Moolenaar0d660222005-01-07 21:51:51 +000018098 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018099f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 typval_T *argvars;
18101 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018102{
18103 char_u *str;
18104 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018105 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018106 regmatch_T regmatch;
18107 char_u patbuf[NUMBUFLEN];
18108 char_u *save_cpo;
18109 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018110 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018111 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018112 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018113
18114 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18115 save_cpo = p_cpo;
18116 p_cpo = (char_u *)"";
18117
18118 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018119 if (argvars[1].v_type != VAR_UNKNOWN)
18120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018121 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18122 if (pat == NULL)
18123 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018124 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018125 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018126 }
18127 if (pat == NULL || *pat == NUL)
18128 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018129
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018130 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018132 if (typeerr)
18133 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134
Bram Moolenaar0d660222005-01-07 21:51:51 +000018135 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18136 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018138 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018139 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018140 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018141 if (*str == NUL)
18142 match = FALSE; /* empty item at the end */
18143 else
18144 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018145 if (match)
18146 end = regmatch.startp[0];
18147 else
18148 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018149 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18150 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018152 if (list_append_string(rettv->vval.v_list, str,
18153 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018154 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155 }
18156 if (!match)
18157 break;
18158 /* Advance to just after the match. */
18159 if (regmatch.endp[0] > str)
18160 col = 0;
18161 else
18162 {
18163 /* Don't get stuck at the same match. */
18164#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018165 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018166#else
18167 col = 1;
18168#endif
18169 }
18170 str = regmatch.endp[0];
18171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172
Bram Moolenaar473de612013-06-08 18:19:48 +020018173 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175
Bram Moolenaar0d660222005-01-07 21:51:51 +000018176 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018179#ifdef FEAT_FLOAT
18180/*
18181 * "sqrt()" function
18182 */
18183 static void
18184f_sqrt(argvars, rettv)
18185 typval_T *argvars;
18186 typval_T *rettv;
18187{
18188 float_T f;
18189
18190 rettv->v_type = VAR_FLOAT;
18191 if (get_float_arg(argvars, &f) == OK)
18192 rettv->vval.v_float = sqrt(f);
18193 else
18194 rettv->vval.v_float = 0.0;
18195}
18196
18197/*
18198 * "str2float()" function
18199 */
18200 static void
18201f_str2float(argvars, rettv)
18202 typval_T *argvars;
18203 typval_T *rettv;
18204{
18205 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18206
18207 if (*p == '+')
18208 p = skipwhite(p + 1);
18209 (void)string2float(p, &rettv->vval.v_float);
18210 rettv->v_type = VAR_FLOAT;
18211}
18212#endif
18213
Bram Moolenaar2c932302006-03-18 21:42:09 +000018214/*
18215 * "str2nr()" function
18216 */
18217 static void
18218f_str2nr(argvars, rettv)
18219 typval_T *argvars;
18220 typval_T *rettv;
18221{
18222 int base = 10;
18223 char_u *p;
18224 long n;
18225
18226 if (argvars[1].v_type != VAR_UNKNOWN)
18227 {
18228 base = get_tv_number(&argvars[1]);
18229 if (base != 8 && base != 10 && base != 16)
18230 {
18231 EMSG(_(e_invarg));
18232 return;
18233 }
18234 }
18235
18236 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018237 if (*p == '+')
18238 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018239 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018240 rettv->vval.v_number = n;
18241}
18242
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243#ifdef HAVE_STRFTIME
18244/*
18245 * "strftime({format}[, {time}])" function
18246 */
18247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018248f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018249 typval_T *argvars;
18250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251{
18252 char_u result_buf[256];
18253 struct tm *curtime;
18254 time_t seconds;
18255 char_u *p;
18256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018259 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018260 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 seconds = time(NULL);
18262 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018263 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 curtime = localtime(&seconds);
18265 /* MSVC returns NULL for an invalid value of seconds. */
18266 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018267 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268 else
18269 {
18270# ifdef FEAT_MBYTE
18271 vimconv_T conv;
18272 char_u *enc;
18273
18274 conv.vc_type = CONV_NONE;
18275 enc = enc_locale();
18276 convert_setup(&conv, p_enc, enc);
18277 if (conv.vc_type != CONV_NONE)
18278 p = string_convert(&conv, p, NULL);
18279# endif
18280 if (p != NULL)
18281 (void)strftime((char *)result_buf, sizeof(result_buf),
18282 (char *)p, curtime);
18283 else
18284 result_buf[0] = NUL;
18285
18286# ifdef FEAT_MBYTE
18287 if (conv.vc_type != CONV_NONE)
18288 vim_free(p);
18289 convert_setup(&conv, enc, p_enc);
18290 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018291 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 else
18293# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018294 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295
18296# ifdef FEAT_MBYTE
18297 /* Release conversion descriptors */
18298 convert_setup(&conv, NULL, NULL);
18299 vim_free(enc);
18300# endif
18301 }
18302}
18303#endif
18304
18305/*
18306 * "stridx()" function
18307 */
18308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018309f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018310 typval_T *argvars;
18311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312{
18313 char_u buf[NUMBUFLEN];
18314 char_u *needle;
18315 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018316 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018318 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018320 needle = get_tv_string_chk(&argvars[1]);
18321 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018322 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018323 if (needle == NULL || haystack == NULL)
18324 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325
Bram Moolenaar33570922005-01-25 22:26:29 +000018326 if (argvars[2].v_type != VAR_UNKNOWN)
18327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018328 int error = FALSE;
18329
18330 start_idx = get_tv_number_chk(&argvars[2], &error);
18331 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018332 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018333 if (start_idx >= 0)
18334 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018335 }
18336
18337 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18338 if (pos != NULL)
18339 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340}
18341
18342/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018343 * "string()" function
18344 */
18345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018346f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018347 typval_T *argvars;
18348 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018349{
18350 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018351 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018353 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018354 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018355 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018356 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018357 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358}
18359
18360/*
18361 * "strlen()" function
18362 */
18363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018364f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018365 typval_T *argvars;
18366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018368 rettv->vval.v_number = (varnumber_T)(STRLEN(
18369 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370}
18371
18372/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018373 * "strchars()" function
18374 */
18375 static void
18376f_strchars(argvars, rettv)
18377 typval_T *argvars;
18378 typval_T *rettv;
18379{
18380 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018381 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018382#ifdef FEAT_MBYTE
18383 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018384 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018385#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018386
18387 if (argvars[1].v_type != VAR_UNKNOWN)
18388 skipcc = get_tv_number_chk(&argvars[1], NULL);
18389 if (skipcc < 0 || skipcc > 1)
18390 EMSG(_(e_invarg));
18391 else
18392 {
18393#ifdef FEAT_MBYTE
18394 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18395 while (*s != NUL)
18396 {
18397 func_mb_ptr2char_adv(&s);
18398 ++len;
18399 }
18400 rettv->vval.v_number = len;
18401#else
18402 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18403#endif
18404 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018405}
18406
18407/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018408 * "strdisplaywidth()" function
18409 */
18410 static void
18411f_strdisplaywidth(argvars, rettv)
18412 typval_T *argvars;
18413 typval_T *rettv;
18414{
18415 char_u *s = get_tv_string(&argvars[0]);
18416 int col = 0;
18417
18418 if (argvars[1].v_type != VAR_UNKNOWN)
18419 col = get_tv_number(&argvars[1]);
18420
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018421 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018422}
18423
18424/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018425 * "strwidth()" function
18426 */
18427 static void
18428f_strwidth(argvars, rettv)
18429 typval_T *argvars;
18430 typval_T *rettv;
18431{
18432 char_u *s = get_tv_string(&argvars[0]);
18433
18434 rettv->vval.v_number = (varnumber_T)(
18435#ifdef FEAT_MBYTE
18436 mb_string2cells(s, -1)
18437#else
18438 STRLEN(s)
18439#endif
18440 );
18441}
18442
18443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444 * "strpart()" function
18445 */
18446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018447f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018448 typval_T *argvars;
18449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450{
18451 char_u *p;
18452 int n;
18453 int len;
18454 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018455 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018457 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 slen = (int)STRLEN(p);
18459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018460 n = get_tv_number_chk(&argvars[1], &error);
18461 if (error)
18462 len = 0;
18463 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018464 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 else
18466 len = slen - n; /* default len: all bytes that are available. */
18467
18468 /*
18469 * Only return the overlap between the specified part and the actual
18470 * string.
18471 */
18472 if (n < 0)
18473 {
18474 len += n;
18475 n = 0;
18476 }
18477 else if (n > slen)
18478 n = slen;
18479 if (len < 0)
18480 len = 0;
18481 else if (n + len > slen)
18482 len = slen - n;
18483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484 rettv->v_type = VAR_STRING;
18485 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486}
18487
18488/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018489 * "strridx()" function
18490 */
18491 static void
18492f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018493 typval_T *argvars;
18494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018495{
18496 char_u buf[NUMBUFLEN];
18497 char_u *needle;
18498 char_u *haystack;
18499 char_u *rest;
18500 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018501 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018503 needle = get_tv_string_chk(&argvars[1]);
18504 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018505
18506 rettv->vval.v_number = -1;
18507 if (needle == NULL || haystack == NULL)
18508 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018509
18510 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018511 if (argvars[2].v_type != VAR_UNKNOWN)
18512 {
18513 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018514 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018515 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018516 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018517 }
18518 else
18519 end_idx = haystack_len;
18520
Bram Moolenaar0d660222005-01-07 21:51:51 +000018521 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018522 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018523 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018524 lastmatch = haystack + end_idx;
18525 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018526 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018527 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018528 for (rest = haystack; *rest != '\0'; ++rest)
18529 {
18530 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018531 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018532 break;
18533 lastmatch = rest;
18534 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018535 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018536
18537 if (lastmatch == NULL)
18538 rettv->vval.v_number = -1;
18539 else
18540 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18541}
18542
18543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 * "strtrans()" function
18545 */
18546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018548 typval_T *argvars;
18549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018551 rettv->v_type = VAR_STRING;
18552 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553}
18554
18555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018556 * "submatch()" function
18557 */
18558 static void
18559f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018560 typval_T *argvars;
18561 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018562{
Bram Moolenaar41571762014-04-02 19:00:58 +020018563 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018564 int no;
18565 int retList = 0;
18566
18567 no = (int)get_tv_number_chk(&argvars[0], &error);
18568 if (error)
18569 return;
18570 error = FALSE;
18571 if (argvars[1].v_type != VAR_UNKNOWN)
18572 retList = get_tv_number_chk(&argvars[1], &error);
18573 if (error)
18574 return;
18575
18576 if (retList == 0)
18577 {
18578 rettv->v_type = VAR_STRING;
18579 rettv->vval.v_string = reg_submatch(no);
18580 }
18581 else
18582 {
18583 rettv->v_type = VAR_LIST;
18584 rettv->vval.v_list = reg_submatch_list(no);
18585 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018586}
18587
18588/*
18589 * "substitute()" function
18590 */
18591 static void
18592f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018593 typval_T *argvars;
18594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018595{
18596 char_u patbuf[NUMBUFLEN];
18597 char_u subbuf[NUMBUFLEN];
18598 char_u flagsbuf[NUMBUFLEN];
18599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018600 char_u *str = get_tv_string_chk(&argvars[0]);
18601 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18602 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18603 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18604
Bram Moolenaar0d660222005-01-07 21:51:51 +000018605 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018606 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18607 rettv->vval.v_string = NULL;
18608 else
18609 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610}
18611
18612/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018613 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018617 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619{
18620 int id = 0;
18621#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018622 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 long col;
18624 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018625 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018627 lnum = get_tv_lnum(argvars); /* -1 on type error */
18628 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18629 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018632 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018633 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634#endif
18635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018636 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637}
18638
18639/*
18640 * "synIDattr(id, what [, mode])" function
18641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018643f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018644 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646{
18647 char_u *p = NULL;
18648#ifdef FEAT_SYN_HL
18649 int id;
18650 char_u *what;
18651 char_u *mode;
18652 char_u modebuf[NUMBUFLEN];
18653 int modec;
18654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018655 id = get_tv_number(&argvars[0]);
18656 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018657 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018661 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 modec = 0; /* replace invalid with current */
18663 }
18664 else
18665 {
18666#ifdef FEAT_GUI
18667 if (gui.in_use)
18668 modec = 'g';
18669 else
18670#endif
18671 if (t_colors > 1)
18672 modec = 'c';
18673 else
18674 modec = 't';
18675 }
18676
18677
18678 switch (TOLOWER_ASC(what[0]))
18679 {
18680 case 'b':
18681 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18682 p = highlight_color(id, what, modec);
18683 else /* bold */
18684 p = highlight_has_attr(id, HL_BOLD, modec);
18685 break;
18686
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018687 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 p = highlight_color(id, what, modec);
18689 break;
18690
18691 case 'i':
18692 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18693 p = highlight_has_attr(id, HL_INVERSE, modec);
18694 else /* italic */
18695 p = highlight_has_attr(id, HL_ITALIC, modec);
18696 break;
18697
18698 case 'n': /* name */
18699 p = get_highlight_name(NULL, id - 1);
18700 break;
18701
18702 case 'r': /* reverse */
18703 p = highlight_has_attr(id, HL_INVERSE, modec);
18704 break;
18705
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018706 case 's':
18707 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18708 p = highlight_color(id, what, modec);
18709 else /* standout */
18710 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711 break;
18712
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018713 case 'u':
18714 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18715 /* underline */
18716 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18717 else
18718 /* undercurl */
18719 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 break;
18721 }
18722
18723 if (p != NULL)
18724 p = vim_strsave(p);
18725#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 rettv->v_type = VAR_STRING;
18727 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728}
18729
18730/*
18731 * "synIDtrans(id)" function
18732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018734f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018735 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737{
18738 int id;
18739
18740#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018741 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742
18743 if (id > 0)
18744 id = syn_get_final_id(id);
18745 else
18746#endif
18747 id = 0;
18748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018749 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750}
18751
18752/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018753 * "synconcealed(lnum, col)" function
18754 */
18755 static void
18756f_synconcealed(argvars, rettv)
18757 typval_T *argvars UNUSED;
18758 typval_T *rettv;
18759{
18760#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18761 long lnum;
18762 long col;
18763 int syntax_flags = 0;
18764 int cchar;
18765 int matchid = 0;
18766 char_u str[NUMBUFLEN];
18767#endif
18768
18769 rettv->v_type = VAR_LIST;
18770 rettv->vval.v_list = NULL;
18771
18772#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18773 lnum = get_tv_lnum(argvars); /* -1 on type error */
18774 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18775
18776 vim_memset(str, NUL, sizeof(str));
18777
18778 if (rettv_list_alloc(rettv) != FAIL)
18779 {
18780 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18781 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18782 && curwin->w_p_cole > 0)
18783 {
18784 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18785 syntax_flags = get_syntax_info(&matchid);
18786
18787 /* get the conceal character */
18788 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18789 {
18790 cchar = syn_get_sub_char();
18791 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18792 cchar = lcs_conceal;
18793 if (cchar != NUL)
18794 {
18795# ifdef FEAT_MBYTE
18796 if (has_mbyte)
18797 (*mb_char2bytes)(cchar, str);
18798 else
18799# endif
18800 str[0] = cchar;
18801 }
18802 }
18803 }
18804
18805 list_append_number(rettv->vval.v_list,
18806 (syntax_flags & HL_CONCEAL) != 0);
18807 /* -1 to auto-determine strlen */
18808 list_append_string(rettv->vval.v_list, str, -1);
18809 list_append_number(rettv->vval.v_list, matchid);
18810 }
18811#endif
18812}
18813
18814/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018815 * "synstack(lnum, col)" function
18816 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018817 static void
18818f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018819 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018820 typval_T *rettv;
18821{
18822#ifdef FEAT_SYN_HL
18823 long lnum;
18824 long col;
18825 int i;
18826 int id;
18827#endif
18828
18829 rettv->v_type = VAR_LIST;
18830 rettv->vval.v_list = NULL;
18831
18832#ifdef FEAT_SYN_HL
18833 lnum = get_tv_lnum(argvars); /* -1 on type error */
18834 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18835
18836 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018837 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018838 && rettv_list_alloc(rettv) != FAIL)
18839 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018840 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018841 for (i = 0; ; ++i)
18842 {
18843 id = syn_get_stack_item(i);
18844 if (id < 0)
18845 break;
18846 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18847 break;
18848 }
18849 }
18850#endif
18851}
18852
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018854get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018855 typval_T *argvars;
18856 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018857 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018859 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018861 char_u *infile = NULL;
18862 char_u buf[NUMBUFLEN];
18863 int err = FALSE;
18864 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018865 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018866 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018868 rettv->v_type = VAR_STRING;
18869 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018870 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018871 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018872
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018873 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018874 {
18875 /*
18876 * Write the string to a temp file, to be used for input of the shell
18877 * command.
18878 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018879 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018880 {
18881 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018882 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018883 }
18884
18885 fd = mch_fopen((char *)infile, WRITEBIN);
18886 if (fd == NULL)
18887 {
18888 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018889 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018890 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018891 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018892 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018893 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18894 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018895 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018896 else
18897 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018898 size_t len;
18899
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018900 p = get_tv_string_buf_chk(&argvars[1], buf);
18901 if (p == NULL)
18902 {
18903 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018904 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018905 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018906 len = STRLEN(p);
18907 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018908 err = TRUE;
18909 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018910 if (fclose(fd) != 0)
18911 err = TRUE;
18912 if (err)
18913 {
18914 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018915 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018916 }
18917 }
18918
Bram Moolenaar52a72462014-08-29 15:53:52 +020018919 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18920 * echoes typeahead, that messes up the display. */
18921 if (!msg_silent)
18922 flags += SHELL_COOKED;
18923
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018924 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018926 int len;
18927 listitem_T *li;
18928 char_u *s = NULL;
18929 char_u *start;
18930 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018931 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932
Bram Moolenaar52a72462014-08-29 15:53:52 +020018933 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018934 if (res == NULL)
18935 goto errret;
18936
18937 list = list_alloc();
18938 if (list == NULL)
18939 goto errret;
18940
18941 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018943 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018944 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018945 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018946 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018947
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018948 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018949 if (s == NULL)
18950 goto errret;
18951
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018952 for (p = s; start < end; ++p, ++start)
18953 *p = *start == NUL ? NL : *start;
18954 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018955
18956 li = listitem_alloc();
18957 if (li == NULL)
18958 {
18959 vim_free(s);
18960 goto errret;
18961 }
18962 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018963 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018964 li->li_tv.vval.v_string = s;
18965 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018967
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018968 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018969 rettv->v_type = VAR_LIST;
18970 rettv->vval.v_list = list;
18971 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018973 else
18974 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018975 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018976#ifdef USE_CR
18977 /* translate <CR> into <NL> */
18978 if (res != NULL)
18979 {
18980 char_u *s;
18981
18982 for (s = res; *s; ++s)
18983 {
18984 if (*s == CAR)
18985 *s = NL;
18986 }
18987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988#else
18989# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018990 /* translate <CR><NL> into <NL> */
18991 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018993 char_u *s, *d;
18994
18995 d = res;
18996 for (s = res; *s; ++s)
18997 {
18998 if (s[0] == CAR && s[1] == NL)
18999 ++s;
19000 *d++ = *s;
19001 }
19002 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004# endif
19005#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019006 rettv->vval.v_string = res;
19007 res = NULL;
19008 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019009
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019010errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019011 if (infile != NULL)
19012 {
19013 mch_remove(infile);
19014 vim_free(infile);
19015 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019016 if (res != NULL)
19017 vim_free(res);
19018 if (list != NULL)
19019 list_free(list, TRUE);
19020}
19021
19022/*
19023 * "system()" function
19024 */
19025 static void
19026f_system(argvars, rettv)
19027 typval_T *argvars;
19028 typval_T *rettv;
19029{
19030 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19031}
19032
19033/*
19034 * "systemlist()" function
19035 */
19036 static void
19037f_systemlist(argvars, rettv)
19038 typval_T *argvars;
19039 typval_T *rettv;
19040{
19041 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042}
19043
19044/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019045 * "tabpagebuflist()" function
19046 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019047 static void
19048f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019049 typval_T *argvars UNUSED;
19050 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019051{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019052#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019053 tabpage_T *tp;
19054 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019055
19056 if (argvars[0].v_type == VAR_UNKNOWN)
19057 wp = firstwin;
19058 else
19059 {
19060 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19061 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019062 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019063 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019064 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019065 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019066 for (; wp != NULL; wp = wp->w_next)
19067 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019068 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019069 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019070 }
19071#endif
19072}
19073
19074
19075/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019076 * "tabpagenr()" function
19077 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019078 static void
19079f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019080 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019081 typval_T *rettv;
19082{
19083 int nr = 1;
19084#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019085 char_u *arg;
19086
19087 if (argvars[0].v_type != VAR_UNKNOWN)
19088 {
19089 arg = get_tv_string_chk(&argvars[0]);
19090 nr = 0;
19091 if (arg != NULL)
19092 {
19093 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019094 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019095 else
19096 EMSG2(_(e_invexpr2), arg);
19097 }
19098 }
19099 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019100 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019101#endif
19102 rettv->vval.v_number = nr;
19103}
19104
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019105
19106#ifdef FEAT_WINDOWS
19107static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19108
19109/*
19110 * Common code for tabpagewinnr() and winnr().
19111 */
19112 static int
19113get_winnr(tp, argvar)
19114 tabpage_T *tp;
19115 typval_T *argvar;
19116{
19117 win_T *twin;
19118 int nr = 1;
19119 win_T *wp;
19120 char_u *arg;
19121
19122 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19123 if (argvar->v_type != VAR_UNKNOWN)
19124 {
19125 arg = get_tv_string_chk(argvar);
19126 if (arg == NULL)
19127 nr = 0; /* type error; errmsg already given */
19128 else if (STRCMP(arg, "$") == 0)
19129 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19130 else if (STRCMP(arg, "#") == 0)
19131 {
19132 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19133 if (twin == NULL)
19134 nr = 0;
19135 }
19136 else
19137 {
19138 EMSG2(_(e_invexpr2), arg);
19139 nr = 0;
19140 }
19141 }
19142
19143 if (nr > 0)
19144 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19145 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019146 {
19147 if (wp == NULL)
19148 {
19149 /* didn't find it in this tabpage */
19150 nr = 0;
19151 break;
19152 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019153 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019154 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019155 return nr;
19156}
19157#endif
19158
19159/*
19160 * "tabpagewinnr()" function
19161 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019162 static void
19163f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019164 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019165 typval_T *rettv;
19166{
19167 int nr = 1;
19168#ifdef FEAT_WINDOWS
19169 tabpage_T *tp;
19170
19171 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19172 if (tp == NULL)
19173 nr = 0;
19174 else
19175 nr = get_winnr(tp, &argvars[1]);
19176#endif
19177 rettv->vval.v_number = nr;
19178}
19179
19180
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019181/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019182 * "tagfiles()" function
19183 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019184 static void
19185f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019186 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019187 typval_T *rettv;
19188{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019189 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019190 tagname_T tn;
19191 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019192
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019193 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019194 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019195 fname = alloc(MAXPATHL);
19196 if (fname == NULL)
19197 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019198
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019199 for (first = TRUE; ; first = FALSE)
19200 if (get_tagfname(&tn, first, fname) == FAIL
19201 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019202 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019203 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019204 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019205}
19206
19207/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019208 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019209 */
19210 static void
19211f_taglist(argvars, rettv)
19212 typval_T *argvars;
19213 typval_T *rettv;
19214{
19215 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019216
19217 tag_pattern = get_tv_string(&argvars[0]);
19218
19219 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019220 if (*tag_pattern == NUL)
19221 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019222
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019223 if (rettv_list_alloc(rettv) == OK)
19224 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019225}
19226
19227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 * "tempname()" function
19229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019232 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234{
19235 static int x = 'A';
19236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019237 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019238 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239
19240 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19241 * names. Skip 'I' and 'O', they are used for shell redirection. */
19242 do
19243 {
19244 if (x == 'Z')
19245 x = '0';
19246 else if (x == '9')
19247 x = 'A';
19248 else
19249 {
19250#ifdef EBCDIC
19251 if (x == 'I')
19252 x = 'J';
19253 else if (x == 'R')
19254 x = 'S';
19255 else
19256#endif
19257 ++x;
19258 }
19259 } while (x == 'I' || x == 'O');
19260}
19261
19262/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019263 * "test(list)" function: Just checking the walls...
19264 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019265 static void
19266f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019267 typval_T *argvars UNUSED;
19268 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019269{
19270 /* Used for unit testing. Change the code below to your liking. */
19271#if 0
19272 listitem_T *li;
19273 list_T *l;
19274 char_u *bad, *good;
19275
19276 if (argvars[0].v_type != VAR_LIST)
19277 return;
19278 l = argvars[0].vval.v_list;
19279 if (l == NULL)
19280 return;
19281 li = l->lv_first;
19282 if (li == NULL)
19283 return;
19284 bad = get_tv_string(&li->li_tv);
19285 li = li->li_next;
19286 if (li == NULL)
19287 return;
19288 good = get_tv_string(&li->li_tv);
19289 rettv->vval.v_number = test_edit_score(bad, good);
19290#endif
19291}
19292
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019293#ifdef FEAT_FLOAT
19294/*
19295 * "tan()" function
19296 */
19297 static void
19298f_tan(argvars, rettv)
19299 typval_T *argvars;
19300 typval_T *rettv;
19301{
19302 float_T f;
19303
19304 rettv->v_type = VAR_FLOAT;
19305 if (get_float_arg(argvars, &f) == OK)
19306 rettv->vval.v_float = tan(f);
19307 else
19308 rettv->vval.v_float = 0.0;
19309}
19310
19311/*
19312 * "tanh()" function
19313 */
19314 static void
19315f_tanh(argvars, rettv)
19316 typval_T *argvars;
19317 typval_T *rettv;
19318{
19319 float_T f;
19320
19321 rettv->v_type = VAR_FLOAT;
19322 if (get_float_arg(argvars, &f) == OK)
19323 rettv->vval.v_float = tanh(f);
19324 else
19325 rettv->vval.v_float = 0.0;
19326}
19327#endif
19328
Bram Moolenaard52d9742005-08-21 22:20:28 +000019329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 * "tolower(string)" function
19331 */
19332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019333f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019334 typval_T *argvars;
19335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336{
19337 char_u *p;
19338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019339 p = vim_strsave(get_tv_string(&argvars[0]));
19340 rettv->v_type = VAR_STRING;
19341 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342
19343 if (p != NULL)
19344 while (*p != NUL)
19345 {
19346#ifdef FEAT_MBYTE
19347 int l;
19348
19349 if (enc_utf8)
19350 {
19351 int c, lc;
19352
19353 c = utf_ptr2char(p);
19354 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019355 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 /* TODO: reallocate string when byte count changes. */
19357 if (utf_char2len(lc) == l)
19358 utf_char2bytes(lc, p);
19359 p += l;
19360 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019361 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 p += l; /* skip multi-byte character */
19363 else
19364#endif
19365 {
19366 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19367 ++p;
19368 }
19369 }
19370}
19371
19372/*
19373 * "toupper(string)" function
19374 */
19375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019376f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019377 typval_T *argvars;
19378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019380 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019381 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382}
19383
19384/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019385 * "tr(string, fromstr, tostr)" function
19386 */
19387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019388f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019389 typval_T *argvars;
19390 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019391{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019392 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019393 char_u *fromstr;
19394 char_u *tostr;
19395 char_u *p;
19396#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019397 int inlen;
19398 int fromlen;
19399 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019400 int idx;
19401 char_u *cpstr;
19402 int cplen;
19403 int first = TRUE;
19404#endif
19405 char_u buf[NUMBUFLEN];
19406 char_u buf2[NUMBUFLEN];
19407 garray_T ga;
19408
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019409 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019410 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19411 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019412
19413 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414 rettv->v_type = VAR_STRING;
19415 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019416 if (fromstr == NULL || tostr == NULL)
19417 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019418 ga_init2(&ga, (int)sizeof(char), 80);
19419
19420#ifdef FEAT_MBYTE
19421 if (!has_mbyte)
19422#endif
19423 /* not multi-byte: fromstr and tostr must be the same length */
19424 if (STRLEN(fromstr) != STRLEN(tostr))
19425 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019426#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019427error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019428#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019429 EMSG2(_(e_invarg2), fromstr);
19430 ga_clear(&ga);
19431 return;
19432 }
19433
19434 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019435 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019436 {
19437#ifdef FEAT_MBYTE
19438 if (has_mbyte)
19439 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019440 inlen = (*mb_ptr2len)(in_str);
19441 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019442 cplen = inlen;
19443 idx = 0;
19444 for (p = fromstr; *p != NUL; p += fromlen)
19445 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019446 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019447 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019448 {
19449 for (p = tostr; *p != NUL; p += tolen)
19450 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019451 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019452 if (idx-- == 0)
19453 {
19454 cplen = tolen;
19455 cpstr = p;
19456 break;
19457 }
19458 }
19459 if (*p == NUL) /* tostr is shorter than fromstr */
19460 goto error;
19461 break;
19462 }
19463 ++idx;
19464 }
19465
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019466 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019467 {
19468 /* Check that fromstr and tostr have the same number of
19469 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019470 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019471 first = FALSE;
19472 for (p = tostr; *p != NUL; p += tolen)
19473 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019474 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019475 --idx;
19476 }
19477 if (idx != 0)
19478 goto error;
19479 }
19480
19481 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019482 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019483 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019484
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019485 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019486 }
19487 else
19488#endif
19489 {
19490 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019491 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019492 if (p != NULL)
19493 ga_append(&ga, tostr[p - fromstr]);
19494 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019495 ga_append(&ga, *in_str);
19496 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019497 }
19498 }
19499
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019500 /* add a terminating NUL */
19501 ga_grow(&ga, 1);
19502 ga_append(&ga, NUL);
19503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019504 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019505}
19506
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019507#ifdef FEAT_FLOAT
19508/*
19509 * "trunc({float})" function
19510 */
19511 static void
19512f_trunc(argvars, rettv)
19513 typval_T *argvars;
19514 typval_T *rettv;
19515{
19516 float_T f;
19517
19518 rettv->v_type = VAR_FLOAT;
19519 if (get_float_arg(argvars, &f) == OK)
19520 /* trunc() is not in C90, use floor() or ceil() instead. */
19521 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19522 else
19523 rettv->vval.v_float = 0.0;
19524}
19525#endif
19526
Bram Moolenaar8299df92004-07-10 09:47:34 +000019527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 * "type(expr)" function
19529 */
19530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019531f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019532 typval_T *argvars;
19533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019535 int n;
19536
19537 switch (argvars[0].v_type)
19538 {
19539 case VAR_NUMBER: n = 0; break;
19540 case VAR_STRING: n = 1; break;
19541 case VAR_FUNC: n = 2; break;
19542 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019543 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019544#ifdef FEAT_FLOAT
19545 case VAR_FLOAT: n = 5; break;
19546#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019547 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19548 }
19549 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550}
19551
19552/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019553 * "undofile(name)" function
19554 */
19555 static void
19556f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019557 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019558 typval_T *rettv;
19559{
19560 rettv->v_type = VAR_STRING;
19561#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019562 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019563 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019564
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019565 if (*fname == NUL)
19566 {
19567 /* If there is no file name there will be no undo file. */
19568 rettv->vval.v_string = NULL;
19569 }
19570 else
19571 {
19572 char_u *ffname = FullName_save(fname, FALSE);
19573
19574 if (ffname != NULL)
19575 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19576 vim_free(ffname);
19577 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019578 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019579#else
19580 rettv->vval.v_string = NULL;
19581#endif
19582}
19583
19584/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019585 * "undotree()" function
19586 */
19587 static void
19588f_undotree(argvars, rettv)
19589 typval_T *argvars UNUSED;
19590 typval_T *rettv;
19591{
19592 if (rettv_dict_alloc(rettv) == OK)
19593 {
19594 dict_T *dict = rettv->vval.v_dict;
19595 list_T *list;
19596
Bram Moolenaar730cde92010-06-27 05:18:54 +020019597 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019598 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019599 dict_add_nr_str(dict, "save_last",
19600 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019601 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19602 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019603 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019604
19605 list = list_alloc();
19606 if (list != NULL)
19607 {
19608 u_eval_tree(curbuf->b_u_oldhead, list);
19609 dict_add_list(dict, "entries", list);
19610 }
19611 }
19612}
19613
19614/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019615 * "values(dict)" function
19616 */
19617 static void
19618f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 typval_T *argvars;
19620 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019621{
19622 dict_list(argvars, rettv, 1);
19623}
19624
19625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 * "virtcol(string)" function
19627 */
19628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019629f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019630 typval_T *argvars;
19631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632{
19633 colnr_T vcol = 0;
19634 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019635 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019637 fp = var2fpos(&argvars[0], FALSE, &fnum);
19638 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19639 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 {
19641 getvvcol(curwin, fp, NULL, NULL, &vcol);
19642 ++vcol;
19643 }
19644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646}
19647
19648/*
19649 * "visualmode()" function
19650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019652f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019653 typval_T *argvars UNUSED;
19654 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 char_u str[2];
19657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 str[0] = curbuf->b_visual_mode_eval;
19660 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019661 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662
19663 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019664 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666}
19667
19668/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019669 * "wildmenumode()" function
19670 */
19671 static void
19672f_wildmenumode(argvars, rettv)
19673 typval_T *argvars UNUSED;
19674 typval_T *rettv UNUSED;
19675{
19676#ifdef FEAT_WILDMENU
19677 if (wild_menu_showing)
19678 rettv->vval.v_number = 1;
19679#endif
19680}
19681
19682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 * "winbufnr(nr)" function
19684 */
19685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019686f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 typval_T *argvars;
19688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689{
19690 win_T *wp;
19691
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019692 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019694 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019696 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697}
19698
19699/*
19700 * "wincol()" function
19701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019703f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019704 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706{
19707 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019708 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709}
19710
19711/*
19712 * "winheight(nr)" function
19713 */
19714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019716 typval_T *argvars;
19717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718{
19719 win_T *wp;
19720
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019721 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019723 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726}
19727
19728/*
19729 * "winline()" function
19730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019732f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019733 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735{
19736 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019737 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738}
19739
19740/*
19741 * "winnr()" function
19742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019744f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019745 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747{
19748 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019749
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019751 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019753 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754}
19755
19756/*
19757 * "winrestcmd()" function
19758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019761 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763{
19764#ifdef FEAT_WINDOWS
19765 win_T *wp;
19766 int winnr = 1;
19767 garray_T ga;
19768 char_u buf[50];
19769
19770 ga_init2(&ga, (int)sizeof(char), 70);
19771 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19772 {
19773 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19774 ga_concat(&ga, buf);
19775# ifdef FEAT_VERTSPLIT
19776 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19777 ga_concat(&ga, buf);
19778# endif
19779 ++winnr;
19780 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019781 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019783 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019787 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788}
19789
19790/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019791 * "winrestview()" function
19792 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019793 static void
19794f_winrestview(argvars, rettv)
19795 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019796 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019797{
19798 dict_T *dict;
19799
19800 if (argvars[0].v_type != VAR_DICT
19801 || (dict = argvars[0].vval.v_dict) == NULL)
19802 EMSG(_(e_invarg));
19803 else
19804 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019805 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19806 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19807 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19808 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019809#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019810 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19811 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019812#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019813 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19814 {
19815 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19816 curwin->w_set_curswant = FALSE;
19817 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019818
Bram Moolenaar82c25852014-05-28 16:47:16 +020019819 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19820 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019821#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019822 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19823 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019824#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019825 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19826 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19827 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19828 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019829
19830 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019831 win_new_height(curwin, curwin->w_height);
19832# ifdef FEAT_VERTSPLIT
19833 win_new_width(curwin, W_WIDTH(curwin));
19834# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019835 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019836
Bram Moolenaarb851a962014-10-31 15:45:52 +010019837 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019838 curwin->w_topline = 1;
19839 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19840 curwin->w_topline = curbuf->b_ml.ml_line_count;
19841#ifdef FEAT_DIFF
19842 check_topfill(curwin, TRUE);
19843#endif
19844 }
19845}
19846
19847/*
19848 * "winsaveview()" function
19849 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019850 static void
19851f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019852 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019853 typval_T *rettv;
19854{
19855 dict_T *dict;
19856
Bram Moolenaara800b422010-06-27 01:15:55 +020019857 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019858 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019859 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019860
19861 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19862 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19863#ifdef FEAT_VIRTUALEDIT
19864 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19865#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019866 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019867 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19868
19869 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19870#ifdef FEAT_DIFF
19871 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19872#endif
19873 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19874 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19875}
19876
19877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 * "winwidth(nr)" function
19879 */
19880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019881f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019882 typval_T *argvars;
19883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884{
19885 win_T *wp;
19886
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019887 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019889 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 else
19891#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019892 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019894 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895#endif
19896}
19897
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019899 * Write list of strings to file
19900 */
19901 static int
19902write_list(fd, list, binary)
19903 FILE *fd;
19904 list_T *list;
19905 int binary;
19906{
19907 listitem_T *li;
19908 int c;
19909 int ret = OK;
19910 char_u *s;
19911
19912 for (li = list->lv_first; li != NULL; li = li->li_next)
19913 {
19914 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19915 {
19916 if (*s == '\n')
19917 c = putc(NUL, fd);
19918 else
19919 c = putc(*s, fd);
19920 if (c == EOF)
19921 {
19922 ret = FAIL;
19923 break;
19924 }
19925 }
19926 if (!binary || li->li_next != NULL)
19927 if (putc('\n', fd) == EOF)
19928 {
19929 ret = FAIL;
19930 break;
19931 }
19932 if (ret == FAIL)
19933 {
19934 EMSG(_(e_write));
19935 break;
19936 }
19937 }
19938 return ret;
19939}
19940
19941/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019942 * "writefile()" function
19943 */
19944 static void
19945f_writefile(argvars, rettv)
19946 typval_T *argvars;
19947 typval_T *rettv;
19948{
19949 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019950 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019951 char_u *fname;
19952 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019953 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019954
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019955 if (check_restricted() || check_secure())
19956 return;
19957
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019958 if (argvars[0].v_type != VAR_LIST)
19959 {
19960 EMSG2(_(e_listarg), "writefile()");
19961 return;
19962 }
19963 if (argvars[0].vval.v_list == NULL)
19964 return;
19965
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019966 if (argvars[2].v_type != VAR_UNKNOWN)
19967 {
19968 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19969 binary = TRUE;
19970 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19971 append = TRUE;
19972 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019973
19974 /* Always open the file in binary mode, library functions have a mind of
19975 * their own about CR-LF conversion. */
19976 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019977 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19978 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019979 {
19980 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19981 ret = -1;
19982 }
19983 else
19984 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019985 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19986 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019987 fclose(fd);
19988 }
19989
19990 rettv->vval.v_number = ret;
19991}
19992
19993/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019994 * "xor(expr, expr)" function
19995 */
19996 static void
19997f_xor(argvars, rettv)
19998 typval_T *argvars;
19999 typval_T *rettv;
20000{
20001 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20002 ^ get_tv_number_chk(&argvars[1], NULL);
20003}
20004
20005
20006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020008 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 */
20010 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020011var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020012 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020013 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020014 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020016 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020018 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019
Bram Moolenaara5525202006-03-02 22:52:09 +000020020 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020021 if (varp->v_type == VAR_LIST)
20022 {
20023 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020024 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020025 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020026 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020027
20028 l = varp->vval.v_list;
20029 if (l == NULL)
20030 return NULL;
20031
20032 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020033 pos.lnum = list_find_nr(l, 0L, &error);
20034 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020035 return NULL; /* invalid line number */
20036
20037 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020038 pos.col = list_find_nr(l, 1L, &error);
20039 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020040 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020041 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020042
20043 /* We accept "$" for the column number: last column. */
20044 li = list_find(l, 1L);
20045 if (li != NULL && li->li_tv.v_type == VAR_STRING
20046 && li->li_tv.vval.v_string != NULL
20047 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20048 pos.col = len + 1;
20049
Bram Moolenaara5525202006-03-02 22:52:09 +000020050 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020051 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020052 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020053 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020054
Bram Moolenaara5525202006-03-02 22:52:09 +000020055#ifdef FEAT_VIRTUALEDIT
20056 /* Get the virtual offset. Defaults to zero. */
20057 pos.coladd = list_find_nr(l, 2L, &error);
20058 if (error)
20059 pos.coladd = 0;
20060#endif
20061
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020062 return &pos;
20063 }
20064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020065 name = get_tv_string_chk(varp);
20066 if (name == NULL)
20067 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020068 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020070 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20071 {
20072 if (VIsual_active)
20073 return &VIsual;
20074 return &curwin->w_cursor;
20075 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020076 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020078 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20080 return NULL;
20081 return pp;
20082 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020083
20084#ifdef FEAT_VIRTUALEDIT
20085 pos.coladd = 0;
20086#endif
20087
Bram Moolenaar477933c2007-07-17 14:32:23 +000020088 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020089 {
20090 pos.col = 0;
20091 if (name[1] == '0') /* "w0": first visible line */
20092 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020093 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020094 pos.lnum = curwin->w_topline;
20095 return &pos;
20096 }
20097 else if (name[1] == '$') /* "w$": last visible line */
20098 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020099 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020100 pos.lnum = curwin->w_botline - 1;
20101 return &pos;
20102 }
20103 }
20104 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020106 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 {
20108 pos.lnum = curbuf->b_ml.ml_line_count;
20109 pos.col = 0;
20110 }
20111 else
20112 {
20113 pos.lnum = curwin->w_cursor.lnum;
20114 pos.col = (colnr_T)STRLEN(ml_get_curline());
20115 }
20116 return &pos;
20117 }
20118 return NULL;
20119}
20120
20121/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020122 * Convert list in "arg" into a position and optional file number.
20123 * When "fnump" is NULL there is no file number, only 3 items.
20124 * Note that the column is passed on as-is, the caller may want to decrement
20125 * it to use 1 for the first column.
20126 * Return FAIL when conversion is not possible, doesn't check the position for
20127 * validity.
20128 */
20129 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020130list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020131 typval_T *arg;
20132 pos_T *posp;
20133 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020134 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020135{
20136 list_T *l = arg->vval.v_list;
20137 long i = 0;
20138 long n;
20139
Bram Moolenaar493c1782014-05-28 14:34:46 +020020140 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20141 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020142 if (arg->v_type != VAR_LIST
20143 || l == NULL
20144 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020145 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020146 return FAIL;
20147
20148 if (fnump != NULL)
20149 {
20150 n = list_find_nr(l, i++, NULL); /* fnum */
20151 if (n < 0)
20152 return FAIL;
20153 if (n == 0)
20154 n = curbuf->b_fnum; /* current buffer */
20155 *fnump = n;
20156 }
20157
20158 n = list_find_nr(l, i++, NULL); /* lnum */
20159 if (n < 0)
20160 return FAIL;
20161 posp->lnum = n;
20162
20163 n = list_find_nr(l, i++, NULL); /* col */
20164 if (n < 0)
20165 return FAIL;
20166 posp->col = n;
20167
20168#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020169 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020170 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020171 posp->coladd = 0;
20172 else
20173 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020174#endif
20175
Bram Moolenaar493c1782014-05-28 14:34:46 +020020176 if (curswantp != NULL)
20177 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20178
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020179 return OK;
20180}
20181
20182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 * Get the length of an environment variable name.
20184 * Advance "arg" to the first character after the name.
20185 * Return 0 for error.
20186 */
20187 static int
20188get_env_len(arg)
20189 char_u **arg;
20190{
20191 char_u *p;
20192 int len;
20193
20194 for (p = *arg; vim_isIDc(*p); ++p)
20195 ;
20196 if (p == *arg) /* no name found */
20197 return 0;
20198
20199 len = (int)(p - *arg);
20200 *arg = p;
20201 return len;
20202}
20203
20204/*
20205 * Get the length of the name of a function or internal variable.
20206 * "arg" is advanced to the first non-white character after the name.
20207 * Return 0 if something is wrong.
20208 */
20209 static int
20210get_id_len(arg)
20211 char_u **arg;
20212{
20213 char_u *p;
20214 int len;
20215
20216 /* Find the end of the name. */
20217 for (p = *arg; eval_isnamec(*p); ++p)
20218 ;
20219 if (p == *arg) /* no name found */
20220 return 0;
20221
20222 len = (int)(p - *arg);
20223 *arg = skipwhite(p);
20224
20225 return len;
20226}
20227
20228/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020229 * Get the length of the name of a variable or function.
20230 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020232 * Return -1 if curly braces expansion failed.
20233 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 * If the name contains 'magic' {}'s, expand them and return the
20235 * expanded name in an allocated string via 'alias' - caller must free.
20236 */
20237 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020238get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 char_u **arg;
20240 char_u **alias;
20241 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020242 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243{
20244 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 char_u *p;
20246 char_u *expr_start;
20247 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248
20249 *alias = NULL; /* default to no alias */
20250
20251 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20252 && (*arg)[2] == (int)KE_SNR)
20253 {
20254 /* hard coded <SNR>, already translated */
20255 *arg += 3;
20256 return get_id_len(arg) + 3;
20257 }
20258 len = eval_fname_script(*arg);
20259 if (len > 0)
20260 {
20261 /* literal "<SID>", "s:" or "<SNR>" */
20262 *arg += len;
20263 }
20264
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020266 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020268 p = find_name_end(*arg, &expr_start, &expr_end,
20269 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 if (expr_start != NULL)
20271 {
20272 char_u *temp_string;
20273
20274 if (!evaluate)
20275 {
20276 len += (int)(p - *arg);
20277 *arg = skipwhite(p);
20278 return len;
20279 }
20280
20281 /*
20282 * Include any <SID> etc in the expanded string:
20283 * Thus the -len here.
20284 */
20285 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20286 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020287 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 *alias = temp_string;
20289 *arg = skipwhite(p);
20290 return (int)STRLEN(temp_string);
20291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292
20293 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020294 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 EMSG2(_(e_invexpr2), *arg);
20296
20297 return len;
20298}
20299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020300/*
20301 * Find the end of a variable or function name, taking care of magic braces.
20302 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20303 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020304 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020305 * Return a pointer to just after the name. Equal to "arg" if there is no
20306 * valid name.
20307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020309find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 char_u *arg;
20311 char_u **expr_start;
20312 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020313 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020315 int mb_nest = 0;
20316 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 char_u *p;
20318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020319 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020321 *expr_start = NULL;
20322 *expr_end = NULL;
20323 }
20324
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020325 /* Quick check for valid starting character. */
20326 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20327 return arg;
20328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020329 for (p = arg; *p != NUL
20330 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020331 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020332 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020333 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020334 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020335 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020336 if (*p == '\'')
20337 {
20338 /* skip over 'string' to avoid counting [ and ] inside it. */
20339 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20340 ;
20341 if (*p == NUL)
20342 break;
20343 }
20344 else if (*p == '"')
20345 {
20346 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20347 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20348 if (*p == '\\' && p[1] != NUL)
20349 ++p;
20350 if (*p == NUL)
20351 break;
20352 }
20353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020354 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 if (*p == '[')
20357 ++br_nest;
20358 else if (*p == ']')
20359 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020362 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 if (*p == '{')
20365 {
20366 mb_nest++;
20367 if (expr_start != NULL && *expr_start == NULL)
20368 *expr_start = p;
20369 }
20370 else if (*p == '}')
20371 {
20372 mb_nest--;
20373 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20374 *expr_end = p;
20375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 }
20378
20379 return p;
20380}
20381
20382/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020383 * Expands out the 'magic' {}'s in a variable/function name.
20384 * Note that this can call itself recursively, to deal with
20385 * constructs like foo{bar}{baz}{bam}
20386 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20387 * "in_start" ^
20388 * "expr_start" ^
20389 * "expr_end" ^
20390 * "in_end" ^
20391 *
20392 * Returns a new allocated string, which the caller must free.
20393 * Returns NULL for failure.
20394 */
20395 static char_u *
20396make_expanded_name(in_start, expr_start, expr_end, in_end)
20397 char_u *in_start;
20398 char_u *expr_start;
20399 char_u *expr_end;
20400 char_u *in_end;
20401{
20402 char_u c1;
20403 char_u *retval = NULL;
20404 char_u *temp_result;
20405 char_u *nextcmd = NULL;
20406
20407 if (expr_end == NULL || in_end == NULL)
20408 return NULL;
20409 *expr_start = NUL;
20410 *expr_end = NUL;
20411 c1 = *in_end;
20412 *in_end = NUL;
20413
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020414 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020415 if (temp_result != NULL && nextcmd == NULL)
20416 {
20417 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20418 + (in_end - expr_end) + 1));
20419 if (retval != NULL)
20420 {
20421 STRCPY(retval, in_start);
20422 STRCAT(retval, temp_result);
20423 STRCAT(retval, expr_end + 1);
20424 }
20425 }
20426 vim_free(temp_result);
20427
20428 *in_end = c1; /* put char back for error messages */
20429 *expr_start = '{';
20430 *expr_end = '}';
20431
20432 if (retval != NULL)
20433 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020434 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020435 if (expr_start != NULL)
20436 {
20437 /* Further expansion! */
20438 temp_result = make_expanded_name(retval, expr_start,
20439 expr_end, temp_result);
20440 vim_free(retval);
20441 retval = temp_result;
20442 }
20443 }
20444
20445 return retval;
20446}
20447
20448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020450 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 */
20452 static int
20453eval_isnamec(c)
20454 int c;
20455{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020456 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20457}
20458
20459/*
20460 * Return TRUE if character "c" can be used as the first character in a
20461 * variable or function name (excluding '{' and '}').
20462 */
20463 static int
20464eval_isnamec1(c)
20465 int c;
20466{
20467 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468}
20469
20470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 * Set number v: variable to "val".
20472 */
20473 void
20474set_vim_var_nr(idx, val)
20475 int idx;
20476 long val;
20477{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020478 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479}
20480
20481/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020482 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 */
20484 long
20485get_vim_var_nr(idx)
20486 int idx;
20487{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020488 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489}
20490
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020491/*
20492 * Get string v: variable value. Uses a static buffer, can only be used once.
20493 */
20494 char_u *
20495get_vim_var_str(idx)
20496 int idx;
20497{
20498 return get_tv_string(&vimvars[idx].vv_tv);
20499}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020500
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020502 * Get List v: variable value. Caller must take care of reference count when
20503 * needed.
20504 */
20505 list_T *
20506get_vim_var_list(idx)
20507 int idx;
20508{
20509 return vimvars[idx].vv_list;
20510}
20511
20512/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020513 * Set v:char to character "c".
20514 */
20515 void
20516set_vim_var_char(c)
20517 int c;
20518{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020519 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020520
20521#ifdef FEAT_MBYTE
20522 if (has_mbyte)
20523 buf[(*mb_char2bytes)(c, buf)] = NUL;
20524 else
20525#endif
20526 {
20527 buf[0] = c;
20528 buf[1] = NUL;
20529 }
20530 set_vim_var_string(VV_CHAR, buf, -1);
20531}
20532
20533/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020534 * Set v:count to "count" and v:count1 to "count1".
20535 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 */
20537 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020538set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 long count;
20540 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020541 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020543 if (set_prevcount)
20544 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020545 vimvars[VV_COUNT].vv_nr = count;
20546 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547}
20548
20549/*
20550 * Set string v: variable to a copy of "val".
20551 */
20552 void
20553set_vim_var_string(idx, val, len)
20554 int idx;
20555 char_u *val;
20556 int len; /* length of "val" to use or -1 (whole string) */
20557{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020558 /* Need to do this (at least) once, since we can't initialize a union.
20559 * Will always be invoked when "v:progname" is set. */
20560 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20561
Bram Moolenaare9a41262005-01-15 22:18:47 +000020562 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020564 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020566 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020568 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569}
20570
20571/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020572 * Set List v: variable to "val".
20573 */
20574 void
20575set_vim_var_list(idx, val)
20576 int idx;
20577 list_T *val;
20578{
20579 list_unref(vimvars[idx].vv_list);
20580 vimvars[idx].vv_list = val;
20581 if (val != NULL)
20582 ++val->lv_refcount;
20583}
20584
20585/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020586 * Set Dictionary v: variable to "val".
20587 */
20588 void
20589set_vim_var_dict(idx, val)
20590 int idx;
20591 dict_T *val;
20592{
20593 int todo;
20594 hashitem_T *hi;
20595
20596 dict_unref(vimvars[idx].vv_dict);
20597 vimvars[idx].vv_dict = val;
20598 if (val != NULL)
20599 {
20600 ++val->dv_refcount;
20601
20602 /* Set readonly */
20603 todo = (int)val->dv_hashtab.ht_used;
20604 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20605 {
20606 if (HASHITEM_EMPTY(hi))
20607 continue;
20608 --todo;
20609 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20610 }
20611 }
20612}
20613
20614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 * Set v:register if needed.
20616 */
20617 void
20618set_reg_var(c)
20619 int c;
20620{
20621 char_u regname;
20622
20623 if (c == 0 || c == ' ')
20624 regname = '"';
20625 else
20626 regname = c;
20627 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020628 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 set_vim_var_string(VV_REG, &regname, 1);
20630}
20631
20632/*
20633 * Get or set v:exception. If "oldval" == NULL, return the current value.
20634 * Otherwise, restore the value to "oldval" and return NULL.
20635 * Must always be called in pairs to save and restore v:exception! Does not
20636 * take care of memory allocations.
20637 */
20638 char_u *
20639v_exception(oldval)
20640 char_u *oldval;
20641{
20642 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020643 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644
Bram Moolenaare9a41262005-01-15 22:18:47 +000020645 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 return NULL;
20647}
20648
20649/*
20650 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20651 * Otherwise, restore the value to "oldval" and return NULL.
20652 * Must always be called in pairs to save and restore v:throwpoint! Does not
20653 * take care of memory allocations.
20654 */
20655 char_u *
20656v_throwpoint(oldval)
20657 char_u *oldval;
20658{
20659 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020660 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661
Bram Moolenaare9a41262005-01-15 22:18:47 +000020662 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 return NULL;
20664}
20665
20666#if defined(FEAT_AUTOCMD) || defined(PROTO)
20667/*
20668 * Set v:cmdarg.
20669 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20670 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20671 * Must always be called in pairs!
20672 */
20673 char_u *
20674set_cmdarg(eap, oldarg)
20675 exarg_T *eap;
20676 char_u *oldarg;
20677{
20678 char_u *oldval;
20679 char_u *newval;
20680 unsigned len;
20681
Bram Moolenaare9a41262005-01-15 22:18:47 +000020682 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020683 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020685 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020686 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020687 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 }
20689
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020690 if (eap->force_bin == FORCE_BIN)
20691 len = 6;
20692 else if (eap->force_bin == FORCE_NOBIN)
20693 len = 8;
20694 else
20695 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020696
20697 if (eap->read_edit)
20698 len += 7;
20699
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020700 if (eap->force_ff != 0)
20701 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20702# ifdef FEAT_MBYTE
20703 if (eap->force_enc != 0)
20704 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020705 if (eap->bad_char != 0)
20706 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020707# endif
20708
20709 newval = alloc(len + 1);
20710 if (newval == NULL)
20711 return NULL;
20712
20713 if (eap->force_bin == FORCE_BIN)
20714 sprintf((char *)newval, " ++bin");
20715 else if (eap->force_bin == FORCE_NOBIN)
20716 sprintf((char *)newval, " ++nobin");
20717 else
20718 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020719
20720 if (eap->read_edit)
20721 STRCAT(newval, " ++edit");
20722
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020723 if (eap->force_ff != 0)
20724 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20725 eap->cmd + eap->force_ff);
20726# ifdef FEAT_MBYTE
20727 if (eap->force_enc != 0)
20728 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20729 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020730 if (eap->bad_char == BAD_KEEP)
20731 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20732 else if (eap->bad_char == BAD_DROP)
20733 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20734 else if (eap->bad_char != 0)
20735 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020736# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020737 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020738 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739}
20740#endif
20741
20742/*
20743 * Get the value of internal variable "name".
20744 * Return OK or FAIL.
20745 */
20746 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020747get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 char_u *name;
20749 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020750 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020751 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020752 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020753 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754{
20755 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020756 typval_T *tv = NULL;
20757 typval_T atv;
20758 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760
20761 /* truncate the name, so that we can use strcmp() */
20762 cc = name[len];
20763 name[len] = NUL;
20764
20765 /*
20766 * Check for "b:changedtick".
20767 */
20768 if (STRCMP(name, "b:changedtick") == 0)
20769 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020770 atv.v_type = VAR_NUMBER;
20771 atv.vval.v_number = curbuf->b_changedtick;
20772 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 }
20774
20775 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 * Check for user-defined variables.
20777 */
20778 else
20779 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020780 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020782 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020783 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020784 if (dip != NULL)
20785 *dip = v;
20786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 }
20788
Bram Moolenaare9a41262005-01-15 22:18:47 +000020789 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020791 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020792 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 ret = FAIL;
20794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020795 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020796 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797
20798 name[len] = cc;
20799
20800 return ret;
20801}
20802
20803/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020804 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20805 * Also handle function call with Funcref variable: func(expr)
20806 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20807 */
20808 static int
20809handle_subscript(arg, rettv, evaluate, verbose)
20810 char_u **arg;
20811 typval_T *rettv;
20812 int evaluate; /* do more than finding the end */
20813 int verbose; /* give error messages */
20814{
20815 int ret = OK;
20816 dict_T *selfdict = NULL;
20817 char_u *s;
20818 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020819 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020820
20821 while (ret == OK
20822 && (**arg == '['
20823 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020824 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020825 && !vim_iswhite(*(*arg - 1)))
20826 {
20827 if (**arg == '(')
20828 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020829 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020830 if (evaluate)
20831 {
20832 functv = *rettv;
20833 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020834
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020835 /* Invoke the function. Recursive! */
20836 s = functv.vval.v_string;
20837 }
20838 else
20839 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020840 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020841 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20842 &len, evaluate, selfdict);
20843
20844 /* Clear the funcref afterwards, so that deleting it while
20845 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020846 if (evaluate)
20847 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020848
20849 /* Stop the expression evaluation when immediately aborting on
20850 * error, or when an interrupt occurred or an exception was thrown
20851 * but not caught. */
20852 if (aborting())
20853 {
20854 if (ret == OK)
20855 clear_tv(rettv);
20856 ret = FAIL;
20857 }
20858 dict_unref(selfdict);
20859 selfdict = NULL;
20860 }
20861 else /* **arg == '[' || **arg == '.' */
20862 {
20863 dict_unref(selfdict);
20864 if (rettv->v_type == VAR_DICT)
20865 {
20866 selfdict = rettv->vval.v_dict;
20867 if (selfdict != NULL)
20868 ++selfdict->dv_refcount;
20869 }
20870 else
20871 selfdict = NULL;
20872 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20873 {
20874 clear_tv(rettv);
20875 ret = FAIL;
20876 }
20877 }
20878 }
20879 dict_unref(selfdict);
20880 return ret;
20881}
20882
20883/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020884 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020885 * value).
20886 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020887 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020888alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020889{
Bram Moolenaar33570922005-01-25 22:26:29 +000020890 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020891}
20892
20893/*
20894 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 * The string "s" must have been allocated, it is consumed.
20896 * Return NULL for out of memory, the variable otherwise.
20897 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020898 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020899alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 char_u *s;
20901{
Bram Moolenaar33570922005-01-25 22:26:29 +000020902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020904 rettv = alloc_tv();
20905 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020907 rettv->v_type = VAR_STRING;
20908 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 }
20910 else
20911 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020912 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913}
20914
20915/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020916 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020918 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020919free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020920 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921{
20922 if (varp != NULL)
20923 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020924 switch (varp->v_type)
20925 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020926 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020927 func_unref(varp->vval.v_string);
20928 /*FALLTHROUGH*/
20929 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020930 vim_free(varp->vval.v_string);
20931 break;
20932 case VAR_LIST:
20933 list_unref(varp->vval.v_list);
20934 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020935 case VAR_DICT:
20936 dict_unref(varp->vval.v_dict);
20937 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020938 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020939#ifdef FEAT_FLOAT
20940 case VAR_FLOAT:
20941#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020942 case VAR_UNKNOWN:
20943 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020944 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020945 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020946 break;
20947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 vim_free(varp);
20949 }
20950}
20951
20952/*
20953 * Free the memory for a variable value and set the value to NULL or 0.
20954 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020955 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020956clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020957 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958{
20959 if (varp != NULL)
20960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020961 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020963 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020964 func_unref(varp->vval.v_string);
20965 /*FALLTHROUGH*/
20966 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020967 vim_free(varp->vval.v_string);
20968 varp->vval.v_string = NULL;
20969 break;
20970 case VAR_LIST:
20971 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020972 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020973 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020974 case VAR_DICT:
20975 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020976 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020977 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020978 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020979 varp->vval.v_number = 0;
20980 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020981#ifdef FEAT_FLOAT
20982 case VAR_FLOAT:
20983 varp->vval.v_float = 0.0;
20984 break;
20985#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020986 case VAR_UNKNOWN:
20987 break;
20988 default:
20989 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020991 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 }
20993}
20994
20995/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020996 * Set the value of a variable to NULL without freeing items.
20997 */
20998 static void
20999init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021000 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021001{
21002 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021003 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004}
21005
21006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 * Get the number value of a variable.
21008 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021009 * For incompatible types, return 0.
21010 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21011 * caller of incompatible types: it sets *denote to TRUE if "denote"
21012 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 */
21014 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021015get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021016 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021018 int error = FALSE;
21019
21020 return get_tv_number_chk(varp, &error); /* return 0L on error */
21021}
21022
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021023 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021024get_tv_number_chk(varp, denote)
21025 typval_T *varp;
21026 int *denote;
21027{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021028 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021030 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021032 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021033 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021034#ifdef FEAT_FLOAT
21035 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021036 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021037 break;
21038#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021039 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021040 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021041 break;
21042 case VAR_STRING:
21043 if (varp->vval.v_string != NULL)
21044 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021045 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021046 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021047 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021048 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021049 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021050 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021051 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021052 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021053 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021054 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021055 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021057 if (denote == NULL) /* useful for values that must be unsigned */
21058 n = -1;
21059 else
21060 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021061 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062}
21063
21064/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021065 * Get the lnum from the first argument.
21066 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021067 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 */
21069 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021070get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021071 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072{
Bram Moolenaar33570922005-01-25 22:26:29 +000021073 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 linenr_T lnum;
21075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021076 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 if (lnum == 0) /* no valid number, try using line() */
21078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021079 rettv.v_type = VAR_NUMBER;
21080 f_line(argvars, &rettv);
21081 lnum = rettv.vval.v_number;
21082 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 }
21084 return lnum;
21085}
21086
21087/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021088 * Get the lnum from the first argument.
21089 * Also accepts "$", then "buf" is used.
21090 * Returns 0 on error.
21091 */
21092 static linenr_T
21093get_tv_lnum_buf(argvars, buf)
21094 typval_T *argvars;
21095 buf_T *buf;
21096{
21097 if (argvars[0].v_type == VAR_STRING
21098 && argvars[0].vval.v_string != NULL
21099 && argvars[0].vval.v_string[0] == '$'
21100 && buf != NULL)
21101 return buf->b_ml.ml_line_count;
21102 return get_tv_number_chk(&argvars[0], NULL);
21103}
21104
21105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 * Get the string value of a variable.
21107 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021108 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21109 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 * If the String variable has never been set, return an empty string.
21111 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021112 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21113 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 */
21115 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021116get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021117 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118{
21119 static char_u mybuf[NUMBUFLEN];
21120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021121 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122}
21123
21124 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021125get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021126 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021127 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021129 char_u *res = get_tv_string_buf_chk(varp, buf);
21130
21131 return res != NULL ? res : (char_u *)"";
21132}
21133
Bram Moolenaar7d647822014-04-05 21:28:56 +020021134/*
21135 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21136 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021137 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021138get_tv_string_chk(varp)
21139 typval_T *varp;
21140{
21141 static char_u mybuf[NUMBUFLEN];
21142
21143 return get_tv_string_buf_chk(varp, mybuf);
21144}
21145
21146 static char_u *
21147get_tv_string_buf_chk(varp, buf)
21148 typval_T *varp;
21149 char_u *buf;
21150{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021151 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021153 case VAR_NUMBER:
21154 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21155 return buf;
21156 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021157 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021158 break;
21159 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021160 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021161 break;
21162 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021163 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021164 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021165#ifdef FEAT_FLOAT
21166 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021167 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021168 break;
21169#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021170 case VAR_STRING:
21171 if (varp->vval.v_string != NULL)
21172 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021173 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021174 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021175 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021176 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021178 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179}
21180
21181/*
21182 * Find variable "name" in the list of variables.
21183 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021184 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021185 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021186 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021188 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021189find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021191 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021192 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021195 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196
Bram Moolenaara7043832005-01-21 11:56:39 +000021197 ht = find_var_ht(name, &varname);
21198 if (htp != NULL)
21199 *htp = ht;
21200 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021202 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203}
21204
21205/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021206 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021207 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021209 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021210find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021211 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021212 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021213 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021214 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021215{
Bram Moolenaar33570922005-01-25 22:26:29 +000021216 hashitem_T *hi;
21217
21218 if (*varname == NUL)
21219 {
21220 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021221 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021222 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021223 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021224 case 'g': return &globvars_var;
21225 case 'v': return &vimvars_var;
21226 case 'b': return &curbuf->b_bufvar;
21227 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021228#ifdef FEAT_WINDOWS
21229 case 't': return &curtab->tp_winvar;
21230#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021231 case 'l': return current_funccal == NULL
21232 ? NULL : &current_funccal->l_vars_var;
21233 case 'a': return current_funccal == NULL
21234 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021235 }
21236 return NULL;
21237 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021238
21239 hi = hash_find(ht, varname);
21240 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021241 {
21242 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021243 * worked find the variable again. Don't auto-load a script if it was
21244 * loaded already, otherwise it would be loaded every time when
21245 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021246 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021247 {
21248 /* Note: script_autoload() may make "hi" invalid. It must either
21249 * be obtained again or not used. */
21250 if (!script_autoload(varname, FALSE) || aborting())
21251 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021252 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021253 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021254 if (HASHITEM_EMPTY(hi))
21255 return NULL;
21256 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021257 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021258}
21259
21260/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021261 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021262 * Set "varname" to the start of name without ':'.
21263 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021264 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021265find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 char_u *name;
21267 char_u **varname;
21268{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021269 hashitem_T *hi;
21270
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 if (name[1] != ':')
21272 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021273 /* The name must not start with a colon or #. */
21274 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 return NULL;
21276 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021277
21278 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021279 hi = hash_find(&compat_hashtab, name);
21280 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021281 return &compat_hashtab;
21282
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 return &globvarht; /* global variable */
21285 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 }
21287 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021288 if (*name == 'g') /* global variable */
21289 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021290 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21291 */
21292 if (vim_strchr(name + 2, ':') != NULL
21293 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021294 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021296 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021298 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021299#ifdef FEAT_WINDOWS
21300 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021301 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021302#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021303 if (*name == 'v') /* v: variable */
21304 return &vimvarht;
21305 if (*name == 'a' && current_funccal != NULL) /* function argument */
21306 return &current_funccal->l_avars.dv_hashtab;
21307 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21308 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 if (*name == 's' /* script variable */
21310 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21311 return &SCRIPT_VARS(current_SID);
21312 return NULL;
21313}
21314
21315/*
21316 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021317 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 * Returns NULL when it doesn't exist.
21319 */
21320 char_u *
21321get_var_value(name)
21322 char_u *name;
21323{
Bram Moolenaar33570922005-01-25 22:26:29 +000021324 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021326 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 if (v == NULL)
21328 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021329 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330}
21331
21332/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021333 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 * sourcing this script and when executing functions defined in the script.
21335 */
21336 void
21337new_script_vars(id)
21338 scid_T id;
21339{
Bram Moolenaara7043832005-01-21 11:56:39 +000021340 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021341 hashtab_T *ht;
21342 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021343
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21345 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021346 /* Re-allocating ga_data means that an ht_array pointing to
21347 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021348 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021349 for (i = 1; i <= ga_scripts.ga_len; ++i)
21350 {
21351 ht = &SCRIPT_VARS(i);
21352 if (ht->ht_mask == HT_INIT_SIZE - 1)
21353 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021354 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021355 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021356 }
21357
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 while (ga_scripts.ga_len < id)
21359 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021360 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021361 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021362 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364 }
21365 }
21366}
21367
21368/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021369 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21370 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 */
21372 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021373init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021374 dict_T *dict;
21375 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021376 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377{
Bram Moolenaar33570922005-01-25 22:26:29 +000021378 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021379 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021380 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021381 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021382 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021383 dict_var->di_tv.vval.v_dict = dict;
21384 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021385 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021386 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21387 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388}
21389
21390/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021391 * Unreference a dictionary initialized by init_var_dict().
21392 */
21393 void
21394unref_var_dict(dict)
21395 dict_T *dict;
21396{
21397 /* Now the dict needs to be freed if no one else is using it, go back to
21398 * normal reference counting. */
21399 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21400 dict_unref(dict);
21401}
21402
21403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021405 * Frees all allocated variables and the value they contain.
21406 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 */
21408 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021409vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021410 hashtab_T *ht;
21411{
21412 vars_clear_ext(ht, TRUE);
21413}
21414
21415/*
21416 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21417 */
21418 static void
21419vars_clear_ext(ht, free_val)
21420 hashtab_T *ht;
21421 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422{
Bram Moolenaara7043832005-01-21 11:56:39 +000021423 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021424 hashitem_T *hi;
21425 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426
Bram Moolenaar33570922005-01-25 22:26:29 +000021427 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021428 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021429 for (hi = ht->ht_array; todo > 0; ++hi)
21430 {
21431 if (!HASHITEM_EMPTY(hi))
21432 {
21433 --todo;
21434
Bram Moolenaar33570922005-01-25 22:26:29 +000021435 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021436 * ht_array might change then. hash_clear() takes care of it
21437 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021438 v = HI2DI(hi);
21439 if (free_val)
21440 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021441 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021442 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021443 }
21444 }
21445 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021446 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447}
21448
Bram Moolenaara7043832005-01-21 11:56:39 +000021449/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021450 * Delete a variable from hashtab "ht" at item "hi".
21451 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021454delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021455 hashtab_T *ht;
21456 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457{
Bram Moolenaar33570922005-01-25 22:26:29 +000021458 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021459
21460 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021461 clear_tv(&di->di_tv);
21462 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463}
21464
21465/*
21466 * List the value of one internal variable.
21467 */
21468 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021469list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021472 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021474 char_u *tofree;
21475 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021476 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021477
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021478 current_copyID += COPYID_INC;
21479 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021480 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021481 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021482 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483}
21484
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021486list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 char_u *prefix;
21488 char_u *name;
21489 int type;
21490 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021491 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492{
Bram Moolenaar31859182007-08-14 20:41:13 +000021493 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21494 msg_start();
21495 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 if (name != NULL) /* "a:" vars don't have a name stored */
21497 msg_puts(name);
21498 msg_putchar(' ');
21499 msg_advance(22);
21500 if (type == VAR_NUMBER)
21501 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021502 else if (type == VAR_FUNC)
21503 msg_putchar('*');
21504 else if (type == VAR_LIST)
21505 {
21506 msg_putchar('[');
21507 if (*string == '[')
21508 ++string;
21509 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021510 else if (type == VAR_DICT)
21511 {
21512 msg_putchar('{');
21513 if (*string == '{')
21514 ++string;
21515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 else
21517 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021518
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021520
21521 if (type == VAR_FUNC)
21522 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021523 if (*first)
21524 {
21525 msg_clr_eos();
21526 *first = FALSE;
21527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528}
21529
21530/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021531 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 * If the variable already exists, the value is updated.
21533 * Otherwise the variable is created.
21534 */
21535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021536set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021538 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021539 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540{
Bram Moolenaar33570922005-01-25 22:26:29 +000021541 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021543 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021545 ht = find_var_ht(name, &varname);
21546 if (ht == NULL || *varname == NUL)
21547 {
21548 EMSG2(_(e_illvar), name);
21549 return;
21550 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021551 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021552
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021553 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21554 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021555
Bram Moolenaar33570922005-01-25 22:26:29 +000021556 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021558 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021559 if (var_check_ro(v->di_flags, name, FALSE)
21560 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021561 return;
21562 if (v->di_tv.v_type != tv->v_type
21563 && !((v->di_tv.v_type == VAR_STRING
21564 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021565 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021566 || tv->v_type == VAR_NUMBER))
21567#ifdef FEAT_FLOAT
21568 && !((v->di_tv.v_type == VAR_NUMBER
21569 || v->di_tv.v_type == VAR_FLOAT)
21570 && (tv->v_type == VAR_NUMBER
21571 || tv->v_type == VAR_FLOAT))
21572#endif
21573 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021574 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021575 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576 return;
21577 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021578
21579 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021580 * Handle setting internal v: variables separately where needed to
21581 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021582 */
21583 if (ht == &vimvarht)
21584 {
21585 if (v->di_tv.v_type == VAR_STRING)
21586 {
21587 vim_free(v->di_tv.vval.v_string);
21588 if (copy || tv->v_type != VAR_STRING)
21589 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21590 else
21591 {
21592 /* Take over the string to avoid an extra alloc/free. */
21593 v->di_tv.vval.v_string = tv->vval.v_string;
21594 tv->vval.v_string = NULL;
21595 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021596 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021597 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021598 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021599 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021600 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021601 if (STRCMP(varname, "searchforward") == 0)
21602 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021603#ifdef FEAT_SEARCH_EXTRA
21604 else if (STRCMP(varname, "hlsearch") == 0)
21605 {
21606 no_hlsearch = !v->di_tv.vval.v_number;
21607 redraw_all_later(SOME_VALID);
21608 }
21609#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021610 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021611 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021612 else if (v->di_tv.v_type != tv->v_type)
21613 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021614 }
21615
21616 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 }
21618 else /* add a new variable */
21619 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021620 /* Can't add "v:" variable. */
21621 if (ht == &vimvarht)
21622 {
21623 EMSG2(_(e_illvar), name);
21624 return;
21625 }
21626
Bram Moolenaar92124a32005-06-17 22:03:40 +000021627 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021628 if (!valid_varname(varname))
21629 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021630
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021631 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21632 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021633 if (v == NULL)
21634 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021635 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021638 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 return;
21640 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021641 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021643
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021644 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021645 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021646 else
21647 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021648 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021649 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021650 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652}
21653
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021654/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021655 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 * Also give an error message.
21657 */
21658 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021659var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 int flags;
21661 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021662 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021663{
21664 if (flags & DI_FLAGS_RO)
21665 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021666 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 return TRUE;
21668 }
21669 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21670 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021671 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021672 return TRUE;
21673 }
21674 return FALSE;
21675}
21676
21677/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021678 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21679 * Also give an error message.
21680 */
21681 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021682var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021683 int flags;
21684 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021685 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021686{
21687 if (flags & DI_FLAGS_FIX)
21688 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021689 EMSG2(_("E795: Cannot delete variable %s"),
21690 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021691 return TRUE;
21692 }
21693 return FALSE;
21694}
21695
21696/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021697 * Check if a funcref is assigned to a valid variable name.
21698 * Return TRUE and give an error if not.
21699 */
21700 static int
21701var_check_func_name(name, new_var)
21702 char_u *name; /* points to start of variable name */
21703 int new_var; /* TRUE when creating the variable */
21704{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021705 /* Allow for w: b: s: and t:. */
21706 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021707 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21708 ? name[2] : name[0]))
21709 {
21710 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21711 name);
21712 return TRUE;
21713 }
21714 /* Don't allow hiding a function. When "v" is not NULL we might be
21715 * assigning another function to the same var, the type is checked
21716 * below. */
21717 if (new_var && function_exists(name))
21718 {
21719 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21720 name);
21721 return TRUE;
21722 }
21723 return FALSE;
21724}
21725
21726/*
21727 * Check if a variable name is valid.
21728 * Return FALSE and give an error if not.
21729 */
21730 static int
21731valid_varname(varname)
21732 char_u *varname;
21733{
21734 char_u *p;
21735
21736 for (p = varname; *p != NUL; ++p)
21737 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21738 && *p != AUTOLOAD_CHAR)
21739 {
21740 EMSG2(_(e_illvar), varname);
21741 return FALSE;
21742 }
21743 return TRUE;
21744}
21745
21746/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021747 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021748 * Also give an error message, using "name" or _("name") when use_gettext is
21749 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021750 */
21751 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021752tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021753 int lock;
21754 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021755 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021756{
21757 if (lock & VAR_LOCKED)
21758 {
21759 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021760 name == NULL ? (char_u *)_("Unknown")
21761 : use_gettext ? (char_u *)_(name)
21762 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021763 return TRUE;
21764 }
21765 if (lock & VAR_FIXED)
21766 {
21767 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021768 name == NULL ? (char_u *)_("Unknown")
21769 : use_gettext ? (char_u *)_(name)
21770 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021771 return TRUE;
21772 }
21773 return FALSE;
21774}
21775
21776/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021777 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021778 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021779 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021780 * It is OK for "from" and "to" to point to the same item. This is used to
21781 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021782 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021783 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021784copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021785 typval_T *from;
21786 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021788 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021789 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021790 switch (from->v_type)
21791 {
21792 case VAR_NUMBER:
21793 to->vval.v_number = from->vval.v_number;
21794 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021795#ifdef FEAT_FLOAT
21796 case VAR_FLOAT:
21797 to->vval.v_float = from->vval.v_float;
21798 break;
21799#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021800 case VAR_STRING:
21801 case VAR_FUNC:
21802 if (from->vval.v_string == NULL)
21803 to->vval.v_string = NULL;
21804 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021805 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021806 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021807 if (from->v_type == VAR_FUNC)
21808 func_ref(to->vval.v_string);
21809 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021810 break;
21811 case VAR_LIST:
21812 if (from->vval.v_list == NULL)
21813 to->vval.v_list = NULL;
21814 else
21815 {
21816 to->vval.v_list = from->vval.v_list;
21817 ++to->vval.v_list->lv_refcount;
21818 }
21819 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021820 case VAR_DICT:
21821 if (from->vval.v_dict == NULL)
21822 to->vval.v_dict = NULL;
21823 else
21824 {
21825 to->vval.v_dict = from->vval.v_dict;
21826 ++to->vval.v_dict->dv_refcount;
21827 }
21828 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021829 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021830 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021831 break;
21832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833}
21834
21835/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021836 * Make a copy of an item.
21837 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021838 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21839 * reference to an already copied list/dict can be used.
21840 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021841 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021842 static int
21843item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021844 typval_T *from;
21845 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021846 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021847 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021848{
21849 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021850 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021851
Bram Moolenaar33570922005-01-25 22:26:29 +000021852 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021853 {
21854 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021855 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021856 }
21857 ++recurse;
21858
21859 switch (from->v_type)
21860 {
21861 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021862#ifdef FEAT_FLOAT
21863 case VAR_FLOAT:
21864#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021865 case VAR_STRING:
21866 case VAR_FUNC:
21867 copy_tv(from, to);
21868 break;
21869 case VAR_LIST:
21870 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021871 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021872 if (from->vval.v_list == NULL)
21873 to->vval.v_list = NULL;
21874 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21875 {
21876 /* use the copy made earlier */
21877 to->vval.v_list = from->vval.v_list->lv_copylist;
21878 ++to->vval.v_list->lv_refcount;
21879 }
21880 else
21881 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21882 if (to->vval.v_list == NULL)
21883 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021884 break;
21885 case VAR_DICT:
21886 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021887 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021888 if (from->vval.v_dict == NULL)
21889 to->vval.v_dict = NULL;
21890 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21891 {
21892 /* use the copy made earlier */
21893 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21894 ++to->vval.v_dict->dv_refcount;
21895 }
21896 else
21897 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21898 if (to->vval.v_dict == NULL)
21899 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021900 break;
21901 default:
21902 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021903 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021904 }
21905 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021906 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021907}
21908
21909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 * ":echo expr1 ..." print each argument separated with a space, add a
21911 * newline at the end.
21912 * ":echon expr1 ..." print each argument plain.
21913 */
21914 void
21915ex_echo(eap)
21916 exarg_T *eap;
21917{
21918 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021919 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021920 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 char_u *p;
21922 int needclr = TRUE;
21923 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021924 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925
21926 if (eap->skip)
21927 ++emsg_skip;
21928 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021930 /* If eval1() causes an error message the text from the command may
21931 * still need to be cleared. E.g., "echo 22,44". */
21932 need_clr_eos = needclr;
21933
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021935 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 {
21937 /*
21938 * Report the invalid expression unless the expression evaluation
21939 * has been cancelled due to an aborting error, an interrupt, or an
21940 * exception.
21941 */
21942 if (!aborting())
21943 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021944 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945 break;
21946 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021947 need_clr_eos = FALSE;
21948
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 if (!eap->skip)
21950 {
21951 if (atstart)
21952 {
21953 atstart = FALSE;
21954 /* Call msg_start() after eval1(), evaluating the expression
21955 * may cause a message to appear. */
21956 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021957 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021958 /* Mark the saved text as finishing the line, so that what
21959 * follows is displayed on a new line when scrolling back
21960 * at the more prompt. */
21961 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 }
21965 else if (eap->cmdidx == CMD_echo)
21966 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021967 current_copyID += COPYID_INC;
21968 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021969 if (p != NULL)
21970 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021972 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021974 if (*p != TAB && needclr)
21975 {
21976 /* remove any text still there from the command */
21977 msg_clr_eos();
21978 needclr = FALSE;
21979 }
21980 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 }
21982 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021983 {
21984#ifdef FEAT_MBYTE
21985 if (has_mbyte)
21986 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021987 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021988
21989 (void)msg_outtrans_len_attr(p, i, echo_attr);
21990 p += i - 1;
21991 }
21992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021994 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021997 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 arg = skipwhite(arg);
22001 }
22002 eap->nextcmd = check_nextcmd(arg);
22003
22004 if (eap->skip)
22005 --emsg_skip;
22006 else
22007 {
22008 /* remove text that may still be there from the command */
22009 if (needclr)
22010 msg_clr_eos();
22011 if (eap->cmdidx == CMD_echo)
22012 msg_end();
22013 }
22014}
22015
22016/*
22017 * ":echohl {name}".
22018 */
22019 void
22020ex_echohl(eap)
22021 exarg_T *eap;
22022{
22023 int id;
22024
22025 id = syn_name2id(eap->arg);
22026 if (id == 0)
22027 echo_attr = 0;
22028 else
22029 echo_attr = syn_id2attr(id);
22030}
22031
22032/*
22033 * ":execute expr1 ..." execute the result of an expression.
22034 * ":echomsg expr1 ..." Print a message
22035 * ":echoerr expr1 ..." Print an error
22036 * Each gets spaces around each argument and a newline at the end for
22037 * echo commands
22038 */
22039 void
22040ex_execute(eap)
22041 exarg_T *eap;
22042{
22043 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022044 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 int ret = OK;
22046 char_u *p;
22047 garray_T ga;
22048 int len;
22049 int save_did_emsg;
22050
22051 ga_init2(&ga, 1, 80);
22052
22053 if (eap->skip)
22054 ++emsg_skip;
22055 while (*arg != NUL && *arg != '|' && *arg != '\n')
22056 {
22057 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022058 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 {
22060 /*
22061 * Report the invalid expression unless the expression evaluation
22062 * has been cancelled due to an aborting error, an interrupt, or an
22063 * exception.
22064 */
22065 if (!aborting())
22066 EMSG2(_(e_invexpr2), p);
22067 ret = FAIL;
22068 break;
22069 }
22070
22071 if (!eap->skip)
22072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022073 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 len = (int)STRLEN(p);
22075 if (ga_grow(&ga, len + 2) == FAIL)
22076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022077 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 ret = FAIL;
22079 break;
22080 }
22081 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 ga.ga_len += len;
22085 }
22086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022087 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 arg = skipwhite(arg);
22089 }
22090
22091 if (ret != FAIL && ga.ga_data != NULL)
22092 {
22093 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022096 out_flush();
22097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 else if (eap->cmdidx == CMD_echoerr)
22099 {
22100 /* We don't want to abort following commands, restore did_emsg. */
22101 save_did_emsg = did_emsg;
22102 EMSG((char_u *)ga.ga_data);
22103 if (!force_abort)
22104 did_emsg = save_did_emsg;
22105 }
22106 else if (eap->cmdidx == CMD_execute)
22107 do_cmdline((char_u *)ga.ga_data,
22108 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22109 }
22110
22111 ga_clear(&ga);
22112
22113 if (eap->skip)
22114 --emsg_skip;
22115
22116 eap->nextcmd = check_nextcmd(arg);
22117}
22118
22119/*
22120 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22121 * "arg" points to the "&" or '+' when called, to "option" when returning.
22122 * Returns NULL when no option name found. Otherwise pointer to the char
22123 * after the option name.
22124 */
22125 static char_u *
22126find_option_end(arg, opt_flags)
22127 char_u **arg;
22128 int *opt_flags;
22129{
22130 char_u *p = *arg;
22131
22132 ++p;
22133 if (*p == 'g' && p[1] == ':')
22134 {
22135 *opt_flags = OPT_GLOBAL;
22136 p += 2;
22137 }
22138 else if (*p == 'l' && p[1] == ':')
22139 {
22140 *opt_flags = OPT_LOCAL;
22141 p += 2;
22142 }
22143 else
22144 *opt_flags = 0;
22145
22146 if (!ASCII_ISALPHA(*p))
22147 return NULL;
22148 *arg = p;
22149
22150 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22151 p += 4; /* termcap option */
22152 else
22153 while (ASCII_ISALPHA(*p))
22154 ++p;
22155 return p;
22156}
22157
22158/*
22159 * ":function"
22160 */
22161 void
22162ex_function(eap)
22163 exarg_T *eap;
22164{
22165 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022166 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 int j;
22168 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022170 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 char_u *name = NULL;
22172 char_u *p;
22173 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022174 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 garray_T newargs;
22176 garray_T newlines;
22177 int varargs = FALSE;
22178 int mustend = FALSE;
22179 int flags = 0;
22180 ufunc_T *fp;
22181 int indent;
22182 int nesting;
22183 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022184 dictitem_T *v;
22185 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022186 static int func_nr = 0; /* number for nameless function */
22187 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022188 hashtab_T *ht;
22189 int todo;
22190 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022191 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192
22193 /*
22194 * ":function" without argument: list functions.
22195 */
22196 if (ends_excmd(*eap->arg))
22197 {
22198 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022199 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022200 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022201 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022202 {
22203 if (!HASHITEM_EMPTY(hi))
22204 {
22205 --todo;
22206 fp = HI2UF(hi);
22207 if (!isdigit(*fp->uf_name))
22208 list_func_head(fp, FALSE);
22209 }
22210 }
22211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 eap->nextcmd = check_nextcmd(eap->arg);
22213 return;
22214 }
22215
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022216 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022217 * ":function /pat": list functions matching pattern.
22218 */
22219 if (*eap->arg == '/')
22220 {
22221 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22222 if (!eap->skip)
22223 {
22224 regmatch_T regmatch;
22225
22226 c = *p;
22227 *p = NUL;
22228 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22229 *p = c;
22230 if (regmatch.regprog != NULL)
22231 {
22232 regmatch.rm_ic = p_ic;
22233
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022234 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022235 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22236 {
22237 if (!HASHITEM_EMPTY(hi))
22238 {
22239 --todo;
22240 fp = HI2UF(hi);
22241 if (!isdigit(*fp->uf_name)
22242 && vim_regexec(&regmatch, fp->uf_name, 0))
22243 list_func_head(fp, FALSE);
22244 }
22245 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022246 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022247 }
22248 }
22249 if (*p == '/')
22250 ++p;
22251 eap->nextcmd = check_nextcmd(p);
22252 return;
22253 }
22254
22255 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022256 * Get the function name. There are these situations:
22257 * func normal function name
22258 * "name" == func, "fudi.fd_dict" == NULL
22259 * dict.func new dictionary entry
22260 * "name" == NULL, "fudi.fd_dict" set,
22261 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22262 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022263 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022264 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22265 * dict.func existing dict entry that's not a Funcref
22266 * "name" == NULL, "fudi.fd_dict" set,
22267 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022268 * s:func script-local function name
22269 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022272 name = trans_function_name(&p, eap->skip, 0, &fudi);
22273 paren = (vim_strchr(p, '(') != NULL);
22274 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 {
22276 /*
22277 * Return on an invalid expression in braces, unless the expression
22278 * evaluation has been cancelled due to an aborting error, an
22279 * interrupt, or an exception.
22280 */
22281 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022282 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022283 if (!eap->skip && fudi.fd_newkey != NULL)
22284 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022285 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 else
22289 eap->skip = TRUE;
22290 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022291
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 /* An error in a function call during evaluation of an expression in magic
22293 * braces should not cause the function not to be defined. */
22294 saved_did_emsg = did_emsg;
22295 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296
22297 /*
22298 * ":function func" with only function name: list function.
22299 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022300 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301 {
22302 if (!ends_excmd(*skipwhite(p)))
22303 {
22304 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022305 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 }
22307 eap->nextcmd = check_nextcmd(p);
22308 if (eap->nextcmd != NULL)
22309 *p = NUL;
22310 if (!eap->skip && !got_int)
22311 {
22312 fp = find_func(name);
22313 if (fp != NULL)
22314 {
22315 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022316 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022318 if (FUNCLINE(fp, j) == NULL)
22319 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 msg_putchar('\n');
22321 msg_outnum((long)(j + 1));
22322 if (j < 9)
22323 msg_putchar(' ');
22324 if (j < 99)
22325 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022326 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 out_flush(); /* show a line at a time */
22328 ui_breakcheck();
22329 }
22330 if (!got_int)
22331 {
22332 msg_putchar('\n');
22333 msg_puts((char_u *)" endfunction");
22334 }
22335 }
22336 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022337 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022339 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 }
22341
22342 /*
22343 * ":function name(arg1, arg2)" Define function.
22344 */
22345 p = skipwhite(p);
22346 if (*p != '(')
22347 {
22348 if (!eap->skip)
22349 {
22350 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022351 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 }
22353 /* attempt to continue by skipping some text */
22354 if (vim_strchr(p, '(') != NULL)
22355 p = vim_strchr(p, '(');
22356 }
22357 p = skipwhite(p + 1);
22358
22359 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22360 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22361
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022362 if (!eap->skip)
22363 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022364 /* Check the name of the function. Unless it's a dictionary function
22365 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022366 if (name != NULL)
22367 arg = name;
22368 else
22369 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022370 if (arg != NULL && (fudi.fd_di == NULL
22371 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022372 {
22373 if (*arg == K_SPECIAL)
22374 j = 3;
22375 else
22376 j = 0;
22377 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22378 : eval_isnamec(arg[j])))
22379 ++j;
22380 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022381 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022382 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022383 /* Disallow using the g: dict. */
22384 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22385 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022386 }
22387
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 /*
22389 * Isolate the arguments: "arg1, arg2, ...)"
22390 */
22391 while (*p != ')')
22392 {
22393 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22394 {
22395 varargs = TRUE;
22396 p += 3;
22397 mustend = TRUE;
22398 }
22399 else
22400 {
22401 arg = p;
22402 while (ASCII_ISALNUM(*p) || *p == '_')
22403 ++p;
22404 if (arg == p || isdigit(*arg)
22405 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22406 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22407 {
22408 if (!eap->skip)
22409 EMSG2(_("E125: Illegal argument: %s"), arg);
22410 break;
22411 }
22412 if (ga_grow(&newargs, 1) == FAIL)
22413 goto erret;
22414 c = *p;
22415 *p = NUL;
22416 arg = vim_strsave(arg);
22417 if (arg == NULL)
22418 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022419
22420 /* Check for duplicate argument name. */
22421 for (i = 0; i < newargs.ga_len; ++i)
22422 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22423 {
22424 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022425 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022426 goto erret;
22427 }
22428
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22430 *p = c;
22431 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 if (*p == ',')
22433 ++p;
22434 else
22435 mustend = TRUE;
22436 }
22437 p = skipwhite(p);
22438 if (mustend && *p != ')')
22439 {
22440 if (!eap->skip)
22441 EMSG2(_(e_invarg2), eap->arg);
22442 break;
22443 }
22444 }
22445 ++p; /* skip the ')' */
22446
Bram Moolenaare9a41262005-01-15 22:18:47 +000022447 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 for (;;)
22449 {
22450 p = skipwhite(p);
22451 if (STRNCMP(p, "range", 5) == 0)
22452 {
22453 flags |= FC_RANGE;
22454 p += 5;
22455 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022456 else if (STRNCMP(p, "dict", 4) == 0)
22457 {
22458 flags |= FC_DICT;
22459 p += 4;
22460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 else if (STRNCMP(p, "abort", 5) == 0)
22462 {
22463 flags |= FC_ABORT;
22464 p += 5;
22465 }
22466 else
22467 break;
22468 }
22469
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022470 /* When there is a line break use what follows for the function body.
22471 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22472 if (*p == '\n')
22473 line_arg = p + 1;
22474 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 EMSG(_(e_trailing));
22476
22477 /*
22478 * Read the body of the function, until ":endfunction" is found.
22479 */
22480 if (KeyTyped)
22481 {
22482 /* Check if the function already exists, don't let the user type the
22483 * whole function before telling him it doesn't work! For a script we
22484 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022485 if (!eap->skip && !eap->forceit)
22486 {
22487 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22488 EMSG(_(e_funcdict));
22489 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022490 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022493 if (!eap->skip && did_emsg)
22494 goto erret;
22495
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 msg_putchar('\n'); /* don't overwrite the function name */
22497 cmdline_row = msg_row;
22498 }
22499
22500 indent = 2;
22501 nesting = 0;
22502 for (;;)
22503 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022504 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022505 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022506 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022507 saved_wait_return = FALSE;
22508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022510 sourcing_lnum_off = sourcing_lnum;
22511
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022512 if (line_arg != NULL)
22513 {
22514 /* Use eap->arg, split up in parts by line breaks. */
22515 theline = line_arg;
22516 p = vim_strchr(theline, '\n');
22517 if (p == NULL)
22518 line_arg += STRLEN(line_arg);
22519 else
22520 {
22521 *p = NUL;
22522 line_arg = p + 1;
22523 }
22524 }
22525 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 theline = getcmdline(':', 0L, indent);
22527 else
22528 theline = eap->getline(':', eap->cookie, indent);
22529 if (KeyTyped)
22530 lines_left = Rows - 1;
22531 if (theline == NULL)
22532 {
22533 EMSG(_("E126: Missing :endfunction"));
22534 goto erret;
22535 }
22536
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022537 /* Detect line continuation: sourcing_lnum increased more than one. */
22538 if (sourcing_lnum > sourcing_lnum_off + 1)
22539 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22540 else
22541 sourcing_lnum_off = 0;
22542
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 if (skip_until != NULL)
22544 {
22545 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22546 * don't check for ":endfunc". */
22547 if (STRCMP(theline, skip_until) == 0)
22548 {
22549 vim_free(skip_until);
22550 skip_until = NULL;
22551 }
22552 }
22553 else
22554 {
22555 /* skip ':' and blanks*/
22556 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22557 ;
22558
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022559 /* Check for "endfunction". */
22560 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022562 if (line_arg == NULL)
22563 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564 break;
22565 }
22566
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022567 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 * at "end". */
22569 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22570 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022571 else if (STRNCMP(p, "if", 2) == 0
22572 || STRNCMP(p, "wh", 2) == 0
22573 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 || STRNCMP(p, "try", 3) == 0)
22575 indent += 2;
22576
22577 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022578 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022580 if (*p == '!')
22581 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022583 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22584 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022586 ++nesting;
22587 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 }
22589 }
22590
22591 /* Check for ":append" or ":insert". */
22592 p = skip_range(p, NULL);
22593 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22594 || (p[0] == 'i'
22595 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22596 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22597 skip_until = vim_strsave((char_u *)".");
22598
22599 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22600 arg = skipwhite(skiptowhite(p));
22601 if (arg[0] == '<' && arg[1] =='<'
22602 && ((p[0] == 'p' && p[1] == 'y'
22603 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22604 || (p[0] == 'p' && p[1] == 'e'
22605 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22606 || (p[0] == 't' && p[1] == 'c'
22607 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022608 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22609 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22611 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022612 || (p[0] == 'm' && p[1] == 'z'
22613 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 ))
22615 {
22616 /* ":python <<" continues until a dot, like ":append" */
22617 p = skipwhite(arg + 2);
22618 if (*p == NUL)
22619 skip_until = vim_strsave((char_u *)".");
22620 else
22621 skip_until = vim_strsave(p);
22622 }
22623 }
22624
22625 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022626 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022627 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022628 if (line_arg == NULL)
22629 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022631 }
22632
22633 /* Copy the line to newly allocated memory. get_one_sourceline()
22634 * allocates 250 bytes per line, this saves 80% on average. The cost
22635 * is an extra alloc/free. */
22636 p = vim_strsave(theline);
22637 if (p != NULL)
22638 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022639 if (line_arg == NULL)
22640 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022641 theline = p;
22642 }
22643
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022644 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22645
22646 /* Add NULL lines for continuation lines, so that the line count is
22647 * equal to the index in the growarray. */
22648 while (sourcing_lnum_off-- > 0)
22649 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022650
22651 /* Check for end of eap->arg. */
22652 if (line_arg != NULL && *line_arg == NUL)
22653 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 }
22655
22656 /* Don't define the function when skipping commands or when an error was
22657 * detected. */
22658 if (eap->skip || did_emsg)
22659 goto erret;
22660
22661 /*
22662 * If there are no errors, add the function
22663 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022664 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022665 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022666 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022667 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022668 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022669 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022670 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022671 goto erret;
22672 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022673
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 fp = find_func(name);
22675 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022677 if (!eap->forceit)
22678 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022679 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022680 goto erret;
22681 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022682 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022683 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022684 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022685 name);
22686 goto erret;
22687 }
22688 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022689 ga_clear_strings(&(fp->uf_args));
22690 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022691 vim_free(name);
22692 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 }
22695 else
22696 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022697 char numbuf[20];
22698
22699 fp = NULL;
22700 if (fudi.fd_newkey == NULL && !eap->forceit)
22701 {
22702 EMSG(_(e_funcdict));
22703 goto erret;
22704 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022705 if (fudi.fd_di == NULL)
22706 {
22707 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022708 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022709 goto erret;
22710 }
22711 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022712 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022713 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022714
22715 /* Give the function a sequential number. Can only be used with a
22716 * Funcref! */
22717 vim_free(name);
22718 sprintf(numbuf, "%d", ++func_nr);
22719 name = vim_strsave((char_u *)numbuf);
22720 if (name == NULL)
22721 goto erret;
22722 }
22723
22724 if (fp == NULL)
22725 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022726 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022727 {
22728 int slen, plen;
22729 char_u *scriptname;
22730
22731 /* Check that the autoload name matches the script name. */
22732 j = FAIL;
22733 if (sourcing_name != NULL)
22734 {
22735 scriptname = autoload_name(name);
22736 if (scriptname != NULL)
22737 {
22738 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022739 plen = (int)STRLEN(p);
22740 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022741 if (slen > plen && fnamecmp(p,
22742 sourcing_name + slen - plen) == 0)
22743 j = OK;
22744 vim_free(scriptname);
22745 }
22746 }
22747 if (j == FAIL)
22748 {
22749 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22750 goto erret;
22751 }
22752 }
22753
22754 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 if (fp == NULL)
22756 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022757
22758 if (fudi.fd_dict != NULL)
22759 {
22760 if (fudi.fd_di == NULL)
22761 {
22762 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022763 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022764 if (fudi.fd_di == NULL)
22765 {
22766 vim_free(fp);
22767 goto erret;
22768 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022769 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22770 {
22771 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022772 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022773 goto erret;
22774 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022775 }
22776 else
22777 /* overwrite existing dict entry */
22778 clear_tv(&fudi.fd_di->di_tv);
22779 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022780 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022781 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022782 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022783
22784 /* behave like "dict" was used */
22785 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022786 }
22787
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022789 STRCPY(fp->uf_name, name);
22790 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022792 fp->uf_args = newargs;
22793 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022794#ifdef FEAT_PROFILE
22795 fp->uf_tml_count = NULL;
22796 fp->uf_tml_total = NULL;
22797 fp->uf_tml_self = NULL;
22798 fp->uf_profiling = FALSE;
22799 if (prof_def_func())
22800 func_do_profile(fp);
22801#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022802 fp->uf_varargs = varargs;
22803 fp->uf_flags = flags;
22804 fp->uf_calls = 0;
22805 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022806 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807
22808erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 ga_clear_strings(&newargs);
22810 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022811ret_free:
22812 vim_free(skip_until);
22813 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022816 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817}
22818
22819/*
22820 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022821 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022823 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022824 * TFN_INT: internal function name OK
22825 * TFN_QUIET: be quiet
22826 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 * Advances "pp" to just after the function name (if no error).
22828 */
22829 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022830trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 char_u **pp;
22832 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022833 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022836 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 char_u *start;
22838 char_u *end;
22839 int lead;
22840 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022842 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022843
22844 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022845 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022847
22848 /* Check for hard coded <SNR>: already translated function ID (from a user
22849 * command). */
22850 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22851 && (*pp)[2] == (int)KE_SNR)
22852 {
22853 *pp += 3;
22854 len = get_id_len(pp) + 3;
22855 return vim_strnsave(start, len);
22856 }
22857
22858 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22859 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022861 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022863
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022864 /* Note that TFN_ flags use the same values as GLV_ flags. */
22865 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022866 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022867 if (end == start)
22868 {
22869 if (!skip)
22870 EMSG(_("E129: Function name required"));
22871 goto theend;
22872 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022873 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022874 {
22875 /*
22876 * Report an invalid expression in braces, unless the expression
22877 * evaluation has been cancelled due to an aborting error, an
22878 * interrupt, or an exception.
22879 */
22880 if (!aborting())
22881 {
22882 if (end != NULL)
22883 EMSG2(_(e_invarg2), start);
22884 }
22885 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022886 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022887 goto theend;
22888 }
22889
22890 if (lv.ll_tv != NULL)
22891 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022892 if (fdp != NULL)
22893 {
22894 fdp->fd_dict = lv.ll_dict;
22895 fdp->fd_newkey = lv.ll_newkey;
22896 lv.ll_newkey = NULL;
22897 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022899 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22900 {
22901 name = vim_strsave(lv.ll_tv->vval.v_string);
22902 *pp = end;
22903 }
22904 else
22905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022906 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22907 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022908 EMSG(_(e_funcref));
22909 else
22910 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022911 name = NULL;
22912 }
22913 goto theend;
22914 }
22915
22916 if (lv.ll_name == NULL)
22917 {
22918 /* Error found, but continue after the function name. */
22919 *pp = end;
22920 goto theend;
22921 }
22922
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022923 /* Check if the name is a Funcref. If so, use the value. */
22924 if (lv.ll_exp_name != NULL)
22925 {
22926 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022927 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022928 if (name == lv.ll_exp_name)
22929 name = NULL;
22930 }
22931 else
22932 {
22933 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022934 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022935 if (name == *pp)
22936 name = NULL;
22937 }
22938 if (name != NULL)
22939 {
22940 name = vim_strsave(name);
22941 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022942 if (STRNCMP(name, "<SNR>", 5) == 0)
22943 {
22944 /* Change "<SNR>" to the byte sequence. */
22945 name[0] = K_SPECIAL;
22946 name[1] = KS_EXTRA;
22947 name[2] = (int)KE_SNR;
22948 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22949 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022950 goto theend;
22951 }
22952
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022953 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022954 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022955 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022956 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22957 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22958 {
22959 /* When there was "s:" already or the name expanded to get a
22960 * leading "s:" then remove it. */
22961 lv.ll_name += 2;
22962 len -= 2;
22963 lead = 2;
22964 }
22965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022966 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022967 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022968 /* skip over "s:" and "g:" */
22969 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022970 lv.ll_name += 2;
22971 len = (int)(end - lv.ll_name);
22972 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022973
22974 /*
22975 * Copy the function name to allocated memory.
22976 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22977 * Accept <SNR>123_name() outside a script.
22978 */
22979 if (skip)
22980 lead = 0; /* do nothing */
22981 else if (lead > 0)
22982 {
22983 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022984 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22985 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022986 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022987 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022988 if (current_SID <= 0)
22989 {
22990 EMSG(_(e_usingsid));
22991 goto theend;
22992 }
22993 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22994 lead += (int)STRLEN(sid_buf);
22995 }
22996 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022997 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022998 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022999 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023000 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023001 goto theend;
23002 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023003 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023004 {
23005 char_u *cp = vim_strchr(lv.ll_name, ':');
23006
23007 if (cp != NULL && cp < end)
23008 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023009 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023010 goto theend;
23011 }
23012 }
23013
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023014 name = alloc((unsigned)(len + lead + 1));
23015 if (name != NULL)
23016 {
23017 if (lead > 0)
23018 {
23019 name[0] = K_SPECIAL;
23020 name[1] = KS_EXTRA;
23021 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023022 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023023 STRCPY(name + 3, sid_buf);
23024 }
23025 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023026 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023027 }
23028 *pp = end;
23029
23030theend:
23031 clear_lval(&lv);
23032 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033}
23034
23035/*
23036 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23037 * Return 2 if "p" starts with "s:".
23038 * Return 0 otherwise.
23039 */
23040 static int
23041eval_fname_script(p)
23042 char_u *p;
23043{
23044 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23045 || STRNICMP(p + 1, "SNR>", 4) == 0))
23046 return 5;
23047 if (p[0] == 's' && p[1] == ':')
23048 return 2;
23049 return 0;
23050}
23051
23052/*
23053 * Return TRUE if "p" starts with "<SID>" or "s:".
23054 * Only works if eval_fname_script() returned non-zero for "p"!
23055 */
23056 static int
23057eval_fname_sid(p)
23058 char_u *p;
23059{
23060 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23061}
23062
23063/*
23064 * List the head of the function: "name(arg1, arg2)".
23065 */
23066 static void
23067list_func_head(fp, indent)
23068 ufunc_T *fp;
23069 int indent;
23070{
23071 int j;
23072
23073 msg_start();
23074 if (indent)
23075 MSG_PUTS(" ");
23076 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023077 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 {
23079 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023080 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 }
23082 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023083 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023085 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 {
23087 if (j)
23088 MSG_PUTS(", ");
23089 msg_puts(FUNCARG(fp, j));
23090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023091 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 {
23093 if (j)
23094 MSG_PUTS(", ");
23095 MSG_PUTS("...");
23096 }
23097 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023098 if (fp->uf_flags & FC_ABORT)
23099 MSG_PUTS(" abort");
23100 if (fp->uf_flags & FC_RANGE)
23101 MSG_PUTS(" range");
23102 if (fp->uf_flags & FC_DICT)
23103 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023104 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023105 if (p_verbose > 0)
23106 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023107}
23108
23109/*
23110 * Find a function by name, return pointer to it in ufuncs.
23111 * Return NULL for unknown function.
23112 */
23113 static ufunc_T *
23114find_func(name)
23115 char_u *name;
23116{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023117 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023119 hi = hash_find(&func_hashtab, name);
23120 if (!HASHITEM_EMPTY(hi))
23121 return HI2UF(hi);
23122 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123}
23124
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023125#if defined(EXITFREE) || defined(PROTO)
23126 void
23127free_all_functions()
23128{
23129 hashitem_T *hi;
23130
23131 /* Need to start all over every time, because func_free() may change the
23132 * hash table. */
23133 while (func_hashtab.ht_used > 0)
23134 for (hi = func_hashtab.ht_array; ; ++hi)
23135 if (!HASHITEM_EMPTY(hi))
23136 {
23137 func_free(HI2UF(hi));
23138 break;
23139 }
23140}
23141#endif
23142
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023143 int
23144translated_function_exists(name)
23145 char_u *name;
23146{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023147 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023148 return find_internal_func(name) >= 0;
23149 return find_func(name) != NULL;
23150}
23151
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023152/*
23153 * Return TRUE if a function "name" exists.
23154 */
23155 static int
23156function_exists(name)
23157 char_u *name;
23158{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023159 char_u *nm = name;
23160 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023161 int n = FALSE;
23162
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023163 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23164 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023165 nm = skipwhite(nm);
23166
23167 /* Only accept "funcname", "funcname ", "funcname (..." and
23168 * "funcname(...", not "funcname!...". */
23169 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023170 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023171 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023172 return n;
23173}
23174
Bram Moolenaara1544c02013-05-30 12:35:52 +020023175 char_u *
23176get_expanded_name(name, check)
23177 char_u *name;
23178 int check;
23179{
23180 char_u *nm = name;
23181 char_u *p;
23182
23183 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23184
23185 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023186 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023187 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023188
Bram Moolenaara1544c02013-05-30 12:35:52 +020023189 vim_free(p);
23190 return NULL;
23191}
23192
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023193/*
23194 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023195 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23196 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023197 */
23198 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023199builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023200 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023201 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023202{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023203 char_u *p;
23204
23205 if (!ASCII_ISLOWER(name[0]))
23206 return FALSE;
23207 p = vim_strchr(name, AUTOLOAD_CHAR);
23208 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023209}
23210
Bram Moolenaar05159a02005-02-26 23:04:13 +000023211#if defined(FEAT_PROFILE) || defined(PROTO)
23212/*
23213 * Start profiling function "fp".
23214 */
23215 static void
23216func_do_profile(fp)
23217 ufunc_T *fp;
23218{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023219 int len = fp->uf_lines.ga_len;
23220
23221 if (len == 0)
23222 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023223 fp->uf_tm_count = 0;
23224 profile_zero(&fp->uf_tm_self);
23225 profile_zero(&fp->uf_tm_total);
23226 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023227 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023228 if (fp->uf_tml_total == NULL)
23229 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023230 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023231 if (fp->uf_tml_self == NULL)
23232 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023233 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023234 fp->uf_tml_idx = -1;
23235 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23236 || fp->uf_tml_self == NULL)
23237 return; /* out of memory */
23238
23239 fp->uf_profiling = TRUE;
23240}
23241
23242/*
23243 * Dump the profiling results for all functions in file "fd".
23244 */
23245 void
23246func_dump_profile(fd)
23247 FILE *fd;
23248{
23249 hashitem_T *hi;
23250 int todo;
23251 ufunc_T *fp;
23252 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023253 ufunc_T **sorttab;
23254 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023255
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023256 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023257 if (todo == 0)
23258 return; /* nothing to dump */
23259
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023260 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023261
Bram Moolenaar05159a02005-02-26 23:04:13 +000023262 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23263 {
23264 if (!HASHITEM_EMPTY(hi))
23265 {
23266 --todo;
23267 fp = HI2UF(hi);
23268 if (fp->uf_profiling)
23269 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023270 if (sorttab != NULL)
23271 sorttab[st_len++] = fp;
23272
Bram Moolenaar05159a02005-02-26 23:04:13 +000023273 if (fp->uf_name[0] == K_SPECIAL)
23274 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23275 else
23276 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23277 if (fp->uf_tm_count == 1)
23278 fprintf(fd, "Called 1 time\n");
23279 else
23280 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23281 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23282 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23283 fprintf(fd, "\n");
23284 fprintf(fd, "count total (s) self (s)\n");
23285
23286 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23287 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023288 if (FUNCLINE(fp, i) == NULL)
23289 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023290 prof_func_line(fd, fp->uf_tml_count[i],
23291 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023292 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23293 }
23294 fprintf(fd, "\n");
23295 }
23296 }
23297 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023298
23299 if (sorttab != NULL && st_len > 0)
23300 {
23301 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23302 prof_total_cmp);
23303 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23304 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23305 prof_self_cmp);
23306 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23307 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023308
23309 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023310}
Bram Moolenaar73830342005-02-28 22:48:19 +000023311
23312 static void
23313prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23314 FILE *fd;
23315 ufunc_T **sorttab;
23316 int st_len;
23317 char *title;
23318 int prefer_self; /* when equal print only self time */
23319{
23320 int i;
23321 ufunc_T *fp;
23322
23323 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23324 fprintf(fd, "count total (s) self (s) function\n");
23325 for (i = 0; i < 20 && i < st_len; ++i)
23326 {
23327 fp = sorttab[i];
23328 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23329 prefer_self);
23330 if (fp->uf_name[0] == K_SPECIAL)
23331 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23332 else
23333 fprintf(fd, " %s()\n", fp->uf_name);
23334 }
23335 fprintf(fd, "\n");
23336}
23337
23338/*
23339 * Print the count and times for one function or function line.
23340 */
23341 static void
23342prof_func_line(fd, count, total, self, prefer_self)
23343 FILE *fd;
23344 int count;
23345 proftime_T *total;
23346 proftime_T *self;
23347 int prefer_self; /* when equal print only self time */
23348{
23349 if (count > 0)
23350 {
23351 fprintf(fd, "%5d ", count);
23352 if (prefer_self && profile_equal(total, self))
23353 fprintf(fd, " ");
23354 else
23355 fprintf(fd, "%s ", profile_msg(total));
23356 if (!prefer_self && profile_equal(total, self))
23357 fprintf(fd, " ");
23358 else
23359 fprintf(fd, "%s ", profile_msg(self));
23360 }
23361 else
23362 fprintf(fd, " ");
23363}
23364
23365/*
23366 * Compare function for total time sorting.
23367 */
23368 static int
23369#ifdef __BORLANDC__
23370_RTLENTRYF
23371#endif
23372prof_total_cmp(s1, s2)
23373 const void *s1;
23374 const void *s2;
23375{
23376 ufunc_T *p1, *p2;
23377
23378 p1 = *(ufunc_T **)s1;
23379 p2 = *(ufunc_T **)s2;
23380 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23381}
23382
23383/*
23384 * Compare function for self time sorting.
23385 */
23386 static int
23387#ifdef __BORLANDC__
23388_RTLENTRYF
23389#endif
23390prof_self_cmp(s1, s2)
23391 const void *s1;
23392 const void *s2;
23393{
23394 ufunc_T *p1, *p2;
23395
23396 p1 = *(ufunc_T **)s1;
23397 p2 = *(ufunc_T **)s2;
23398 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23399}
23400
Bram Moolenaar05159a02005-02-26 23:04:13 +000023401#endif
23402
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023403/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023404 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023405 * Return TRUE if a package was loaded.
23406 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023407 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023408script_autoload(name, reload)
23409 char_u *name;
23410 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023411{
23412 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023413 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023414 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023415 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023416
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023417 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023418 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023419 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023420 return FALSE;
23421
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023422 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023423
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023424 /* Find the name in the list of previously loaded package names. Skip
23425 * "autoload/", it's always the same. */
23426 for (i = 0; i < ga_loaded.ga_len; ++i)
23427 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23428 break;
23429 if (!reload && i < ga_loaded.ga_len)
23430 ret = FALSE; /* was loaded already */
23431 else
23432 {
23433 /* Remember the name if it wasn't loaded already. */
23434 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23435 {
23436 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23437 tofree = NULL;
23438 }
23439
23440 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023441 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023442 ret = TRUE;
23443 }
23444
23445 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023446 return ret;
23447}
23448
23449/*
23450 * Return the autoload script name for a function or variable name.
23451 * Returns NULL when out of memory.
23452 */
23453 static char_u *
23454autoload_name(name)
23455 char_u *name;
23456{
23457 char_u *p;
23458 char_u *scriptname;
23459
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023460 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023461 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23462 if (scriptname == NULL)
23463 return FALSE;
23464 STRCPY(scriptname, "autoload/");
23465 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023466 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023467 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023468 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023469 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023470 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023471}
23472
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23474
23475/*
23476 * Function given to ExpandGeneric() to obtain the list of user defined
23477 * function names.
23478 */
23479 char_u *
23480get_user_func_name(xp, idx)
23481 expand_T *xp;
23482 int idx;
23483{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023484 static long_u done;
23485 static hashitem_T *hi;
23486 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487
23488 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023490 done = 0;
23491 hi = func_hashtab.ht_array;
23492 }
23493 if (done < func_hashtab.ht_used)
23494 {
23495 if (done++ > 0)
23496 ++hi;
23497 while (HASHITEM_EMPTY(hi))
23498 ++hi;
23499 fp = HI2UF(hi);
23500
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023501 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023502 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023503
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023504 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23505 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506
23507 cat_func_name(IObuff, fp);
23508 if (xp->xp_context != EXPAND_USER_FUNC)
23509 {
23510 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023511 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 STRCAT(IObuff, ")");
23513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 return IObuff;
23515 }
23516 return NULL;
23517}
23518
23519#endif /* FEAT_CMDL_COMPL */
23520
23521/*
23522 * Copy the function name of "fp" to buffer "buf".
23523 * "buf" must be able to hold the function name plus three bytes.
23524 * Takes care of script-local function names.
23525 */
23526 static void
23527cat_func_name(buf, fp)
23528 char_u *buf;
23529 ufunc_T *fp;
23530{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023531 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 {
23533 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023534 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 }
23536 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023537 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538}
23539
23540/*
23541 * ":delfunction {name}"
23542 */
23543 void
23544ex_delfunction(eap)
23545 exarg_T *eap;
23546{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023547 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 char_u *p;
23549 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023550 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551
23552 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023553 name = trans_function_name(&p, eap->skip, 0, &fudi);
23554 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023556 {
23557 if (fudi.fd_dict != NULL && !eap->skip)
23558 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 if (!ends_excmd(*skipwhite(p)))
23562 {
23563 vim_free(name);
23564 EMSG(_(e_trailing));
23565 return;
23566 }
23567 eap->nextcmd = check_nextcmd(p);
23568 if (eap->nextcmd != NULL)
23569 *p = NUL;
23570
23571 if (!eap->skip)
23572 fp = find_func(name);
23573 vim_free(name);
23574
23575 if (!eap->skip)
23576 {
23577 if (fp == NULL)
23578 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023579 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 return;
23581 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023582 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 {
23584 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23585 return;
23586 }
23587
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023588 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023590 /* Delete the dict item that refers to the function, it will
23591 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023592 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023594 else
23595 func_free(fp);
23596 }
23597}
23598
23599/*
23600 * Free a function and remove it from the list of functions.
23601 */
23602 static void
23603func_free(fp)
23604 ufunc_T *fp;
23605{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023606 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023607
23608 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023609 ga_clear_strings(&(fp->uf_args));
23610 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023611#ifdef FEAT_PROFILE
23612 vim_free(fp->uf_tml_count);
23613 vim_free(fp->uf_tml_total);
23614 vim_free(fp->uf_tml_self);
23615#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023616
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023617 /* remove the function from the function hashtable */
23618 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23619 if (HASHITEM_EMPTY(hi))
23620 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023621 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023622 hash_remove(&func_hashtab, hi);
23623
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023624 vim_free(fp);
23625}
23626
23627/*
23628 * Unreference a Function: decrement the reference count and free it when it
23629 * becomes zero. Only for numbered functions.
23630 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023631 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023632func_unref(name)
23633 char_u *name;
23634{
23635 ufunc_T *fp;
23636
23637 if (name != NULL && isdigit(*name))
23638 {
23639 fp = find_func(name);
23640 if (fp == NULL)
23641 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023642 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023643 {
23644 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023645 * when "uf_calls" becomes zero. */
23646 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023647 func_free(fp);
23648 }
23649 }
23650}
23651
23652/*
23653 * Count a reference to a Function.
23654 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023655 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023656func_ref(name)
23657 char_u *name;
23658{
23659 ufunc_T *fp;
23660
23661 if (name != NULL && isdigit(*name))
23662 {
23663 fp = find_func(name);
23664 if (fp == NULL)
23665 EMSG2(_(e_intern2), "func_ref()");
23666 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023667 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 }
23669}
23670
23671/*
23672 * Call a user function.
23673 */
23674 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023675call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 ufunc_T *fp; /* pointer to function */
23677 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023678 typval_T *argvars; /* arguments */
23679 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 linenr_T firstline; /* first line of range */
23681 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023682 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683{
Bram Moolenaar33570922005-01-25 22:26:29 +000023684 char_u *save_sourcing_name;
23685 linenr_T save_sourcing_lnum;
23686 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023687 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023688 int save_did_emsg;
23689 static int depth = 0;
23690 dictitem_T *v;
23691 int fixvar_idx = 0; /* index in fixvar[] */
23692 int i;
23693 int ai;
23694 char_u numbuf[NUMBUFLEN];
23695 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023696#ifdef FEAT_PROFILE
23697 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023698 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700
23701 /* If depth of calling is getting too high, don't execute the function */
23702 if (depth >= p_mfd)
23703 {
23704 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023705 rettv->v_type = VAR_NUMBER;
23706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 return;
23708 }
23709 ++depth;
23710
23711 line_breakcheck(); /* check for CTRL-C hit */
23712
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023713 fc = (funccall_T *)alloc(sizeof(funccall_T));
23714 fc->caller = current_funccal;
23715 current_funccal = fc;
23716 fc->func = fp;
23717 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023718 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023719 fc->linenr = 0;
23720 fc->returned = FALSE;
23721 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023723 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23724 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725
Bram Moolenaar33570922005-01-25 22:26:29 +000023726 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023727 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023728 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23729 * each argument variable and saves a lot of time.
23730 */
23731 /*
23732 * Init l: variables.
23733 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023734 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023735 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023736 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023737 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23738 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023739 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023740 name = v->di_key;
23741 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023742 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023743 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023744 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023745 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023746 v->di_tv.vval.v_dict = selfdict;
23747 ++selfdict->dv_refcount;
23748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023749
Bram Moolenaar33570922005-01-25 22:26:29 +000023750 /*
23751 * Init a: variables.
23752 * Set a:0 to "argcount".
23753 * Set a:000 to a list with room for the "..." arguments.
23754 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023755 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023756 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023757 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023758 /* Use "name" to avoid a warning from some compiler that checks the
23759 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023760 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023761 name = v->di_key;
23762 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023763 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023764 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023765 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023766 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023767 v->di_tv.vval.v_list = &fc->l_varlist;
23768 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23769 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23770 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023771
23772 /*
23773 * Set a:firstline to "firstline" and a:lastline to "lastline".
23774 * Set a:name to named arguments.
23775 * Set a:N to the "..." arguments.
23776 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023777 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023778 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023779 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023780 (varnumber_T)lastline);
23781 for (i = 0; i < argcount; ++i)
23782 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023783 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023784 if (ai < 0)
23785 /* named argument a:name */
23786 name = FUNCARG(fp, i);
23787 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023788 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023789 /* "..." argument a:1, a:2, etc. */
23790 sprintf((char *)numbuf, "%d", ai + 1);
23791 name = numbuf;
23792 }
23793 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23794 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023795 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023796 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23797 }
23798 else
23799 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023800 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23801 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023802 if (v == NULL)
23803 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023804 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023805 }
23806 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023807 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023808
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023809 /* Note: the values are copied directly to avoid alloc/free.
23810 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023811 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023812 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023813
23814 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23815 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023816 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23817 fc->l_listitems[ai].li_tv = argvars[i];
23818 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023819 }
23820 }
23821
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 /* Don't redraw while executing the function. */
23823 ++RedrawingDisabled;
23824 save_sourcing_name = sourcing_name;
23825 save_sourcing_lnum = sourcing_lnum;
23826 sourcing_lnum = 1;
23827 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023828 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 if (sourcing_name != NULL)
23830 {
23831 if (save_sourcing_name != NULL
23832 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23833 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23834 else
23835 STRCPY(sourcing_name, "function ");
23836 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23837
23838 if (p_verbose >= 12)
23839 {
23840 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023841 verbose_enter_scroll();
23842
Bram Moolenaar555b2802005-05-19 21:08:39 +000023843 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 if (p_verbose >= 14)
23845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023847 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023848 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023849 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850
23851 msg_puts((char_u *)"(");
23852 for (i = 0; i < argcount; ++i)
23853 {
23854 if (i > 0)
23855 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023856 if (argvars[i].v_type == VAR_NUMBER)
23857 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 else
23859 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023860 /* Do not want errors such as E724 here. */
23861 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023862 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023863 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023864 if (s != NULL)
23865 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023866 if (vim_strsize(s) > MSG_BUF_CLEN)
23867 {
23868 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23869 s = buf;
23870 }
23871 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023872 vim_free(tofree);
23873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 }
23875 }
23876 msg_puts((char_u *)")");
23877 }
23878 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023879
23880 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 --no_wait_return;
23882 }
23883 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023884#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023885 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023886 {
23887 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23888 func_do_profile(fp);
23889 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023890 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023891 {
23892 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023893 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023894 profile_zero(&fp->uf_tm_children);
23895 }
23896 script_prof_save(&wait_start);
23897 }
23898#endif
23899
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023901 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 save_did_emsg = did_emsg;
23903 did_emsg = FALSE;
23904
23905 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023906 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23908
23909 --RedrawingDisabled;
23910
23911 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023912 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023914 clear_tv(rettv);
23915 rettv->v_type = VAR_NUMBER;
23916 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 }
23918
Bram Moolenaar05159a02005-02-26 23:04:13 +000023919#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023920 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023921 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023922 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023923 profile_end(&call_start);
23924 profile_sub_wait(&wait_start, &call_start);
23925 profile_add(&fp->uf_tm_total, &call_start);
23926 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023927 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023928 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023929 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23930 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023931 }
23932 }
23933#endif
23934
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 /* when being verbose, mention the return value */
23936 if (p_verbose >= 12)
23937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023939 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023942 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023943 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023944 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023945 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023946 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023948 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023949 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023950 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023951 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023952
Bram Moolenaar555b2802005-05-19 21:08:39 +000023953 /* The value may be very long. Skip the middle part, so that we
23954 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023955 * truncate it at the end. Don't want errors such as E724 here. */
23956 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023957 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023958 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023959 if (s != NULL)
23960 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023961 if (vim_strsize(s) > MSG_BUF_CLEN)
23962 {
23963 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23964 s = buf;
23965 }
23966 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023967 vim_free(tofree);
23968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 }
23970 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023971
23972 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 --no_wait_return;
23974 }
23975
23976 vim_free(sourcing_name);
23977 sourcing_name = save_sourcing_name;
23978 sourcing_lnum = save_sourcing_lnum;
23979 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023980#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023981 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023982 script_prof_restore(&wait_start);
23983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984
23985 if (p_verbose >= 12 && sourcing_name != NULL)
23986 {
23987 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023988 verbose_enter_scroll();
23989
Bram Moolenaar555b2802005-05-19 21:08:39 +000023990 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023992
23993 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 --no_wait_return;
23995 }
23996
23997 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023998 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024000
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024001 /* If the a:000 list and the l: and a: dicts are not referenced we can
24002 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024003 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24004 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24005 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24006 {
24007 free_funccal(fc, FALSE);
24008 }
24009 else
24010 {
24011 hashitem_T *hi;
24012 listitem_T *li;
24013 int todo;
24014
24015 /* "fc" is still in use. This can happen when returning "a:000" or
24016 * assigning "l:" to a global variable.
24017 * Link "fc" in the list for garbage collection later. */
24018 fc->caller = previous_funccal;
24019 previous_funccal = fc;
24020
24021 /* Make a copy of the a: variables, since we didn't do that above. */
24022 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24023 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24024 {
24025 if (!HASHITEM_EMPTY(hi))
24026 {
24027 --todo;
24028 v = HI2DI(hi);
24029 copy_tv(&v->di_tv, &v->di_tv);
24030 }
24031 }
24032
24033 /* Make a copy of the a:000 items, since we didn't do that above. */
24034 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24035 copy_tv(&li->li_tv, &li->li_tv);
24036 }
24037}
24038
24039/*
24040 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024041 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024042 */
24043 static int
24044can_free_funccal(fc, copyID)
24045 funccall_T *fc;
24046 int copyID;
24047{
24048 return (fc->l_varlist.lv_copyID != copyID
24049 && fc->l_vars.dv_copyID != copyID
24050 && fc->l_avars.dv_copyID != copyID);
24051}
24052
24053/*
24054 * Free "fc" and what it contains.
24055 */
24056 static void
24057free_funccal(fc, free_val)
24058 funccall_T *fc;
24059 int free_val; /* a: vars were allocated */
24060{
24061 listitem_T *li;
24062
24063 /* The a: variables typevals may not have been allocated, only free the
24064 * allocated variables. */
24065 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24066
24067 /* free all l: variables */
24068 vars_clear(&fc->l_vars.dv_hashtab);
24069
24070 /* Free the a:000 variables if they were allocated. */
24071 if (free_val)
24072 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24073 clear_tv(&li->li_tv);
24074
24075 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076}
24077
24078/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024079 * Add a number variable "name" to dict "dp" with value "nr".
24080 */
24081 static void
24082add_nr_var(dp, v, name, nr)
24083 dict_T *dp;
24084 dictitem_T *v;
24085 char *name;
24086 varnumber_T nr;
24087{
24088 STRCPY(v->di_key, name);
24089 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24090 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24091 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024092 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024093 v->di_tv.vval.v_number = nr;
24094}
24095
24096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 * ":return [expr]"
24098 */
24099 void
24100ex_return(eap)
24101 exarg_T *eap;
24102{
24103 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024104 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105 int returning = FALSE;
24106
24107 if (current_funccal == NULL)
24108 {
24109 EMSG(_("E133: :return not inside a function"));
24110 return;
24111 }
24112
24113 if (eap->skip)
24114 ++emsg_skip;
24115
24116 eap->nextcmd = NULL;
24117 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024118 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 {
24120 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024121 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024123 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 }
24125 /* It's safer to return also on error. */
24126 else if (!eap->skip)
24127 {
24128 /*
24129 * Return unless the expression evaluation has been cancelled due to an
24130 * aborting error, an interrupt, or an exception.
24131 */
24132 if (!aborting())
24133 returning = do_return(eap, FALSE, TRUE, NULL);
24134 }
24135
24136 /* When skipping or the return gets pending, advance to the next command
24137 * in this line (!returning). Otherwise, ignore the rest of the line.
24138 * Following lines will be ignored by get_func_line(). */
24139 if (returning)
24140 eap->nextcmd = NULL;
24141 else if (eap->nextcmd == NULL) /* no argument */
24142 eap->nextcmd = check_nextcmd(arg);
24143
24144 if (eap->skip)
24145 --emsg_skip;
24146}
24147
24148/*
24149 * Return from a function. Possibly makes the return pending. Also called
24150 * for a pending return at the ":endtry" or after returning from an extra
24151 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024152 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024153 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 * FALSE when the return gets pending.
24155 */
24156 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024157do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 exarg_T *eap;
24159 int reanimate;
24160 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024161 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162{
24163 int idx;
24164 struct condstack *cstack = eap->cstack;
24165
24166 if (reanimate)
24167 /* Undo the return. */
24168 current_funccal->returned = FALSE;
24169
24170 /*
24171 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24172 * not in its finally clause (which then is to be executed next) is found.
24173 * In this case, make the ":return" pending for execution at the ":endtry".
24174 * Otherwise, return normally.
24175 */
24176 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24177 if (idx >= 0)
24178 {
24179 cstack->cs_pending[idx] = CSTP_RETURN;
24180
24181 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024182 /* A pending return again gets pending. "rettv" points to an
24183 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024185 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 else
24187 {
24188 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024189 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024191 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024193 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 {
24195 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024196 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024197 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 else
24199 EMSG(_(e_outofmem));
24200 }
24201 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024202 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203
24204 if (reanimate)
24205 {
24206 /* The pending return value could be overwritten by a ":return"
24207 * without argument in a finally clause; reset the default
24208 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024209 current_funccal->rettv->v_type = VAR_NUMBER;
24210 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 }
24212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024213 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 }
24215 else
24216 {
24217 current_funccal->returned = TRUE;
24218
24219 /* If the return is carried out now, store the return value. For
24220 * a return immediately after reanimation, the value is already
24221 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024222 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024224 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024225 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024227 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 }
24229 }
24230
24231 return idx < 0;
24232}
24233
24234/*
24235 * Free the variable with a pending return value.
24236 */
24237 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024238discard_pending_return(rettv)
24239 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024240{
Bram Moolenaar33570922005-01-25 22:26:29 +000024241 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242}
24243
24244/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024245 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 * is an allocated string. Used by report_pending() for verbose messages.
24247 */
24248 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024249get_return_cmd(rettv)
24250 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024252 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024253 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024254 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024256 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024257 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024258 if (s == NULL)
24259 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024260
24261 STRCPY(IObuff, ":return ");
24262 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24263 if (STRLEN(s) + 8 >= IOSIZE)
24264 STRCPY(IObuff + IOSIZE - 4, "...");
24265 vim_free(tofree);
24266 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267}
24268
24269/*
24270 * Get next function line.
24271 * Called by do_cmdline() to get the next line.
24272 * Returns allocated string, or NULL for end of function.
24273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 char_u *
24275get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024276 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024278 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279{
Bram Moolenaar33570922005-01-25 22:26:29 +000024280 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024281 ufunc_T *fp = fcp->func;
24282 char_u *retval;
24283 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284
24285 /* If breakpoints have been added/deleted need to check for it. */
24286 if (fcp->dbg_tick != debug_tick)
24287 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024288 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 sourcing_lnum);
24290 fcp->dbg_tick = debug_tick;
24291 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024292#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024293 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024294 func_line_end(cookie);
24295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296
Bram Moolenaar05159a02005-02-26 23:04:13 +000024297 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024298 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24299 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 retval = NULL;
24301 else
24302 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024303 /* Skip NULL lines (continuation lines). */
24304 while (fcp->linenr < gap->ga_len
24305 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24306 ++fcp->linenr;
24307 if (fcp->linenr >= gap->ga_len)
24308 retval = NULL;
24309 else
24310 {
24311 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24312 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024313#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024314 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024315 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024316#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 }
24319
24320 /* Did we encounter a breakpoint? */
24321 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24322 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024323 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024325 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326 sourcing_lnum);
24327 fcp->dbg_tick = debug_tick;
24328 }
24329
24330 return retval;
24331}
24332
Bram Moolenaar05159a02005-02-26 23:04:13 +000024333#if defined(FEAT_PROFILE) || defined(PROTO)
24334/*
24335 * Called when starting to read a function line.
24336 * "sourcing_lnum" must be correct!
24337 * When skipping lines it may not actually be executed, but we won't find out
24338 * until later and we need to store the time now.
24339 */
24340 void
24341func_line_start(cookie)
24342 void *cookie;
24343{
24344 funccall_T *fcp = (funccall_T *)cookie;
24345 ufunc_T *fp = fcp->func;
24346
24347 if (fp->uf_profiling && sourcing_lnum >= 1
24348 && sourcing_lnum <= fp->uf_lines.ga_len)
24349 {
24350 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024351 /* Skip continuation lines. */
24352 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24353 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024354 fp->uf_tml_execed = FALSE;
24355 profile_start(&fp->uf_tml_start);
24356 profile_zero(&fp->uf_tml_children);
24357 profile_get_wait(&fp->uf_tml_wait);
24358 }
24359}
24360
24361/*
24362 * Called when actually executing a function line.
24363 */
24364 void
24365func_line_exec(cookie)
24366 void *cookie;
24367{
24368 funccall_T *fcp = (funccall_T *)cookie;
24369 ufunc_T *fp = fcp->func;
24370
24371 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24372 fp->uf_tml_execed = TRUE;
24373}
24374
24375/*
24376 * Called when done with a function line.
24377 */
24378 void
24379func_line_end(cookie)
24380 void *cookie;
24381{
24382 funccall_T *fcp = (funccall_T *)cookie;
24383 ufunc_T *fp = fcp->func;
24384
24385 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24386 {
24387 if (fp->uf_tml_execed)
24388 {
24389 ++fp->uf_tml_count[fp->uf_tml_idx];
24390 profile_end(&fp->uf_tml_start);
24391 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024392 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024393 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24394 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024395 }
24396 fp->uf_tml_idx = -1;
24397 }
24398}
24399#endif
24400
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401/*
24402 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024403 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 */
24405 int
24406func_has_ended(cookie)
24407 void *cookie;
24408{
Bram Moolenaar33570922005-01-25 22:26:29 +000024409 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410
24411 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24412 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024413 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 || fcp->returned);
24415}
24416
24417/*
24418 * return TRUE if cookie indicates a function which "abort"s on errors.
24419 */
24420 int
24421func_has_abort(cookie)
24422 void *cookie;
24423{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024424 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425}
24426
24427#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24428typedef enum
24429{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024430 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24431 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24432 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433} var_flavour_T;
24434
24435static var_flavour_T var_flavour __ARGS((char_u *varname));
24436
24437 static var_flavour_T
24438var_flavour(varname)
24439 char_u *varname;
24440{
24441 char_u *p = varname;
24442
24443 if (ASCII_ISUPPER(*p))
24444 {
24445 while (*(++p))
24446 if (ASCII_ISLOWER(*p))
24447 return VAR_FLAVOUR_SESSION;
24448 return VAR_FLAVOUR_VIMINFO;
24449 }
24450 else
24451 return VAR_FLAVOUR_DEFAULT;
24452}
24453#endif
24454
24455#if defined(FEAT_VIMINFO) || defined(PROTO)
24456/*
24457 * Restore global vars that start with a capital from the viminfo file
24458 */
24459 int
24460read_viminfo_varlist(virp, writing)
24461 vir_T *virp;
24462 int writing;
24463{
24464 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024465 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024466 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024467
24468 if (!writing && (find_viminfo_parameter('!') != NULL))
24469 {
24470 tab = vim_strchr(virp->vir_line + 1, '\t');
24471 if (tab != NULL)
24472 {
24473 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024474 switch (*tab)
24475 {
24476 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024477#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024478 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024479#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024480 case 'D': type = VAR_DICT; break;
24481 case 'L': type = VAR_LIST; break;
24482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483
24484 tab = vim_strchr(tab, '\t');
24485 if (tab != NULL)
24486 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024487 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024488 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024489 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024491#ifdef FEAT_FLOAT
24492 else if (type == VAR_FLOAT)
24493 (void)string2float(tab + 1, &tv.vval.v_float);
24494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024496 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024497 if (type == VAR_DICT || type == VAR_LIST)
24498 {
24499 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24500
24501 if (etv == NULL)
24502 /* Failed to parse back the dict or list, use it as a
24503 * string. */
24504 tv.v_type = VAR_STRING;
24505 else
24506 {
24507 vim_free(tv.vval.v_string);
24508 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024509 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024510 }
24511 }
24512
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024513 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024514
24515 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024516 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024517 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24518 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519 }
24520 }
24521 }
24522
24523 return viminfo_readline(virp);
24524}
24525
24526/*
24527 * Write global vars that start with a capital to the viminfo file
24528 */
24529 void
24530write_viminfo_varlist(fp)
24531 FILE *fp;
24532{
Bram Moolenaar33570922005-01-25 22:26:29 +000024533 hashitem_T *hi;
24534 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024535 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024536 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024537 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024538 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024539 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540
24541 if (find_viminfo_parameter('!') == NULL)
24542 return;
24543
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024544 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024545
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024546 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024547 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024549 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024551 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024552 this_var = HI2DI(hi);
24553 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024554 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024555 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024556 {
24557 case VAR_STRING: s = "STR"; break;
24558 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024559#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024560 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024561#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024562 case VAR_DICT: s = "DIC"; break;
24563 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024564 default: continue;
24565 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024566 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024567 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024568 if (p != NULL)
24569 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024570 vim_free(tofree);
24571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024572 }
24573 }
24574}
24575#endif
24576
24577#if defined(FEAT_SESSION) || defined(PROTO)
24578 int
24579store_session_globals(fd)
24580 FILE *fd;
24581{
Bram Moolenaar33570922005-01-25 22:26:29 +000024582 hashitem_T *hi;
24583 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024584 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 char_u *p, *t;
24586
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024587 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024588 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024589 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024590 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024592 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024593 this_var = HI2DI(hi);
24594 if ((this_var->di_tv.v_type == VAR_NUMBER
24595 || this_var->di_tv.v_type == VAR_STRING)
24596 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024597 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024598 /* Escape special characters with a backslash. Turn a LF and
24599 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024600 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024601 (char_u *)"\\\"\n\r");
24602 if (p == NULL) /* out of memory */
24603 break;
24604 for (t = p; *t != NUL; ++t)
24605 if (*t == '\n')
24606 *t = 'n';
24607 else if (*t == '\r')
24608 *t = 'r';
24609 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024610 this_var->di_key,
24611 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24612 : ' ',
24613 p,
24614 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24615 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024616 || put_eol(fd) == FAIL)
24617 {
24618 vim_free(p);
24619 return FAIL;
24620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 vim_free(p);
24622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024623#ifdef FEAT_FLOAT
24624 else if (this_var->di_tv.v_type == VAR_FLOAT
24625 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24626 {
24627 float_T f = this_var->di_tv.vval.v_float;
24628 int sign = ' ';
24629
24630 if (f < 0)
24631 {
24632 f = -f;
24633 sign = '-';
24634 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024635 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024636 this_var->di_key, sign, f) < 0)
24637 || put_eol(fd) == FAIL)
24638 return FAIL;
24639 }
24640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 }
24642 }
24643 return OK;
24644}
24645#endif
24646
Bram Moolenaar661b1822005-07-28 22:36:45 +000024647/*
24648 * Display script name where an item was last set.
24649 * Should only be invoked when 'verbose' is non-zero.
24650 */
24651 void
24652last_set_msg(scriptID)
24653 scid_T scriptID;
24654{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024655 char_u *p;
24656
Bram Moolenaar661b1822005-07-28 22:36:45 +000024657 if (scriptID != 0)
24658 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024659 p = home_replace_save(NULL, get_scriptname(scriptID));
24660 if (p != NULL)
24661 {
24662 verbose_enter();
24663 MSG_PUTS(_("\n\tLast set from "));
24664 MSG_PUTS(p);
24665 vim_free(p);
24666 verbose_leave();
24667 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024668 }
24669}
24670
Bram Moolenaard812df62008-11-09 12:46:09 +000024671/*
24672 * List v:oldfiles in a nice way.
24673 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024674 void
24675ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024676 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024677{
24678 list_T *l = vimvars[VV_OLDFILES].vv_list;
24679 listitem_T *li;
24680 int nr = 0;
24681
24682 if (l == NULL)
24683 msg((char_u *)_("No old files"));
24684 else
24685 {
24686 msg_start();
24687 msg_scroll = TRUE;
24688 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24689 {
24690 msg_outnum((long)++nr);
24691 MSG_PUTS(": ");
24692 msg_outtrans(get_tv_string(&li->li_tv));
24693 msg_putchar('\n');
24694 out_flush(); /* output one line at a time */
24695 ui_breakcheck();
24696 }
24697 /* Assume "got_int" was set to truncate the listing. */
24698 got_int = FALSE;
24699
24700#ifdef FEAT_BROWSE_CMD
24701 if (cmdmod.browse)
24702 {
24703 quit_more = FALSE;
24704 nr = prompt_for_number(FALSE);
24705 msg_starthere();
24706 if (nr > 0)
24707 {
24708 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24709 (long)nr);
24710
24711 if (p != NULL)
24712 {
24713 p = expand_env_save(p);
24714 eap->arg = p;
24715 eap->cmdidx = CMD_edit;
24716 cmdmod.browse = FALSE;
24717 do_exedit(eap, NULL);
24718 vim_free(p);
24719 }
24720 }
24721 }
24722#endif
24723 }
24724}
24725
Bram Moolenaar53744302015-07-17 17:38:22 +020024726/* reset v:option_new, v:option_old and v:option_type */
24727 void
24728reset_v_option_vars()
24729{
24730 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24731 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24732 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24733}
24734
24735
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736#endif /* FEAT_EVAL */
24737
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024739#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740
24741#ifdef WIN3264
24742/*
24743 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24744 */
24745static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24746static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24747static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24748
24749/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024750 * Get the short path (8.3) for the filename in "fnamep".
24751 * Only works for a valid file name.
24752 * When the path gets longer "fnamep" is changed and the allocated buffer
24753 * is put in "bufp".
24754 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24755 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756 */
24757 static int
24758get_short_pathname(fnamep, bufp, fnamelen)
24759 char_u **fnamep;
24760 char_u **bufp;
24761 int *fnamelen;
24762{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024763 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764 char_u *newbuf;
24765
24766 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767 l = GetShortPathName(*fnamep, *fnamep, len);
24768 if (l > len - 1)
24769 {
24770 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024771 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772 newbuf = vim_strnsave(*fnamep, l);
24773 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024774 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775
24776 vim_free(*bufp);
24777 *fnamep = *bufp = newbuf;
24778
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024779 /* Really should always succeed, as the buffer is big enough. */
24780 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 }
24782
24783 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024784 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785}
24786
24787/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024788 * Get the short path (8.3) for the filename in "fname". The converted
24789 * path is returned in "bufp".
24790 *
24791 * Some of the directories specified in "fname" may not exist. This function
24792 * will shorten the existing directories at the beginning of the path and then
24793 * append the remaining non-existing path.
24794 *
24795 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024796 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024797 * bufp - Pointer to an allocated buffer for the filename.
24798 * fnamelen - Length of the filename pointed to by fname
24799 *
24800 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801 */
24802 static int
24803shortpath_for_invalid_fname(fname, bufp, fnamelen)
24804 char_u **fname;
24805 char_u **bufp;
24806 int *fnamelen;
24807{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024808 char_u *short_fname, *save_fname, *pbuf_unused;
24809 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024811 int old_len, len;
24812 int new_len, sfx_len;
24813 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814
24815 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024816 old_len = *fnamelen;
24817 save_fname = vim_strnsave(*fname, old_len);
24818 pbuf_unused = NULL;
24819 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024821 endp = save_fname + old_len - 1; /* Find the end of the copy */
24822 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024824 /*
24825 * Try shortening the supplied path till it succeeds by removing one
24826 * directory at a time from the tail of the path.
24827 */
24828 len = 0;
24829 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024831 /* go back one path-separator */
24832 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24833 --endp;
24834 if (endp <= save_fname)
24835 break; /* processed the complete path */
24836
24837 /*
24838 * Replace the path separator with a NUL and try to shorten the
24839 * resulting path.
24840 */
24841 ch = *endp;
24842 *endp = 0;
24843 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024844 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024845 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24846 {
24847 retval = FAIL;
24848 goto theend;
24849 }
24850 *endp = ch; /* preserve the string */
24851
24852 if (len > 0)
24853 break; /* successfully shortened the path */
24854
24855 /* failed to shorten the path. Skip the path separator */
24856 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857 }
24858
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024859 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024861 /*
24862 * Succeeded in shortening the path. Now concatenate the shortened
24863 * path with the remaining path at the tail.
24864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024866 /* Compute the length of the new path. */
24867 sfx_len = (int)(save_endp - endp) + 1;
24868 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024870 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024872 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024874 /* There is not enough space in the currently allocated string,
24875 * copy it to a buffer big enough. */
24876 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024878 {
24879 retval = FAIL;
24880 goto theend;
24881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882 }
24883 else
24884 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024885 /* Transfer short_fname to the main buffer (it's big enough),
24886 * unless get_short_pathname() did its work in-place. */
24887 *fname = *bufp = save_fname;
24888 if (short_fname != save_fname)
24889 vim_strncpy(save_fname, short_fname, len);
24890 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024892
24893 /* concat the not-shortened part of the path */
24894 vim_strncpy(*fname + len, endp, sfx_len);
24895 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024897
24898theend:
24899 vim_free(pbuf_unused);
24900 vim_free(save_fname);
24901
24902 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903}
24904
24905/*
24906 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024907 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 */
24909 static int
24910shortpath_for_partial(fnamep, bufp, fnamelen)
24911 char_u **fnamep;
24912 char_u **bufp;
24913 int *fnamelen;
24914{
24915 int sepcount, len, tflen;
24916 char_u *p;
24917 char_u *pbuf, *tfname;
24918 int hasTilde;
24919
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024920 /* Count up the path separators from the RHS.. so we know which part
24921 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024923 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 if (vim_ispathsep(*p))
24925 ++sepcount;
24926
24927 /* Need full path first (use expand_env() to remove a "~/") */
24928 hasTilde = (**fnamep == '~');
24929 if (hasTilde)
24930 pbuf = tfname = expand_env_save(*fnamep);
24931 else
24932 pbuf = tfname = FullName_save(*fnamep, FALSE);
24933
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024934 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024936 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938
24939 if (len == 0)
24940 {
24941 /* Don't have a valid filename, so shorten the rest of the
24942 * path if we can. This CAN give us invalid 8.3 filenames, but
24943 * there's not a lot of point in guessing what it might be.
24944 */
24945 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024946 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024948 }
24949
24950 /* Count the paths backward to find the beginning of the desired string. */
24951 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024952 {
24953#ifdef FEAT_MBYTE
24954 if (has_mbyte)
24955 p -= mb_head_off(tfname, p);
24956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 if (vim_ispathsep(*p))
24958 {
24959 if (sepcount == 0 || (hasTilde && sepcount == 1))
24960 break;
24961 else
24962 sepcount --;
24963 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024965 if (hasTilde)
24966 {
24967 --p;
24968 if (p >= tfname)
24969 *p = '~';
24970 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024971 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024972 }
24973 else
24974 ++p;
24975
24976 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24977 vim_free(*bufp);
24978 *fnamelen = (int)STRLEN(p);
24979 *bufp = pbuf;
24980 *fnamep = p;
24981
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024982 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983}
24984#endif /* WIN3264 */
24985
24986/*
24987 * Adjust a filename, according to a string of modifiers.
24988 * *fnamep must be NUL terminated when called. When returning, the length is
24989 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024990 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991 * When there is an error, *fnamep is set to NULL.
24992 */
24993 int
24994modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24995 char_u *src; /* string with modifiers */
24996 int *usedlen; /* characters after src that are used */
24997 char_u **fnamep; /* file name so far */
24998 char_u **bufp; /* buffer for allocated file name or NULL */
24999 int *fnamelen; /* length of fnamep */
25000{
25001 int valid = 0;
25002 char_u *tail;
25003 char_u *s, *p, *pbuf;
25004 char_u dirname[MAXPATHL];
25005 int c;
25006 int has_fullname = 0;
25007#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025008 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009 int has_shortname = 0;
25010#endif
25011
25012repeat:
25013 /* ":p" - full path/file_name */
25014 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25015 {
25016 has_fullname = 1;
25017
25018 valid |= VALID_PATH;
25019 *usedlen += 2;
25020
25021 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25022 if ((*fnamep)[0] == '~'
25023#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25024 && ((*fnamep)[1] == '/'
25025# ifdef BACKSLASH_IN_FILENAME
25026 || (*fnamep)[1] == '\\'
25027# endif
25028 || (*fnamep)[1] == NUL)
25029
25030#endif
25031 )
25032 {
25033 *fnamep = expand_env_save(*fnamep);
25034 vim_free(*bufp); /* free any allocated file name */
25035 *bufp = *fnamep;
25036 if (*fnamep == NULL)
25037 return -1;
25038 }
25039
25040 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025041 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 {
25043 if (vim_ispathsep(*p)
25044 && p[1] == '.'
25045 && (p[2] == NUL
25046 || vim_ispathsep(p[2])
25047 || (p[2] == '.'
25048 && (p[3] == NUL || vim_ispathsep(p[3])))))
25049 break;
25050 }
25051
25052 /* FullName_save() is slow, don't use it when not needed. */
25053 if (*p != NUL || !vim_isAbsName(*fnamep))
25054 {
25055 *fnamep = FullName_save(*fnamep, *p != NUL);
25056 vim_free(*bufp); /* free any allocated file name */
25057 *bufp = *fnamep;
25058 if (*fnamep == NULL)
25059 return -1;
25060 }
25061
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025062#ifdef WIN3264
25063# if _WIN32_WINNT >= 0x0500
25064 if (vim_strchr(*fnamep, '~') != NULL)
25065 {
25066 /* Expand 8.3 filename to full path. Needed to make sure the same
25067 * file does not have two different names.
25068 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25069 p = alloc(_MAX_PATH + 1);
25070 if (p != NULL)
25071 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025072 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025073 {
25074 vim_free(*bufp);
25075 *bufp = *fnamep = p;
25076 }
25077 else
25078 vim_free(p);
25079 }
25080 }
25081# endif
25082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083 /* Append a path separator to a directory. */
25084 if (mch_isdir(*fnamep))
25085 {
25086 /* Make room for one or two extra characters. */
25087 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25088 vim_free(*bufp); /* free any allocated file name */
25089 *bufp = *fnamep;
25090 if (*fnamep == NULL)
25091 return -1;
25092 add_pathsep(*fnamep);
25093 }
25094 }
25095
25096 /* ":." - path relative to the current directory */
25097 /* ":~" - path relative to the home directory */
25098 /* ":8" - shortname path - postponed till after */
25099 while (src[*usedlen] == ':'
25100 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25101 {
25102 *usedlen += 2;
25103 if (c == '8')
25104 {
25105#ifdef WIN3264
25106 has_shortname = 1; /* Postpone this. */
25107#endif
25108 continue;
25109 }
25110 pbuf = NULL;
25111 /* Need full path first (use expand_env() to remove a "~/") */
25112 if (!has_fullname)
25113 {
25114 if (c == '.' && **fnamep == '~')
25115 p = pbuf = expand_env_save(*fnamep);
25116 else
25117 p = pbuf = FullName_save(*fnamep, FALSE);
25118 }
25119 else
25120 p = *fnamep;
25121
25122 has_fullname = 0;
25123
25124 if (p != NULL)
25125 {
25126 if (c == '.')
25127 {
25128 mch_dirname(dirname, MAXPATHL);
25129 s = shorten_fname(p, dirname);
25130 if (s != NULL)
25131 {
25132 *fnamep = s;
25133 if (pbuf != NULL)
25134 {
25135 vim_free(*bufp); /* free any allocated file name */
25136 *bufp = pbuf;
25137 pbuf = NULL;
25138 }
25139 }
25140 }
25141 else
25142 {
25143 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25144 /* Only replace it when it starts with '~' */
25145 if (*dirname == '~')
25146 {
25147 s = vim_strsave(dirname);
25148 if (s != NULL)
25149 {
25150 *fnamep = s;
25151 vim_free(*bufp);
25152 *bufp = s;
25153 }
25154 }
25155 }
25156 vim_free(pbuf);
25157 }
25158 }
25159
25160 tail = gettail(*fnamep);
25161 *fnamelen = (int)STRLEN(*fnamep);
25162
25163 /* ":h" - head, remove "/file_name", can be repeated */
25164 /* Don't remove the first "/" or "c:\" */
25165 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25166 {
25167 valid |= VALID_HEAD;
25168 *usedlen += 2;
25169 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025170 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025171 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172 *fnamelen = (int)(tail - *fnamep);
25173#ifdef VMS
25174 if (*fnamelen > 0)
25175 *fnamelen += 1; /* the path separator is part of the path */
25176#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025177 if (*fnamelen == 0)
25178 {
25179 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25180 p = vim_strsave((char_u *)".");
25181 if (p == NULL)
25182 return -1;
25183 vim_free(*bufp);
25184 *bufp = *fnamep = tail = p;
25185 *fnamelen = 1;
25186 }
25187 else
25188 {
25189 while (tail > s && !after_pathsep(s, tail))
25190 mb_ptr_back(*fnamep, tail);
25191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025192 }
25193
25194 /* ":8" - shortname */
25195 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25196 {
25197 *usedlen += 2;
25198#ifdef WIN3264
25199 has_shortname = 1;
25200#endif
25201 }
25202
25203#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025204 /*
25205 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 */
25207 if (has_shortname)
25208 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025209 /* Copy the string if it is shortened by :h and when it wasn't copied
25210 * yet, because we are going to change it in place. Avoids changing
25211 * the buffer name for "%:8". */
25212 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213 {
25214 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025215 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025216 return -1;
25217 vim_free(*bufp);
25218 *bufp = *fnamep = p;
25219 }
25220
25221 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025222 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223 if (!has_fullname && !vim_isAbsName(*fnamep))
25224 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025225 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025226 return -1;
25227 }
25228 else
25229 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025230 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231
Bram Moolenaardc935552011-08-17 15:23:23 +020025232 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025234 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 return -1;
25236
25237 if (l == 0)
25238 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025239 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025241 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242 return -1;
25243 }
25244 *fnamelen = l;
25245 }
25246 }
25247#endif /* WIN3264 */
25248
25249 /* ":t" - tail, just the basename */
25250 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25251 {
25252 *usedlen += 2;
25253 *fnamelen -= (int)(tail - *fnamep);
25254 *fnamep = tail;
25255 }
25256
25257 /* ":e" - extension, can be repeated */
25258 /* ":r" - root, without extension, can be repeated */
25259 while (src[*usedlen] == ':'
25260 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25261 {
25262 /* find a '.' in the tail:
25263 * - for second :e: before the current fname
25264 * - otherwise: The last '.'
25265 */
25266 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25267 s = *fnamep - 2;
25268 else
25269 s = *fnamep + *fnamelen - 1;
25270 for ( ; s > tail; --s)
25271 if (s[0] == '.')
25272 break;
25273 if (src[*usedlen + 1] == 'e') /* :e */
25274 {
25275 if (s > tail)
25276 {
25277 *fnamelen += (int)(*fnamep - (s + 1));
25278 *fnamep = s + 1;
25279#ifdef VMS
25280 /* cut version from the extension */
25281 s = *fnamep + *fnamelen - 1;
25282 for ( ; s > *fnamep; --s)
25283 if (s[0] == ';')
25284 break;
25285 if (s > *fnamep)
25286 *fnamelen = s - *fnamep;
25287#endif
25288 }
25289 else if (*fnamep <= tail)
25290 *fnamelen = 0;
25291 }
25292 else /* :r */
25293 {
25294 if (s > tail) /* remove one extension */
25295 *fnamelen = (int)(s - *fnamep);
25296 }
25297 *usedlen += 2;
25298 }
25299
25300 /* ":s?pat?foo?" - substitute */
25301 /* ":gs?pat?foo?" - global substitute */
25302 if (src[*usedlen] == ':'
25303 && (src[*usedlen + 1] == 's'
25304 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25305 {
25306 char_u *str;
25307 char_u *pat;
25308 char_u *sub;
25309 int sep;
25310 char_u *flags;
25311 int didit = FALSE;
25312
25313 flags = (char_u *)"";
25314 s = src + *usedlen + 2;
25315 if (src[*usedlen + 1] == 'g')
25316 {
25317 flags = (char_u *)"g";
25318 ++s;
25319 }
25320
25321 sep = *s++;
25322 if (sep)
25323 {
25324 /* find end of pattern */
25325 p = vim_strchr(s, sep);
25326 if (p != NULL)
25327 {
25328 pat = vim_strnsave(s, (int)(p - s));
25329 if (pat != NULL)
25330 {
25331 s = p + 1;
25332 /* find end of substitution */
25333 p = vim_strchr(s, sep);
25334 if (p != NULL)
25335 {
25336 sub = vim_strnsave(s, (int)(p - s));
25337 str = vim_strnsave(*fnamep, *fnamelen);
25338 if (sub != NULL && str != NULL)
25339 {
25340 *usedlen = (int)(p + 1 - src);
25341 s = do_string_sub(str, pat, sub, flags);
25342 if (s != NULL)
25343 {
25344 *fnamep = s;
25345 *fnamelen = (int)STRLEN(s);
25346 vim_free(*bufp);
25347 *bufp = s;
25348 didit = TRUE;
25349 }
25350 }
25351 vim_free(sub);
25352 vim_free(str);
25353 }
25354 vim_free(pat);
25355 }
25356 }
25357 /* after using ":s", repeat all the modifiers */
25358 if (didit)
25359 goto repeat;
25360 }
25361 }
25362
Bram Moolenaar26df0922014-02-23 23:39:13 +010025363 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25364 {
25365 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25366 if (p == NULL)
25367 return -1;
25368 vim_free(*bufp);
25369 *bufp = *fnamep = p;
25370 *fnamelen = (int)STRLEN(p);
25371 *usedlen += 2;
25372 }
25373
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374 return valid;
25375}
25376
25377/*
25378 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25379 * "flags" can be "g" to do a global substitute.
25380 * Returns an allocated string, NULL for error.
25381 */
25382 char_u *
25383do_string_sub(str, pat, sub, flags)
25384 char_u *str;
25385 char_u *pat;
25386 char_u *sub;
25387 char_u *flags;
25388{
25389 int sublen;
25390 regmatch_T regmatch;
25391 int i;
25392 int do_all;
25393 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025394 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 garray_T ga;
25396 char_u *ret;
25397 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025398 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399
25400 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25401 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025402 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403
25404 ga_init2(&ga, 1, 200);
25405
25406 do_all = (flags[0] == 'g');
25407
25408 regmatch.rm_ic = p_ic;
25409 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25410 if (regmatch.regprog != NULL)
25411 {
25412 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025413 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25415 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025416 /* Skip empty match except for first match. */
25417 if (regmatch.startp[0] == regmatch.endp[0])
25418 {
25419 if (zero_width == regmatch.startp[0])
25420 {
25421 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025422 i = MB_PTR2LEN(tail);
25423 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25424 (size_t)i);
25425 ga.ga_len += i;
25426 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025427 continue;
25428 }
25429 zero_width = regmatch.startp[0];
25430 }
25431
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432 /*
25433 * Get some space for a temporary buffer to do the substitution
25434 * into. It will contain:
25435 * - The text up to where the match is.
25436 * - The substituted text.
25437 * - The text after the match.
25438 */
25439 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025440 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25442 {
25443 ga_clear(&ga);
25444 break;
25445 }
25446
25447 /* copy the text up to where the match is */
25448 i = (int)(regmatch.startp[0] - tail);
25449 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25450 /* add the substituted text */
25451 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25452 + ga.ga_len + i, TRUE, TRUE, FALSE);
25453 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025454 tail = regmatch.endp[0];
25455 if (*tail == NUL)
25456 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 if (!do_all)
25458 break;
25459 }
25460
25461 if (ga.ga_data != NULL)
25462 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25463
Bram Moolenaar473de612013-06-08 18:19:48 +020025464 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465 }
25466
25467 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25468 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025469 if (p_cpo == empty_option)
25470 p_cpo = save_cpo;
25471 else
25472 /* Darn, evaluating {sub} expression changed the value. */
25473 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474
25475 return ret;
25476}
25477
25478#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */