blob: f163829abce75c7bc2d862cb63cdb2ec8c63210b [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 Moolenaar33570922005-01-25 22:26:29 +0000368};
369
370/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_type vv_di.di_tv.v_type
372#define vv_nr vv_di.di_tv.vval.v_number
373#define vv_float vv_di.di_tv.vval.v_float
374#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000375#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200376#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000378
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200379static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000380#define vimvarht vimvardict.dv_hashtab
381
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
383static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000384static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
385static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
386static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
388static void list_glob_vars __ARGS((int *first));
389static void list_buf_vars __ARGS((int *first));
390static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_vim_vars __ARGS((int *first));
395static void list_script_vars __ARGS((int *first));
396static void list_func_vars __ARGS((int *first));
397static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000398static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
399static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100400static 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 +0000401static void clear_lval __ARGS((lval_T *lp));
402static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
403static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000404static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
405static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
406static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
407static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
408static void item_lock __ARGS((typval_T *tv, int deep, int lock));
409static int tv_islocked __ARGS((typval_T *tv));
410
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
412static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000417static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
418static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000420static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000425static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100427static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
428static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
429static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000430static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000432static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
434static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000435static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100437static 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 +0000438static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000439static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200440static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
442static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
448static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000450#ifdef FEAT_FLOAT
451static int string2float __ARGS((char_u *text, float_T *value));
452#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
454static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100455static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static 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 +0200457static 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 +0000458static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000459static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
462static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200463static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100466static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200470static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200475static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100486static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100488static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
491static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000493static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000496static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000499static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000500static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
506static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200507static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
512static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200522static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200524#ifdef FEAT_FLOAT
525static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
526#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000527static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000529static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000535#ifdef FEAT_FLOAT
536static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200538static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000539#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000540static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000541static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000549static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000551static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000552static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000557static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200558static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000566static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000567static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200568static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000569static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000570static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200573static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000574static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100580static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000583static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000597static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100602static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000604static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200617static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000618static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200620#ifdef FEAT_LUA
621static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200628static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000629static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000632static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000636#ifdef vim_mkdir
637static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100640#ifdef FEAT_MZSCHEME
641static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100645static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000646static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000647#ifdef FEAT_FLOAT
648static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000651static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000652static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200653#ifdef FEAT_PYTHON3
654static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
655#endif
656#ifdef FEAT_PYTHON
657static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
658#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000660static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000661static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000673#ifdef FEAT_FLOAT
674static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
675#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200676static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100678static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000681static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000683static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000690static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000691static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000692static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000693static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200695static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000696static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100698#ifdef FEAT_CRYPT
699static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
700#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000701static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200702static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200706static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000709static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000710static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000713#ifdef FEAT_FLOAT
714static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
716#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000717static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200718static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719#ifdef HAVE_STRFTIME
720static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
721#endif
722static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200728static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200729static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000735static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200736static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000737static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200738static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000740static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000741static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000742static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000743static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000745static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200746#ifdef FEAT_FLOAT
747static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
749#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#ifdef FEAT_FLOAT
754static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
755#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200757static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200758static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100759static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100763static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
768static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000770static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000773static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100774static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775
Bram Moolenaar493c1782014-05-28 14:34:46 +0200776static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000777static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static int get_env_len __ARGS((char_u **arg));
779static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000780static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000781static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
782#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
783#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
784 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000785static 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 +0000786static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000787static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200788static 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 +0000789static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static typval_T *alloc_tv __ARGS((void));
791static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static void init_tv __ARGS((typval_T *varp));
793static long get_tv_number __ARGS((typval_T *varp));
794static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000795static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *get_tv_string __ARGS((typval_T *varp));
797static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000798static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100799static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
800static 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 +0000801static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
802static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
803static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000804static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
805static 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 +0000806static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200807static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
808static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200809static int var_check_func_name __ARGS((char_u *name, int new_var));
810static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200811static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000812static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000813static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
814static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
815static int eval_fname_script __ARGS((char_u *p));
816static int eval_fname_sid __ARGS((char_u *p));
817static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static ufunc_T *find_func __ARGS((char_u *name));
819static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200820static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821#ifdef FEAT_PROFILE
822static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000823static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
824static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
825static int
826# ifdef __BORLANDC__
827 _RTLENTRYF
828# endif
829 prof_total_cmp __ARGS((const void *s1, const void *s2));
830static int
831# ifdef __BORLANDC__
832 _RTLENTRYF
833# endif
834 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000835#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200836static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000837static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000838static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000839static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840static 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 +0000841static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
842static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000843static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000844static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
845static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000846static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000847static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000848static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200849static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200850static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000851
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200852
853#ifdef EBCDIC
854static int compare_func_name __ARGS((const void *s1, const void *s2));
855static void sortFunctions __ARGS(());
856#endif
857
Bram Moolenaar33570922005-01-25 22:26:29 +0000858/*
859 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000860 */
861 void
862eval_init()
863{
Bram Moolenaar33570922005-01-25 22:26:29 +0000864 int i;
865 struct vimvar *p;
866
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200867 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
868 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200869 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000870 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000871 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872
873 for (i = 0; i < VV_LEN; ++i)
874 {
875 p = &vimvars[i];
876 STRCPY(p->vv_di.di_key, p->vv_name);
877 if (p->vv_flags & VV_RO)
878 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
879 else if (p->vv_flags & VV_RO_SBX)
880 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
881 else
882 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000883
884 /* add to v: scope dict, unless the value is not always available */
885 if (p->vv_type != VAR_UNKNOWN)
886 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000888 /* add to compat scope dict */
889 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000890 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000891 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100892 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200893 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200894 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200895
896#ifdef EBCDIC
897 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100898 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200899 */
900 sortFunctions();
901#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000902}
903
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000904#if defined(EXITFREE) || defined(PROTO)
905 void
906eval_clear()
907{
908 int i;
909 struct vimvar *p;
910
911 for (i = 0; i < VV_LEN; ++i)
912 {
913 p = &vimvars[i];
914 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000915 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000916 vim_free(p->vv_str);
917 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000918 }
919 else if (p->vv_di.di_tv.v_type == VAR_LIST)
920 {
921 list_unref(p->vv_list);
922 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000923 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 }
925 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000926 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000927 hash_clear(&compat_hashtab);
928
Bram Moolenaard9fba312005-06-26 22:34:35 +0000929 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100930# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200931 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100932# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000933
934 /* global variables */
935 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000936
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000937 /* autoloaded script names */
938 ga_clear_strings(&ga_loaded);
939
Bram Moolenaarcca74132013-09-25 21:00:28 +0200940 /* Script-local variables. First clear all the variables and in a second
941 * loop free the scriptvar_T, because a variable in one script might hold
942 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200945 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200946 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200947 ga_clear(&ga_scripts);
948
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000949 /* unreferenced lists and dicts */
950 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000951
952 /* functions */
953 free_all_functions();
954 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000955}
956#endif
957
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 * Return the name of the executed function.
960 */
961 char_u *
962func_name(cookie)
963 void *cookie;
964{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000965 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the next breakpoint line for a funccall cookie.
970 */
971 linenr_T *
972func_breakpoint(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the address holding the debug tick for a funccall cookie.
980 */
981 int *
982func_dbg_tick(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/*
989 * Return the nesting level for a funccall cookie.
990 */
991 int
992func_level(cookie)
993 void *cookie;
994{
Bram Moolenaar33570922005-01-25 22:26:29 +0000995 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996}
997
998/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000999funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001001/* pointer to list of previously used funccal, still around because some
1002 * item in it is still being used. */
1003funccall_T *previous_funccal = NULL;
1004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005/*
1006 * Return TRUE when a function was ended by a ":return" command.
1007 */
1008 int
1009current_func_returned()
1010{
1011 return current_funccal->returned;
1012}
1013
1014
1015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * Set an internal variable to a string value. Creates the variable if it does
1017 * not already exist.
1018 */
1019 void
1020set_internal_string_var(name, value)
1021 char_u *name;
1022 char_u *value;
1023{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001024 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001025 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 val = vim_strsave(value);
1028 if (val != NULL)
1029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001030 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001031 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001033 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001034 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036 }
1037}
1038
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001040static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041static char_u *redir_endp = NULL;
1042static char_u *redir_varname = NULL;
1043
1044/*
1045 * Start recording command output to a variable
1046 * Returns OK if successfully completed the setup. FAIL otherwise.
1047 */
1048 int
1049var_redir_start(name, append)
1050 char_u *name;
1051 int append; /* append to an existing variable */
1052{
1053 int save_emsg;
1054 int err;
1055 typval_T tv;
1056
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001057 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001058 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 {
1060 EMSG(_(e_invarg));
1061 return FAIL;
1062 }
1063
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001064 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 redir_varname = vim_strsave(name);
1066 if (redir_varname == NULL)
1067 return FAIL;
1068
1069 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1070 if (redir_lval == NULL)
1071 {
1072 var_redir_stop();
1073 return FAIL;
1074 }
1075
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001076 /* The output is stored in growarray "redir_ga" until redirection ends. */
1077 ga_init2(&redir_ga, (int)sizeof(char), 500);
1078
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001080 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001081 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1083 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001084 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (redir_endp != NULL && *redir_endp != NUL)
1086 /* Trailing characters are present after the variable name */
1087 EMSG(_(e_trailing));
1088 else
1089 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 var_redir_stop();
1092 return FAIL;
1093 }
1094
1095 /* check if we can write to the variable: set it to or append an empty
1096 * string */
1097 save_emsg = did_emsg;
1098 did_emsg = FALSE;
1099 tv.v_type = VAR_STRING;
1100 tv.vval.v_string = (char_u *)"";
1101 if (append)
1102 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1103 else
1104 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001105 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001107 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 if (err)
1109 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001110 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 var_redir_stop();
1112 return FAIL;
1113 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114
1115 return OK;
1116}
1117
1118/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001119 * Append "value[value_len]" to the variable set by var_redir_start().
1120 * The actual appending is postponed until redirection ends, because the value
1121 * appended may in fact be the string we write to, changing it may cause freed
1122 * memory to be used:
1123 * :redir => foo
1124 * :let foo
1125 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 */
1127 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133
1134 if (redir_lval == NULL)
1135 return;
1136
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001138 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001140 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 {
1144 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146 }
1147 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001148 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149}
1150
1151/*
1152 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001153 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 */
1155 void
1156var_redir_stop()
1157{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158 typval_T tv;
1159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 if (redir_lval != NULL)
1161 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 /* If there was no error: assign the text to the variable. */
1163 if (redir_endp != NULL)
1164 {
1165 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1166 tv.v_type = VAR_STRING;
1167 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001168 /* Call get_lval() again, if it's inside a Dict or List it may
1169 * have changed. */
1170 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001171 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001172 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1173 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1174 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001175 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001176
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001177 /* free the collected output */
1178 vim_free(redir_ga.ga_data);
1179 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001180
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 vim_free(redir_lval);
1182 redir_lval = NULL;
1183 }
1184 vim_free(redir_varname);
1185 redir_varname = NULL;
1186}
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188# if defined(FEAT_MBYTE) || defined(PROTO)
1189 int
1190eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1191 char_u *enc_from;
1192 char_u *enc_to;
1193 char_u *fname_from;
1194 char_u *fname_to;
1195{
1196 int err = FALSE;
1197
1198 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1199 set_vim_var_string(VV_CC_TO, enc_to, -1);
1200 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1201 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1202 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_CC_FROM, NULL, -1);
1205 set_vim_var_string(VV_CC_TO, NULL, -1);
1206 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1207 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1208
1209 if (err)
1210 return FAIL;
1211 return OK;
1212}
1213# endif
1214
1215# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1216 int
1217eval_printexpr(fname, args)
1218 char_u *fname;
1219 char_u *args;
1220{
1221 int err = FALSE;
1222
1223 set_vim_var_string(VV_FNAME_IN, fname, -1);
1224 set_vim_var_string(VV_CMDARG, args, -1);
1225 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1226 err = TRUE;
1227 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1228 set_vim_var_string(VV_CMDARG, NULL, -1);
1229
1230 if (err)
1231 {
1232 mch_remove(fname);
1233 return FAIL;
1234 }
1235 return OK;
1236}
1237# endif
1238
1239# if defined(FEAT_DIFF) || defined(PROTO)
1240 void
1241eval_diff(origfile, newfile, outfile)
1242 char_u *origfile;
1243 char_u *newfile;
1244 char_u *outfile;
1245{
1246 int err = FALSE;
1247
1248 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1249 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1250 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1251 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1252 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1253 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1254 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1255}
1256
1257 void
1258eval_patch(origfile, difffile, outfile)
1259 char_u *origfile;
1260 char_u *difffile;
1261 char_u *outfile;
1262{
1263 int err;
1264
1265 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1266 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1267 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1268 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1269 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1270 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1271 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1272}
1273# endif
1274
1275/*
1276 * Top level evaluation function, returning a boolean.
1277 * Sets "error" to TRUE if there was an error.
1278 * Return TRUE or FALSE.
1279 */
1280 int
1281eval_to_bool(arg, error, nextcmd, skip)
1282 char_u *arg;
1283 int *error;
1284 char_u **nextcmd;
1285 int skip; /* only parse, don't execute */
1286{
Bram Moolenaar33570922005-01-25 22:26:29 +00001287 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 int retval = FALSE;
1289
1290 if (skip)
1291 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001292 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 else
1295 {
1296 *error = FALSE;
1297 if (!skip)
1298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001299 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302 }
1303 if (skip)
1304 --emsg_skip;
1305
1306 return retval;
1307}
1308
1309/*
1310 * Top level evaluation function, returning a string. If "skip" is TRUE,
1311 * only parsing to "nextcmd" is done, without reporting errors. Return
1312 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1313 */
1314 char_u *
1315eval_to_string_skip(arg, nextcmd, skip)
1316 char_u *arg;
1317 char_u **nextcmd;
1318 int skip; /* only parse, don't execute */
1319{
Bram Moolenaar33570922005-01-25 22:26:29 +00001320 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 char_u *retval;
1322
1323 if (skip)
1324 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001325 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 retval = NULL;
1327 else
1328 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 retval = vim_strsave(get_tv_string(&tv));
1330 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 if (skip)
1333 --emsg_skip;
1334
1335 return retval;
1336}
1337
1338/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001339 * Skip over an expression at "*pp".
1340 * Return FAIL for an error, OK otherwise.
1341 */
1342 int
1343skip_expr(pp)
1344 char_u **pp;
1345{
Bram Moolenaar33570922005-01-25 22:26:29 +00001346 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347
1348 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001350}
1351
1352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001354 * When "convert" is TRUE convert a List into a sequence of lines and convert
1355 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * Return pointer to allocated memory, or NULL for failure.
1357 */
1358 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 char_u *arg;
1361 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar33570922005-01-25 22:26:29 +00001364 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001367#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 retval = NULL;
1373 else
1374 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001376 {
1377 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001378 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001379 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001380 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001381 if (tv.vval.v_list->lv_len > 0)
1382 ga_append(&ga, NL);
1383 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 ga_append(&ga, NUL);
1385 retval = (char_u *)ga.ga_data;
1386 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001387#ifdef FEAT_FLOAT
1388 else if (convert && tv.v_type == VAR_FLOAT)
1389 {
1390 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1391 retval = vim_strsave(numbuf);
1392 }
1393#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 else
1395 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398
1399 return retval;
1400}
1401
1402/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001403 * Call eval_to_string() without using current local variables and using
1404 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 */
1406 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 char_u *arg;
1409 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 char_u *retval;
1413 void *save_funccalp;
1414
1415 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416 if (use_sandbox)
1417 ++sandbox;
1418 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001419 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001420 if (use_sandbox)
1421 --sandbox;
1422 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 restore_funccal(save_funccalp);
1424 return retval;
1425}
1426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427/*
1428 * Top level evaluation function, returning a number.
1429 * Evaluates "expr" silently.
1430 * Returns -1 for an error.
1431 */
1432 int
1433eval_to_number(expr)
1434 char_u *expr;
1435{
Bram Moolenaar33570922005-01-25 22:26:29 +00001436 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001438 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440 ++emsg_off;
1441
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001442 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 retval = -1;
1444 else
1445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001446 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449 --emsg_off;
1450
1451 return retval;
1452}
1453
Bram Moolenaara40058a2005-07-11 22:42:07 +00001454/*
1455 * Prepare v: variable "idx" to be used.
1456 * Save the current typeval in "save_tv".
1457 * When not used yet add the variable to the v: hashtable.
1458 */
1459 static void
1460prepare_vimvar(idx, save_tv)
1461 int idx;
1462 typval_T *save_tv;
1463{
1464 *save_tv = vimvars[idx].vv_tv;
1465 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1466 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1467}
1468
1469/*
1470 * Restore v: variable "idx" to typeval "save_tv".
1471 * When no longer defined, remove the variable from the v: hashtable.
1472 */
1473 static void
1474restore_vimvar(idx, save_tv)
1475 int idx;
1476 typval_T *save_tv;
1477{
1478 hashitem_T *hi;
1479
Bram Moolenaara40058a2005-07-11 22:42:07 +00001480 vimvars[idx].vv_tv = *save_tv;
1481 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1482 {
1483 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1484 if (HASHITEM_EMPTY(hi))
1485 EMSG2(_(e_intern2), "restore_vimvar()");
1486 else
1487 hash_remove(&vimvarht, hi);
1488 }
1489}
1490
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001491#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001492/*
1493 * Evaluate an expression to a list with suggestions.
1494 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001495 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001496 */
1497 list_T *
1498eval_spell_expr(badword, expr)
1499 char_u *badword;
1500 char_u *expr;
1501{
1502 typval_T save_val;
1503 typval_T rettv;
1504 list_T *list = NULL;
1505 char_u *p = skipwhite(expr);
1506
1507 /* Set "v:val" to the bad word. */
1508 prepare_vimvar(VV_VAL, &save_val);
1509 vimvars[VV_VAL].vv_type = VAR_STRING;
1510 vimvars[VV_VAL].vv_str = badword;
1511 if (p_verbose == 0)
1512 ++emsg_off;
1513
1514 if (eval1(&p, &rettv, TRUE) == OK)
1515 {
1516 if (rettv.v_type != VAR_LIST)
1517 clear_tv(&rettv);
1518 else
1519 list = rettv.vval.v_list;
1520 }
1521
1522 if (p_verbose == 0)
1523 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001524 restore_vimvar(VV_VAL, &save_val);
1525
1526 return list;
1527}
1528
1529/*
1530 * "list" is supposed to contain two items: a word and a number. Return the
1531 * word in "pp" and the number as the return value.
1532 * Return -1 if anything isn't right.
1533 * Used to get the good word and score from the eval_spell_expr() result.
1534 */
1535 int
1536get_spellword(list, pp)
1537 list_T *list;
1538 char_u **pp;
1539{
1540 listitem_T *li;
1541
1542 li = list->lv_first;
1543 if (li == NULL)
1544 return -1;
1545 *pp = get_tv_string(&li->li_tv);
1546
1547 li = li->li_next;
1548 if (li == NULL)
1549 return -1;
1550 return get_tv_number(&li->li_tv);
1551}
1552#endif
1553
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001554/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001555 * Top level evaluation function.
1556 * Returns an allocated typval_T with the result.
1557 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 */
1559 typval_T *
1560eval_expr(arg, nextcmd)
1561 char_u *arg;
1562 char_u **nextcmd;
1563{
1564 typval_T *tv;
1565
1566 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 {
1569 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001570 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571 }
1572
1573 return tv;
1574}
1575
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001579 * Uses argv[argc] for the function arguments. Only Number and String
1580 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001581 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001583 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001584call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 char_u *func;
1586 int argc;
1587 char_u **argv;
1588 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001589 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591{
Bram Moolenaar33570922005-01-25 22:26:29 +00001592 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 long n;
1594 int len;
1595 int i;
1596 int doesrange;
1597 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001598 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001600 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604 for (i = 0; i < argc; i++)
1605 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001606 /* Pass a NULL or empty argument as an empty string */
1607 if (argv[i] == NULL || *argv[i] == NUL)
1608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001609 argvars[i].v_type = VAR_STRING;
1610 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001611 continue;
1612 }
1613
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001614 if (str_arg_only)
1615 len = 0;
1616 else
1617 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02001618 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (len != 0 && len == (int)STRLEN(argv[i]))
1620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001621 argvars[i].v_type = VAR_NUMBER;
1622 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
1624 else
1625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001626 argvars[i].v_type = VAR_STRING;
1627 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629 }
1630
1631 if (safe)
1632 {
1633 save_funccalp = save_funccal();
1634 ++sandbox;
1635 }
1636
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1638 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (safe)
1642 {
1643 --sandbox;
1644 restore_funccal(save_funccalp);
1645 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646 vim_free(argvars);
1647
1648 if (ret == FAIL)
1649 clear_tv(rettv);
1650
1651 return ret;
1652}
1653
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001654/*
1655 * Call vimL function "func" and return the result as a number.
1656 * Returns -1 when calling the function fails.
1657 * Uses argv[argc] for the function arguments.
1658 */
1659 long
1660call_func_retnr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
1667 long retval;
1668
1669 /* All arguments are passed as strings, no conversion to number. */
1670 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1671 return -1;
1672
1673 retval = get_tv_number_chk(&rettv, NULL);
1674 clear_tv(&rettv);
1675 return retval;
1676}
1677
1678#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1679 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1680
Bram Moolenaar4f688582007-07-24 12:34:30 +00001681# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001683 * Call vimL function "func" and return the result as a string.
1684 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 * Uses argv[argc] for the function arguments.
1686 */
1687 void *
1688call_func_retstr(func, argc, argv, safe)
1689 char_u *func;
1690 int argc;
1691 char_u **argv;
1692 int safe; /* use the sandbox */
1693{
1694 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001695 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001697 /* All arguments are passed as strings, no conversion to number. */
1698 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 return NULL;
1700
1701 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 return retval;
1704}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001705# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001707/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001708 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001710 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 */
1712 void *
1713call_func_retlist(func, argc, argv, safe)
1714 char_u *func;
1715 int argc;
1716 char_u **argv;
1717 int safe; /* use the sandbox */
1718{
1719 typval_T rettv;
1720
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001721 /* All arguments are passed as strings, no conversion to number. */
1722 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 return NULL;
1724
1725 if (rettv.v_type != VAR_LIST)
1726 {
1727 clear_tv(&rettv);
1728 return NULL;
1729 }
1730
1731 return rettv.vval.v_list;
1732}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733#endif
1734
1735/*
1736 * Save the current function call pointer, and set it to NULL.
1737 * Used when executing autocommands and for ":source".
1738 */
1739 void *
1740save_funccal()
1741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 current_funccal = NULL;
1745 return (void *)fc;
1746}
1747
1748 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749restore_funccal(vfc)
1750 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001752 funccall_T *fc = (funccall_T *)vfc;
1753
1754 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
Bram Moolenaar05159a02005-02-26 23:04:13 +00001757#if defined(FEAT_PROFILE) || defined(PROTO)
1758/*
1759 * Prepare profiling for entering a child or something else that is not
1760 * counted for the script/function itself.
1761 * Should always be called in pair with prof_child_exit().
1762 */
1763 void
1764prof_child_enter(tm)
1765 proftime_T *tm; /* place to store waittime */
1766{
1767 funccall_T *fc = current_funccal;
1768
1769 if (fc != NULL && fc->func->uf_profiling)
1770 profile_start(&fc->prof_child);
1771 script_prof_save(tm);
1772}
1773
1774/*
1775 * Take care of time spent in a child.
1776 * Should always be called after prof_child_enter().
1777 */
1778 void
1779prof_child_exit(tm)
1780 proftime_T *tm; /* where waittime was stored */
1781{
1782 funccall_T *fc = current_funccal;
1783
1784 if (fc != NULL && fc->func->uf_profiling)
1785 {
1786 profile_end(&fc->prof_child);
1787 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1788 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1789 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1790 }
1791 script_prof_restore(tm);
1792}
1793#endif
1794
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#ifdef FEAT_FOLDING
1797/*
1798 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1799 * it in "*cp". Doesn't give error messages.
1800 */
1801 int
1802eval_foldexpr(arg, cp)
1803 char_u *arg;
1804 int *cp;
1805{
Bram Moolenaar33570922005-01-25 22:26:29 +00001806 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 int retval;
1808 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001809 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1810 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
1812 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001813 if (use_sandbox)
1814 ++sandbox;
1815 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 retval = 0;
1819 else
1820 {
1821 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 if (tv.v_type == VAR_NUMBER)
1823 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001824 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 retval = 0;
1826 else
1827 {
1828 /* If the result is a string, check if there is a non-digit before
1829 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (!VIM_ISDIGIT(*s) && *s != '-')
1832 *cp = *s++;
1833 retval = atol((char *)s);
1834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001838 if (use_sandbox)
1839 --sandbox;
1840 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
1842 return retval;
1843}
1844#endif
1845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 * ":let" list all variable values
1848 * ":let var1 var2" list variable values
1849 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001850 * ":let var += expr" assignment command.
1851 * ":let var -= expr" assignment command.
1852 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 */
1855 void
1856ex_let(eap)
1857 exarg_T *eap;
1858{
1859 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 int var_count = 0;
1864 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001865 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001867 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaardb552d602006-03-23 22:59:57 +00001869 argend = skip_var_list(arg, &var_count, &semicolon);
1870 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001872 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1873 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001874 expr = skipwhite(argend);
1875 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1876 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001878 /*
1879 * ":let" without "=": list variables
1880 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 if (*arg == '[')
1882 EMSG(_(e_invarg));
1883 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001889 list_glob_vars(&first);
1890 list_buf_vars(&first);
1891 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001892#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001893 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001894#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001895 list_script_vars(&first);
1896 list_func_vars(&first);
1897 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 eap->nextcmd = check_nextcmd(arg);
1900 }
1901 else
1902 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 op[0] = '=';
1904 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001905 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001907 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1908 op[0] = *expr; /* +=, -= or .= */
1909 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001911 else
1912 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (eap->skip)
1918 {
1919 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 --emsg_skip;
1922 }
1923 else if (i != FAIL)
1924 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
1929 }
1930}
1931
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932/*
1933 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1934 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 * When "nextchars" is not NULL it points to a string with characters that
1936 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1937 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 * Returns OK or FAIL;
1939 */
1940 static int
1941ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1942 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001943 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 int copy; /* copy values from "tv", don't move */
1945 int semicolon; /* from skip_var_list() */
1946 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948{
1949 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001950 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 listitem_T *item;
1953 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954
1955 if (*arg != '[')
1956 {
1957 /*
1958 * ":let var = expr" or ":for var in list"
1959 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 return FAIL;
1962 return OK;
1963 }
1964
1965 /*
1966 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1967 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001968 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 {
1970 EMSG(_(e_listreq));
1971 return FAIL;
1972 }
1973
1974 i = list_len(l);
1975 if (semicolon == 0 && var_count < i)
1976 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001977 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 return FAIL;
1979 }
1980 if (var_count - semicolon > i)
1981 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001982 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 return FAIL;
1984 }
1985
1986 item = l->lv_first;
1987 while (*arg != ']')
1988 {
1989 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 item = item->li_next;
1992 if (arg == NULL)
1993 return FAIL;
1994
1995 arg = skipwhite(arg);
1996 if (*arg == ';')
1997 {
1998 /* Put the rest of the list (may be empty) in the var after ';'.
1999 * Create a new list for this. */
2000 l = list_alloc();
2001 if (l == NULL)
2002 return FAIL;
2003 while (item != NULL)
2004 {
2005 list_append_tv(l, &item->li_tv);
2006 item = item->li_next;
2007 }
2008
2009 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002010 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 ltv.vval.v_list = l;
2012 l->lv_refcount = 1;
2013
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002014 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2015 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 clear_tv(&ltv);
2017 if (arg == NULL)
2018 return FAIL;
2019 break;
2020 }
2021 else if (*arg != ',' && *arg != ']')
2022 {
2023 EMSG2(_(e_intern2), "ex_let_vars()");
2024 return FAIL;
2025 }
2026 }
2027
2028 return OK;
2029}
2030
2031/*
2032 * Skip over assignable variable "var" or list of variables "[var, var]".
2033 * Used for ":let varvar = expr" and ":for varvar in expr".
2034 * For "[var, var]" increment "*var_count" for each variable.
2035 * for "[var, var; var]" set "semicolon".
2036 * Return NULL for an error.
2037 */
2038 static char_u *
2039skip_var_list(arg, var_count, semicolon)
2040 char_u *arg;
2041 int *var_count;
2042 int *semicolon;
2043{
2044 char_u *p, *s;
2045
2046 if (*arg == '[')
2047 {
2048 /* "[var, var]": find the matching ']'. */
2049 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002050 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 {
2052 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2053 s = skip_var_one(p);
2054 if (s == p)
2055 {
2056 EMSG2(_(e_invarg2), p);
2057 return NULL;
2058 }
2059 ++*var_count;
2060
2061 p = skipwhite(s);
2062 if (*p == ']')
2063 break;
2064 else if (*p == ';')
2065 {
2066 if (*semicolon == 1)
2067 {
2068 EMSG(_("Double ; in list of variables"));
2069 return NULL;
2070 }
2071 *semicolon = 1;
2072 }
2073 else if (*p != ',')
2074 {
2075 EMSG2(_(e_invarg2), p);
2076 return NULL;
2077 }
2078 }
2079 return p + 1;
2080 }
2081 else
2082 return skip_var_one(arg);
2083}
2084
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002086 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002087 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002089 static char_u *
2090skip_var_one(arg)
2091 char_u *arg;
2092{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002093 if (*arg == '@' && arg[1] != NUL)
2094 return arg + 2;
2095 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2096 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002097}
2098
Bram Moolenaara7043832005-01-21 11:56:39 +00002099/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 * List variables for hashtab "ht" with prefix "prefix".
2101 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002105 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109{
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 hashitem_T *hi;
2111 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 int todo;
2113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002114 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2116 {
2117 if (!HASHITEM_EMPTY(hi))
2118 {
2119 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 di = HI2DI(hi);
2121 if (empty || di->di_tv.v_type != VAR_STRING
2122 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002124 }
2125 }
2126}
2127
2128/*
2129 * List global variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_glob_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002136}
2137
2138/*
2139 * List buffer variables.
2140 */
2141 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142list_buf_vars(first)
2143 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002145 char_u numbuf[NUMBUFLEN];
2146
Bram Moolenaar429fa852013-04-15 12:27:36 +02002147 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002149
2150 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2152 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002153}
2154
2155/*
2156 * List window variables.
2157 */
2158 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159list_win_vars(first)
2160 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002161{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002162 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164}
2165
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002166#ifdef FEAT_WINDOWS
2167/*
2168 * List tab page variables.
2169 */
2170 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171list_tab_vars(first)
2172 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002174 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176}
2177#endif
2178
Bram Moolenaara7043832005-01-21 11:56:39 +00002179/*
2180 * List Vim variables.
2181 */
2182 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183list_vim_vars(first)
2184 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187}
2188
2189/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002190 * List script-local variables, if there is a script.
2191 */
2192 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193list_script_vars(first)
2194 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195{
2196 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2198 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002199}
2200
2201/*
2202 * List function variables, if there is a function.
2203 */
2204 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205list_func_vars(first)
2206 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002207{
2208 if (current_funccal != NULL)
2209 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211}
2212
2213/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 * List variables in "arg".
2215 */
2216 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 exarg_T *eap;
2219 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221{
2222 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 char_u *name_start;
2226 char_u *arg_subsc;
2227 char_u *tofree;
2228 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229
2230 while (!ends_excmd(*arg) && !got_int)
2231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002234 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2236 {
2237 emsg_severe = TRUE;
2238 EMSG(_(e_trailing));
2239 break;
2240 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 /* get_name_len() takes care of expanding curly braces */
2245 name_start = name = arg;
2246 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2247 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 /* This is mainly to keep test 49 working: when expanding
2250 * curly braces fails overrule the exception error message. */
2251 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 emsg_severe = TRUE;
2254 EMSG2(_(e_invarg2), arg);
2255 break;
2256 }
2257 error = TRUE;
2258 }
2259 else
2260 {
2261 if (tofree != NULL)
2262 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002263 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 else
2266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 /* handle d.key, l[idx], f(expr) */
2268 arg_subsc = arg;
2269 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 case 'g': list_glob_vars(first); break;
2278 case 'b': list_buf_vars(first); break;
2279 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002280#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002281 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002282#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 case 'v': list_vim_vars(first); break;
2284 case 's': list_script_vars(first); break;
2285 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 default:
2287 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 }
2290 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 {
2292 char_u numbuf[NUMBUFLEN];
2293 char_u *tf;
2294 int c;
2295 char_u *s;
2296
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002297 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 c = *arg;
2299 *arg = NUL;
2300 list_one_var_a((char_u *)"",
2301 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 tv.v_type,
2303 s == NULL ? (char_u *)"" : s,
2304 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 *arg = c;
2306 vim_free(tf);
2307 }
2308 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 }
2311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315
2316 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318
2319 return arg;
2320}
2321
2322/*
2323 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2324 * Returns a pointer to the char just after the var name.
2325 * Returns NULL if there is an error.
2326 */
2327 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002330 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002332 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334{
2335 int c1;
2336 char_u *name;
2337 char_u *p;
2338 char_u *arg_end = NULL;
2339 int len;
2340 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342
2343 /*
2344 * ":let $VAR = expr": Set environment variable.
2345 */
2346 if (*arg == '$')
2347 {
2348 /* Find the end of the name. */
2349 ++arg;
2350 name = arg;
2351 len = get_env_len(&arg);
2352 if (len == 0)
2353 EMSG2(_(e_invarg2), name - 1);
2354 else
2355 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002356 if (op != NULL && (*op == '+' || *op == '-'))
2357 EMSG2(_(e_letwrong), op);
2358 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002359 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002361 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 {
2363 c1 = name[len];
2364 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002365 p = get_tv_string_chk(tv);
2366 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 {
2368 int mustfree = FALSE;
2369 char_u *s = vim_getenv(name, &mustfree);
2370
2371 if (s != NULL)
2372 {
2373 p = tofree = concat_str(s, p);
2374 if (mustfree)
2375 vim_free(s);
2376 }
2377 }
2378 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002381 if (STRICMP(name, "HOME") == 0)
2382 init_homedir();
2383 else if (didset_vim && STRICMP(name, "VIM") == 0)
2384 didset_vim = FALSE;
2385 else if (didset_vimruntime
2386 && STRICMP(name, "VIMRUNTIME") == 0)
2387 didset_vimruntime = FALSE;
2388 arg_end = arg;
2389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 }
2393 }
2394 }
2395
2396 /*
2397 * ":let &option = expr": Set option value.
2398 * ":let &l:option = expr": Set local option value.
2399 * ":let &g:option = expr": Set global option value.
2400 */
2401 else if (*arg == '&')
2402 {
2403 /* Find the end of the name. */
2404 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002405 if (p == NULL || (endchars != NULL
2406 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 EMSG(_(e_letunexp));
2408 else
2409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 long n;
2411 int opt_type;
2412 long numval;
2413 char_u *stringval = NULL;
2414 char_u *s;
2415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 c1 = *p;
2417 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418
2419 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 s = get_tv_string_chk(tv); /* != NULL if number or string */
2421 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 {
2423 opt_type = get_option_value(arg, &numval,
2424 &stringval, opt_flags);
2425 if ((opt_type == 1 && *op == '.')
2426 || (opt_type == 0 && *op != '.'))
2427 EMSG2(_(e_letwrong), op);
2428 else
2429 {
2430 if (opt_type == 1) /* number */
2431 {
2432 if (*op == '+')
2433 n = numval + n;
2434 else
2435 n = numval - n;
2436 }
2437 else if (opt_type == 0 && stringval != NULL) /* string */
2438 {
2439 s = concat_str(stringval, s);
2440 vim_free(stringval);
2441 stringval = s;
2442 }
2443 }
2444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 if (s != NULL)
2446 {
2447 set_option_value(arg, n, s, opt_flags);
2448 arg_end = p;
2449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454
2455 /*
2456 * ":let @r = expr": Set register contents.
2457 */
2458 else if (*arg == '@')
2459 {
2460 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 if (op != NULL && (*op == '+' || *op == '-'))
2462 EMSG2(_(e_letwrong), op);
2463 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 EMSG(_(e_letunexp));
2466 else
2467 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002468 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 char_u *s;
2470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 p = get_tv_string_chk(tv);
2472 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002474 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 if (s != NULL)
2476 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002477 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 vim_free(s);
2479 }
2480 }
2481 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 arg_end = arg + 1;
2485 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002486 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 }
2488 }
2489
2490 /*
2491 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002494 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002496 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002498 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2502 EMSG(_(e_letunexp));
2503 else
2504 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 arg_end = p;
2507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 }
2511
2512 else
2513 EMSG2(_(e_invarg2), arg);
2514
2515 return arg_end;
2516}
2517
2518/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2520 */
2521 static int
2522check_changedtick(arg)
2523 char_u *arg;
2524{
2525 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2526 {
2527 EMSG2(_(e_readonlyvar), arg);
2528 return TRUE;
2529 }
2530 return FALSE;
2531}
2532
2533/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 * Get an lval: variable, Dict item or List item that can be assigned a value
2535 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2536 * "name.key", "name.key[expr]" etc.
2537 * Indexing only works if "name" is an existing List or Dictionary.
2538 * "name" points to the start of the name.
2539 * If "rettv" is not NULL it points to the value to be assigned.
2540 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2541 * wrong; must end in space or cmd separator.
2542 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002543 * flags:
2544 * GLV_QUIET: do not give error messages
2545 * GLV_NO_AUTOLOAD: do not use script autoloading
2546 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002548 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 */
2551 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002552get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002553 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 typval_T *rettv;
2555 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 int unlet;
2557 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002558 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002559 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 char_u *expr_start, *expr_end;
2563 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 dictitem_T *v;
2565 typval_T var1;
2566 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002572 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002575 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576
2577 if (skip)
2578 {
2579 /* When skipping just find the end of the name. */
2580 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002581 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 }
2583
2584 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002585 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (expr_start != NULL)
2587 {
2588 /* Don't expand the name when we already know there is an error. */
2589 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2590 && *p != '[' && *p != '.')
2591 {
2592 EMSG(_(e_trailing));
2593 return NULL;
2594 }
2595
2596 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2597 if (lp->ll_exp_name == NULL)
2598 {
2599 /* Report an invalid expression in braces, unless the
2600 * expression evaluation has been cancelled due to an
2601 * aborting error, an interrupt, or an exception. */
2602 if (!aborting() && !quiet)
2603 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002604 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 EMSG2(_(e_invarg2), name);
2606 return NULL;
2607 }
2608 }
2609 lp->ll_name = lp->ll_exp_name;
2610 }
2611 else
2612 lp->ll_name = name;
2613
2614 /* Without [idx] or .key we are done. */
2615 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2616 return p;
2617
2618 cc = *p;
2619 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002620 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 if (v == NULL && !quiet)
2622 EMSG2(_(e_undefvar), lp->ll_name);
2623 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 if (v == NULL)
2625 return NULL;
2626
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 /*
2628 * Loop until no more [idx] or .key is following.
2629 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002630 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2634 && !(lp->ll_tv->v_type == VAR_DICT
2635 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_("E689: Can only index a List or Dictionary"));
2639 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
2644 EMSG(_("E708: [:] must come last"));
2645 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 len = -1;
2649 if (*p == '.')
2650 {
2651 key = p + 1;
2652 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2653 ;
2654 if (len == 0)
2655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
2657 EMSG(_(e_emptykey));
2658 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 }
2660 p = key + len;
2661 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 else
2663 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (*p == ':')
2667 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 else
2669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 empty1 = FALSE;
2671 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002673 if (get_tv_string_chk(&var1) == NULL)
2674 {
2675 /* not a number or string */
2676 clear_tv(&var1);
2677 return NULL;
2678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
2680
2681 /* Optionally get the second index [ :expr]. */
2682 if (*p == ':')
2683 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002687 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 if (!empty1)
2689 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (rettv != NULL && (rettv->v_type != VAR_LIST
2693 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (!empty1)
2698 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701 p = skipwhite(p + 1);
2702 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 else
2705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002713 if (get_tv_string_chk(&var2) == NULL)
2714 {
2715 /* not a number or string */
2716 if (!empty1)
2717 clear_tv(&var1);
2718 clear_tv(&var2);
2719 return NULL;
2720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (!quiet)
2730 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (!empty1)
2732 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737
2738 /* Skip to past ']'. */
2739 ++p;
2740 }
2741
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
2744 if (len == -1)
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002747 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (*key == NUL)
2749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (!quiet)
2751 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 }
2755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 lp->ll_list = NULL;
2757 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002758 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002759
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002760 /* When assigning to a scope dictionary check that a function and
2761 * variable name is valid (only variable name unless it is l: or
2762 * g: dictionary). Disallow overwriting a builtin function. */
2763 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 int prevval;
2766 int wrong;
2767
2768 if (len != -1)
2769 {
2770 prevval = key[len];
2771 key[len] = NUL;
2772 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002773 else
2774 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2776 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 || !valid_varname(key);
2779 if (len != -1)
2780 key[len] = prevval;
2781 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 return NULL;
2783 }
2784
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 /* Can't add "v:" variable. */
2788 if (lp->ll_dict == &vimvardict)
2789 {
2790 EMSG2(_(e_illvar), name);
2791 return NULL;
2792 }
2793
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002794 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002798 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 if (len == -1)
2800 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 }
2803 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 if (len == -1)
2808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 p = NULL;
2811 break;
2812 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002814 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815 return NULL;
2816
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 if (len == -1)
2818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821 else
2822 {
2823 /*
2824 * Get the number and item for the only or first index of the List.
2825 */
2826 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 else
2829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002830 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 clear_tv(&var1);
2832 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002833 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 lp->ll_list = lp->ll_tv->vval.v_list;
2835 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2836 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002838 if (lp->ll_n1 < 0)
2839 {
2840 lp->ll_n1 = 0;
2841 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2842 }
2843 }
2844 if (lp->ll_li == NULL)
2845 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 if (!quiet)
2849 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 }
2852
2853 /*
2854 * May need to find the item or absolute index for the second
2855 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 * When no index given: "lp->ll_empty2" is TRUE.
2857 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002861 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 {
2868 if (!quiet)
2869 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 }
2874
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2876 if (lp->ll_n1 < 0)
2877 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2878 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 {
2880 if (!quiet)
2881 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002883 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 }
2889
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 return p;
2891}
2892
2893/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 */
2896 static void
2897clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899{
2900 vim_free(lp->ll_exp_name);
2901 vim_free(lp->ll_newkey);
2902}
2903
2904/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002905 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 */
2909 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002910set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916{
2917 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 listitem_T *ri;
2919 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920
2921 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 cc = *endp;
2926 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927 if (op != NULL && *op != '=')
2928 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002929 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002931 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002932 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002933 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002934 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002936 if ((di == NULL
2937 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2938 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2939 FALSE)))
2940 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 set_var(lp->ll_name, &tv, FALSE);
2942 clear_tv(&tv);
2943 }
2944 }
2945 else
2946 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002948 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002950 else if (tv_check_lock(lp->ll_newkey == NULL
2951 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002952 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 else if (lp->ll_range)
2955 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002956 listitem_T *ll_li = lp->ll_li;
2957 int ll_n1 = lp->ll_n1;
2958
2959 /*
2960 * Check whether any of the list items is locked
2961 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002962 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002963 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002964 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002965 return;
2966 ri = ri->li_next;
2967 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2968 break;
2969 ll_li = ll_li->li_next;
2970 ++ll_n1;
2971 }
2972
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 /*
2974 * Assign the List values to the list items.
2975 */
2976 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002977 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 if (op != NULL && *op != '=')
2979 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2980 else
2981 {
2982 clear_tv(&lp->ll_li->li_tv);
2983 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2984 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 ri = ri->li_next;
2986 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2987 break;
2988 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002991 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 ri = NULL;
2994 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 lp->ll_li = lp->ll_li->li_next;
2998 ++lp->ll_n1;
2999 }
3000 if (ri != NULL)
3001 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003002 else if (lp->ll_empty2
3003 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 : lp->ll_n1 != lp->ll_n2)
3005 EMSG(_("E711: List value has not enough items"));
3006 }
3007 else
3008 {
3009 /*
3010 * Assign to a List or Dictionary item.
3011 */
3012 if (lp->ll_newkey != NULL)
3013 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 if (op != NULL && *op != '=')
3015 {
3016 EMSG2(_(e_letwrong), op);
3017 return;
3018 }
3019
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003021 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 if (di == NULL)
3023 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003024 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3025 {
3026 vim_free(di);
3027 return;
3028 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003030 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 else if (op != NULL && *op != '=')
3032 {
3033 tv_op(lp->ll_tv, rettv, op);
3034 return;
3035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003036 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003038
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 /*
3040 * Assign the value to the variable or list item.
3041 */
3042 if (copy)
3043 copy_tv(rettv, lp->ll_tv);
3044 else
3045 {
3046 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003047 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 }
3050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051}
3052
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3055 * Returns OK or FAIL.
3056 */
3057 static int
3058tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003059 typval_T *tv1;
3060 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003061 char_u *op;
3062{
3063 long n;
3064 char_u numbuf[NUMBUFLEN];
3065 char_u *s;
3066
3067 /* Can't do anything with a Funcref or a Dict on the right. */
3068 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3069 {
3070 switch (tv1->v_type)
3071 {
3072 case VAR_DICT:
3073 case VAR_FUNC:
3074 break;
3075
3076 case VAR_LIST:
3077 if (*op != '+' || tv2->v_type != VAR_LIST)
3078 break;
3079 /* List += List */
3080 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3081 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3082 return OK;
3083
3084 case VAR_NUMBER:
3085 case VAR_STRING:
3086 if (tv2->v_type == VAR_LIST)
3087 break;
3088 if (*op == '+' || *op == '-')
3089 {
3090 /* nr += nr or nr -= nr*/
3091 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092#ifdef FEAT_FLOAT
3093 if (tv2->v_type == VAR_FLOAT)
3094 {
3095 float_T f = n;
3096
3097 if (*op == '+')
3098 f += tv2->vval.v_float;
3099 else
3100 f -= tv2->vval.v_float;
3101 clear_tv(tv1);
3102 tv1->v_type = VAR_FLOAT;
3103 tv1->vval.v_float = f;
3104 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003106#endif
3107 {
3108 if (*op == '+')
3109 n += get_tv_number(tv2);
3110 else
3111 n -= get_tv_number(tv2);
3112 clear_tv(tv1);
3113 tv1->v_type = VAR_NUMBER;
3114 tv1->vval.v_number = n;
3115 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 }
3117 else
3118 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119 if (tv2->v_type == VAR_FLOAT)
3120 break;
3121
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 /* str .= str */
3123 s = get_tv_string(tv1);
3124 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_STRING;
3127 tv1->vval.v_string = s;
3128 }
3129 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130
3131#ifdef FEAT_FLOAT
3132 case VAR_FLOAT:
3133 {
3134 float_T f;
3135
3136 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3137 && tv2->v_type != VAR_NUMBER
3138 && tv2->v_type != VAR_STRING))
3139 break;
3140 if (tv2->v_type == VAR_FLOAT)
3141 f = tv2->vval.v_float;
3142 else
3143 f = get_tv_number(tv2);
3144 if (*op == '+')
3145 tv1->vval.v_float += f;
3146 else
3147 tv1->vval.v_float -= f;
3148 }
3149 return OK;
3150#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003151 }
3152 }
3153
3154 EMSG2(_(e_letwrong), op);
3155 return FAIL;
3156}
3157
3158/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159 * Add a watcher to a list.
3160 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003161 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 list_T *l;
3164 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165{
3166 lw->lw_next = l->lv_watch;
3167 l->lv_watch = lw;
3168}
3169
3170/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003171 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 * No warning when it isn't found...
3173 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003174 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 list_T *l;
3177 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178{
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180
3181 lwp = &l->lv_watch;
3182 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3183 {
3184 if (lw == lwrem)
3185 {
3186 *lwp = lw->lw_next;
3187 break;
3188 }
3189 lwp = &lw->lw_next;
3190 }
3191}
3192
3193/*
3194 * Just before removing an item from a list: advance watchers to the next
3195 * item.
3196 */
3197 static void
3198list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 list_T *l;
3200 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201{
Bram Moolenaar33570922005-01-25 22:26:29 +00003202 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203
3204 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3205 if (lw->lw_item == item)
3206 lw->lw_item = item->li_next;
3207}
3208
3209/*
3210 * Evaluate the expression used in a ":for var in expr" command.
3211 * "arg" points to "var".
3212 * Set "*errp" to TRUE for an error, FALSE otherwise;
3213 * Return a pointer that holds the info. Null when there is an error.
3214 */
3215 void *
3216eval_for_line(arg, errp, nextcmdp, skip)
3217 char_u *arg;
3218 int *errp;
3219 char_u **nextcmdp;
3220 int skip;
3221{
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 typval_T tv;
3225 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226
3227 *errp = TRUE; /* default: there is an error */
3228
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 if (fi == NULL)
3231 return NULL;
3232
3233 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3234 if (expr == NULL)
3235 return fi;
3236
3237 expr = skipwhite(expr);
3238 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3239 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003240 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 return fi;
3242 }
3243
3244 if (skip)
3245 ++emsg_skip;
3246 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3247 {
3248 *errp = FALSE;
3249 if (!skip)
3250 {
3251 l = tv.vval.v_list;
3252 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003253 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003255 clear_tv(&tv);
3256 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 else
3258 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003259 /* No need to increment the refcount, it's already set for the
3260 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 fi->fi_list = l;
3262 list_add_watch(l, &fi->fi_lw);
3263 fi->fi_lw.lw_item = l->lv_first;
3264 }
3265 }
3266 }
3267 if (skip)
3268 --emsg_skip;
3269
3270 return fi;
3271}
3272
3273/*
3274 * Use the first item in a ":for" list. Advance to the next.
3275 * Assign the values to the variable (list). "arg" points to the first one.
3276 * Return TRUE when a valid item was found, FALSE when at end of list or
3277 * something wrong.
3278 */
3279 int
3280next_for_item(fi_void, arg)
3281 void *fi_void;
3282 char_u *arg;
3283{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003284 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003286 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287
3288 item = fi->fi_lw.lw_item;
3289 if (item == NULL)
3290 result = FALSE;
3291 else
3292 {
3293 fi->fi_lw.lw_item = item->li_next;
3294 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3295 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3296 }
3297 return result;
3298}
3299
3300/*
3301 * Free the structure used to store info used by ":for".
3302 */
3303 void
3304free_for_info(fi_void)
3305 void *fi_void;
3306{
Bram Moolenaar33570922005-01-25 22:26:29 +00003307 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003309 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003310 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003312 list_unref(fi->fi_list);
3313 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 vim_free(fi);
3315}
3316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3318
3319 void
3320set_context_for_expression(xp, arg, cmdidx)
3321 expand_T *xp;
3322 char_u *arg;
3323 cmdidx_T cmdidx;
3324{
3325 int got_eq = FALSE;
3326 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 if (cmdidx == CMD_let)
3330 {
3331 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003332 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 {
3334 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003335 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 {
3337 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003338 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 if (vim_iswhite(*p))
3340 break;
3341 }
3342 return;
3343 }
3344 }
3345 else
3346 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3347 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 while ((xp->xp_pattern = vim_strpbrk(arg,
3349 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3350 {
3351 c = *xp->xp_pattern;
3352 if (c == '&')
3353 {
3354 c = xp->xp_pattern[1];
3355 if (c == '&')
3356 {
3357 ++xp->xp_pattern;
3358 xp->xp_context = cmdidx != CMD_let || got_eq
3359 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3360 }
3361 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003364 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3365 xp->xp_pattern += 2;
3366
3367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 else if (c == '$')
3370 {
3371 /* environment variable */
3372 xp->xp_context = EXPAND_ENV_VARS;
3373 }
3374 else if (c == '=')
3375 {
3376 got_eq = TRUE;
3377 xp->xp_context = EXPAND_EXPRESSION;
3378 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003379 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 && xp->xp_context == EXPAND_FUNCTIONS
3381 && vim_strchr(xp->xp_pattern, '(') == NULL)
3382 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003383 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 break;
3385 }
3386 else if (cmdidx != CMD_let || got_eq)
3387 {
3388 if (c == '"') /* string */
3389 {
3390 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3391 if (c == '\\' && xp->xp_pattern[1] != NUL)
3392 ++xp->xp_pattern;
3393 xp->xp_context = EXPAND_NOTHING;
3394 }
3395 else if (c == '\'') /* literal string */
3396 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003397 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3399 /* skip */ ;
3400 xp->xp_context = EXPAND_NOTHING;
3401 }
3402 else if (c == '|')
3403 {
3404 if (xp->xp_pattern[1] == '|')
3405 {
3406 ++xp->xp_pattern;
3407 xp->xp_context = EXPAND_EXPRESSION;
3408 }
3409 else
3410 xp->xp_context = EXPAND_COMMANDS;
3411 }
3412 else
3413 xp->xp_context = EXPAND_EXPRESSION;
3414 }
3415 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003416 /* Doesn't look like something valid, expand as an expression
3417 * anyway. */
3418 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 arg = xp->xp_pattern;
3420 if (*arg != NUL)
3421 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3422 /* skip */ ;
3423 }
3424 xp->xp_pattern = arg;
3425}
3426
3427#endif /* FEAT_CMDL_COMPL */
3428
3429/*
3430 * ":1,25call func(arg1, arg2)" function call.
3431 */
3432 void
3433ex_call(eap)
3434 exarg_T *eap;
3435{
3436 char_u *arg = eap->arg;
3437 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003439 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003441 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 linenr_T lnum;
3443 int doesrange;
3444 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003445 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003447 if (eap->skip)
3448 {
3449 /* trans_function_name() doesn't work well when skipping, use eval0()
3450 * instead to skip to any following command, e.g. for:
3451 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003452 ++emsg_skip;
3453 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3454 clear_tv(&rettv);
3455 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003456 return;
3457 }
3458
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003459 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003460 if (fudi.fd_newkey != NULL)
3461 {
3462 /* Still need to give an error message for missing key. */
3463 EMSG2(_(e_dictkey), fudi.fd_newkey);
3464 vim_free(fudi.fd_newkey);
3465 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003466 if (tofree == NULL)
3467 return;
3468
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003469 /* Increase refcount on dictionary, it could get deleted when evaluating
3470 * the arguments. */
3471 if (fudi.fd_dict != NULL)
3472 ++fudi.fd_dict->dv_refcount;
3473
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003474 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003475 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003476 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
Bram Moolenaar532c7802005-01-27 14:44:31 +00003478 /* Skip white space to allow ":call func ()". Not good, but required for
3479 * backward compatibility. */
3480 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 if (*startarg != '(')
3484 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003485 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 goto end;
3487 }
3488
3489 /*
3490 * When skipping, evaluate the function once, to find the end of the
3491 * arguments.
3492 * When the function takes a range, this is discovered after the first
3493 * call, and the loop is broken.
3494 */
3495 if (eap->skip)
3496 {
3497 ++emsg_skip;
3498 lnum = eap->line2; /* do it once, also with an invalid range */
3499 }
3500 else
3501 lnum = eap->line1;
3502 for ( ; lnum <= eap->line2; ++lnum)
3503 {
3504 if (!eap->skip && eap->addr_count > 0)
3505 {
3506 curwin->w_cursor.lnum = lnum;
3507 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003508#ifdef FEAT_VIRTUALEDIT
3509 curwin->w_cursor.coladd = 0;
3510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003513 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003514 eap->line1, eap->line2, &doesrange,
3515 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 failed = TRUE;
3518 break;
3519 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003520
3521 /* Handle a function returning a Funcref, Dictionary or List. */
3522 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3523 {
3524 failed = TRUE;
3525 break;
3526 }
3527
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003528 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (doesrange || eap->skip)
3530 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003533 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003535 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (aborting())
3537 break;
3538 }
3539 if (eap->skip)
3540 --emsg_skip;
3541
3542 if (!failed)
3543 {
3544 /* Check for trailing illegal characters and a following command. */
3545 if (!ends_excmd(*arg))
3546 {
3547 emsg_severe = TRUE;
3548 EMSG(_(e_trailing));
3549 }
3550 else
3551 eap->nextcmd = check_nextcmd(arg);
3552 }
3553
3554end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003555 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003556 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557}
3558
3559/*
3560 * ":unlet[!] var1 ... " command.
3561 */
3562 void
3563ex_unlet(eap)
3564 exarg_T *eap;
3565{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003566 ex_unletlock(eap, eap->arg, 0);
3567}
3568
3569/*
3570 * ":lockvar" and ":unlockvar" commands
3571 */
3572 void
3573ex_lockvar(eap)
3574 exarg_T *eap;
3575{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 int deep = 2;
3578
3579 if (eap->forceit)
3580 deep = -1;
3581 else if (vim_isdigit(*arg))
3582 {
3583 deep = getdigits(&arg);
3584 arg = skipwhite(arg);
3585 }
3586
3587 ex_unletlock(eap, arg, deep);
3588}
3589
3590/*
3591 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3592 */
3593 static void
3594ex_unletlock(eap, argstart, deep)
3595 exarg_T *eap;
3596 char_u *argstart;
3597 int deep;
3598{
3599 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003602 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604 do
3605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003607 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003608 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 if (lv.ll_name == NULL)
3610 error = TRUE; /* error but continue parsing */
3611 if (name_end == NULL || (!vim_iswhite(*name_end)
3612 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (name_end != NULL)
3615 {
3616 emsg_severe = TRUE;
3617 EMSG(_(e_trailing));
3618 }
3619 if (!(eap->skip || error))
3620 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 break;
3622 }
3623
3624 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003625 {
3626 if (eap->cmdidx == CMD_unlet)
3627 {
3628 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3629 error = TRUE;
3630 }
3631 else
3632 {
3633 if (do_lock_var(&lv, name_end, deep,
3634 eap->cmdidx == CMD_lockvar) == FAIL)
3635 error = TRUE;
3636 }
3637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 if (!eap->skip)
3640 clear_lval(&lv);
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 arg = skipwhite(name_end);
3643 } while (!ends_excmd(*arg));
3644
3645 eap->nextcmd = check_nextcmd(arg);
3646}
3647
Bram Moolenaar8c711452005-01-14 21:53:12 +00003648 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003650 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003652 int forceit;
3653{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 int ret = OK;
3655 int cc;
3656
3657 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 cc = *name_end;
3660 *name_end = NUL;
3661
3662 /* Normal name or expanded name. */
3663 if (check_changedtick(lp->ll_name))
3664 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003665 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003669 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003670 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003671 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003672 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 else if (lp->ll_range)
3675 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003676 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003677 listitem_T *ll_li = lp->ll_li;
3678 int ll_n1 = lp->ll_n1;
3679
3680 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3681 {
3682 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003684 return FAIL;
3685 ll_li = li;
3686 ++ll_n1;
3687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688
3689 /* Delete a range of List items. */
3690 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3691 {
3692 li = lp->ll_li->li_next;
3693 listitem_remove(lp->ll_list, lp->ll_li);
3694 lp->ll_li = li;
3695 ++lp->ll_n1;
3696 }
3697 }
3698 else
3699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 /* unlet a List item. */
3702 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003705 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 }
3707
3708 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709}
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711/*
3712 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003713 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 */
3715 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003716do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
Bram Moolenaar33570922005-01-25 22:26:29 +00003720 hashtab_T *ht;
3721 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003722 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003723 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003724 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
Bram Moolenaar33570922005-01-25 22:26:29 +00003726 ht = find_var_ht(name, &varname);
3727 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003729 if (ht == &globvarht)
3730 d = &globvardict;
3731 else if (current_funccal != NULL
3732 && ht == &current_funccal->l_vars.dv_hashtab)
3733 d = &current_funccal->l_vars;
3734 else
3735 {
3736 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3737 d = di->di_tv.vval.v_dict;
3738 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 hi = hash_find(ht, varname);
3740 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003741 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003742 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003743 if (var_check_fixed(di->di_flags, name, FALSE)
3744 || var_check_ro(di->di_flags, name, FALSE)
3745 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003746 return FAIL;
3747 delete_var(ht, hi);
3748 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003751 if (forceit)
3752 return OK;
3753 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 return FAIL;
3755}
3756
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003757/*
3758 * Lock or unlock variable indicated by "lp".
3759 * "deep" is the levels to go (-1 for unlimited);
3760 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3761 */
3762 static int
3763do_lock_var(lp, name_end, deep, lock)
3764 lval_T *lp;
3765 char_u *name_end;
3766 int deep;
3767 int lock;
3768{
3769 int ret = OK;
3770 int cc;
3771 dictitem_T *di;
3772
3773 if (deep == 0) /* nothing to do */
3774 return OK;
3775
3776 if (lp->ll_tv == NULL)
3777 {
3778 cc = *name_end;
3779 *name_end = NUL;
3780
3781 /* Normal name or expanded name. */
3782 if (check_changedtick(lp->ll_name))
3783 ret = FAIL;
3784 else
3785 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003786 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787 if (di == NULL)
3788 ret = FAIL;
3789 else
3790 {
3791 if (lock)
3792 di->di_flags |= DI_FLAGS_LOCK;
3793 else
3794 di->di_flags &= ~DI_FLAGS_LOCK;
3795 item_lock(&di->di_tv, deep, lock);
3796 }
3797 }
3798 *name_end = cc;
3799 }
3800 else if (lp->ll_range)
3801 {
3802 listitem_T *li = lp->ll_li;
3803
3804 /* (un)lock a range of List items. */
3805 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3806 {
3807 item_lock(&li->li_tv, deep, lock);
3808 li = li->li_next;
3809 ++lp->ll_n1;
3810 }
3811 }
3812 else if (lp->ll_list != NULL)
3813 /* (un)lock a List item. */
3814 item_lock(&lp->ll_li->li_tv, deep, lock);
3815 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003816 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003817 item_lock(&lp->ll_di->di_tv, deep, lock);
3818
3819 return ret;
3820}
3821
3822/*
3823 * Lock or unlock an item. "deep" is nr of levels to go.
3824 */
3825 static void
3826item_lock(tv, deep, lock)
3827 typval_T *tv;
3828 int deep;
3829 int lock;
3830{
3831 static int recurse = 0;
3832 list_T *l;
3833 listitem_T *li;
3834 dict_T *d;
3835 hashitem_T *hi;
3836 int todo;
3837
3838 if (recurse >= DICT_MAXNEST)
3839 {
3840 EMSG(_("E743: variable nested too deep for (un)lock"));
3841 return;
3842 }
3843 if (deep == 0)
3844 return;
3845 ++recurse;
3846
3847 /* lock/unlock the item itself */
3848 if (lock)
3849 tv->v_lock |= VAR_LOCKED;
3850 else
3851 tv->v_lock &= ~VAR_LOCKED;
3852
3853 switch (tv->v_type)
3854 {
3855 case VAR_LIST:
3856 if ((l = tv->vval.v_list) != NULL)
3857 {
3858 if (lock)
3859 l->lv_lock |= VAR_LOCKED;
3860 else
3861 l->lv_lock &= ~VAR_LOCKED;
3862 if (deep < 0 || deep > 1)
3863 /* recursive: lock/unlock the items the List contains */
3864 for (li = l->lv_first; li != NULL; li = li->li_next)
3865 item_lock(&li->li_tv, deep - 1, lock);
3866 }
3867 break;
3868 case VAR_DICT:
3869 if ((d = tv->vval.v_dict) != NULL)
3870 {
3871 if (lock)
3872 d->dv_lock |= VAR_LOCKED;
3873 else
3874 d->dv_lock &= ~VAR_LOCKED;
3875 if (deep < 0 || deep > 1)
3876 {
3877 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003878 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003879 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3880 {
3881 if (!HASHITEM_EMPTY(hi))
3882 {
3883 --todo;
3884 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3885 }
3886 }
3887 }
3888 }
3889 }
3890 --recurse;
3891}
3892
Bram Moolenaara40058a2005-07-11 22:42:07 +00003893/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003894 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3895 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003896 */
3897 static int
3898tv_islocked(tv)
3899 typval_T *tv;
3900{
3901 return (tv->v_lock & VAR_LOCKED)
3902 || (tv->v_type == VAR_LIST
3903 && tv->vval.v_list != NULL
3904 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3905 || (tv->v_type == VAR_DICT
3906 && tv->vval.v_dict != NULL
3907 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3908}
3909
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3911/*
3912 * Delete all "menutrans_" variables.
3913 */
3914 void
3915del_menutrans_vars()
3916{
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003918 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
Bram Moolenaar33570922005-01-25 22:26:29 +00003920 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003921 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 {
3924 if (!HASHITEM_EMPTY(hi))
3925 {
3926 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3928 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 }
3930 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003931 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932}
3933#endif
3934
3935#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3936
3937/*
3938 * Local string buffer for the next two functions to store a variable name
3939 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3940 * get_user_var_name().
3941 */
3942
3943static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3944
3945static char_u *varnamebuf = NULL;
3946static int varnamebuflen = 0;
3947
3948/*
3949 * Function to concatenate a prefix and a variable name.
3950 */
3951 static char_u *
3952cat_prefix_varname(prefix, name)
3953 int prefix;
3954 char_u *name;
3955{
3956 int len;
3957
3958 len = (int)STRLEN(name) + 3;
3959 if (len > varnamebuflen)
3960 {
3961 vim_free(varnamebuf);
3962 len += 10; /* some additional space */
3963 varnamebuf = alloc(len);
3964 if (varnamebuf == NULL)
3965 {
3966 varnamebuflen = 0;
3967 return NULL;
3968 }
3969 varnamebuflen = len;
3970 }
3971 *varnamebuf = prefix;
3972 varnamebuf[1] = ':';
3973 STRCPY(varnamebuf + 2, name);
3974 return varnamebuf;
3975}
3976
3977/*
3978 * Function given to ExpandGeneric() to obtain the list of user defined
3979 * (global/buffer/window/built-in) variable names.
3980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 char_u *
3982get_user_var_name(xp, idx)
3983 expand_T *xp;
3984 int idx;
3985{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003986 static long_u gdone;
3987 static long_u bdone;
3988 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003989#ifdef FEAT_WINDOWS
3990 static long_u tdone;
3991#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003992 static int vidx;
3993 static hashitem_T *hi;
3994 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003997 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003998 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003999#ifdef FEAT_WINDOWS
4000 tdone = 0;
4001#endif
4002 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004003
4004 /* Global variables */
4005 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004007 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004008 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004009 else
4010 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 while (HASHITEM_EMPTY(hi))
4012 ++hi;
4013 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4014 return cat_prefix_varname('g', hi->hi_key);
4015 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004017
4018 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004019 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004024 else
4025 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 while (HASHITEM_EMPTY(hi))
4027 ++hi;
4028 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return (char_u *)"b:changedtick";
4034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035
4036 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004037 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004040 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004042 else
4043 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004044 while (HASHITEM_EMPTY(hi))
4045 ++hi;
4046 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004049#ifdef FEAT_WINDOWS
4050 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004051 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004052 if (tdone < ht->ht_used)
4053 {
4054 if (tdone++ == 0)
4055 hi = ht->ht_array;
4056 else
4057 ++hi;
4058 while (HASHITEM_EMPTY(hi))
4059 ++hi;
4060 return cat_prefix_varname('t', hi->hi_key);
4061 }
4062#endif
4063
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 /* v: variables */
4065 if (vidx < VV_LEN)
4066 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 vim_free(varnamebuf);
4069 varnamebuf = NULL;
4070 varnamebuflen = 0;
4071 return NULL;
4072}
4073
4074#endif /* FEAT_CMDL_COMPL */
4075
4076/*
4077 * types for expressions.
4078 */
4079typedef enum
4080{
4081 TYPE_UNKNOWN = 0
4082 , TYPE_EQUAL /* == */
4083 , TYPE_NEQUAL /* != */
4084 , TYPE_GREATER /* > */
4085 , TYPE_GEQUAL /* >= */
4086 , TYPE_SMALLER /* < */
4087 , TYPE_SEQUAL /* <= */
4088 , TYPE_MATCH /* =~ */
4089 , TYPE_NOMATCH /* !~ */
4090} exptype_T;
4091
4092/*
4093 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4096 */
4097
4098/*
4099 * Handle zero level expression.
4100 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004102 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 * Return OK or FAIL.
4104 */
4105 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 char_u **nextcmd;
4110 int evaluate;
4111{
4112 int ret;
4113 char_u *p;
4114
4115 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (ret == FAIL || !ends_excmd(*p))
4118 {
4119 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 /*
4122 * Report the invalid expression unless the expression evaluation has
4123 * been cancelled due to an aborting error, an interrupt, or an
4124 * exception.
4125 */
4126 if (!aborting())
4127 EMSG2(_(e_invexpr2), arg);
4128 ret = FAIL;
4129 }
4130 if (nextcmd != NULL)
4131 *nextcmd = check_nextcmd(p);
4132
4133 return ret;
4134}
4135
4136/*
4137 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004138 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 *
4140 * "arg" must point to the first non-white of the expression.
4141 * "arg" is advanced to the next non-white after the recognized expression.
4142 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004143 * Note: "rettv.v_lock" is not set.
4144 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 * Return OK or FAIL.
4146 */
4147 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 int evaluate;
4152{
4153 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004154 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /*
4157 * Get the first variable.
4158 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return FAIL;
4161
4162 if ((*arg)[0] == '?')
4163 {
4164 result = FALSE;
4165 if (evaluate)
4166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 int error = FALSE;
4168
4169 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 if (error)
4173 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175
4176 /*
4177 * Get the second variable.
4178 */
4179 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return FAIL;
4182
4183 /*
4184 * Check for the ":".
4185 */
4186 if ((*arg)[0] != ':')
4187 {
4188 EMSG(_("E109: Missing ':' after '?'"));
4189 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192 }
4193
4194 /*
4195 * Get the third variable.
4196 */
4197 *arg = skipwhite(*arg + 1);
4198 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4199 {
4200 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return FAIL;
4203 }
4204 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207
4208 return OK;
4209}
4210
4211/*
4212 * Handle first level expression:
4213 * expr2 || expr2 || expr2 logical OR
4214 *
4215 * "arg" must point to the first non-white of the expression.
4216 * "arg" is advanced to the next non-white after the recognized expression.
4217 *
4218 * Return OK or FAIL.
4219 */
4220 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 int evaluate;
4225{
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 long result;
4228 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004229 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 /*
4232 * Get the first variable.
4233 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 return FAIL;
4236
4237 /*
4238 * Repeat until there is no following "||".
4239 */
4240 first = TRUE;
4241 result = FALSE;
4242 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4243 {
4244 if (evaluate && first)
4245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 if (error)
4250 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 first = FALSE;
4252 }
4253
4254 /*
4255 * Get the second variable.
4256 */
4257 *arg = skipwhite(*arg + 2);
4258 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4259 return FAIL;
4260
4261 /*
4262 * Compute the result.
4263 */
4264 if (evaluate && !result)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (error)
4270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 if (evaluate)
4273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 rettv->v_type = VAR_NUMBER;
4275 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 }
4278
4279 return OK;
4280}
4281
4282/*
4283 * Handle second level expression:
4284 * expr3 && expr3 && expr3 logical AND
4285 *
4286 * "arg" must point to the first non-white of the expression.
4287 * "arg" is advanced to the next non-white after the recognized expression.
4288 *
4289 * Return OK or FAIL.
4290 */
4291 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 int evaluate;
4296{
Bram Moolenaar33570922005-01-25 22:26:29 +00004297 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 long result;
4299 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
4302 /*
4303 * Get the first variable.
4304 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 return FAIL;
4307
4308 /*
4309 * Repeat until there is no following "&&".
4310 */
4311 first = TRUE;
4312 result = TRUE;
4313 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4314 {
4315 if (evaluate && first)
4316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (error)
4321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 first = FALSE;
4323 }
4324
4325 /*
4326 * Get the second variable.
4327 */
4328 *arg = skipwhite(*arg + 2);
4329 if (eval4(arg, &var2, evaluate && result) == FAIL)
4330 return FAIL;
4331
4332 /*
4333 * Compute the result.
4334 */
4335 if (evaluate && result)
4336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (error)
4341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343 if (evaluate)
4344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 rettv->v_type = VAR_NUMBER;
4346 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 }
4349
4350 return OK;
4351}
4352
4353/*
4354 * Handle third level expression:
4355 * var1 == var2
4356 * var1 =~ var2
4357 * var1 != var2
4358 * var1 !~ var2
4359 * var1 > var2
4360 * var1 >= var2
4361 * var1 < var2
4362 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004363 * var1 is var2
4364 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 *
4366 * "arg" must point to the first non-white of the expression.
4367 * "arg" is advanced to the next non-white after the recognized expression.
4368 *
4369 * Return OK or FAIL.
4370 */
4371 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 int evaluate;
4376{
Bram Moolenaar33570922005-01-25 22:26:29 +00004377 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u *p;
4379 int i;
4380 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004381 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 int len = 2;
4383 long n1, n2;
4384 char_u *s1, *s2;
4385 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4386 regmatch_T regmatch;
4387 int ic;
4388 char_u *save_cpo;
4389
4390 /*
4391 * Get the first variable.
4392 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return FAIL;
4395
4396 p = *arg;
4397 switch (p[0])
4398 {
4399 case '=': if (p[1] == '=')
4400 type = TYPE_EQUAL;
4401 else if (p[1] == '~')
4402 type = TYPE_MATCH;
4403 break;
4404 case '!': if (p[1] == '=')
4405 type = TYPE_NEQUAL;
4406 else if (p[1] == '~')
4407 type = TYPE_NOMATCH;
4408 break;
4409 case '>': if (p[1] != '=')
4410 {
4411 type = TYPE_GREATER;
4412 len = 1;
4413 }
4414 else
4415 type = TYPE_GEQUAL;
4416 break;
4417 case '<': if (p[1] != '=')
4418 {
4419 type = TYPE_SMALLER;
4420 len = 1;
4421 }
4422 else
4423 type = TYPE_SEQUAL;
4424 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004425 case 'i': if (p[1] == 's')
4426 {
4427 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4428 len = 5;
4429 if (!vim_isIDc(p[len]))
4430 {
4431 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4432 type_is = TRUE;
4433 }
4434 }
4435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437
4438 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004439 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 */
4441 if (type != TYPE_UNKNOWN)
4442 {
4443 /* extra question mark appended: ignore case */
4444 if (p[len] == '?')
4445 {
4446 ic = TRUE;
4447 ++len;
4448 }
4449 /* extra '#' appended: match case */
4450 else if (p[len] == '#')
4451 {
4452 ic = FALSE;
4453 ++len;
4454 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004455 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 else
4457 ic = p_ic;
4458
4459 /*
4460 * Get the second variable.
4461 */
4462 *arg = skipwhite(p + len);
4463 if (eval5(arg, &var2, evaluate) == FAIL)
4464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 return FAIL;
4467 }
4468
4469 if (evaluate)
4470 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 if (type_is && rettv->v_type != var2.v_type)
4472 {
4473 /* For "is" a different type always means FALSE, for "notis"
4474 * it means TRUE. */
4475 n1 = (type == TYPE_NEQUAL);
4476 }
4477 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4478 {
4479 if (type_is)
4480 {
4481 n1 = (rettv->v_type == var2.v_type
4482 && rettv->vval.v_list == var2.vval.v_list);
4483 if (type == TYPE_NEQUAL)
4484 n1 = !n1;
4485 }
4486 else if (rettv->v_type != var2.v_type
4487 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4488 {
4489 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004490 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004491 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004492 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 clear_tv(rettv);
4494 clear_tv(&var2);
4495 return FAIL;
4496 }
4497 else
4498 {
4499 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004500 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4501 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 if (type == TYPE_NEQUAL)
4503 n1 = !n1;
4504 }
4505 }
4506
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004507 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4508 {
4509 if (type_is)
4510 {
4511 n1 = (rettv->v_type == var2.v_type
4512 && rettv->vval.v_dict == var2.vval.v_dict);
4513 if (type == TYPE_NEQUAL)
4514 n1 = !n1;
4515 }
4516 else if (rettv->v_type != var2.v_type
4517 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4518 {
4519 if (rettv->v_type != var2.v_type)
4520 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4521 else
4522 EMSG(_("E736: Invalid operation for Dictionary"));
4523 clear_tv(rettv);
4524 clear_tv(&var2);
4525 return FAIL;
4526 }
4527 else
4528 {
4529 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004530 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4531 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004532 if (type == TYPE_NEQUAL)
4533 n1 = !n1;
4534 }
4535 }
4536
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004537 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4538 {
4539 if (rettv->v_type != var2.v_type
4540 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4541 {
4542 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004543 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004545 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 clear_tv(rettv);
4547 clear_tv(&var2);
4548 return FAIL;
4549 }
4550 else
4551 {
4552 /* Compare two Funcrefs for being equal or unequal. */
4553 if (rettv->vval.v_string == NULL
4554 || var2.vval.v_string == NULL)
4555 n1 = FALSE;
4556 else
4557 n1 = STRCMP(rettv->vval.v_string,
4558 var2.vval.v_string) == 0;
4559 if (type == TYPE_NEQUAL)
4560 n1 = !n1;
4561 }
4562 }
4563
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004564#ifdef FEAT_FLOAT
4565 /*
4566 * If one of the two variables is a float, compare as a float.
4567 * When using "=~" or "!~", always compare as string.
4568 */
4569 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4570 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4571 {
4572 float_T f1, f2;
4573
4574 if (rettv->v_type == VAR_FLOAT)
4575 f1 = rettv->vval.v_float;
4576 else
4577 f1 = get_tv_number(rettv);
4578 if (var2.v_type == VAR_FLOAT)
4579 f2 = var2.vval.v_float;
4580 else
4581 f2 = get_tv_number(&var2);
4582 n1 = FALSE;
4583 switch (type)
4584 {
4585 case TYPE_EQUAL: n1 = (f1 == f2); break;
4586 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4587 case TYPE_GREATER: n1 = (f1 > f2); break;
4588 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4589 case TYPE_SMALLER: n1 = (f1 < f2); break;
4590 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4591 case TYPE_UNKNOWN:
4592 case TYPE_MATCH:
4593 case TYPE_NOMATCH: break; /* avoid gcc warning */
4594 }
4595 }
4596#endif
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 /*
4599 * If one of the two variables is a number, compare as a number.
4600 * When using "=~" or "!~", always compare as string.
4601 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004602 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 n1 = get_tv_number(rettv);
4606 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 switch (type)
4608 {
4609 case TYPE_EQUAL: n1 = (n1 == n2); break;
4610 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4611 case TYPE_GREATER: n1 = (n1 > n2); break;
4612 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4613 case TYPE_SMALLER: n1 = (n1 < n2); break;
4614 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4615 case TYPE_UNKNOWN:
4616 case TYPE_MATCH:
4617 case TYPE_NOMATCH: break; /* avoid gcc warning */
4618 }
4619 }
4620 else
4621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 s1 = get_tv_string_buf(rettv, buf1);
4623 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4625 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4626 else
4627 i = 0;
4628 n1 = FALSE;
4629 switch (type)
4630 {
4631 case TYPE_EQUAL: n1 = (i == 0); break;
4632 case TYPE_NEQUAL: n1 = (i != 0); break;
4633 case TYPE_GREATER: n1 = (i > 0); break;
4634 case TYPE_GEQUAL: n1 = (i >= 0); break;
4635 case TYPE_SMALLER: n1 = (i < 0); break;
4636 case TYPE_SEQUAL: n1 = (i <= 0); break;
4637
4638 case TYPE_MATCH:
4639 case TYPE_NOMATCH:
4640 /* avoid 'l' flag in 'cpoptions' */
4641 save_cpo = p_cpo;
4642 p_cpo = (char_u *)"";
4643 regmatch.regprog = vim_regcomp(s2,
4644 RE_MAGIC + RE_STRING);
4645 regmatch.rm_ic = ic;
4646 if (regmatch.regprog != NULL)
4647 {
4648 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004649 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (type == TYPE_NOMATCH)
4651 n1 = !n1;
4652 }
4653 p_cpo = save_cpo;
4654 break;
4655
4656 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4657 }
4658 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 clear_tv(rettv);
4660 clear_tv(&var2);
4661 rettv->v_type = VAR_NUMBER;
4662 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
4664 }
4665
4666 return OK;
4667}
4668
4669/*
4670 * Handle fourth level expression:
4671 * + number addition
4672 * - number subtraction
4673 * . string concatenation
4674 *
4675 * "arg" must point to the first non-white of the expression.
4676 * "arg" is advanced to the next non-white after the recognized expression.
4677 *
4678 * Return OK or FAIL.
4679 */
4680 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 int evaluate;
4685{
Bram Moolenaar33570922005-01-25 22:26:29 +00004686 typval_T var2;
4687 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 int op;
4689 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004690#ifdef FEAT_FLOAT
4691 float_T f1 = 0, f2 = 0;
4692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 char_u *s1, *s2;
4694 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4695 char_u *p;
4696
4697 /*
4698 * Get the first variable.
4699 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004700 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 return FAIL;
4702
4703 /*
4704 * Repeat computing, until no '+', '-' or '.' is following.
4705 */
4706 for (;;)
4707 {
4708 op = **arg;
4709 if (op != '+' && op != '-' && op != '.')
4710 break;
4711
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712 if ((op != '+' || rettv->v_type != VAR_LIST)
4713#ifdef FEAT_FLOAT
4714 && (op == '.' || rettv->v_type != VAR_FLOAT)
4715#endif
4716 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 {
4718 /* For "list + ...", an illegal use of the first operand as
4719 * a number cannot be determined before evaluating the 2nd
4720 * operand: if this is also a list, all is ok.
4721 * For "something . ...", "something - ..." or "non-list + ...",
4722 * we know that the first operand needs to be a string or number
4723 * without evaluating the 2nd operand. So check before to avoid
4724 * side effects after an error. */
4725 if (evaluate && get_tv_string_chk(rettv) == NULL)
4726 {
4727 clear_tv(rettv);
4728 return FAIL;
4729 }
4730 }
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 /*
4733 * Get the second variable.
4734 */
4735 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004736 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004738 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 return FAIL;
4740 }
4741
4742 if (evaluate)
4743 {
4744 /*
4745 * Compute the result.
4746 */
4747 if (op == '.')
4748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4750 s2 = get_tv_string_buf_chk(&var2, buf2);
4751 if (s2 == NULL) /* type error ? */
4752 {
4753 clear_tv(rettv);
4754 clear_tv(&var2);
4755 return FAIL;
4756 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004757 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
4759 rettv->v_type = VAR_STRING;
4760 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004762 else if (op == '+' && rettv->v_type == VAR_LIST
4763 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004764 {
4765 /* concatenate Lists */
4766 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4767 &var3) == FAIL)
4768 {
4769 clear_tv(rettv);
4770 clear_tv(&var2);
4771 return FAIL;
4772 }
4773 clear_tv(rettv);
4774 *rettv = var3;
4775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 else
4777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 int error = FALSE;
4779
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#ifdef FEAT_FLOAT
4781 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783 f1 = rettv->vval.v_float;
4784 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 else
4787#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 n1 = get_tv_number_chk(rettv, &error);
4790 if (error)
4791 {
4792 /* This can only happen for "list + non-list". For
4793 * "non-list + ..." or "something - ...", we returned
4794 * before evaluating the 2nd operand. */
4795 clear_tv(rettv);
4796 return FAIL;
4797 }
4798#ifdef FEAT_FLOAT
4799 if (var2.v_type == VAR_FLOAT)
4800 f1 = n1;
4801#endif
4802 }
4803#ifdef FEAT_FLOAT
4804 if (var2.v_type == VAR_FLOAT)
4805 {
4806 f2 = var2.vval.v_float;
4807 n2 = 0;
4808 }
4809 else
4810#endif
4811 {
4812 n2 = get_tv_number_chk(&var2, &error);
4813 if (error)
4814 {
4815 clear_tv(rettv);
4816 clear_tv(&var2);
4817 return FAIL;
4818 }
4819#ifdef FEAT_FLOAT
4820 if (rettv->v_type == VAR_FLOAT)
4821 f2 = n2;
4822#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004823 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004825
4826#ifdef FEAT_FLOAT
4827 /* If there is a float on either side the result is a float. */
4828 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4829 {
4830 if (op == '+')
4831 f1 = f1 + f2;
4832 else
4833 f1 = f1 - f2;
4834 rettv->v_type = VAR_FLOAT;
4835 rettv->vval.v_float = f1;
4836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838#endif
4839 {
4840 if (op == '+')
4841 n1 = n1 + n2;
4842 else
4843 n1 = n1 - n2;
4844 rettv->v_type = VAR_NUMBER;
4845 rettv->vval.v_number = n1;
4846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850 }
4851 return OK;
4852}
4853
4854/*
4855 * Handle fifth level expression:
4856 * * number multiplication
4857 * / number division
4858 * % number modulo
4859 *
4860 * "arg" must point to the first non-white of the expression.
4861 * "arg" is advanced to the next non-white after the recognized expression.
4862 *
4863 * Return OK or FAIL.
4864 */
4865 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004866eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004870 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
Bram Moolenaar33570922005-01-25 22:26:29 +00004872 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 int op;
4874 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004875#ifdef FEAT_FLOAT
4876 int use_float = FALSE;
4877 float_T f1 = 0, f2;
4878#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 /*
4882 * Get the first variable.
4883 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004884 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 return FAIL;
4886
4887 /*
4888 * Repeat computing, until no '*', '/' or '%' is following.
4889 */
4890 for (;;)
4891 {
4892 op = **arg;
4893 if (op != '*' && op != '/' && op != '%')
4894 break;
4895
4896 if (evaluate)
4897 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898#ifdef FEAT_FLOAT
4899 if (rettv->v_type == VAR_FLOAT)
4900 {
4901 f1 = rettv->vval.v_float;
4902 use_float = TRUE;
4903 n1 = 0;
4904 }
4905 else
4906#endif
4907 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004908 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004909 if (error)
4910 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 else
4913 n1 = 0;
4914
4915 /*
4916 * Get the second variable.
4917 */
4918 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004919 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 return FAIL;
4921
4922 if (evaluate)
4923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924#ifdef FEAT_FLOAT
4925 if (var2.v_type == VAR_FLOAT)
4926 {
4927 if (!use_float)
4928 {
4929 f1 = n1;
4930 use_float = TRUE;
4931 }
4932 f2 = var2.vval.v_float;
4933 n2 = 0;
4934 }
4935 else
4936#endif
4937 {
4938 n2 = get_tv_number_chk(&var2, &error);
4939 clear_tv(&var2);
4940 if (error)
4941 return FAIL;
4942#ifdef FEAT_FLOAT
4943 if (use_float)
4944 f2 = n2;
4945#endif
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 /*
4949 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004950 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952#ifdef FEAT_FLOAT
4953 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 if (op == '*')
4956 f1 = f1 * f2;
4957 else if (op == '/')
4958 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004959# ifdef VMS
4960 /* VMS crashes on divide by zero, work around it */
4961 if (f2 == 0.0)
4962 {
4963 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004964 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004966 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004967 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004968 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004969 }
4970 else
4971 f1 = f1 / f2;
4972# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973 /* We rely on the floating point library to handle divide
4974 * by zero to result in "inf" and not a crash. */
4975 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004976# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004980 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 return FAIL;
4982 }
4983 rettv->v_type = VAR_FLOAT;
4984 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 if (op == '*')
4990 n1 = n1 * n2;
4991 else if (op == '/')
4992 {
4993 if (n2 == 0) /* give an error message? */
4994 {
4995 if (n1 == 0)
4996 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4997 else if (n1 < 0)
4998 n1 = -0x7fffffffL;
4999 else
5000 n1 = 0x7fffffffL;
5001 }
5002 else
5003 n1 = n1 / n2;
5004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 {
5007 if (n2 == 0) /* give an error message? */
5008 n1 = 0;
5009 else
5010 n1 = n1 % n2;
5011 }
5012 rettv->v_type = VAR_NUMBER;
5013 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
5016 }
5017
5018 return OK;
5019}
5020
5021/*
5022 * Handle sixth level expression:
5023 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005024 * "string" string constant
5025 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 * &option-name option value
5027 * @r register contents
5028 * identifier variable value
5029 * function() function call
5030 * $VAR environment variable
5031 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005032 * [expr, expr] List
5033 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 *
5035 * Also handle:
5036 * ! in front logical NOT
5037 * - in front unary minus
5038 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 * trailing [] subscript in String or List
5040 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *
5042 * "arg" must point to the first non-white of the expression.
5043 * "arg" is advanced to the next non-white after the recognized expression.
5044 *
5045 * Return OK or FAIL.
5046 */
5047 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005048eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005052 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 long n;
5055 int len;
5056 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 char_u *start_leader, *end_leader;
5058 int ret = OK;
5059 char_u *alias;
5060
5061 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005063 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
5067 /*
5068 * Skip '!' and '-' characters. They are handled later.
5069 */
5070 start_leader = *arg;
5071 while (**arg == '!' || **arg == '-' || **arg == '+')
5072 *arg = skipwhite(*arg + 1);
5073 end_leader = *arg;
5074
5075 switch (**arg)
5076 {
5077 /*
5078 * Number constant.
5079 */
5080 case '0':
5081 case '1':
5082 case '2':
5083 case '3':
5084 case '4':
5085 case '5':
5086 case '6':
5087 case '7':
5088 case '8':
5089 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005090 {
5091#ifdef FEAT_FLOAT
5092 char_u *p = skipdigits(*arg + 1);
5093 int get_float = FALSE;
5094
5095 /* We accept a float when the format matches
5096 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005097 * strict to avoid backwards compatibility problems.
5098 * Don't look for a float after the "." operator, so that
5099 * ":let vers = 1.2.3" doesn't fail. */
5100 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 get_float = TRUE;
5103 p = skipdigits(p + 2);
5104 if (*p == 'e' || *p == 'E')
5105 {
5106 ++p;
5107 if (*p == '-' || *p == '+')
5108 ++p;
5109 if (!vim_isdigit(*p))
5110 get_float = FALSE;
5111 else
5112 p = skipdigits(p + 1);
5113 }
5114 if (ASCII_ISALPHA(*p) || *p == '.')
5115 get_float = FALSE;
5116 }
5117 if (get_float)
5118 {
5119 float_T f;
5120
5121 *arg += string2float(*arg, &f);
5122 if (evaluate)
5123 {
5124 rettv->v_type = VAR_FLOAT;
5125 rettv->vval.v_float = f;
5126 }
5127 }
5128 else
5129#endif
5130 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005131 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005132 *arg += len;
5133 if (evaluate)
5134 {
5135 rettv->v_type = VAR_NUMBER;
5136 rettv->vval.v_number = n;
5137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 /*
5143 * String constant: "string".
5144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147
5148 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152 break;
5153
5154 /*
5155 * List: [expr, expr]
5156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 break;
5159
5160 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 * Dictionary: {key: val, key: val}
5162 */
5163 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5164 break;
5165
5166 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005169 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171
5172 /*
5173 * Environment variable: $VAR.
5174 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Register contents: @r.
5180 */
5181 case '@': ++*arg;
5182 if (evaluate)
5183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005184 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005185 rettv->vval.v_string = get_reg_contents(**arg,
5186 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188 if (**arg != NUL)
5189 ++*arg;
5190 break;
5191
5192 /*
5193 * nested expression: (expression).
5194 */
5195 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (**arg == ')')
5198 ++*arg;
5199 else if (ret == OK)
5200 {
5201 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 ret = FAIL;
5204 }
5205 break;
5206
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 break;
5209 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210
5211 if (ret == NOTDONE)
5212 {
5213 /*
5214 * Must be a variable or function name.
5215 * Can also be a curly-braces kind of name: {expr}.
5216 */
5217 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005218 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 if (alias != NULL)
5220 s = alias;
5221
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005222 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 ret = FAIL;
5224 else
5225 {
5226 if (**arg == '(') /* recursive! */
5227 {
5228 /* If "s" is the name of a variable of type VAR_FUNC
5229 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005230 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231
5232 /* Invoke the function. */
5233 ret = get_func_tv(s, len, rettv, arg,
5234 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005235 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005236
5237 /* If evaluate is FALSE rettv->v_type was not set in
5238 * get_func_tv, but it's needed in handle_subscript() to parse
5239 * what follows. So set it here. */
5240 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5241 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005242 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005243 rettv->v_type = VAR_FUNC;
5244 }
5245
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 /* Stop the expression evaluation when immediately
5247 * aborting on error, or when an interrupt occurred or
5248 * an exception was thrown but not caught. */
5249 if (aborting())
5250 {
5251 if (ret == OK)
5252 clear_tv(rettv);
5253 ret = FAIL;
5254 }
5255 }
5256 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005257 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005258 else
5259 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005261 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 }
5263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 *arg = skipwhite(*arg);
5265
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005266 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5267 * expr(expr). */
5268 if (ret == OK)
5269 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 /*
5272 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5273 */
5274 if (ret == OK && evaluate && end_leader > start_leader)
5275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005276 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005277 int val = 0;
5278#ifdef FEAT_FLOAT
5279 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005281 if (rettv->v_type == VAR_FLOAT)
5282 f = rettv->vval.v_float;
5283 else
5284#endif
5285 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 clear_tv(rettv);
5289 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 else
5292 {
5293 while (end_leader > start_leader)
5294 {
5295 --end_leader;
5296 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005297 {
5298#ifdef FEAT_FLOAT
5299 if (rettv->v_type == VAR_FLOAT)
5300 f = !f;
5301 else
5302#endif
5303 val = !val;
5304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005306 {
5307#ifdef FEAT_FLOAT
5308 if (rettv->v_type == VAR_FLOAT)
5309 f = -f;
5310 else
5311#endif
5312 val = -val;
5313 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005315#ifdef FEAT_FLOAT
5316 if (rettv->v_type == VAR_FLOAT)
5317 {
5318 clear_tv(rettv);
5319 rettv->vval.v_float = f;
5320 }
5321 else
5322#endif
5323 {
5324 clear_tv(rettv);
5325 rettv->v_type = VAR_NUMBER;
5326 rettv->vval.v_number = val;
5327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330
5331 return ret;
5332}
5333
5334/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005335 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5336 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5338 */
5339 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005340eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005342 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005344 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345{
5346 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 long len = -1;
5350 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005354 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005356 if (verbose)
5357 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 return FAIL;
5359 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005360#ifdef FEAT_FLOAT
5361 else if (rettv->v_type == VAR_FLOAT)
5362 {
5363 if (verbose)
5364 EMSG(_(e_float_as_string));
5365 return FAIL;
5366 }
5367#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 /*
5372 * dict.name
5373 */
5374 key = *arg + 1;
5375 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5376 ;
5377 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 }
5381 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383 /*
5384 * something[idx]
5385 *
5386 * Get the (first) variable from inside the [].
5387 */
5388 *arg = skipwhite(*arg + 1);
5389 if (**arg == ':')
5390 empty1 = TRUE;
5391 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5392 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005393 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5394 {
5395 /* not a number or string */
5396 clear_tv(&var1);
5397 return FAIL;
5398 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399
5400 /*
5401 * Get the second variable from inside the [:].
5402 */
5403 if (**arg == ':')
5404 {
5405 range = TRUE;
5406 *arg = skipwhite(*arg + 1);
5407 if (**arg == ']')
5408 empty2 = TRUE;
5409 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005411 if (!empty1)
5412 clear_tv(&var1);
5413 return FAIL;
5414 }
5415 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5416 {
5417 /* not a number or string */
5418 if (!empty1)
5419 clear_tv(&var1);
5420 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421 return FAIL;
5422 }
5423 }
5424
5425 /* Check for the ']'. */
5426 if (**arg != ']')
5427 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005428 if (verbose)
5429 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 clear_tv(&var1);
5431 if (range)
5432 clear_tv(&var2);
5433 return FAIL;
5434 }
5435 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 }
5437
5438 if (evaluate)
5439 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005440 n1 = 0;
5441 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005443 n1 = get_tv_number(&var1);
5444 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 }
5446 if (range)
5447 {
5448 if (empty2)
5449 n2 = -1;
5450 else
5451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 n2 = get_tv_number(&var2);
5453 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 }
5456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
5459 case VAR_NUMBER:
5460 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 len = (long)STRLEN(s);
5463 if (range)
5464 {
5465 /* The resulting variable is a substring. If the indexes
5466 * are out of range the result is empty. */
5467 if (n1 < 0)
5468 {
5469 n1 = len + n1;
5470 if (n1 < 0)
5471 n1 = 0;
5472 }
5473 if (n2 < 0)
5474 n2 = len + n2;
5475 else if (n2 >= len)
5476 n2 = len;
5477 if (n1 >= len || n2 < 0 || n1 > n2)
5478 s = NULL;
5479 else
5480 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5481 }
5482 else
5483 {
5484 /* The resulting variable is a string of a single
5485 * character. If the index is too big or negative the
5486 * result is empty. */
5487 if (n1 >= len || n1 < 0)
5488 s = NULL;
5489 else
5490 s = vim_strnsave(s + n1, 1);
5491 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 clear_tv(rettv);
5493 rettv->v_type = VAR_STRING;
5494 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 break;
5496
5497 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005498 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 if (n1 < 0)
5500 n1 = len + n1;
5501 if (!empty1 && (n1 < 0 || n1 >= len))
5502 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005503 /* For a range we allow invalid values and return an empty
5504 * list. A list index out of range is an error. */
5505 if (!range)
5506 {
5507 if (verbose)
5508 EMSGN(_(e_listidx), n1);
5509 return FAIL;
5510 }
5511 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513 if (range)
5514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005515 list_T *l;
5516 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517
5518 if (n2 < 0)
5519 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005520 else if (n2 >= len)
5521 n2 = len - 1;
5522 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005523 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 l = list_alloc();
5525 if (l == NULL)
5526 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528 n1 <= n2; ++n1)
5529 {
5530 if (list_append_tv(l, &item->li_tv) == FAIL)
5531 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005532 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 return FAIL;
5534 }
5535 item = item->li_next;
5536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 clear_tv(rettv);
5538 rettv->v_type = VAR_LIST;
5539 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005540 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 }
5542 else
5543 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005544 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 clear_tv(rettv);
5546 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 }
5548 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005549
5550 case VAR_DICT:
5551 if (range)
5552 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005553 if (verbose)
5554 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 if (len == -1)
5556 clear_tv(&var1);
5557 return FAIL;
5558 }
5559 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005560 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561
5562 if (len == -1)
5563 {
5564 key = get_tv_string(&var1);
5565 if (*key == NUL)
5566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005567 if (verbose)
5568 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 clear_tv(&var1);
5570 return FAIL;
5571 }
5572 }
5573
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005574 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005576 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005577 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578 if (len == -1)
5579 clear_tv(&var1);
5580 if (item == NULL)
5581 return FAIL;
5582
5583 copy_tv(&item->di_tv, &var1);
5584 clear_tv(rettv);
5585 *rettv = var1;
5586 }
5587 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 }
5589 }
5590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 return OK;
5592}
5593
5594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 * Get an option value.
5596 * "arg" points to the '&' or '+' before the option name.
5597 * "arg" is advanced to character after the option name.
5598 * Return OK or FAIL.
5599 */
5600 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005603 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 int evaluate;
5605{
5606 char_u *option_end;
5607 long numval;
5608 char_u *stringval;
5609 int opt_type;
5610 int c;
5611 int working = (**arg == '+'); /* has("+option") */
5612 int ret = OK;
5613 int opt_flags;
5614
5615 /*
5616 * Isolate the option name and find its value.
5617 */
5618 option_end = find_option_end(arg, &opt_flags);
5619 if (option_end == NULL)
5620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 EMSG2(_("E112: Option name missing: %s"), *arg);
5623 return FAIL;
5624 }
5625
5626 if (!evaluate)
5627 {
5628 *arg = option_end;
5629 return OK;
5630 }
5631
5632 c = *option_end;
5633 *option_end = NUL;
5634 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
5637 if (opt_type == -3) /* invalid name */
5638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 EMSG2(_("E113: Unknown option: %s"), *arg);
5641 ret = FAIL;
5642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
5645 if (opt_type == -2) /* hidden string option */
5646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 rettv->v_type = VAR_STRING;
5648 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
5650 else if (opt_type == -1) /* hidden number option */
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 rettv->v_type = VAR_NUMBER;
5653 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655 else if (opt_type == 1) /* number option */
5656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 rettv->v_type = VAR_NUMBER;
5658 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 else /* string option */
5661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 rettv->v_type = VAR_STRING;
5663 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665 }
5666 else if (working && (opt_type == -2 || opt_type == -1))
5667 ret = FAIL;
5668
5669 *option_end = c; /* put back for error messages */
5670 *arg = option_end;
5671
5672 return ret;
5673}
5674
5675/*
5676 * Allocate a variable for a string constant.
5677 * Return OK or FAIL.
5678 */
5679 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 int evaluate;
5684{
5685 char_u *p;
5686 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 int extra = 0;
5688
5689 /*
5690 * Find the end of the string, skipping backslashed characters.
5691 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005692 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
5694 if (*p == '\\' && p[1] != NUL)
5695 {
5696 ++p;
5697 /* A "\<x>" form occupies at least 4 characters, and produces up
5698 * to 6 characters: reserve space for 2 extra */
5699 if (*p == '<')
5700 extra += 2;
5701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
5703
5704 if (*p != '"')
5705 {
5706 EMSG2(_("E114: Missing quote: %s"), *arg);
5707 return FAIL;
5708 }
5709
5710 /* If only parsing, set *arg and return here */
5711 if (!evaluate)
5712 {
5713 *arg = p + 1;
5714 return OK;
5715 }
5716
5717 /*
5718 * Copy the string into allocated memory, handling backslashed
5719 * characters.
5720 */
5721 name = alloc((unsigned)(p - *arg + extra));
5722 if (name == NULL)
5723 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 rettv->v_type = VAR_STRING;
5725 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 if (*p == '\\')
5730 {
5731 switch (*++p)
5732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 case 'b': *name++ = BS; ++p; break;
5734 case 'e': *name++ = ESC; ++p; break;
5735 case 'f': *name++ = FF; ++p; break;
5736 case 'n': *name++ = NL; ++p; break;
5737 case 'r': *name++ = CAR; ++p; break;
5738 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740 case 'X': /* hex: "\x1", "\x12" */
5741 case 'x':
5742 case 'u': /* Unicode: "\u0023" */
5743 case 'U':
5744 if (vim_isxdigit(p[1]))
5745 {
5746 int n, nr;
5747 int c = toupper(*p);
5748
5749 if (c == 'X')
5750 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005751 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005753 else
5754 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 nr = 0;
5756 while (--n >= 0 && vim_isxdigit(p[1]))
5757 {
5758 ++p;
5759 nr = (nr << 4) + hex2nr(*p);
5760 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762#ifdef FEAT_MBYTE
5763 /* For "\u" store the number according to
5764 * 'encoding'. */
5765 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 else
5768#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 break;
5772
5773 /* octal: "\1", "\12", "\123" */
5774 case '0':
5775 case '1':
5776 case '2':
5777 case '3':
5778 case '4':
5779 case '5':
5780 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 case '7': *name = *p++ - '0';
5782 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 *name = (*name << 3) + *p++ - '0';
5785 if (*p >= '0' && *p <= '7')
5786 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 break;
5790
5791 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 if (extra != 0)
5794 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 break;
5797 }
5798 /* FALLTHROUGH */
5799
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 break;
5802 }
5803 }
5804 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 *arg = p + 1;
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 return OK;
5812}
5813
5814/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 * Return OK or FAIL.
5817 */
5818 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005819get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 int evaluate;
5823{
5824 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005825 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005826 int reduce = 0;
5827
5828 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005830 */
5831 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5832 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005836 break;
5837 ++reduce;
5838 ++p;
5839 }
5840 }
5841
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 return FAIL;
5846 }
5847
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 if (!evaluate)
5850 {
5851 *arg = p + 1;
5852 return OK;
5853 }
5854
5855 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 */
5858 str = alloc((unsigned)((p - *arg) - reduce));
5859 if (str == NULL)
5860 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 rettv->v_type = VAR_STRING;
5862 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 break;
5870 ++p;
5871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 *arg = p + 1;
5876
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 return OK;
5878}
5879
5880/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 * Allocate a variable for a List and fill it from "*arg".
5882 * Return OK or FAIL.
5883 */
5884 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005885get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005887 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 int evaluate;
5889{
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 list_T *l = NULL;
5891 typval_T tv;
5892 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893
5894 if (evaluate)
5895 {
5896 l = list_alloc();
5897 if (l == NULL)
5898 return FAIL;
5899 }
5900
5901 *arg = skipwhite(*arg + 1);
5902 while (**arg != ']' && **arg != NUL)
5903 {
5904 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5905 goto failret;
5906 if (evaluate)
5907 {
5908 item = listitem_alloc();
5909 if (item != NULL)
5910 {
5911 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005912 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 list_append(l, item);
5914 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005915 else
5916 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 }
5918
5919 if (**arg == ']')
5920 break;
5921 if (**arg != ',')
5922 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 goto failret;
5925 }
5926 *arg = skipwhite(*arg + 1);
5927 }
5928
5929 if (**arg != ']')
5930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932failret:
5933 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005934 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 return FAIL;
5936 }
5937
5938 *arg = skipwhite(*arg + 1);
5939 if (evaluate)
5940 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005941 rettv->v_type = VAR_LIST;
5942 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 ++l->lv_refcount;
5944 }
5945
5946 return OK;
5947}
5948
5949/*
5950 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005951 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005953 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954list_alloc()
5955{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005956 list_T *l;
5957
5958 l = (list_T *)alloc_clear(sizeof(list_T));
5959 if (l != NULL)
5960 {
5961 /* Prepend the list to the list of lists for garbage collection. */
5962 if (first_list != NULL)
5963 first_list->lv_used_prev = l;
5964 l->lv_used_prev = NULL;
5965 l->lv_used_next = first_list;
5966 first_list = l;
5967 }
5968 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969}
5970
5971/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005972 * Allocate an empty list for a return value.
5973 * Returns OK or FAIL.
5974 */
5975 static int
5976rettv_list_alloc(rettv)
5977 typval_T *rettv;
5978{
5979 list_T *l = list_alloc();
5980
5981 if (l == NULL)
5982 return FAIL;
5983
5984 rettv->vval.v_list = l;
5985 rettv->v_type = VAR_LIST;
5986 ++l->lv_refcount;
5987 return OK;
5988}
5989
5990/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 * Unreference a list: decrement the reference count and free it when it
5992 * becomes zero.
5993 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005994 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005998 if (l != NULL && --l->lv_refcount <= 0)
5999 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000}
6001
6002/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006003 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004 * Ignores the reference count.
6005 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006006 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006007list_free(l, recurse)
6008 list_T *l;
6009 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006013 /* Remove the list from the list of lists for garbage collection. */
6014 if (l->lv_used_prev == NULL)
6015 first_list = l->lv_used_next;
6016 else
6017 l->lv_used_prev->lv_used_next = l->lv_used_next;
6018 if (l->lv_used_next != NULL)
6019 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6020
Bram Moolenaard9fba312005-06-26 22:34:35 +00006021 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006023 /* Remove the item before deleting it. */
6024 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006025 if (recurse || (item->li_tv.v_type != VAR_LIST
6026 && item->li_tv.v_type != VAR_DICT))
6027 clear_tv(&item->li_tv);
6028 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029 }
6030 vim_free(l);
6031}
6032
6033/*
6034 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006035 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006037 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038listitem_alloc()
6039{
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041}
6042
6043/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006044 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006046 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006050 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 vim_free(item);
6052}
6053
6054/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006055 * Remove a list item from a List and free it. Also clears the value.
6056 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006057 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006058listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 list_T *l;
6060 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006061{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006062 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006063 listitem_free(item);
6064}
6065
6066/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 * Get the number of items in a list.
6068 */
6069 static long
6070list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006071 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 if (l == NULL)
6074 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006075 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076}
6077
6078/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006079 * Return TRUE when two lists have exactly the same values.
6080 */
6081 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006082list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 list_T *l1;
6084 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087{
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006089
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006090 if (l1 == NULL || l2 == NULL)
6091 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006092 if (l1 == l2)
6093 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006094 if (list_len(l1) != list_len(l2))
6095 return FALSE;
6096
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 for (item1 = l1->lv_first, item2 = l2->lv_first;
6098 item1 != NULL && item2 != NULL;
6099 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101 return FALSE;
6102 return item1 == NULL && item2 == NULL;
6103}
6104
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006105#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6106 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006107/*
6108 * Return the dictitem that an entry in a hashtable points to.
6109 */
6110 dictitem_T *
6111dict_lookup(hi)
6112 hashitem_T *hi;
6113{
6114 return HI2DI(hi);
6115}
6116#endif
6117
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006119 * Return TRUE when two dictionaries have exactly the same key/values.
6120 */
6121 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 dict_T *d1;
6124 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006125 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006126 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006127{
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 hashitem_T *hi;
6129 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006130 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006132 if (d1 == NULL || d2 == NULL)
6133 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006134 if (d1 == d2)
6135 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136 if (dict_len(d1) != dict_len(d2))
6137 return FALSE;
6138
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006139 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 if (!HASHITEM_EMPTY(hi))
6143 {
6144 item2 = dict_find(d2, hi->hi_key, -1);
6145 if (item2 == NULL)
6146 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006147 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148 return FALSE;
6149 --todo;
6150 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151 }
6152 return TRUE;
6153}
6154
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006155static int tv_equal_recurse_limit;
6156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006158 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006159 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006160 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 */
6162 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006163tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 typval_T *tv1;
6165 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166 int ic; /* ignore case */
6167 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168{
6169 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006170 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006172 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006174 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006175 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006177 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006178 * recursiveness to a limit. We guess they are equal then.
6179 * A fixed limit has the problem of still taking an awful long time.
6180 * Reduce the limit every time running into it. That should work fine for
6181 * deeply linked structures that are not recursively linked and catch
6182 * recursiveness quickly. */
6183 if (!recursive)
6184 tv_equal_recurse_limit = 1000;
6185 if (recursive_cnt >= tv_equal_recurse_limit)
6186 {
6187 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006188 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006190
6191 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 ++recursive_cnt;
6195 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6196 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006197 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198
6199 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006200 ++recursive_cnt;
6201 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6202 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006203 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006204
6205 case VAR_FUNC:
6206 return (tv1->vval.v_string != NULL
6207 && tv2->vval.v_string != NULL
6208 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6209
6210 case VAR_NUMBER:
6211 return tv1->vval.v_number == tv2->vval.v_number;
6212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006213#ifdef FEAT_FLOAT
6214 case VAR_FLOAT:
6215 return tv1->vval.v_float == tv2->vval.v_float;
6216#endif
6217
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006218 case VAR_STRING:
6219 s1 = get_tv_string_buf(tv1, buf1);
6220 s2 = get_tv_string_buf(tv2, buf2);
6221 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006222 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006223
6224 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225 return TRUE;
6226}
6227
6228/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229 * Locate item with index "n" in list "l" and return it.
6230 * A negative index is counted from the end; -1 is the last item.
6231 * Returns NULL when "n" is out of range.
6232 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006233 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 long n;
6237{
Bram Moolenaar33570922005-01-25 22:26:29 +00006238 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239 long idx;
6240
6241 if (l == NULL)
6242 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006243
6244 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006245 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006246 n = l->lv_len + n;
6247
6248 /* Check for index out of range. */
6249 if (n < 0 || n >= l->lv_len)
6250 return NULL;
6251
6252 /* When there is a cached index may start search from there. */
6253 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006255 if (n < l->lv_idx / 2)
6256 {
6257 /* closest to the start of the list */
6258 item = l->lv_first;
6259 idx = 0;
6260 }
6261 else if (n > (l->lv_idx + l->lv_len) / 2)
6262 {
6263 /* closest to the end of the list */
6264 item = l->lv_last;
6265 idx = l->lv_len - 1;
6266 }
6267 else
6268 {
6269 /* closest to the cached index */
6270 item = l->lv_idx_item;
6271 idx = l->lv_idx;
6272 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273 }
6274 else
6275 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006276 if (n < l->lv_len / 2)
6277 {
6278 /* closest to the start of the list */
6279 item = l->lv_first;
6280 idx = 0;
6281 }
6282 else
6283 {
6284 /* closest to the end of the list */
6285 item = l->lv_last;
6286 idx = l->lv_len - 1;
6287 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006289
6290 while (n > idx)
6291 {
6292 /* search forward */
6293 item = item->li_next;
6294 ++idx;
6295 }
6296 while (n < idx)
6297 {
6298 /* search backward */
6299 item = item->li_prev;
6300 --idx;
6301 }
6302
6303 /* cache the used index */
6304 l->lv_idx = idx;
6305 l->lv_idx_item = item;
6306
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 return item;
6308}
6309
6310/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006311 * Get list item "l[idx]" as a number.
6312 */
6313 static long
6314list_find_nr(l, idx, errorp)
6315 list_T *l;
6316 long idx;
6317 int *errorp; /* set to TRUE when something wrong */
6318{
6319 listitem_T *li;
6320
6321 li = list_find(l, idx);
6322 if (li == NULL)
6323 {
6324 if (errorp != NULL)
6325 *errorp = TRUE;
6326 return -1L;
6327 }
6328 return get_tv_number_chk(&li->li_tv, errorp);
6329}
6330
6331/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006332 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6333 */
6334 char_u *
6335list_find_str(l, idx)
6336 list_T *l;
6337 long idx;
6338{
6339 listitem_T *li;
6340
6341 li = list_find(l, idx - 1);
6342 if (li == NULL)
6343 {
6344 EMSGN(_(e_listidx), idx);
6345 return NULL;
6346 }
6347 return get_tv_string(&li->li_tv);
6348}
6349
6350/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006351 * Locate "item" list "l" and return its index.
6352 * Returns -1 when "item" is not in the list.
6353 */
6354 static long
6355list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 list_T *l;
6357 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006358{
6359 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006361
6362 if (l == NULL)
6363 return -1;
6364 idx = 0;
6365 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6366 ++idx;
6367 if (li == NULL)
6368 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006369 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006370}
6371
6372/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 * Append item "item" to the end of list "l".
6374 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006375 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 list_T *l;
6378 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379{
6380 if (l->lv_last == NULL)
6381 {
6382 /* empty list */
6383 l->lv_first = item;
6384 l->lv_last = item;
6385 item->li_prev = NULL;
6386 }
6387 else
6388 {
6389 l->lv_last->li_next = item;
6390 item->li_prev = l->lv_last;
6391 l->lv_last = item;
6392 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006393 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 item->li_next = NULL;
6395}
6396
6397/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006401 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 list_T *l;
6404 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006406 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407
Bram Moolenaar05159a02005-02-26 23:04:13 +00006408 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006410 copy_tv(tv, &li->li_tv);
6411 list_append(l, li);
6412 return OK;
6413}
6414
6415/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006416 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 * Return FAIL when out of memory.
6418 */
6419 int
6420list_append_dict(list, dict)
6421 list_T *list;
6422 dict_T *dict;
6423{
6424 listitem_T *li = listitem_alloc();
6425
6426 if (li == NULL)
6427 return FAIL;
6428 li->li_tv.v_type = VAR_DICT;
6429 li->li_tv.v_lock = 0;
6430 li->li_tv.vval.v_dict = dict;
6431 list_append(list, li);
6432 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 return OK;
6434}
6435
6436/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006437 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006438 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006439 * Returns FAIL when out of memory.
6440 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006441 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006442list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006443 list_T *l;
6444 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006445 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446{
6447 listitem_T *li = listitem_alloc();
6448
6449 if (li == NULL)
6450 return FAIL;
6451 list_append(l, li);
6452 li->li_tv.v_type = VAR_STRING;
6453 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006454 if (str == NULL)
6455 li->li_tv.vval.v_string = NULL;
6456 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006457 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006458 return FAIL;
6459 return OK;
6460}
6461
6462/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006463 * Append "n" to list "l".
6464 * Returns FAIL when out of memory.
6465 */
6466 static int
6467list_append_number(l, n)
6468 list_T *l;
6469 varnumber_T n;
6470{
6471 listitem_T *li;
6472
6473 li = listitem_alloc();
6474 if (li == NULL)
6475 return FAIL;
6476 li->li_tv.v_type = VAR_NUMBER;
6477 li->li_tv.v_lock = 0;
6478 li->li_tv.vval.v_number = n;
6479 list_append(l, li);
6480 return OK;
6481}
6482
6483/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485 * If "item" is NULL append at the end.
6486 * Return FAIL when out of memory.
6487 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006488 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006489list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006490 list_T *l;
6491 typval_T *tv;
6492 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493{
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495
6496 if (ni == NULL)
6497 return FAIL;
6498 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006499 list_insert(l, ni, item);
6500 return OK;
6501}
6502
6503 void
6504list_insert(l, ni, item)
6505 list_T *l;
6506 listitem_T *ni;
6507 listitem_T *item;
6508{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 if (item == NULL)
6510 /* Append new item at end of list. */
6511 list_append(l, ni);
6512 else
6513 {
6514 /* Insert new item before existing item. */
6515 ni->li_prev = item->li_prev;
6516 ni->li_next = item;
6517 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006520 ++l->lv_idx;
6521 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 l->lv_idx_item = NULL;
6526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530}
6531
6532/*
6533 * Extend "l1" with "l2".
6534 * If "bef" is NULL append at the end, otherwise insert before this item.
6535 * Returns FAIL when out of memory.
6536 */
6537 static int
6538list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 list_T *l1;
6540 list_T *l2;
6541 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542{
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006544 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006546 /* We also quit the loop when we have inserted the original item count of
6547 * the list, avoid a hang when we extend a list with itself. */
6548 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6550 return FAIL;
6551 return OK;
6552}
6553
6554/*
6555 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6556 * Return FAIL when out of memory.
6557 */
6558 static int
6559list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 list_T *l1;
6561 list_T *l2;
6562 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563{
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006566 if (l1 == NULL || l2 == NULL)
6567 return FAIL;
6568
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006570 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571 if (l == NULL)
6572 return FAIL;
6573 tv->v_type = VAR_LIST;
6574 tv->vval.v_list = l;
6575
6576 /* append all items from the second list */
6577 return list_extend(l, l2, NULL);
6578}
6579
6580/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006581 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584 * Returns NULL when out of memory.
6585 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591{
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 list_T *copy;
6593 listitem_T *item;
6594 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
6596 if (orig == NULL)
6597 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598
6599 copy = list_alloc();
6600 if (copy != NULL)
6601 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006602 if (copyID != 0)
6603 {
6604 /* Do this before adding the items, because one of the items may
6605 * refer back to this list. */
6606 orig->lv_copyID = copyID;
6607 orig->lv_copylist = copy;
6608 }
6609 for (item = orig->lv_first; item != NULL && !got_int;
6610 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 {
6612 ni = listitem_alloc();
6613 if (ni == NULL)
6614 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006616 {
6617 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6618 {
6619 vim_free(ni);
6620 break;
6621 }
6622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006624 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 list_append(copy, ni);
6626 }
6627 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628 if (item != NULL)
6629 {
6630 list_unref(copy);
6631 copy = NULL;
6632 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 }
6634
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 return copy;
6636}
6637
6638/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006640 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006641 * This used to be called list_remove, but that conflicts with a Sun header
6642 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006644 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006645vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 list_T *l;
6647 listitem_T *item;
6648 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649{
Bram Moolenaar33570922005-01-25 22:26:29 +00006650 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652 /* notify watchers */
6653 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006655 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006656 list_fix_watch(l, ip);
6657 if (ip == item2)
6658 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006660
6661 if (item2->li_next == NULL)
6662 l->lv_last = item->li_prev;
6663 else
6664 item2->li_next->li_prev = item->li_prev;
6665 if (item->li_prev == NULL)
6666 l->lv_first = item2->li_next;
6667 else
6668 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006669 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670}
6671
6672/*
6673 * Return an allocated string with the string representation of a list.
6674 * May return NULL.
6675 */
6676 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006679 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680{
6681 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682
6683 if (tv->vval.v_list == NULL)
6684 return NULL;
6685 ga_init2(&ga, (int)sizeof(char), 80);
6686 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006687 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006688 {
6689 vim_free(ga.ga_data);
6690 return NULL;
6691 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692 ga_append(&ga, ']');
6693 ga_append(&ga, NUL);
6694 return (char_u *)ga.ga_data;
6695}
6696
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697typedef struct join_S {
6698 char_u *s;
6699 char_u *tofree;
6700} join_T;
6701
6702 static int
6703list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6704 garray_T *gap; /* to store the result in */
6705 list_T *l;
6706 char_u *sep;
6707 int echo_style;
6708 int copyID;
6709 garray_T *join_gap; /* to keep each list item string */
6710{
6711 int i;
6712 join_T *p;
6713 int len;
6714 int sumlen = 0;
6715 int first = TRUE;
6716 char_u *tofree;
6717 char_u numbuf[NUMBUFLEN];
6718 listitem_T *item;
6719 char_u *s;
6720
6721 /* Stringify each item in the list. */
6722 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6723 {
6724 if (echo_style)
6725 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6726 else
6727 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6728 if (s == NULL)
6729 return FAIL;
6730
6731 len = (int)STRLEN(s);
6732 sumlen += len;
6733
6734 ga_grow(join_gap, 1);
6735 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6736 if (tofree != NULL || s != numbuf)
6737 {
6738 p->s = s;
6739 p->tofree = tofree;
6740 }
6741 else
6742 {
6743 p->s = vim_strnsave(s, len);
6744 p->tofree = p->s;
6745 }
6746
6747 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006748 if (did_echo_string_emsg) /* recursion error, bail out */
6749 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006750 }
6751
6752 /* Allocate result buffer with its total size, avoid re-allocation and
6753 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6754 if (join_gap->ga_len >= 2)
6755 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6756 if (ga_grow(gap, sumlen + 2) == FAIL)
6757 return FAIL;
6758
6759 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6760 {
6761 if (first)
6762 first = FALSE;
6763 else
6764 ga_concat(gap, sep);
6765 p = ((join_T *)join_gap->ga_data) + i;
6766
6767 if (p->s != NULL)
6768 ga_concat(gap, p->s);
6769 line_breakcheck();
6770 }
6771
6772 return OK;
6773}
6774
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006775/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006777 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006778 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006780 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006781list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006785 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006786 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006788 garray_T join_ga;
6789 int retval;
6790 join_T *p;
6791 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792
Bram Moolenaard39a7512015-04-16 22:51:22 +02006793 if (l->lv_len < 1)
6794 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006795 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6796 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6797
6798 /* Dispose each item in join_ga. */
6799 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006801 p = (join_T *)join_ga.ga_data;
6802 for (i = 0; i < join_ga.ga_len; ++i)
6803 {
6804 vim_free(p->tofree);
6805 ++p;
6806 }
6807 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006809
6810 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811}
6812
6813/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006814 * Garbage collection for lists and dictionaries.
6815 *
6816 * We use reference counts to be able to free most items right away when they
6817 * are no longer used. But for composite items it's possible that it becomes
6818 * unused while the reference count is > 0: When there is a recursive
6819 * reference. Example:
6820 * :let l = [1, 2, 3]
6821 * :let d = {9: l}
6822 * :let l[1] = d
6823 *
6824 * Since this is quite unusual we handle this with garbage collection: every
6825 * once in a while find out which lists and dicts are not referenced from any
6826 * variable.
6827 *
6828 * Here is a good reference text about garbage collection (refers to Python
6829 * but it applies to all reference-counting mechanisms):
6830 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832
6833/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 * Do garbage collection for lists and dicts.
6835 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 int
6838garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006841 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 buf_T *buf;
6843 win_T *wp;
6844 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006845 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006846 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006848#ifdef FEAT_WINDOWS
6849 tabpage_T *tp;
6850#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006852 /* Only do this once. */
6853 want_garbage_collect = FALSE;
6854 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006855 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006856
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 /* We advance by two because we add one for items referenced through
6858 * previous_funccal. */
6859 current_copyID += COPYID_INC;
6860 copyID = current_copyID;
6861
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 /*
6863 * 1. Go through all accessible variables and mark all lists and dicts
6864 * with copyID.
6865 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866
6867 /* Don't free variables in the previous_funccal list unless they are only
6868 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006869 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6871 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6873 NULL);
6874 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6875 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006876 }
6877
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 /* script-local variables */
6879 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881
6882 /* buffer-local variables */
6883 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6885 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
6887 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006888 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006891#ifdef FEAT_AUTOCMD
6892 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6894 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006895#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006897#ifdef FEAT_WINDOWS
6898 /* tabpage-local variables */
6899 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6901 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902#endif
6903
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906
6907 /* function-local variables */
6908 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6909 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6911 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912 }
6913
Bram Moolenaard812df62008-11-09 12:46:09 +00006914 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006916
Bram Moolenaar1dced572012-04-05 16:54:08 +02006917#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006919#endif
6920
Bram Moolenaardb913952012-06-29 12:54:53 +02006921#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006923#endif
6924
6925#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006927#endif
6928
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 /*
6932 * 2. Free lists and dictionaries that are not referenced.
6933 */
6934 did_free = free_unref_items(copyID);
6935
6936 /*
6937 * 3. Check if any funccal can be freed now.
6938 */
6939 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006940 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 if (can_free_funccal(*pfc, copyID))
6942 {
6943 fc = *pfc;
6944 *pfc = fc->caller;
6945 free_funccal(fc, TRUE);
6946 did_free = TRUE;
6947 did_free_funccal = TRUE;
6948 }
6949 else
6950 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 if (did_free_funccal)
6953 /* When a funccal was freed some more items might be garbage
6954 * collected, so run again. */
6955 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006956 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 else if (p_verbose > 0)
6958 {
6959 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6960 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961
6962 return did_free;
6963}
6964
6965/*
6966 * Free lists and dictionaries that are no longer referenced.
6967 */
6968 static int
6969free_unref_items(copyID)
6970 int copyID;
6971{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006972 dict_T *dd, *dd_next;
6973 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 int did_free = FALSE;
6975
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 */
6979 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 {
6981 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006984 /* Free the Dictionary and ordinary items it contains, but don't
6985 * recurse into Lists and Dictionaries, they will be in the list
6986 * of dicts or list of lists. */
6987 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006990 dd = dd_next;
6991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992
6993 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 * Go through the list of lists and free items without the copyID.
6995 * But don't free a list that has a watcher (used in a for loop), these
6996 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 */
6998 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006999 {
7000 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7002 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007004 /* Free the List and ordinary items it contains, but don't recurse
7005 * into Lists and Dictionaries, they will be in the list of dicts
7006 * or list of lists. */
7007 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007010 ll = ll_next;
7011 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 return did_free;
7013}
7014
7015/*
7016 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 * "list_stack" is used to add lists to be marked. Can be NULL.
7018 *
7019 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 int
7022set_ref_in_ht(ht, copyID, list_stack)
7023 hashtab_T *ht;
7024 int copyID;
7025 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026{
7027 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 hashtab_T *cur_ht;
7031 ht_stack_T *ht_stack = NULL;
7032 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 cur_ht = ht;
7035 for (;;)
7036 {
7037 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 /* Mark each item in the hashtab. If the item contains a hashtab
7040 * it is added to ht_stack, if it contains a list it is added to
7041 * list_stack. */
7042 todo = (int)cur_ht->ht_used;
7043 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7044 if (!HASHITEM_EMPTY(hi))
7045 {
7046 --todo;
7047 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7048 &ht_stack, list_stack);
7049 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051
7052 if (ht_stack == NULL)
7053 break;
7054
7055 /* take an item from the stack */
7056 cur_ht = ht_stack->ht;
7057 tempitem = ht_stack;
7058 ht_stack = ht_stack->prev;
7059 free(tempitem);
7060 }
7061
7062 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063}
7064
7065/*
7066 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7068 *
7069 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 int
7072set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073 list_T *l;
7074 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 listitem_T *li;
7078 int abort = FALSE;
7079 list_T *cur_l;
7080 list_stack_T *list_stack = NULL;
7081 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 cur_l = l;
7084 for (;;)
7085 {
7086 if (!abort)
7087 /* Mark each item in the list. If the item contains a hashtab
7088 * it is added to ht_stack, if it contains a list it is added to
7089 * list_stack. */
7090 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7091 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7092 ht_stack, &list_stack);
7093 if (list_stack == NULL)
7094 break;
7095
7096 /* take an item from the stack */
7097 cur_l = list_stack->list;
7098 tempitem = list_stack;
7099 list_stack = list_stack->prev;
7100 free(tempitem);
7101 }
7102
7103 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104}
7105
7106/*
7107 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 * "list_stack" is used to add lists to be marked. Can be NULL.
7109 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7110 *
7111 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 int
7114set_ref_in_item(tv, copyID, ht_stack, list_stack)
7115 typval_T *tv;
7116 int copyID;
7117 ht_stack_T **ht_stack;
7118 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119{
7120 dict_T *dd;
7121 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007122 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007123
7124 switch (tv->v_type)
7125 {
7126 case VAR_DICT:
7127 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007128 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007129 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007130 /* Didn't see this dict yet. */
7131 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 if (ht_stack == NULL)
7133 {
7134 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7135 }
7136 else
7137 {
7138 ht_stack_T *newitem = (ht_stack_T*)malloc(
7139 sizeof(ht_stack_T));
7140 if (newitem == NULL)
7141 abort = TRUE;
7142 else
7143 {
7144 newitem->ht = &dd->dv_hashtab;
7145 newitem->prev = *ht_stack;
7146 *ht_stack = newitem;
7147 }
7148 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007150 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151
7152 case VAR_LIST:
7153 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007154 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007155 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007156 /* Didn't see this list yet. */
7157 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007158 if (list_stack == NULL)
7159 {
7160 abort = set_ref_in_list(ll, copyID, ht_stack);
7161 }
7162 else
7163 {
7164 list_stack_T *newitem = (list_stack_T*)malloc(
7165 sizeof(list_stack_T));
7166 if (newitem == NULL)
7167 abort = TRUE;
7168 else
7169 {
7170 newitem->list = ll;
7171 newitem->prev = *list_stack;
7172 *list_stack = newitem;
7173 }
7174 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007177 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007179}
7180
7181/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 * Allocate an empty header for a dictionary.
7183 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007184 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007185dict_alloc()
7186{
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007192 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193 if (first_dict != NULL)
7194 first_dict->dv_used_prev = d;
7195 d->dv_used_next = first_dict;
7196 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007197 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007200 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007201 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007202 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007203 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206}
7207
7208/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007209 * Allocate an empty dict for a return value.
7210 * Returns OK or FAIL.
7211 */
7212 static int
7213rettv_dict_alloc(rettv)
7214 typval_T *rettv;
7215{
7216 dict_T *d = dict_alloc();
7217
7218 if (d == NULL)
7219 return FAIL;
7220
7221 rettv->vval.v_dict = d;
7222 rettv->v_type = VAR_DICT;
7223 ++d->dv_refcount;
7224 return OK;
7225}
7226
7227
7228/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 * Unreference a Dictionary: decrement the reference count and free it when it
7230 * becomes zero.
7231 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007232 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007236 if (d != NULL && --d->dv_refcount <= 0)
7237 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238}
7239
7240/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007241 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 * Ignores the reference count.
7243 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007244 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007245dict_free(d, recurse)
7246 dict_T *d;
7247 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007251 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007253 /* Remove the dict from the list of dicts for garbage collection. */
7254 if (d->dv_used_prev == NULL)
7255 first_dict = d->dv_used_next;
7256 else
7257 d->dv_used_prev->dv_used_next = d->dv_used_next;
7258 if (d->dv_used_next != NULL)
7259 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7260
7261 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007262 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007263 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 if (!HASHITEM_EMPTY(hi))
7267 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007268 /* Remove the item before deleting it, just in case there is
7269 * something recursive causing trouble. */
7270 di = HI2DI(hi);
7271 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007272 if (recurse || (di->di_tv.v_type != VAR_LIST
7273 && di->di_tv.v_type != VAR_DICT))
7274 clear_tv(&di->di_tv);
7275 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 --todo;
7277 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 vim_free(d);
7281}
7282
7283/*
7284 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 * The "key" is copied to the new item.
7286 * Note that the value of the item "di_tv" still needs to be initialized!
7287 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007289 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290dictitem_alloc(key)
7291 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292{
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007295 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007297 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007299 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007300 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302}
7303
7304/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 * Make a copy of a Dictionary item.
7306 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310{
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007312
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007313 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7314 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315 if (di != NULL)
7316 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007318 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 copy_tv(&org->di_tv, &di->di_tv);
7320 }
7321 return di;
7322}
7323
7324/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 * Remove item "item" from Dictionary "dict" and free it.
7326 */
7327 static void
7328dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 dict_T *dict;
7330 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331{
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 if (HASHITEM_EMPTY(hi))
7336 EMSG2(_(e_intern2), "dictitem_remove()");
7337 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 dictitem_free(item);
7340}
7341
7342/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343 * Free a dict item. Also clears the value.
7344 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007345 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007350 if (item->di_flags & DI_FLAGS_ALLOC)
7351 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352}
7353
7354/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7356 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007357 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 * Returns NULL when out of memory.
7359 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007364 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365{
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 dict_T *copy;
7367 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370
7371 if (orig == NULL)
7372 return NULL;
7373
7374 copy = dict_alloc();
7375 if (copy != NULL)
7376 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 if (copyID != 0)
7378 {
7379 orig->dv_copyID = copyID;
7380 orig->dv_copydict = copy;
7381 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007382 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 --todo;
7388
7389 di = dictitem_alloc(hi->hi_key);
7390 if (di == NULL)
7391 break;
7392 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007393 {
7394 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7395 copyID) == FAIL)
7396 {
7397 vim_free(di);
7398 break;
7399 }
7400 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 else
7402 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7403 if (dict_add(copy, di) == FAIL)
7404 {
7405 dictitem_free(di);
7406 break;
7407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007412 if (todo > 0)
7413 {
7414 dict_unref(copy);
7415 copy = NULL;
7416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 }
7418
7419 return copy;
7420}
7421
7422/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007424 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007426 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 dict_T *d;
7429 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430{
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432}
7433
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007435 * Add a number or string entry to dictionary "d".
7436 * When "str" is NULL use number "nr", otherwise use "str".
7437 * Returns FAIL when out of memory and when key already exists.
7438 */
7439 int
7440dict_add_nr_str(d, key, nr, str)
7441 dict_T *d;
7442 char *key;
7443 long nr;
7444 char_u *str;
7445{
7446 dictitem_T *item;
7447
7448 item = dictitem_alloc((char_u *)key);
7449 if (item == NULL)
7450 return FAIL;
7451 item->di_tv.v_lock = 0;
7452 if (str == NULL)
7453 {
7454 item->di_tv.v_type = VAR_NUMBER;
7455 item->di_tv.vval.v_number = nr;
7456 }
7457 else
7458 {
7459 item->di_tv.v_type = VAR_STRING;
7460 item->di_tv.vval.v_string = vim_strsave(str);
7461 }
7462 if (dict_add(d, item) == FAIL)
7463 {
7464 dictitem_free(item);
7465 return FAIL;
7466 }
7467 return OK;
7468}
7469
7470/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007471 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007472 * Returns FAIL when out of memory and when key already exists.
7473 */
7474 int
7475dict_add_list(d, key, list)
7476 dict_T *d;
7477 char *key;
7478 list_T *list;
7479{
7480 dictitem_T *item;
7481
7482 item = dictitem_alloc((char_u *)key);
7483 if (item == NULL)
7484 return FAIL;
7485 item->di_tv.v_lock = 0;
7486 item->di_tv.v_type = VAR_LIST;
7487 item->di_tv.vval.v_list = list;
7488 if (dict_add(d, item) == FAIL)
7489 {
7490 dictitem_free(item);
7491 return FAIL;
7492 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007493 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007494 return OK;
7495}
7496
7497/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007498 * Get the number of items in a Dictionary.
7499 */
7500 static long
7501dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007504 if (d == NULL)
7505 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007506 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507}
7508
7509/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 * Find item "key[len]" in Dictionary "d".
7511 * If "len" is negative use strlen(key).
7512 * Returns NULL when not found.
7513 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007514 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 char_u *key;
7518 int len;
7519{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520#define AKEYLEN 200
7521 char_u buf[AKEYLEN];
7522 char_u *akey;
7523 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 if (len < 0)
7527 akey = key;
7528 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 tofree = akey = vim_strnsave(key, len);
7531 if (akey == NULL)
7532 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007533 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 else
7535 {
7536 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007537 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007538 akey = buf;
7539 }
7540
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542 vim_free(tofree);
7543 if (HASHITEM_EMPTY(hi))
7544 return NULL;
7545 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546}
7547
7548/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007549 * Get a string item from a dictionary.
7550 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007551 * Returns NULL if the entry doesn't exist or out of memory.
7552 */
7553 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007554get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007555 dict_T *d;
7556 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007557 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007558{
7559 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007560 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007561
7562 di = dict_find(d, key, -1);
7563 if (di == NULL)
7564 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007565 s = get_tv_string(&di->di_tv);
7566 if (save && s != NULL)
7567 s = vim_strsave(s);
7568 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007569}
7570
7571/*
7572 * Get a number item from a dictionary.
7573 * Returns 0 if the entry doesn't exist or out of memory.
7574 */
7575 long
7576get_dict_number(d, key)
7577 dict_T *d;
7578 char_u *key;
7579{
7580 dictitem_T *di;
7581
7582 di = dict_find(d, key, -1);
7583 if (di == NULL)
7584 return 0;
7585 return get_tv_number(&di->di_tv);
7586}
7587
7588/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 * Return an allocated string with the string representation of a Dictionary.
7590 * May return NULL.
7591 */
7592 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007595 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596{
7597 garray_T ga;
7598 int first = TRUE;
7599 char_u *tofree;
7600 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007604 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 return NULL;
7608 ga_init2(&ga, (int)sizeof(char), 80);
7609 ga_append(&ga, '{');
7610
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007611 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007612 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007616 --todo;
7617
7618 if (first)
7619 first = FALSE;
7620 else
7621 ga_concat(&ga, (char_u *)", ");
7622
7623 tofree = string_quote(hi->hi_key, FALSE);
7624 if (tofree != NULL)
7625 {
7626 ga_concat(&ga, tofree);
7627 vim_free(tofree);
7628 }
7629 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 if (s != NULL)
7632 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007634 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007635 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007636 line_breakcheck();
7637
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007640 if (todo > 0)
7641 {
7642 vim_free(ga.ga_data);
7643 return NULL;
7644 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645
7646 ga_append(&ga, '}');
7647 ga_append(&ga, NUL);
7648 return (char_u *)ga.ga_data;
7649}
7650
7651/*
7652 * Allocate a variable for a Dictionary and fill it from "*arg".
7653 * Return OK or FAIL. Returns NOTDONE for {expr}.
7654 */
7655 static int
7656get_dict_tv(arg, rettv, evaluate)
7657 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 int evaluate;
7660{
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 dict_T *d = NULL;
7662 typval_T tvkey;
7663 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007664 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668
7669 /*
7670 * First check if it's not a curly-braces thing: {expr}.
7671 * Must do this without evaluating, otherwise a function may be called
7672 * twice. Unfortunately this means we need to call eval1() twice for the
7673 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 if (*start != '}')
7677 {
7678 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7679 return FAIL;
7680 if (*start == '}')
7681 return NOTDONE;
7682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683
7684 if (evaluate)
7685 {
7686 d = dict_alloc();
7687 if (d == NULL)
7688 return FAIL;
7689 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690 tvkey.v_type = VAR_UNKNOWN;
7691 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692
7693 *arg = skipwhite(*arg + 1);
7694 while (**arg != '}' && **arg != NUL)
7695 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 goto failret;
7698 if (**arg != ':')
7699 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007700 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007701 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 goto failret;
7703 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007704 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007706 key = get_tv_string_buf_chk(&tvkey, buf);
7707 if (key == NULL || *key == NUL)
7708 {
7709 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7710 if (key != NULL)
7711 EMSG(_(e_emptykey));
7712 clear_tv(&tvkey);
7713 goto failret;
7714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716
7717 *arg = skipwhite(*arg + 1);
7718 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7719 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007720 if (evaluate)
7721 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 goto failret;
7723 }
7724 if (evaluate)
7725 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007726 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 if (item != NULL)
7728 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007729 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007730 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007731 clear_tv(&tv);
7732 goto failret;
7733 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007734 item = dictitem_alloc(key);
7735 clear_tv(&tvkey);
7736 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007739 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007740 if (dict_add(d, item) == FAIL)
7741 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 }
7743 }
7744
7745 if (**arg == '}')
7746 break;
7747 if (**arg != ',')
7748 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007749 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 goto failret;
7751 }
7752 *arg = skipwhite(*arg + 1);
7753 }
7754
7755 if (**arg != '}')
7756 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007757 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758failret:
7759 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007760 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761 return FAIL;
7762 }
7763
7764 *arg = skipwhite(*arg + 1);
7765 if (evaluate)
7766 {
7767 rettv->v_type = VAR_DICT;
7768 rettv->vval.v_dict = d;
7769 ++d->dv_refcount;
7770 }
7771
7772 return OK;
7773}
7774
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 * Return a string with the string representation of a variable.
7777 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007778 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007779 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007780 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007781 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782 */
7783 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007784echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007786 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007787 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007788 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 static int recurse = 0;
7791 char_u *r = NULL;
7792
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007794 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007795 if (!did_echo_string_emsg)
7796 {
7797 /* Only give this message once for a recursive call to avoid
7798 * flooding the user with errors. And stop iterating over lists
7799 * and dicts. */
7800 did_echo_string_emsg = TRUE;
7801 EMSG(_("E724: variable nested too deep for displaying"));
7802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007804 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007805 }
7806 ++recurse;
7807
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007808 switch (tv->v_type)
7809 {
7810 case VAR_FUNC:
7811 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007812 r = tv->vval.v_string;
7813 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007814
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007815 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007816 if (tv->vval.v_list == NULL)
7817 {
7818 *tofree = NULL;
7819 r = NULL;
7820 }
7821 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7822 {
7823 *tofree = NULL;
7824 r = (char_u *)"[...]";
7825 }
7826 else
7827 {
7828 tv->vval.v_list->lv_copyID = copyID;
7829 *tofree = list2string(tv, copyID);
7830 r = *tofree;
7831 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007833
Bram Moolenaar8c711452005-01-14 21:53:12 +00007834 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007835 if (tv->vval.v_dict == NULL)
7836 {
7837 *tofree = NULL;
7838 r = NULL;
7839 }
7840 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7841 {
7842 *tofree = NULL;
7843 r = (char_u *)"{...}";
7844 }
7845 else
7846 {
7847 tv->vval.v_dict->dv_copyID = copyID;
7848 *tofree = dict2string(tv, copyID);
7849 r = *tofree;
7850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853 case VAR_STRING:
7854 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 *tofree = NULL;
7856 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007858
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#ifdef FEAT_FLOAT
7860 case VAR_FLOAT:
7861 *tofree = NULL;
7862 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7863 r = numbuf;
7864 break;
7865#endif
7866
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007868 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007871
Bram Moolenaar8502c702014-06-17 12:51:16 +02007872 if (--recurse == 0)
7873 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007875}
7876
7877/*
7878 * Return a string with the string representation of a variable.
7879 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7880 * "numbuf" is used for a number.
7881 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007882 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 */
7884 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007885tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887 char_u **tofree;
7888 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007889 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890{
7891 switch (tv->v_type)
7892 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 case VAR_FUNC:
7894 *tofree = string_quote(tv->vval.v_string, TRUE);
7895 return *tofree;
7896 case VAR_STRING:
7897 *tofree = string_quote(tv->vval.v_string, FALSE);
7898 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899#ifdef FEAT_FLOAT
7900 case VAR_FLOAT:
7901 *tofree = NULL;
7902 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7903 return numbuf;
7904#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007907 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007910 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007912 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913}
7914
7915/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 * Return string "str" in ' quotes, doubling ' characters.
7917 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007918 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 */
7920 static char_u *
7921string_quote(str, function)
7922 char_u *str;
7923 int function;
7924{
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 char_u *p, *r, *s;
7927
Bram Moolenaar33570922005-01-25 22:26:29 +00007928 len = (function ? 13 : 3);
7929 if (str != NULL)
7930 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007931 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007932 for (p = str; *p != NUL; mb_ptr_adv(p))
7933 if (*p == '\'')
7934 ++len;
7935 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007936 s = r = alloc(len);
7937 if (r != NULL)
7938 {
7939 if (function)
7940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 r += 10;
7943 }
7944 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 if (str != NULL)
7947 for (p = str; *p != NUL; )
7948 {
7949 if (*p == '\'')
7950 *r++ = '\'';
7951 MB_COPY_CHAR(p, r);
7952 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007954 if (function)
7955 *r++ = ')';
7956 *r++ = NUL;
7957 }
7958 return s;
7959}
7960
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007961#ifdef FEAT_FLOAT
7962/*
7963 * Convert the string "text" to a floating point number.
7964 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7965 * this always uses a decimal point.
7966 * Returns the length of the text that was consumed.
7967 */
7968 static int
7969string2float(text, value)
7970 char_u *text;
7971 float_T *value; /* result stored here */
7972{
7973 char *s = (char *)text;
7974 float_T f;
7975
7976 f = strtod(s, &s);
7977 *value = f;
7978 return (int)((char_u *)s - text);
7979}
7980#endif
7981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 * Get the value of an environment variable.
7984 * "arg" is pointing to the '$'. It is advanced to after the name.
7985 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007986 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 */
7988 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007989get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 int evaluate;
7993{
7994 char_u *string = NULL;
7995 int len;
7996 int cc;
7997 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007998 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999
8000 ++*arg;
8001 name = *arg;
8002 len = get_env_len(arg);
8003 if (evaluate)
8004 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008005 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008006 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008007
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008008 cc = name[len];
8009 name[len] = NUL;
8010 /* first try vim_getenv(), fast for normal environment vars */
8011 string = vim_getenv(name, &mustfree);
8012 if (string != NULL && *string != NUL)
8013 {
8014 if (!mustfree)
8015 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008017 else
8018 {
8019 if (mustfree)
8020 vim_free(string);
8021
8022 /* next try expanding things like $VIM and ${HOME} */
8023 string = expand_env_save(name - 1);
8024 if (string != NULL && *string == '$')
8025 {
8026 vim_free(string);
8027 string = NULL;
8028 }
8029 }
8030 name[len] = cc;
8031
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032 rettv->v_type = VAR_STRING;
8033 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 }
8035
8036 return OK;
8037}
8038
8039/*
8040 * Array with names and number of arguments of all internal functions
8041 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8042 */
8043static struct fst
8044{
8045 char *f_name; /* function name */
8046 char f_min_argc; /* minimal number of arguments */
8047 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008049 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050} functions[] =
8051{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008052#ifdef FEAT_FLOAT
8053 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008054 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008056 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008057 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"append", 2, 2, f_append},
8059 {"argc", 0, 0, f_argc},
8060 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008061 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008062 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008063#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008064 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008065 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008066 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008069 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"bufexists", 1, 1, f_bufexists},
8071 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8072 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8073 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8074 {"buflisted", 1, 1, f_buflisted},
8075 {"bufloaded", 1, 1, f_bufloaded},
8076 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008077 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"bufwinnr", 1, 1, f_bufwinnr},
8079 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008080 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008081 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008082 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083#ifdef FEAT_FLOAT
8084 {"ceil", 1, 1, f_ceil},
8085#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008086 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008087 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008089 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008091#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008092 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008093 {"complete_add", 1, 1, f_complete_add},
8094 {"complete_check", 0, 0, f_complete_check},
8095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008098#ifdef FEAT_FLOAT
8099 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008100 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008102 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008104 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008105 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"delete", 1, 1, f_delete},
8107 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008108 {"diff_filler", 1, 1, f_diff_filler},
8109 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008110 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008112 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"eventhandler", 0, 0, f_eventhandler},
8114 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008115 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008117#ifdef FEAT_FLOAT
8118 {"exp", 1, 1, f_exp},
8119#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008120 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008121 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008122 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8124 {"filereadable", 1, 1, f_filereadable},
8125 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008126 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008127 {"finddir", 1, 3, f_finddir},
8128 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008129#ifdef FEAT_FLOAT
8130 {"float2nr", 1, 1, f_float2nr},
8131 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008132 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008134 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"fnamemodify", 2, 2, f_fnamemodify},
8136 {"foldclosed", 1, 1, f_foldclosed},
8137 {"foldclosedend", 1, 1, f_foldclosedend},
8138 {"foldlevel", 1, 1, f_foldlevel},
8139 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008140 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008142 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008143 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008144 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008145 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008146 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"getchar", 0, 1, f_getchar},
8148 {"getcharmod", 0, 0, f_getcharmod},
8149 {"getcmdline", 0, 0, f_getcmdline},
8150 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008151 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008152 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008153 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008155 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008156 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"getfsize", 1, 1, f_getfsize},
8158 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008159 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008160 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008161 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008162 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008163 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008164 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008165 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008166 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008168 {"gettabvar", 2, 3, f_gettabvar},
8169 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"getwinposx", 0, 0, f_getwinposx},
8171 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008172 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008173 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008174 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008175 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008177 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008178 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008179 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8181 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8182 {"histadd", 2, 2, f_histadd},
8183 {"histdel", 1, 2, f_histdel},
8184 {"histget", 1, 2, f_histget},
8185 {"histnr", 1, 1, f_histnr},
8186 {"hlID", 1, 1, f_hlID},
8187 {"hlexists", 1, 1, f_hlexists},
8188 {"hostname", 0, 0, f_hostname},
8189 {"iconv", 3, 3, f_iconv},
8190 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008191 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008192 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008194 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"inputrestore", 0, 0, f_inputrestore},
8196 {"inputsave", 0, 0, f_inputsave},
8197 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008198 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008199 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008201 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008202 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008203 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008204 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008206 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"libcall", 3, 3, f_libcall},
8208 {"libcallnr", 3, 3, f_libcallnr},
8209 {"line", 1, 1, f_line},
8210 {"line2byte", 1, 1, f_line2byte},
8211 {"lispindent", 1, 1, f_lispindent},
8212 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008213#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008214 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008215 {"log10", 1, 1, f_log10},
8216#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008217#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008218 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008219#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008220 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008221 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008222 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008223 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008224 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008225 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008226 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008227 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008228 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008229 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008230 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008231 {"max", 1, 1, f_max},
8232 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008233#ifdef vim_mkdir
8234 {"mkdir", 1, 3, f_mkdir},
8235#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008236 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008237#ifdef FEAT_MZSCHEME
8238 {"mzeval", 1, 1, f_mzeval},
8239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008241 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008242 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008243 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008244#ifdef FEAT_FLOAT
8245 {"pow", 2, 2, f_pow},
8246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008248 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008249 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008250#ifdef FEAT_PYTHON3
8251 {"py3eval", 1, 1, f_py3eval},
8252#endif
8253#ifdef FEAT_PYTHON
8254 {"pyeval", 1, 1, f_pyeval},
8255#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008256 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008257 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008258 {"reltime", 0, 2, f_reltime},
8259 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {"remote_expr", 2, 3, f_remote_expr},
8261 {"remote_foreground", 1, 1, f_remote_foreground},
8262 {"remote_peek", 1, 2, f_remote_peek},
8263 {"remote_read", 1, 1, f_remote_read},
8264 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008265 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008267 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008269 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008270#ifdef FEAT_FLOAT
8271 {"round", 1, 1, f_round},
8272#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008273 {"screenattr", 2, 2, f_screenattr},
8274 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008275 {"screencol", 0, 0, f_screencol},
8276 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008277 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008278 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008279 {"searchpair", 3, 7, f_searchpair},
8280 {"searchpairpos", 3, 7, f_searchpairpos},
8281 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"server2client", 2, 2, f_server2client},
8283 {"serverlist", 0, 0, f_serverlist},
8284 {"setbufvar", 3, 3, f_setbufvar},
8285 {"setcmdpos", 1, 1, f_setcmdpos},
8286 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008287 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008288 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008289 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008290 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008292 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008293 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008295#ifdef FEAT_CRYPT
8296 {"sha256", 1, 1, f_sha256},
8297#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008298 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008299 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008301#ifdef FEAT_FLOAT
8302 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008303 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008304#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008305 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008306 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008307 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008308 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008309 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008310#ifdef FEAT_FLOAT
8311 {"sqrt", 1, 1, f_sqrt},
8312 {"str2float", 1, 1, f_str2float},
8313#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008314 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008315 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008316 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317#ifdef HAVE_STRFTIME
8318 {"strftime", 1, 2, f_strftime},
8319#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008320 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"strlen", 1, 1, f_strlen},
8323 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008324 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008326 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008327 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"substitute", 4, 4, f_substitute},
8329 {"synID", 3, 3, f_synID},
8330 {"synIDattr", 2, 3, f_synIDattr},
8331 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008332 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008333 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008334 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008335 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008336 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008337 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008338 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008339 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008340 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008341#ifdef FEAT_FLOAT
8342 {"tan", 1, 1, f_tan},
8343 {"tanh", 1, 1, f_tanh},
8344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008346 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {"tolower", 1, 1, f_tolower},
8348 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008349 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008350#ifdef FEAT_FLOAT
8351 {"trunc", 1, 1, f_trunc},
8352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008354 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008355 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008356 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008357 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"virtcol", 1, 1, f_virtcol},
8359 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008360 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"winbufnr", 1, 1, f_winbufnr},
8362 {"wincol", 0, 0, f_wincol},
8363 {"winheight", 1, 1, f_winheight},
8364 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008365 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008367 {"winrestview", 1, 1, f_winrestview},
8368 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008370 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008371 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372};
8373
8374#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8375
8376/*
8377 * Function given to ExpandGeneric() to obtain the list of internal
8378 * or user defined function names.
8379 */
8380 char_u *
8381get_function_name(xp, idx)
8382 expand_T *xp;
8383 int idx;
8384{
8385 static int intidx = -1;
8386 char_u *name;
8387
8388 if (idx == 0)
8389 intidx = -1;
8390 if (intidx < 0)
8391 {
8392 name = get_user_func_name(xp, idx);
8393 if (name != NULL)
8394 return name;
8395 }
8396 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8397 {
8398 STRCPY(IObuff, functions[intidx].f_name);
8399 STRCAT(IObuff, "(");
8400 if (functions[intidx].f_max_argc == 0)
8401 STRCAT(IObuff, ")");
8402 return IObuff;
8403 }
8404
8405 return NULL;
8406}
8407
8408/*
8409 * Function given to ExpandGeneric() to obtain the list of internal or
8410 * user defined variable or function names.
8411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 char_u *
8413get_expr_name(xp, idx)
8414 expand_T *xp;
8415 int idx;
8416{
8417 static int intidx = -1;
8418 char_u *name;
8419
8420 if (idx == 0)
8421 intidx = -1;
8422 if (intidx < 0)
8423 {
8424 name = get_function_name(xp, idx);
8425 if (name != NULL)
8426 return name;
8427 }
8428 return get_user_var_name(xp, ++intidx);
8429}
8430
8431#endif /* FEAT_CMDL_COMPL */
8432
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008433#if defined(EBCDIC) || defined(PROTO)
8434/*
8435 * Compare struct fst by function name.
8436 */
8437 static int
8438compare_func_name(s1, s2)
8439 const void *s1;
8440 const void *s2;
8441{
8442 struct fst *p1 = (struct fst *)s1;
8443 struct fst *p2 = (struct fst *)s2;
8444
8445 return STRCMP(p1->f_name, p2->f_name);
8446}
8447
8448/*
8449 * Sort the function table by function name.
8450 * The sorting of the table above is ASCII dependant.
8451 * On machines using EBCDIC we have to sort it.
8452 */
8453 static void
8454sortFunctions()
8455{
8456 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8457
8458 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8459}
8460#endif
8461
8462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463/*
8464 * Find internal function in table above.
8465 * Return index, or -1 if not found
8466 */
8467 static int
8468find_internal_func(name)
8469 char_u *name; /* name of the function */
8470{
8471 int first = 0;
8472 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8473 int cmp;
8474 int x;
8475
8476 /*
8477 * Find the function name in the table. Binary search.
8478 */
8479 while (first <= last)
8480 {
8481 x = first + ((unsigned)(last - first) >> 1);
8482 cmp = STRCMP(name, functions[x].f_name);
8483 if (cmp < 0)
8484 last = x - 1;
8485 else if (cmp > 0)
8486 first = x + 1;
8487 else
8488 return x;
8489 }
8490 return -1;
8491}
8492
8493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008494 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8495 * name it contains, otherwise return "name".
8496 */
8497 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008498deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 char_u *name;
8500 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008501 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008502{
Bram Moolenaar33570922005-01-25 22:26:29 +00008503 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008504 int cc;
8505
8506 cc = name[*lenp];
8507 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008508 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008511 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008512 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008513 {
8514 *lenp = 0;
8515 return (char_u *)""; /* just in case */
8516 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008517 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008518 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519 }
8520
8521 return name;
8522}
8523
8524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 * Allocate a variable for the result of a function.
8526 * Return OK or FAIL.
8527 */
8528 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008529get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8530 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 char_u *name; /* name of the function */
8532 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 char_u **arg; /* argument, pointing to the '(' */
8535 linenr_T firstline; /* first line of range */
8536 linenr_T lastline; /* last line of range */
8537 int *doesrange; /* return: function handled range */
8538 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008539 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
8541 char_u *argp;
8542 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008543 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 int argcount = 0; /* number of arguments found */
8545
8546 /*
8547 * Get the arguments.
8548 */
8549 argp = *arg;
8550 while (argcount < MAX_FUNC_ARGS)
8551 {
8552 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8553 if (*argp == ')' || *argp == ',' || *argp == NUL)
8554 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8556 {
8557 ret = FAIL;
8558 break;
8559 }
8560 ++argcount;
8561 if (*argp != ',')
8562 break;
8563 }
8564 if (*argp == ')')
8565 ++argp;
8566 else
8567 ret = FAIL;
8568
8569 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008571 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 {
8574 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008575 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008577 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
8580 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
8583 *arg = skipwhite(argp);
8584 return ret;
8585}
8586
8587
8588/*
8589 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008590 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008591 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 */
8593 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008594call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008596 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008600 typval_T *argvars; /* vars for arguments, must have "argcount"
8601 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 linenr_T firstline; /* first line of range */
8603 linenr_T lastline; /* last line of range */
8604 int *doesrange; /* return: function handled range */
8605 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
8608 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609#define ERROR_UNKNOWN 0
8610#define ERROR_TOOMANY 1
8611#define ERROR_TOOFEW 2
8612#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008613#define ERROR_DICT 4
8614#define ERROR_NONE 5
8615#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 int error = ERROR_NONE;
8617 int i;
8618 int llen;
8619 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620#define FLEN_FIXED 40
8621 char_u fname_buf[FLEN_FIXED + 1];
8622 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008623 char_u *name;
8624
8625 /* Make a copy of the name, if it comes from a funcref variable it could
8626 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008627 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008628 if (name == NULL)
8629 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630
8631 /*
8632 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8633 * Change <SNR>123_name() to K_SNR 123_name().
8634 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 llen = eval_fname_script(name);
8637 if (llen > 0)
8638 {
8639 fname_buf[0] = K_SPECIAL;
8640 fname_buf[1] = KS_EXTRA;
8641 fname_buf[2] = (int)KE_SNR;
8642 i = 3;
8643 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8644 {
8645 if (current_SID <= 0)
8646 error = ERROR_SCRIPT;
8647 else
8648 {
8649 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8650 i = (int)STRLEN(fname_buf);
8651 }
8652 }
8653 if (i + STRLEN(name + llen) < FLEN_FIXED)
8654 {
8655 STRCPY(fname_buf + i, name + llen);
8656 fname = fname_buf;
8657 }
8658 else
8659 {
8660 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8661 if (fname == NULL)
8662 error = ERROR_OTHER;
8663 else
8664 {
8665 mch_memmove(fname, fname_buf, (size_t)i);
8666 STRCPY(fname + i, name + llen);
8667 }
8668 }
8669 }
8670 else
8671 fname = name;
8672
8673 *doesrange = FALSE;
8674
8675
8676 /* execute the function if no errors detected and executing */
8677 if (evaluate && error == ERROR_NONE)
8678 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008679 char_u *rfname = fname;
8680
8681 /* Ignore "g:" before a function name. */
8682 if (fname[0] == 'g' && fname[1] == ':')
8683 rfname = fname + 2;
8684
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008685 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8686 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 error = ERROR_UNKNOWN;
8688
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008689 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
8691 /*
8692 * User defined function.
8693 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008694 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008697 /* Trigger FuncUndefined event, may load the function. */
8698 if (fp == NULL
8699 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008700 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008701 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008703 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008704 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 }
8706#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008707 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008708 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008709 {
8710 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008712 }
8713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 if (fp != NULL)
8715 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008716 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008718 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008720 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008722 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008723 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 else
8725 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008726 int did_save_redo = FALSE;
8727
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 /*
8729 * Call the user function.
8730 * Save and restore search patterns, script variables and
8731 * redo buffer.
8732 */
8733 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008734 if (!ins_compl_active())
8735 {
8736 saveRedobuff();
8737 did_save_redo = TRUE;
8738 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008739 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008741 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008742 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8743 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8744 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008745 /* Function was unreferenced while being used, free it
8746 * now. */
8747 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008748 if (did_save_redo)
8749 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 restore_search_patterns();
8751 error = ERROR_NONE;
8752 }
8753 }
8754 }
8755 else
8756 {
8757 /*
8758 * Find the function name in the table, call its implementation.
8759 */
8760 i = find_internal_func(fname);
8761 if (i >= 0)
8762 {
8763 if (argcount < functions[i].f_min_argc)
8764 error = ERROR_TOOFEW;
8765 else if (argcount > functions[i].f_max_argc)
8766 error = ERROR_TOOMANY;
8767 else
8768 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008769 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 error = ERROR_NONE;
8772 }
8773 }
8774 }
8775 /*
8776 * The function call (or "FuncUndefined" autocommand sequence) might
8777 * have been aborted by an error, an interrupt, or an explicitly thrown
8778 * exception that has not been caught so far. This situation can be
8779 * tested for by calling aborting(). For an error in an internal
8780 * function or for the "E132" error in call_user_func(), however, the
8781 * throw point at which the "force_abort" flag (temporarily reset by
8782 * emsg()) is normally updated has not been reached yet. We need to
8783 * update that flag first to make aborting() reliable.
8784 */
8785 update_force_abort();
8786 }
8787 if (error == ERROR_NONE)
8788 ret = OK;
8789
8790 /*
8791 * Report an error unless the argument evaluation or function call has been
8792 * cancelled due to an aborting error, an interrupt, or an exception.
8793 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008794 if (!aborting())
8795 {
8796 switch (error)
8797 {
8798 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008799 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008800 break;
8801 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008802 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008803 break;
8804 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008805 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008806 name);
8807 break;
8808 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008809 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008810 name);
8811 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008812 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008813 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008814 name);
8815 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008816 }
8817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 if (fname != name && fname != fname_buf)
8820 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008821 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
8823 return ret;
8824}
8825
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008826/*
8827 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008828 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008829 */
8830 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008831emsg_funcname(ermsg, name)
8832 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008833 char_u *name;
8834{
8835 char_u *p;
8836
8837 if (*name == K_SPECIAL)
8838 p = concat_str((char_u *)"<SNR>", name + 3);
8839 else
8840 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008841 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008842 if (p != name)
8843 vim_free(p);
8844}
8845
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008846/*
8847 * Return TRUE for a non-zero Number and a non-empty String.
8848 */
8849 static int
8850non_zero_arg(argvars)
8851 typval_T *argvars;
8852{
8853 return ((argvars[0].v_type == VAR_NUMBER
8854 && argvars[0].vval.v_number != 0)
8855 || (argvars[0].v_type == VAR_STRING
8856 && argvars[0].vval.v_string != NULL
8857 && *argvars[0].vval.v_string != NUL));
8858}
8859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860/*********************************************
8861 * Implementation of the built-in functions
8862 */
8863
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008864#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008865static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8866
8867/*
8868 * Get the float value of "argvars[0]" into "f".
8869 * Returns FAIL when the argument is not a Number or Float.
8870 */
8871 static int
8872get_float_arg(argvars, f)
8873 typval_T *argvars;
8874 float_T *f;
8875{
8876 if (argvars[0].v_type == VAR_FLOAT)
8877 {
8878 *f = argvars[0].vval.v_float;
8879 return OK;
8880 }
8881 if (argvars[0].v_type == VAR_NUMBER)
8882 {
8883 *f = (float_T)argvars[0].vval.v_number;
8884 return OK;
8885 }
8886 EMSG(_("E808: Number or Float required"));
8887 return FAIL;
8888}
8889
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008890/*
8891 * "abs(expr)" function
8892 */
8893 static void
8894f_abs(argvars, rettv)
8895 typval_T *argvars;
8896 typval_T *rettv;
8897{
8898 if (argvars[0].v_type == VAR_FLOAT)
8899 {
8900 rettv->v_type = VAR_FLOAT;
8901 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8902 }
8903 else
8904 {
8905 varnumber_T n;
8906 int error = FALSE;
8907
8908 n = get_tv_number_chk(&argvars[0], &error);
8909 if (error)
8910 rettv->vval.v_number = -1;
8911 else if (n > 0)
8912 rettv->vval.v_number = n;
8913 else
8914 rettv->vval.v_number = -n;
8915 }
8916}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008917
8918/*
8919 * "acos()" function
8920 */
8921 static void
8922f_acos(argvars, rettv)
8923 typval_T *argvars;
8924 typval_T *rettv;
8925{
8926 float_T f;
8927
8928 rettv->v_type = VAR_FLOAT;
8929 if (get_float_arg(argvars, &f) == OK)
8930 rettv->vval.v_float = acos(f);
8931 else
8932 rettv->vval.v_float = 0.0;
8933}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008934#endif
8935
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008937 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 */
8939 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008940f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 typval_T *argvars;
8942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008947 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008949 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008950 && !tv_check_lock(l->lv_lock,
8951 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008952 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 }
8955 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008956 EMSG(_(e_listreq));
8957}
8958
8959/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008960 * "and(expr, expr)" function
8961 */
8962 static void
8963f_and(argvars, rettv)
8964 typval_T *argvars;
8965 typval_T *rettv;
8966{
8967 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8968 & get_tv_number_chk(&argvars[1], NULL);
8969}
8970
8971/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008972 * "append(lnum, string/list)" function
8973 */
8974 static void
8975f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *argvars;
8977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008978{
8979 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 list_T *l = NULL;
8982 listitem_T *li = NULL;
8983 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008984 long added = 0;
8985
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008986 /* When coming here from Insert mode, sync undo, so that this can be
8987 * undone separately from what was previously inserted. */
8988 if (u_sync_once == 2)
8989 {
8990 u_sync_once = 1; /* notify that u_sync() was called */
8991 u_sync(TRUE);
8992 }
8993
Bram Moolenaar0d660222005-01-07 21:51:51 +00008994 lnum = get_tv_lnum(argvars);
8995 if (lnum >= 0
8996 && lnum <= curbuf->b_ml.ml_line_count
8997 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008998 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008999 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001 l = argvars[1].vval.v_list;
9002 if (l == NULL)
9003 return;
9004 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009006 for (;;)
9007 {
9008 if (l == NULL)
9009 tv = &argvars[1]; /* append a string */
9010 else if (li == NULL)
9011 break; /* end of list */
9012 else
9013 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009014 line = get_tv_string_chk(tv);
9015 if (line == NULL) /* type error */
9016 {
9017 rettv->vval.v_number = 1; /* Failed */
9018 break;
9019 }
9020 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009021 ++added;
9022 if (l == NULL)
9023 break;
9024 li = li->li_next;
9025 }
9026
9027 appended_lines_mark(lnum, added);
9028 if (curwin->w_cursor.lnum > lnum)
9029 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 else
9032 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033}
9034
9035/*
9036 * "argc()" function
9037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009040 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044}
9045
9046/*
9047 * "argidx()" function
9048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009051 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055}
9056
9057/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009058 * "arglistid()" function
9059 */
9060 static void
9061f_arglistid(argvars, rettv)
9062 typval_T *argvars UNUSED;
9063 typval_T *rettv;
9064{
9065 win_T *wp;
9066 tabpage_T *tp = NULL;
9067 long n;
9068
9069 rettv->vval.v_number = -1;
9070 if (argvars[0].v_type != VAR_UNKNOWN)
9071 {
9072 if (argvars[1].v_type != VAR_UNKNOWN)
9073 {
9074 n = get_tv_number(&argvars[1]);
9075 if (n >= 0)
9076 tp = find_tabpage(n);
9077 }
9078 else
9079 tp = curtab;
9080
9081 if (tp != NULL)
9082 {
9083 wp = find_win_by_nr(&argvars[0], tp);
9084 if (wp != NULL)
9085 rettv->vval.v_number = wp->w_alist->id;
9086 }
9087 }
9088 else
9089 rettv->vval.v_number = curwin->w_alist->id;
9090}
9091
9092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 * "argv(nr)" function
9094 */
9095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009097 typval_T *argvars;
9098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099{
9100 int idx;
9101
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009102 if (argvars[0].v_type != VAR_UNKNOWN)
9103 {
9104 idx = get_tv_number_chk(&argvars[0], NULL);
9105 if (idx >= 0 && idx < ARGCOUNT)
9106 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9107 else
9108 rettv->vval.v_string = NULL;
9109 rettv->v_type = VAR_STRING;
9110 }
9111 else if (rettv_list_alloc(rettv) == OK)
9112 for (idx = 0; idx < ARGCOUNT; ++idx)
9113 list_append_string(rettv->vval.v_list,
9114 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115}
9116
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009117#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009118/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009119 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009120 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009121 static void
9122f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009123 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009124 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009125{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009126 float_T f;
9127
9128 rettv->v_type = VAR_FLOAT;
9129 if (get_float_arg(argvars, &f) == OK)
9130 rettv->vval.v_float = asin(f);
9131 else
9132 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009133}
9134
9135/*
9136 * "atan()" function
9137 */
9138 static void
9139f_atan(argvars, rettv)
9140 typval_T *argvars;
9141 typval_T *rettv;
9142{
9143 float_T f;
9144
9145 rettv->v_type = VAR_FLOAT;
9146 if (get_float_arg(argvars, &f) == OK)
9147 rettv->vval.v_float = atan(f);
9148 else
9149 rettv->vval.v_float = 0.0;
9150}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009151
9152/*
9153 * "atan2()" function
9154 */
9155 static void
9156f_atan2(argvars, rettv)
9157 typval_T *argvars;
9158 typval_T *rettv;
9159{
9160 float_T fx, fy;
9161
9162 rettv->v_type = VAR_FLOAT;
9163 if (get_float_arg(argvars, &fx) == OK
9164 && get_float_arg(&argvars[1], &fy) == OK)
9165 rettv->vval.v_float = atan2(fx, fy);
9166 else
9167 rettv->vval.v_float = 0.0;
9168}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009169#endif
9170
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171/*
9172 * "browse(save, title, initdir, default)" function
9173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009176 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179#ifdef FEAT_BROWSE
9180 int save;
9181 char_u *title;
9182 char_u *initdir;
9183 char_u *defname;
9184 char_u buf[NUMBUFLEN];
9185 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009188 save = get_tv_number_chk(&argvars[0], &error);
9189 title = get_tv_string_chk(&argvars[1]);
9190 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9191 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 if (error || title == NULL || initdir == NULL || defname == NULL)
9194 rettv->vval.v_string = NULL;
9195 else
9196 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009197 do_browse(save ? BROWSE_SAVE : 0,
9198 title, defname, NULL, initdir, NULL, curbuf);
9199#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009201#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009203}
9204
9205/*
9206 * "browsedir(title, initdir)" function
9207 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009210 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009211 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009212{
9213#ifdef FEAT_BROWSE
9214 char_u *title;
9215 char_u *initdir;
9216 char_u buf[NUMBUFLEN];
9217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009218 title = get_tv_string_chk(&argvars[0]);
9219 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 if (title == NULL || initdir == NULL)
9222 rettv->vval.v_string = NULL;
9223 else
9224 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009225 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009227 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230}
9231
Bram Moolenaar33570922005-01-25 22:26:29 +00009232static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009233
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234/*
9235 * Find a buffer by number or exact name.
9236 */
9237 static buf_T *
9238find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009239 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009243 if (avar->v_type == VAR_NUMBER)
9244 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009245 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009247 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009248 if (buf == NULL)
9249 {
9250 /* No full path name match, try a match with a URL or a "nofile"
9251 * buffer, these don't use the full path. */
9252 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9253 if (buf->b_fname != NULL
9254 && (path_with_url(buf->b_fname)
9255#ifdef FEAT_QUICKFIX
9256 || bt_nofile(buf)
9257#endif
9258 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009259 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009260 break;
9261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 }
9263 return buf;
9264}
9265
9266/*
9267 * "bufexists(expr)" function
9268 */
9269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009270f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009271 typval_T *argvars;
9272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275}
9276
9277/*
9278 * "buflisted(expr)" function
9279 */
9280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009282 typval_T *argvars;
9283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284{
9285 buf_T *buf;
9286
9287 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009288 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289}
9290
9291/*
9292 * "bufloaded(expr)" function
9293 */
9294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009296 typval_T *argvars;
9297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298{
9299 buf_T *buf;
9300
9301 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303}
9304
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009305static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009306
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307/*
9308 * Get buffer by number or pattern.
9309 */
9310 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009311get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009313 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009315 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 int save_magic;
9317 char_u *save_cpo;
9318 buf_T *buf;
9319
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 if (tv->v_type == VAR_NUMBER)
9321 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009322 if (tv->v_type != VAR_STRING)
9323 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 if (name == NULL || *name == NUL)
9325 return curbuf;
9326 if (name[0] == '$' && name[1] == NUL)
9327 return lastbuf;
9328
9329 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9330 save_magic = p_magic;
9331 p_magic = TRUE;
9332 save_cpo = p_cpo;
9333 p_cpo = (char_u *)"";
9334
9335 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009336 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337
9338 p_magic = save_magic;
9339 p_cpo = save_cpo;
9340
9341 /* If not found, try expanding the name, like done for bufexists(). */
9342 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
9345 return buf;
9346}
9347
9348/*
9349 * "bufname(expr)" function
9350 */
9351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009353 typval_T *argvars;
9354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 buf_T *buf;
9357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009360 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009363 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 --emsg_off;
9367}
9368
9369/*
9370 * "bufnr(expr)" function
9371 */
9372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009374 typval_T *argvars;
9375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
9377 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009378 int error = FALSE;
9379 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009383 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009384 --emsg_off;
9385
9386 /* If the buffer isn't found and the second argument is not zero create a
9387 * new buffer. */
9388 if (buf == NULL
9389 && argvars[1].v_type != VAR_UNKNOWN
9390 && get_tv_number_chk(&argvars[1], &error) != 0
9391 && !error
9392 && (name = get_tv_string_chk(&argvars[0])) != NULL
9393 && !error)
9394 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9395
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400}
9401
9402/*
9403 * "bufwinnr(nr)" function
9404 */
9405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009407 typval_T *argvars;
9408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410#ifdef FEAT_WINDOWS
9411 win_T *wp;
9412 int winnr = 0;
9413#endif
9414 buf_T *buf;
9415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009416 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009418 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#ifdef FEAT_WINDOWS
9420 for (wp = firstwin; wp; wp = wp->w_next)
9421 {
9422 ++winnr;
9423 if (wp->w_buffer == buf)
9424 break;
9425 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429#endif
9430 --emsg_off;
9431}
9432
9433/*
9434 * "byte2line(byte)" function
9435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009438 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
9441#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443#else
9444 long boff = 0;
9445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009446 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 (linenr_T)0, &boff);
9452#endif
9453}
9454
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009455 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009456byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 typval_T *argvars;
9458 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009459 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009460{
9461#ifdef FEAT_MBYTE
9462 char_u *t;
9463#endif
9464 char_u *str;
9465 long idx;
9466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 str = get_tv_string_chk(&argvars[0]);
9468 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009471 return;
9472
9473#ifdef FEAT_MBYTE
9474 t = str;
9475 for ( ; idx > 0; idx--)
9476 {
9477 if (*t == NUL) /* EOL reached */
9478 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009479 if (enc_utf8 && comp)
9480 t += utf_ptr2len(t);
9481 else
9482 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009483 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009484 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009485#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009486 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009488#endif
9489}
9490
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009491/*
9492 * "byteidx()" function
9493 */
9494 static void
9495f_byteidx(argvars, rettv)
9496 typval_T *argvars;
9497 typval_T *rettv;
9498{
9499 byteidx(argvars, rettv, FALSE);
9500}
9501
9502/*
9503 * "byteidxcomp()" function
9504 */
9505 static void
9506f_byteidxcomp(argvars, rettv)
9507 typval_T *argvars;
9508 typval_T *rettv;
9509{
9510 byteidx(argvars, rettv, TRUE);
9511}
9512
Bram Moolenaardb913952012-06-29 12:54:53 +02009513 int
9514func_call(name, args, selfdict, rettv)
9515 char_u *name;
9516 typval_T *args;
9517 dict_T *selfdict;
9518 typval_T *rettv;
9519{
9520 listitem_T *item;
9521 typval_T argv[MAX_FUNC_ARGS + 1];
9522 int argc = 0;
9523 int dummy;
9524 int r = 0;
9525
9526 for (item = args->vval.v_list->lv_first; item != NULL;
9527 item = item->li_next)
9528 {
9529 if (argc == MAX_FUNC_ARGS)
9530 {
9531 EMSG(_("E699: Too many arguments"));
9532 break;
9533 }
9534 /* Make a copy of each argument. This is needed to be able to set
9535 * v_lock to VAR_FIXED in the copy without changing the original list.
9536 */
9537 copy_tv(&item->li_tv, &argv[argc++]);
9538 }
9539
9540 if (item == NULL)
9541 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9542 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9543 &dummy, TRUE, selfdict);
9544
9545 /* Free the arguments. */
9546 while (argc > 0)
9547 clear_tv(&argv[--argc]);
9548
9549 return r;
9550}
9551
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009552/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009553 * "call(func, arglist)" function
9554 */
9555 static void
9556f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 typval_T *argvars;
9558 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009559{
9560 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009561 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009562
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009563 if (argvars[1].v_type != VAR_LIST)
9564 {
9565 EMSG(_(e_listreq));
9566 return;
9567 }
9568 if (argvars[1].vval.v_list == NULL)
9569 return;
9570
9571 if (argvars[0].v_type == VAR_FUNC)
9572 func = argvars[0].vval.v_string;
9573 else
9574 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 if (*func == NUL)
9576 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009577
Bram Moolenaare9a41262005-01-15 22:18:47 +00009578 if (argvars[2].v_type != VAR_UNKNOWN)
9579 {
9580 if (argvars[2].v_type != VAR_DICT)
9581 {
9582 EMSG(_(e_dictreq));
9583 return;
9584 }
9585 selfdict = argvars[2].vval.v_dict;
9586 }
9587
Bram Moolenaardb913952012-06-29 12:54:53 +02009588 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009589}
9590
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009591#ifdef FEAT_FLOAT
9592/*
9593 * "ceil({float})" function
9594 */
9595 static void
9596f_ceil(argvars, rettv)
9597 typval_T *argvars;
9598 typval_T *rettv;
9599{
9600 float_T f;
9601
9602 rettv->v_type = VAR_FLOAT;
9603 if (get_float_arg(argvars, &f) == OK)
9604 rettv->vval.v_float = ceil(f);
9605 else
9606 rettv->vval.v_float = 0.0;
9607}
9608#endif
9609
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009610/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009611 * "changenr()" function
9612 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009613 static void
9614f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009615 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009616 typval_T *rettv;
9617{
9618 rettv->vval.v_number = curbuf->b_u_seq_cur;
9619}
9620
9621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 * "char2nr(string)" function
9623 */
9624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009626 typval_T *argvars;
9627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629#ifdef FEAT_MBYTE
9630 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009631 {
9632 int utf8 = 0;
9633
9634 if (argvars[1].v_type != VAR_UNKNOWN)
9635 utf8 = get_tv_number_chk(&argvars[1], NULL);
9636
9637 if (utf8)
9638 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9639 else
9640 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 else
9643#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009644 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645}
9646
9647/*
9648 * "cindent(lnum)" function
9649 */
9650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009652 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
9655#ifdef FEAT_CINDENT
9656 pos_T pos;
9657 linenr_T lnum;
9658
9659 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9662 {
9663 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 curwin->w_cursor = pos;
9666 }
9667 else
9668#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670}
9671
9672/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009673 * "clearmatches()" function
9674 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009675 static void
9676f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009677 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009678 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009679{
9680#ifdef FEAT_SEARCH_EXTRA
9681 clear_matches(curwin);
9682#endif
9683}
9684
9685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 * "col(string)" function
9687 */
9688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009690 typval_T *argvars;
9691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692{
9693 colnr_T col = 0;
9694 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009695 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009697 fp = var2fpos(&argvars[0], FALSE, &fnum);
9698 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 {
9700 if (fp->col == MAXCOL)
9701 {
9702 /* '> can be MAXCOL, get the length of the line then */
9703 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009704 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 else
9706 col = MAXCOL;
9707 }
9708 else
9709 {
9710 col = fp->col + 1;
9711#ifdef FEAT_VIRTUALEDIT
9712 /* col(".") when the cursor is on the NUL at the end of the line
9713 * because of "coladd" can be seen as an extra column. */
9714 if (virtual_active() && fp == &curwin->w_cursor)
9715 {
9716 char_u *p = ml_get_cursor();
9717
9718 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9719 curwin->w_virtcol - curwin->w_cursor.coladd))
9720 {
9721# ifdef FEAT_MBYTE
9722 int l;
9723
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009724 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 col += l;
9726# else
9727 if (*p != NUL && p[1] == NUL)
9728 ++col;
9729# endif
9730 }
9731 }
9732#endif
9733 }
9734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736}
9737
Bram Moolenaar572cb562005-08-05 21:35:02 +00009738#if defined(FEAT_INS_EXPAND)
9739/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009740 * "complete()" function
9741 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009742 static void
9743f_complete(argvars, rettv)
9744 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009745 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009746{
9747 int startcol;
9748
9749 if ((State & INSERT) == 0)
9750 {
9751 EMSG(_("E785: complete() can only be used in Insert mode"));
9752 return;
9753 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009754
9755 /* Check for undo allowed here, because if something was already inserted
9756 * the line was already saved for undo and this check isn't done. */
9757 if (!undo_allowed())
9758 return;
9759
Bram Moolenaarade00832006-03-10 21:46:58 +00009760 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9761 {
9762 EMSG(_(e_invarg));
9763 return;
9764 }
9765
9766 startcol = get_tv_number_chk(&argvars[0], NULL);
9767 if (startcol <= 0)
9768 return;
9769
9770 set_completion(startcol - 1, argvars[1].vval.v_list);
9771}
9772
9773/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009774 * "complete_add()" function
9775 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009776 static void
9777f_complete_add(argvars, rettv)
9778 typval_T *argvars;
9779 typval_T *rettv;
9780{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009781 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009782}
9783
9784/*
9785 * "complete_check()" function
9786 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009787 static void
9788f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009789 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009790 typval_T *rettv;
9791{
9792 int saved = RedrawingDisabled;
9793
9794 RedrawingDisabled = 0;
9795 ins_compl_check_keys(0);
9796 rettv->vval.v_number = compl_interrupted;
9797 RedrawingDisabled = saved;
9798}
9799#endif
9800
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801/*
9802 * "confirm(message, buttons[, default [, type]])" function
9803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009806 typval_T *argvars UNUSED;
9807 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9810 char_u *message;
9811 char_u *buttons = NULL;
9812 char_u buf[NUMBUFLEN];
9813 char_u buf2[NUMBUFLEN];
9814 int def = 1;
9815 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 char_u *typestr;
9817 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009819 message = get_tv_string_chk(&argvars[0]);
9820 if (message == NULL)
9821 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009822 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9825 if (buttons == NULL)
9826 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009829 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009830 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9833 if (typestr == NULL)
9834 error = TRUE;
9835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009837 switch (TOUPPER_ASC(*typestr))
9838 {
9839 case 'E': type = VIM_ERROR; break;
9840 case 'Q': type = VIM_QUESTION; break;
9841 case 'I': type = VIM_INFO; break;
9842 case 'W': type = VIM_WARNING; break;
9843 case 'G': type = VIM_GENERIC; break;
9844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 }
9846 }
9847 }
9848 }
9849
9850 if (buttons == NULL || *buttons == NUL)
9851 buttons = (char_u *)_("&Ok");
9852
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009853 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009854 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009855 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856#endif
9857}
9858
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009859/*
9860 * "copy()" function
9861 */
9862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009866{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009867 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009868}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009870#ifdef FEAT_FLOAT
9871/*
9872 * "cos()" function
9873 */
9874 static void
9875f_cos(argvars, rettv)
9876 typval_T *argvars;
9877 typval_T *rettv;
9878{
9879 float_T f;
9880
9881 rettv->v_type = VAR_FLOAT;
9882 if (get_float_arg(argvars, &f) == OK)
9883 rettv->vval.v_float = cos(f);
9884 else
9885 rettv->vval.v_float = 0.0;
9886}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009887
9888/*
9889 * "cosh()" function
9890 */
9891 static void
9892f_cosh(argvars, rettv)
9893 typval_T *argvars;
9894 typval_T *rettv;
9895{
9896 float_T f;
9897
9898 rettv->v_type = VAR_FLOAT;
9899 if (get_float_arg(argvars, &f) == OK)
9900 rettv->vval.v_float = cosh(f);
9901 else
9902 rettv->vval.v_float = 0.0;
9903}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009904#endif
9905
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009907 * "count()" function
9908 */
9909 static void
9910f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 typval_T *argvars;
9912 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009914 long n = 0;
9915 int ic = FALSE;
9916
Bram Moolenaare9a41262005-01-15 22:18:47 +00009917 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 listitem_T *li;
9920 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009922
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923 if ((l = argvars[0].vval.v_list) != NULL)
9924 {
9925 li = l->lv_first;
9926 if (argvars[2].v_type != VAR_UNKNOWN)
9927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 int error = FALSE;
9929
9930 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009931 if (argvars[3].v_type != VAR_UNKNOWN)
9932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 idx = get_tv_number_chk(&argvars[3], &error);
9934 if (!error)
9935 {
9936 li = list_find(l, idx);
9937 if (li == NULL)
9938 EMSGN(_(e_listidx), idx);
9939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009940 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009941 if (error)
9942 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009943 }
9944
9945 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009946 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 ++n;
9948 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009949 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 else if (argvars[0].v_type == VAR_DICT)
9951 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009952 int todo;
9953 dict_T *d;
9954 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009955
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009956 if ((d = argvars[0].vval.v_dict) != NULL)
9957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 int error = FALSE;
9959
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 if (argvars[2].v_type != VAR_UNKNOWN)
9961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009962 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 if (argvars[3].v_type != VAR_UNKNOWN)
9964 EMSG(_(e_invarg));
9965 }
9966
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009967 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009968 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009969 {
9970 if (!HASHITEM_EMPTY(hi))
9971 {
9972 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009973 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009974 ++n;
9975 }
9976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 }
9978 }
9979 else
9980 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009981 rettv->vval.v_number = n;
9982}
9983
9984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9986 *
9987 * Checks the existence of a cscope connection.
9988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009991 typval_T *argvars UNUSED;
9992 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994#ifdef FEAT_CSCOPE
9995 int num = 0;
9996 char_u *dbpath = NULL;
9997 char_u *prepend = NULL;
9998 char_u buf[NUMBUFLEN];
9999
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010000 if (argvars[0].v_type != VAR_UNKNOWN
10001 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003 num = (int)get_tv_number(&argvars[0]);
10004 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010005 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 }
10008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010#endif
10011}
10012
10013/*
10014 * "cursor(lnum, col)" function
10015 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010016 * Moves the cursor to the specified line and column.
10017 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010021 typval_T *argvars;
10022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023{
10024 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010025#ifdef FEAT_VIRTUALEDIT
10026 long coladd = 0;
10027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010029 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010030 if (argvars[1].v_type == VAR_UNKNOWN)
10031 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010032 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010033 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010034
Bram Moolenaar493c1782014-05-28 14:34:46 +020010035 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010036 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010037 line = pos.lnum;
10038 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010039#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010040 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010041#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010042 if (curswant >= 0)
10043 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010044 }
10045 else
10046 {
10047 line = get_tv_lnum(argvars);
10048 col = get_tv_number_chk(&argvars[1], NULL);
10049#ifdef FEAT_VIRTUALEDIT
10050 if (argvars[2].v_type != VAR_UNKNOWN)
10051 coladd = get_tv_number_chk(&argvars[2], NULL);
10052#endif
10053 }
10054 if (line < 0 || col < 0
10055#ifdef FEAT_VIRTUALEDIT
10056 || coladd < 0
10057#endif
10058 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010059 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 if (line > 0)
10061 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 if (col > 0)
10063 curwin->w_cursor.col = col - 1;
10064#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010065 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066#endif
10067
10068 /* Make sure the cursor is in a valid position. */
10069 check_cursor();
10070#ifdef FEAT_MBYTE
10071 /* Correct cursor for multi-byte character. */
10072 if (has_mbyte)
10073 mb_adjust_cursor();
10074#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010075
10076 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010077 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
10080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010081 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 */
10083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010085 typval_T *argvars;
10086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010088 int noref = 0;
10089
10090 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010092 if (noref < 0 || noref > 1)
10093 EMSG(_(e_invarg));
10094 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010095 {
10096 current_copyID += COPYID_INC;
10097 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099}
10100
10101/*
10102 * "delete()" function
10103 */
10104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010106 typval_T *argvars;
10107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108{
10109 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113}
10114
10115/*
10116 * "did_filetype()" function
10117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010120 typval_T *argvars UNUSED;
10121 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122{
10123#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125#endif
10126}
10127
10128/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010129 * "diff_filler()" function
10130 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010133 typval_T *argvars UNUSED;
10134 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010135{
10136#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010138#endif
10139}
10140
10141/*
10142 * "diff_hlID()" function
10143 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010146 typval_T *argvars UNUSED;
10147 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010148{
10149#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010151 static linenr_T prev_lnum = 0;
10152 static int changedtick = 0;
10153 static int fnum = 0;
10154 static int change_start = 0;
10155 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010156 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010157 int filler_lines;
10158 int col;
10159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 if (lnum < 0) /* ignore type error in {lnum} arg */
10161 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010162 if (lnum != prev_lnum
10163 || changedtick != curbuf->b_changedtick
10164 || fnum != curbuf->b_fnum)
10165 {
10166 /* New line, buffer, change: need to get the values. */
10167 filler_lines = diff_check(curwin, lnum);
10168 if (filler_lines < 0)
10169 {
10170 if (filler_lines == -1)
10171 {
10172 change_start = MAXCOL;
10173 change_end = -1;
10174 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10175 hlID = HLF_ADD; /* added line */
10176 else
10177 hlID = HLF_CHD; /* changed line */
10178 }
10179 else
10180 hlID = HLF_ADD; /* added line */
10181 }
10182 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010183 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010184 prev_lnum = lnum;
10185 changedtick = curbuf->b_changedtick;
10186 fnum = curbuf->b_fnum;
10187 }
10188
10189 if (hlID == HLF_CHD || hlID == HLF_TXD)
10190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010192 if (col >= change_start && col <= change_end)
10193 hlID = HLF_TXD; /* changed text */
10194 else
10195 hlID = HLF_CHD; /* changed line */
10196 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010197 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010198#endif
10199}
10200
10201/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010202 * "empty({expr})" function
10203 */
10204 static void
10205f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010206 typval_T *argvars;
10207 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010208{
10209 int n;
10210
10211 switch (argvars[0].v_type)
10212 {
10213 case VAR_STRING:
10214 case VAR_FUNC:
10215 n = argvars[0].vval.v_string == NULL
10216 || *argvars[0].vval.v_string == NUL;
10217 break;
10218 case VAR_NUMBER:
10219 n = argvars[0].vval.v_number == 0;
10220 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010221#ifdef FEAT_FLOAT
10222 case VAR_FLOAT:
10223 n = argvars[0].vval.v_float == 0.0;
10224 break;
10225#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010226 case VAR_LIST:
10227 n = argvars[0].vval.v_list == NULL
10228 || argvars[0].vval.v_list->lv_first == NULL;
10229 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 case VAR_DICT:
10231 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010232 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010234 default:
10235 EMSG2(_(e_intern2), "f_empty()");
10236 n = 0;
10237 }
10238
10239 rettv->vval.v_number = n;
10240}
10241
10242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 * "escape({string}, {chars})" function
10244 */
10245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010246f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 typval_T *argvars;
10248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
10250 char_u buf[NUMBUFLEN];
10251
Bram Moolenaar758711c2005-02-02 23:11:38 +000010252 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10253 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010254 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255}
10256
10257/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010258 * "eval()" function
10259 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010260 static void
10261f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 typval_T *argvars;
10263 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010264{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010265 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 s = get_tv_string_chk(&argvars[0]);
10268 if (s != NULL)
10269 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010270
Bram Moolenaar615b9972015-01-14 17:15:05 +010010271 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10273 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010274 if (p != NULL && !aborting())
10275 EMSG2(_(e_invexpr2), p);
10276 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010278 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010280 else if (*s != NUL)
10281 EMSG(_(e_trailing));
10282}
10283
10284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 * "eventhandler()" function
10286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010289 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293}
10294
10295/*
10296 * "executable()" function
10297 */
10298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010300 typval_T *argvars;
10301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010303 char_u *name = get_tv_string(&argvars[0]);
10304
10305 /* Check in $PATH and also check directly if there is a directory name. */
10306 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10307 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010308}
10309
10310/*
10311 * "exepath()" function
10312 */
10313 static void
10314f_exepath(argvars, rettv)
10315 typval_T *argvars;
10316 typval_T *rettv;
10317{
10318 char_u *p = NULL;
10319
Bram Moolenaarb5971142015-03-21 17:32:19 +010010320 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010321 rettv->v_type = VAR_STRING;
10322 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323}
10324
10325/*
10326 * "exists()" function
10327 */
10328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010330 typval_T *argvars;
10331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332{
10333 char_u *p;
10334 char_u *name;
10335 int n = FALSE;
10336 int len = 0;
10337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (*p == '$') /* environment variable */
10340 {
10341 /* first try "normal" environment variables (fast) */
10342 if (mch_getenv(p + 1) != NULL)
10343 n = TRUE;
10344 else
10345 {
10346 /* try expanding things like $VIM and ${HOME} */
10347 p = expand_env_save(p);
10348 if (p != NULL && *p != '$')
10349 n = TRUE;
10350 vim_free(p);
10351 }
10352 }
10353 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010356 if (*skipwhite(p) != NUL)
10357 n = FALSE; /* trailing garbage */
10358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 else if (*p == '*') /* internal or user defined function */
10360 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010361 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 }
10363 else if (*p == ':')
10364 {
10365 n = cmd_exists(p + 1);
10366 }
10367 else if (*p == '#')
10368 {
10369#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010370 if (p[1] == '#')
10371 n = autocmd_supported(p + 2);
10372 else
10373 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374#endif
10375 }
10376 else /* internal variable */
10377 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010378 char_u *tofree;
10379 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010381 /* get_name_len() takes care of expanding curly braces */
10382 name = p;
10383 len = get_name_len(&p, &tofree, TRUE, FALSE);
10384 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010386 if (tofree != NULL)
10387 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010388 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010389 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010391 /* handle d.key, l[idx], f(expr) */
10392 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10393 if (n)
10394 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 }
10396 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010397 if (*p != NUL)
10398 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010400 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 }
10402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404}
10405
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010406#ifdef FEAT_FLOAT
10407/*
10408 * "exp()" function
10409 */
10410 static void
10411f_exp(argvars, rettv)
10412 typval_T *argvars;
10413 typval_T *rettv;
10414{
10415 float_T f;
10416
10417 rettv->v_type = VAR_FLOAT;
10418 if (get_float_arg(argvars, &f) == OK)
10419 rettv->vval.v_float = exp(f);
10420 else
10421 rettv->vval.v_float = 0.0;
10422}
10423#endif
10424
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425/*
10426 * "expand()" function
10427 */
10428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010430 typval_T *argvars;
10431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 char_u *s;
10434 int len;
10435 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010436 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010439 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010442 if (argvars[1].v_type != VAR_UNKNOWN
10443 && argvars[2].v_type != VAR_UNKNOWN
10444 && get_tv_number_chk(&argvars[2], &error)
10445 && !error)
10446 {
10447 rettv->v_type = VAR_LIST;
10448 rettv->vval.v_list = NULL;
10449 }
10450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 if (*s == '%' || *s == '#' || *s == '<')
10453 {
10454 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010455 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010457 if (rettv->v_type == VAR_LIST)
10458 {
10459 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10460 list_append_string(rettv->vval.v_list, result, -1);
10461 else
10462 vim_free(result);
10463 }
10464 else
10465 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 }
10467 else
10468 {
10469 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010470 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 if (argvars[1].v_type != VAR_UNKNOWN
10472 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010473 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 if (!error)
10475 {
10476 ExpandInit(&xpc);
10477 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010478 if (p_wic)
10479 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010480 if (rettv->v_type == VAR_STRING)
10481 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10482 options, WILD_ALL);
10483 else if (rettv_list_alloc(rettv) != FAIL)
10484 {
10485 int i;
10486
10487 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10488 for (i = 0; i < xpc.xp_numfiles; i++)
10489 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10490 ExpandCleanup(&xpc);
10491 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010492 }
10493 else
10494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 }
10496}
10497
10498/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010499 * Go over all entries in "d2" and add them to "d1".
10500 * When "action" is "error" then a duplicate key is an error.
10501 * When "action" is "force" then a duplicate key is overwritten.
10502 * Otherwise duplicate keys are ignored ("action" is "keep").
10503 */
10504 void
10505dict_extend(d1, d2, action)
10506 dict_T *d1;
10507 dict_T *d2;
10508 char_u *action;
10509{
10510 dictitem_T *di1;
10511 hashitem_T *hi2;
10512 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010513 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010514
10515 todo = (int)d2->dv_hashtab.ht_used;
10516 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10517 {
10518 if (!HASHITEM_EMPTY(hi2))
10519 {
10520 --todo;
10521 di1 = dict_find(d1, hi2->hi_key, -1);
10522 if (d1->dv_scope != 0)
10523 {
10524 /* Disallow replacing a builtin function in l: and g:.
10525 * Check the key to be valid when adding to any
10526 * scope. */
10527 if (d1->dv_scope == VAR_DEF_SCOPE
10528 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10529 && var_check_func_name(hi2->hi_key,
10530 di1 == NULL))
10531 break;
10532 if (!valid_varname(hi2->hi_key))
10533 break;
10534 }
10535 if (di1 == NULL)
10536 {
10537 di1 = dictitem_copy(HI2DI(hi2));
10538 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10539 dictitem_free(di1);
10540 }
10541 else if (*action == 'e')
10542 {
10543 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10544 break;
10545 }
10546 else if (*action == 'f' && HI2DI(hi2) != di1)
10547 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010548 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10549 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010550 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010551 clear_tv(&di1->di_tv);
10552 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10553 }
10554 }
10555 }
10556}
10557
10558/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010559 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010560 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010561 */
10562 static void
10563f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010564 typval_T *argvars;
10565 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010566{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010567 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010568
Bram Moolenaare9a41262005-01-15 22:18:47 +000010569 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010570 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010571 list_T *l1, *l2;
10572 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010575
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 l1 = argvars[0].vval.v_list;
10577 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010578 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010579 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 {
10581 if (argvars[2].v_type != VAR_UNKNOWN)
10582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 before = get_tv_number_chk(&argvars[2], &error);
10584 if (error)
10585 return; /* type error; errmsg already given */
10586
Bram Moolenaar758711c2005-02-02 23:11:38 +000010587 if (before == l1->lv_len)
10588 item = NULL;
10589 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010591 item = list_find(l1, before);
10592 if (item == NULL)
10593 {
10594 EMSGN(_(e_listidx), before);
10595 return;
10596 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 }
10598 }
10599 else
10600 item = NULL;
10601 list_extend(l1, l2, item);
10602
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 copy_tv(&argvars[0], rettv);
10604 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010605 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10607 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010608 dict_T *d1, *d2;
10609 char_u *action;
10610 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611
10612 d1 = argvars[0].vval.v_dict;
10613 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010614 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010615 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 {
10617 /* Check the third argument. */
10618 if (argvars[2].v_type != VAR_UNKNOWN)
10619 {
10620 static char *(av[]) = {"keep", "force", "error"};
10621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010622 action = get_tv_string_chk(&argvars[2]);
10623 if (action == NULL)
10624 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010625 for (i = 0; i < 3; ++i)
10626 if (STRCMP(action, av[i]) == 0)
10627 break;
10628 if (i == 3)
10629 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010630 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 return;
10632 }
10633 }
10634 else
10635 action = (char_u *)"force";
10636
Bram Moolenaara9922d62013-05-30 13:01:18 +020010637 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638
Bram Moolenaare9a41262005-01-15 22:18:47 +000010639 copy_tv(&argvars[0], rettv);
10640 }
10641 }
10642 else
10643 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010644}
10645
10646/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010647 * "feedkeys()" function
10648 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010649 static void
10650f_feedkeys(argvars, rettv)
10651 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010652 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010653{
10654 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010655 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010656 char_u *keys, *flags;
10657 char_u nbuf[NUMBUFLEN];
10658 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010659 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010660
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010661 /* This is not allowed in the sandbox. If the commands would still be
10662 * executed in the sandbox it would be OK, but it probably happens later,
10663 * when "sandbox" is no longer set. */
10664 if (check_secure())
10665 return;
10666
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010667 keys = get_tv_string(&argvars[0]);
10668 if (*keys != NUL)
10669 {
10670 if (argvars[1].v_type != VAR_UNKNOWN)
10671 {
10672 flags = get_tv_string_buf(&argvars[1], nbuf);
10673 for ( ; *flags != NUL; ++flags)
10674 {
10675 switch (*flags)
10676 {
10677 case 'n': remap = FALSE; break;
10678 case 'm': remap = TRUE; break;
10679 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010680 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010681 }
10682 }
10683 }
10684
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010685 /* Need to escape K_SPECIAL and CSI before putting the string in the
10686 * typeahead buffer. */
10687 keys_esc = vim_strsave_escape_csi(keys);
10688 if (keys_esc != NULL)
10689 {
10690 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010691 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010692 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010693 if (vgetc_busy)
10694 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010695 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010696 }
10697}
10698
10699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 * "filereadable()" function
10701 */
10702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010704 typval_T *argvars;
10705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010707 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 char_u *p;
10709 int n;
10710
Bram Moolenaarc236c162008-07-13 17:41:49 +000010711#ifndef O_NONBLOCK
10712# define O_NONBLOCK 0
10713#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010715 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10716 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 {
10718 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010719 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 }
10721 else
10722 n = FALSE;
10723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010724 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725}
10726
10727/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010728 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 * rights to write into.
10730 */
10731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010732f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010733 typval_T *argvars;
10734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010736 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737}
10738
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010739static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010740
10741 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010742findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010743 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010744 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010745 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010746{
10747#ifdef FEAT_SEARCHPATH
10748 char_u *fname;
10749 char_u *fresult = NULL;
10750 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10751 char_u *p;
10752 char_u pathbuf[NUMBUFLEN];
10753 int count = 1;
10754 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010755 int error = FALSE;
10756#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010757
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010758 rettv->vval.v_string = NULL;
10759 rettv->v_type = VAR_STRING;
10760
10761#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010762 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010763
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010764 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10767 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010768 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 else
10770 {
10771 if (*p != NUL)
10772 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010775 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010777 }
10778
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010779 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10780 error = TRUE;
10781
10782 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010784 do
10785 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010786 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010787 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 fresult = find_file_in_path_option(first ? fname : NULL,
10789 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010790 0, first, path,
10791 find_what,
10792 curbuf->b_ffname,
10793 find_what == FINDFILE_DIR
10794 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010796
10797 if (fresult != NULL && rettv->v_type == VAR_LIST)
10798 list_append_string(rettv->vval.v_list, fresult, -1);
10799
10800 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010801 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010802
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010803 if (rettv->v_type == VAR_STRING)
10804 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010805#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010806}
10807
Bram Moolenaar33570922005-01-25 22:26:29 +000010808static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10809static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010810
10811/*
10812 * Implementation of map() and filter().
10813 */
10814 static void
10815filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010816 typval_T *argvars;
10817 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010818 int map;
10819{
10820 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010821 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010822 listitem_T *li, *nli;
10823 list_T *l = NULL;
10824 dictitem_T *di;
10825 hashtab_T *ht;
10826 hashitem_T *hi;
10827 dict_T *d = NULL;
10828 typval_T save_val;
10829 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010830 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010831 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010832 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010833 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010834 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010835 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010836 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010837
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010839 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010840 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010841 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 return;
10843 }
10844 else if (argvars[0].v_type == VAR_DICT)
10845 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010846 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010847 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 return;
10849 }
10850 else
10851 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010852 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010853 return;
10854 }
10855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 expr = get_tv_string_buf_chk(&argvars[1], buf);
10857 /* On type errors, the preceding call has already displayed an error
10858 * message. Avoid a misleading error message for an empty string that
10859 * was not passed as argument. */
10860 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 prepare_vimvar(VV_VAL, &save_val);
10863 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010864
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010865 /* We reset "did_emsg" to be able to detect whether an error
10866 * occurred during evaluation of the expression. */
10867 save_did_emsg = did_emsg;
10868 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010869
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010870 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 vimvars[VV_KEY].vv_type = VAR_STRING;
10874
10875 ht = &d->dv_hashtab;
10876 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010877 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010880 if (!HASHITEM_EMPTY(hi))
10881 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010882 int r;
10883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 --todo;
10885 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010886 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010887 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10888 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010889 break;
10890 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010891 r = filter_map_one(&di->di_tv, expr, map, &rem);
10892 clear_tv(&vimvars[VV_KEY].vv_tv);
10893 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010894 break;
10895 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010896 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010897 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10898 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010899 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 }
10903 }
10904 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905 }
10906 else
10907 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010908 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 for (li = l->lv_first; li != NULL; li = nli)
10911 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010912 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010913 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010915 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010916 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010917 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010918 break;
10919 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010921 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010922 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010923 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010924
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010925 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010927
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010928 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010929 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010930
10931 copy_tv(&argvars[0], rettv);
10932}
10933
10934 static int
10935filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 char_u *expr;
10938 int map;
10939 int *remp;
10940{
Bram Moolenaar33570922005-01-25 22:26:29 +000010941 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010942 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010943 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944
Bram Moolenaar33570922005-01-25 22:26:29 +000010945 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 s = expr;
10947 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010948 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 if (*s != NUL) /* check for trailing chars after expr */
10950 {
10951 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010952 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010953 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010954 }
10955 if (map)
10956 {
10957 /* map(): replace the list item value */
10958 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010959 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 *tv = rettv;
10961 }
10962 else
10963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010964 int error = FALSE;
10965
Bram Moolenaare9a41262005-01-15 22:18:47 +000010966 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969 /* On type error, nothing has been removed; return FAIL to stop the
10970 * loop. The error message was given by get_tv_number_chk(). */
10971 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010972 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010973 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010974 retval = OK;
10975theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010977 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010978}
10979
10980/*
10981 * "filter()" function
10982 */
10983 static void
10984f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 typval_T *argvars;
10986 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010987{
10988 filter_map(argvars, rettv, FALSE);
10989}
10990
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010991/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010992 * "finddir({fname}[, {path}[, {count}]])" function
10993 */
10994 static void
10995f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010996 typval_T *argvars;
10997 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010998{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010999 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011000}
11001
11002/*
11003 * "findfile({fname}[, {path}[, {count}]])" function
11004 */
11005 static void
11006f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011007 typval_T *argvars;
11008 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011009{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011010 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011011}
11012
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011013#ifdef FEAT_FLOAT
11014/*
11015 * "float2nr({float})" function
11016 */
11017 static void
11018f_float2nr(argvars, rettv)
11019 typval_T *argvars;
11020 typval_T *rettv;
11021{
11022 float_T f;
11023
11024 if (get_float_arg(argvars, &f) == OK)
11025 {
11026 if (f < -0x7fffffff)
11027 rettv->vval.v_number = -0x7fffffff;
11028 else if (f > 0x7fffffff)
11029 rettv->vval.v_number = 0x7fffffff;
11030 else
11031 rettv->vval.v_number = (varnumber_T)f;
11032 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011033}
11034
11035/*
11036 * "floor({float})" function
11037 */
11038 static void
11039f_floor(argvars, rettv)
11040 typval_T *argvars;
11041 typval_T *rettv;
11042{
11043 float_T f;
11044
11045 rettv->v_type = VAR_FLOAT;
11046 if (get_float_arg(argvars, &f) == OK)
11047 rettv->vval.v_float = floor(f);
11048 else
11049 rettv->vval.v_float = 0.0;
11050}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011051
11052/*
11053 * "fmod()" function
11054 */
11055 static void
11056f_fmod(argvars, rettv)
11057 typval_T *argvars;
11058 typval_T *rettv;
11059{
11060 float_T fx, fy;
11061
11062 rettv->v_type = VAR_FLOAT;
11063 if (get_float_arg(argvars, &fx) == OK
11064 && get_float_arg(&argvars[1], &fy) == OK)
11065 rettv->vval.v_float = fmod(fx, fy);
11066 else
11067 rettv->vval.v_float = 0.0;
11068}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011069#endif
11070
Bram Moolenaar0d660222005-01-07 21:51:51 +000011071/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011072 * "fnameescape({string})" function
11073 */
11074 static void
11075f_fnameescape(argvars, rettv)
11076 typval_T *argvars;
11077 typval_T *rettv;
11078{
11079 rettv->vval.v_string = vim_strsave_fnameescape(
11080 get_tv_string(&argvars[0]), FALSE);
11081 rettv->v_type = VAR_STRING;
11082}
11083
11084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 * "fnamemodify({fname}, {mods})" function
11086 */
11087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011089 typval_T *argvars;
11090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091{
11092 char_u *fname;
11093 char_u *mods;
11094 int usedlen = 0;
11095 int len;
11096 char_u *fbuf = NULL;
11097 char_u buf[NUMBUFLEN];
11098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099 fname = get_tv_string_chk(&argvars[0]);
11100 mods = get_tv_string_buf_chk(&argvars[1], buf);
11101 if (fname == NULL || mods == NULL)
11102 fname = NULL;
11103 else
11104 {
11105 len = (int)STRLEN(fname);
11106 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 vim_free(fbuf);
11115}
11116
Bram Moolenaar33570922005-01-25 22:26:29 +000011117static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
11119/*
11120 * "foldclosed()" function
11121 */
11122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011124 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011125 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011126 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127{
11128#ifdef FEAT_FOLDING
11129 linenr_T lnum;
11130 linenr_T first, last;
11131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11134 {
11135 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11136 {
11137 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 return;
11142 }
11143 }
11144#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146}
11147
11148/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149 * "foldclosed()" function
11150 */
11151 static void
11152f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011153 typval_T *argvars;
11154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155{
11156 foldclosed_both(argvars, rettv, FALSE);
11157}
11158
11159/*
11160 * "foldclosedend()" function
11161 */
11162 static void
11163f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011164 typval_T *argvars;
11165 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011166{
11167 foldclosed_both(argvars, rettv, TRUE);
11168}
11169
11170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 * "foldlevel()" function
11172 */
11173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011175 typval_T *argvars UNUSED;
11176 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177{
11178#ifdef FEAT_FOLDING
11179 linenr_T lnum;
11180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185}
11186
11187/*
11188 * "foldtext()" function
11189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011192 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194{
11195#ifdef FEAT_FOLDING
11196 linenr_T lnum;
11197 char_u *s;
11198 char_u *r;
11199 int len;
11200 char *txt;
11201#endif
11202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->v_type = VAR_STRING;
11204 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011206 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11207 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11208 <= curbuf->b_ml.ml_line_count
11209 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 {
11211 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011212 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11213 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 {
11215 if (!linewhite(lnum))
11216 break;
11217 ++lnum;
11218 }
11219
11220 /* Find interesting text in this line. */
11221 s = skipwhite(ml_get(lnum));
11222 /* skip C comment-start */
11223 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011226 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011228 {
11229 s = skipwhite(ml_get(lnum + 1));
11230 if (*s == '*')
11231 s = skipwhite(s + 1);
11232 }
11233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 txt = _("+-%s%3ld lines: ");
11235 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 + 20 /* for %3ld */
11238 + STRLEN(s))); /* concatenated */
11239 if (r != NULL)
11240 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011241 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11242 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11243 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 len = (int)STRLEN(r);
11245 STRCAT(r, s);
11246 /* remove 'foldmarker' and 'commentstring' */
11247 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 }
11250 }
11251#endif
11252}
11253
11254/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011255 * "foldtextresult(lnum)" function
11256 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011259 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011261{
11262#ifdef FEAT_FOLDING
11263 linenr_T lnum;
11264 char_u *text;
11265 char_u buf[51];
11266 foldinfo_T foldinfo;
11267 int fold_count;
11268#endif
11269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->v_type = VAR_STRING;
11271 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011272#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 /* treat illegal types and illegal string values for {lnum} the same */
11275 if (lnum < 0)
11276 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011277 fold_count = foldedCount(curwin, lnum, &foldinfo);
11278 if (fold_count > 0)
11279 {
11280 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11281 &foldinfo, buf);
11282 if (text == buf)
11283 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011285 }
11286#endif
11287}
11288
11289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 * "foreground()" function
11291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011294 typval_T *argvars UNUSED;
11295 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297#ifdef FEAT_GUI
11298 if (gui.in_use)
11299 gui_mch_set_foreground();
11300#else
11301# ifdef WIN32
11302 win32_set_foreground();
11303# endif
11304#endif
11305}
11306
11307/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011308 * "function()" function
11309 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011312 typval_T *argvars;
11313 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011314{
11315 char_u *s;
11316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011318 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011319 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011320 /* Don't check an autoload name for existence here. */
11321 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011322 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011323 else
11324 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011325 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011326 {
11327 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011328 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011329
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011330 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11331 * also be called from another script. Using trans_function_name()
11332 * would also work, but some plugins depend on the name being
11333 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011334 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011335 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011336 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011337 if (rettv->vval.v_string != NULL)
11338 {
11339 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011340 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011341 }
11342 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011343 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011344 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011346 }
11347}
11348
11349/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011350 * "garbagecollect()" function
11351 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011352 static void
11353f_garbagecollect(argvars, rettv)
11354 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011355 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011356{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011357 /* This is postponed until we are back at the toplevel, because we may be
11358 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11359 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011360
11361 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11362 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011363}
11364
11365/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011366 * "get()" function
11367 */
11368 static void
11369f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *argvars;
11371 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011372{
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 listitem_T *li;
11374 list_T *l;
11375 dictitem_T *di;
11376 dict_T *d;
11377 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011378
Bram Moolenaare9a41262005-01-15 22:18:47 +000011379 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011380 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011381 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 int error = FALSE;
11384
11385 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11386 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011387 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011390 else if (argvars[0].v_type == VAR_DICT)
11391 {
11392 if ((d = argvars[0].vval.v_dict) != NULL)
11393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011394 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011395 if (di != NULL)
11396 tv = &di->di_tv;
11397 }
11398 }
11399 else
11400 EMSG2(_(e_listdictarg), "get()");
11401
11402 if (tv == NULL)
11403 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011404 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011405 copy_tv(&argvars[2], rettv);
11406 }
11407 else
11408 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011409}
11410
Bram Moolenaar342337a2005-07-21 21:11:17 +000011411static 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 +000011412
11413/*
11414 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011415 * Return a range (from start to end) of lines in rettv from the specified
11416 * buffer.
11417 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011418 */
11419 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011420get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011421 buf_T *buf;
11422 linenr_T start;
11423 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011424 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011425 typval_T *rettv;
11426{
11427 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011428
Bram Moolenaar959a1432013-12-14 12:17:38 +010011429 rettv->v_type = VAR_STRING;
11430 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011431 if (retlist && rettv_list_alloc(rettv) == FAIL)
11432 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011433
11434 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11435 return;
11436
11437 if (!retlist)
11438 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011439 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11440 p = ml_get_buf(buf, start, FALSE);
11441 else
11442 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011443 rettv->vval.v_string = vim_strsave(p);
11444 }
11445 else
11446 {
11447 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011448 return;
11449
11450 if (start < 1)
11451 start = 1;
11452 if (end > buf->b_ml.ml_line_count)
11453 end = buf->b_ml.ml_line_count;
11454 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011455 if (list_append_string(rettv->vval.v_list,
11456 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011457 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011458 }
11459}
11460
11461/*
11462 * "getbufline()" function
11463 */
11464 static void
11465f_getbufline(argvars, rettv)
11466 typval_T *argvars;
11467 typval_T *rettv;
11468{
11469 linenr_T lnum;
11470 linenr_T end;
11471 buf_T *buf;
11472
11473 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11474 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011475 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011476 --emsg_off;
11477
Bram Moolenaar661b1822005-07-28 22:36:45 +000011478 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011479 if (argvars[2].v_type == VAR_UNKNOWN)
11480 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011481 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011482 end = get_tv_lnum_buf(&argvars[2], buf);
11483
Bram Moolenaar342337a2005-07-21 21:11:17 +000011484 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011485}
11486
Bram Moolenaar0d660222005-01-07 21:51:51 +000011487/*
11488 * "getbufvar()" function
11489 */
11490 static void
11491f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *argvars;
11493 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011494{
11495 buf_T *buf;
11496 buf_T *save_curbuf;
11497 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011498 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011499 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11502 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011503 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011504 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011506 rettv->v_type = VAR_STRING;
11507 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011508
11509 if (buf != NULL && varname != NULL)
11510 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011511 /* set curbuf to be our buf, temporarily */
11512 save_curbuf = curbuf;
11513 curbuf = buf;
11514
Bram Moolenaar0d660222005-01-07 21:51:51 +000011515 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011516 {
11517 if (get_option_tv(&varname, rettv, TRUE) == OK)
11518 done = TRUE;
11519 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011520 else if (STRCMP(varname, "changedtick") == 0)
11521 {
11522 rettv->v_type = VAR_NUMBER;
11523 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011524 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011525 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011526 else
11527 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011528 /* Look up the variable. */
11529 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11530 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11531 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011533 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011534 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011535 done = TRUE;
11536 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011537 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011538
11539 /* restore previous notion of curbuf */
11540 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011541 }
11542
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011543 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11544 /* use the default value */
11545 copy_tv(&argvars[2], rettv);
11546
Bram Moolenaar0d660222005-01-07 21:51:51 +000011547 --emsg_off;
11548}
11549
11550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 * "getchar()" function
11552 */
11553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011555 typval_T *argvars;
11556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557{
11558 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011561 /* Position the cursor. Needed after a message that ends in a space. */
11562 windgoto(msg_row, msg_col);
11563
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 ++no_mapping;
11565 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011566 for (;;)
11567 {
11568 if (argvars[0].v_type == VAR_UNKNOWN)
11569 /* getchar(): blocking wait. */
11570 n = safe_vgetc();
11571 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11572 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011573 n = vpeekc_any();
11574 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011575 /* illegal argument or getchar(0) and no char avail: return zero */
11576 n = 0;
11577 else
11578 /* getchar(0) and char avail: return char */
11579 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011580
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011581 if (n == K_IGNORE)
11582 continue;
11583 break;
11584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 --no_mapping;
11586 --allow_keys;
11587
Bram Moolenaar219b8702006-11-01 14:32:36 +000011588 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11589 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11590 vimvars[VV_MOUSE_COL].vv_nr = 0;
11591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 if (IS_SPECIAL(n) || mod_mask != 0)
11594 {
11595 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11596 int i = 0;
11597
11598 /* Turn a special key into three bytes, plus modifier. */
11599 if (mod_mask != 0)
11600 {
11601 temp[i++] = K_SPECIAL;
11602 temp[i++] = KS_MODIFIER;
11603 temp[i++] = mod_mask;
11604 }
11605 if (IS_SPECIAL(n))
11606 {
11607 temp[i++] = K_SPECIAL;
11608 temp[i++] = K_SECOND(n);
11609 temp[i++] = K_THIRD(n);
11610 }
11611#ifdef FEAT_MBYTE
11612 else if (has_mbyte)
11613 i += (*mb_char2bytes)(n, temp + i);
11614#endif
11615 else
11616 temp[i++] = n;
11617 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011618 rettv->v_type = VAR_STRING;
11619 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011620
11621#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011622 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011623 {
11624 int row = mouse_row;
11625 int col = mouse_col;
11626 win_T *win;
11627 linenr_T lnum;
11628# ifdef FEAT_WINDOWS
11629 win_T *wp;
11630# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011631 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011632
11633 if (row >= 0 && col >= 0)
11634 {
11635 /* Find the window at the mouse coordinates and compute the
11636 * text position. */
11637 win = mouse_find_win(&row, &col);
11638 (void)mouse_comp_pos(win, &row, &col, &lnum);
11639# ifdef FEAT_WINDOWS
11640 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011641 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011642# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011643 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011644 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11645 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11646 }
11647 }
11648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 }
11650}
11651
11652/*
11653 * "getcharmod()" function
11654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011656f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011657 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011660 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661}
11662
11663/*
11664 * "getcmdline()" function
11665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011668 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->v_type = VAR_STRING;
11672 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673}
11674
11675/*
11676 * "getcmdpos()" function
11677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011680 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684}
11685
11686/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011687 * "getcmdtype()" function
11688 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011689 static void
11690f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011691 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011692 typval_T *rettv;
11693{
11694 rettv->v_type = VAR_STRING;
11695 rettv->vval.v_string = alloc(2);
11696 if (rettv->vval.v_string != NULL)
11697 {
11698 rettv->vval.v_string[0] = get_cmdline_type();
11699 rettv->vval.v_string[1] = NUL;
11700 }
11701}
11702
11703/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011704 * "getcmdwintype()" function
11705 */
11706 static void
11707f_getcmdwintype(argvars, rettv)
11708 typval_T *argvars UNUSED;
11709 typval_T *rettv;
11710{
11711 rettv->v_type = VAR_STRING;
11712 rettv->vval.v_string = NULL;
11713#ifdef FEAT_CMDWIN
11714 rettv->vval.v_string = alloc(2);
11715 if (rettv->vval.v_string != NULL)
11716 {
11717 rettv->vval.v_string[0] = cmdwin_type;
11718 rettv->vval.v_string[1] = NUL;
11719 }
11720#endif
11721}
11722
11723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 * "getcwd()" function
11725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011731 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011734 rettv->vval.v_string = NULL;
11735 cwd = alloc(MAXPATHL);
11736 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011738 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11739 {
11740 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011742 if (rettv->vval.v_string != NULL)
11743 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011745 }
11746 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 }
11748}
11749
11750/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011751 * "getfontname()" function
11752 */
11753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011756 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758 rettv->v_type = VAR_STRING;
11759 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011760#ifdef FEAT_GUI
11761 if (gui.in_use)
11762 {
11763 GuiFont font;
11764 char_u *name = NULL;
11765
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011766 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011767 {
11768 /* Get the "Normal" font. Either the name saved by
11769 * hl_set_font_name() or from the font ID. */
11770 font = gui.norm_font;
11771 name = hl_get_font_name();
11772 }
11773 else
11774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011775 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011776 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11777 return;
11778 font = gui_mch_get_font(name, FALSE);
11779 if (font == NOFONT)
11780 return; /* Invalid font name, return empty string. */
11781 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011783 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011784 gui_mch_free_font(font);
11785 }
11786#endif
11787}
11788
11789/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011790 * "getfperm({fname})" function
11791 */
11792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011794 typval_T *argvars;
11795 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011796{
11797 char_u *fname;
11798 struct stat st;
11799 char_u *perm = NULL;
11800 char_u flags[] = "rwx";
11801 int i;
11802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011806 if (mch_stat((char *)fname, &st) >= 0)
11807 {
11808 perm = vim_strsave((char_u *)"---------");
11809 if (perm != NULL)
11810 {
11811 for (i = 0; i < 9; i++)
11812 {
11813 if (st.st_mode & (1 << (8 - i)))
11814 perm[i] = flags[i % 3];
11815 }
11816 }
11817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011819}
11820
11821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 * "getfsize({fname})" function
11823 */
11824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011826 typval_T *argvars;
11827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828{
11829 char_u *fname;
11830 struct stat st;
11831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835
11836 if (mch_stat((char *)fname, &st) >= 0)
11837 {
11838 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011843
11844 /* non-perfect check for overflow */
11845 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11846 rettv->vval.v_number = -2;
11847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 }
11849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851}
11852
11853/*
11854 * "getftime({fname})" function
11855 */
11856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 typval_T *argvars;
11859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860{
11861 char_u *fname;
11862 struct stat st;
11863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865
11866 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011867 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870}
11871
11872/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011873 * "getftype({fname})" function
11874 */
11875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011877 typval_T *argvars;
11878 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011879{
11880 char_u *fname;
11881 struct stat st;
11882 char_u *type = NULL;
11883 char *t;
11884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011888 if (mch_lstat((char *)fname, &st) >= 0)
11889 {
11890#ifdef S_ISREG
11891 if (S_ISREG(st.st_mode))
11892 t = "file";
11893 else if (S_ISDIR(st.st_mode))
11894 t = "dir";
11895# ifdef S_ISLNK
11896 else if (S_ISLNK(st.st_mode))
11897 t = "link";
11898# endif
11899# ifdef S_ISBLK
11900 else if (S_ISBLK(st.st_mode))
11901 t = "bdev";
11902# endif
11903# ifdef S_ISCHR
11904 else if (S_ISCHR(st.st_mode))
11905 t = "cdev";
11906# endif
11907# ifdef S_ISFIFO
11908 else if (S_ISFIFO(st.st_mode))
11909 t = "fifo";
11910# endif
11911# ifdef S_ISSOCK
11912 else if (S_ISSOCK(st.st_mode))
11913 t = "fifo";
11914# endif
11915 else
11916 t = "other";
11917#else
11918# ifdef S_IFMT
11919 switch (st.st_mode & S_IFMT)
11920 {
11921 case S_IFREG: t = "file"; break;
11922 case S_IFDIR: t = "dir"; break;
11923# ifdef S_IFLNK
11924 case S_IFLNK: t = "link"; break;
11925# endif
11926# ifdef S_IFBLK
11927 case S_IFBLK: t = "bdev"; break;
11928# endif
11929# ifdef S_IFCHR
11930 case S_IFCHR: t = "cdev"; break;
11931# endif
11932# ifdef S_IFIFO
11933 case S_IFIFO: t = "fifo"; break;
11934# endif
11935# ifdef S_IFSOCK
11936 case S_IFSOCK: t = "socket"; break;
11937# endif
11938 default: t = "other";
11939 }
11940# else
11941 if (mch_isdir(fname))
11942 t = "dir";
11943 else
11944 t = "file";
11945# endif
11946#endif
11947 type = vim_strsave((char_u *)t);
11948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011950}
11951
11952/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011953 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011954 */
11955 static void
11956f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011957 typval_T *argvars;
11958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011959{
11960 linenr_T lnum;
11961 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011962 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011963
11964 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011965 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011966 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011967 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011968 retlist = FALSE;
11969 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011970 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011971 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011972 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011973 retlist = TRUE;
11974 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011975
Bram Moolenaar342337a2005-07-21 21:11:17 +000011976 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011977}
11978
11979/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011980 * "getmatches()" function
11981 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011982 static void
11983f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011984 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011985 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011986{
11987#ifdef FEAT_SEARCH_EXTRA
11988 dict_T *dict;
11989 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011990 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011991
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011992 if (rettv_list_alloc(rettv) == OK)
11993 {
11994 while (cur != NULL)
11995 {
11996 dict = dict_alloc();
11997 if (dict == NULL)
11998 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011999 if (cur->match.regprog == NULL)
12000 {
12001 /* match added with matchaddpos() */
12002 for (i = 0; i < MAXPOSMATCH; ++i)
12003 {
12004 llpos_T *llpos;
12005 char buf[6];
12006 list_T *l;
12007
12008 llpos = &cur->pos.pos[i];
12009 if (llpos->lnum == 0)
12010 break;
12011 l = list_alloc();
12012 if (l == NULL)
12013 break;
12014 list_append_number(l, (varnumber_T)llpos->lnum);
12015 if (llpos->col > 0)
12016 {
12017 list_append_number(l, (varnumber_T)llpos->col);
12018 list_append_number(l, (varnumber_T)llpos->len);
12019 }
12020 sprintf(buf, "pos%d", i + 1);
12021 dict_add_list(dict, buf, l);
12022 }
12023 }
12024 else
12025 {
12026 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12027 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012028 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012029 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12030 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
12031 list_append_dict(rettv->vval.v_list, dict);
12032 cur = cur->next;
12033 }
12034 }
12035#endif
12036}
12037
12038/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012039 * "getpid()" function
12040 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012041 static void
12042f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012043 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012044 typval_T *rettv;
12045{
12046 rettv->vval.v_number = mch_get_pid();
12047}
12048
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012049static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12050
12051/*
12052 * "getcurpos()" function
12053 */
12054 static void
12055f_getcurpos(argvars, rettv)
12056 typval_T *argvars;
12057 typval_T *rettv;
12058{
12059 getpos_both(argvars, rettv, TRUE);
12060}
12061
Bram Moolenaar18081e32008-02-20 19:11:07 +000012062/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012063 * "getpos(string)" function
12064 */
12065 static void
12066f_getpos(argvars, rettv)
12067 typval_T *argvars;
12068 typval_T *rettv;
12069{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012070 getpos_both(argvars, rettv, FALSE);
12071}
12072
12073 static void
12074getpos_both(argvars, rettv, getcurpos)
12075 typval_T *argvars;
12076 typval_T *rettv;
12077 int getcurpos;
12078{
Bram Moolenaara5525202006-03-02 22:52:09 +000012079 pos_T *fp;
12080 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012081 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012082
12083 if (rettv_list_alloc(rettv) == OK)
12084 {
12085 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012086 if (getcurpos)
12087 fp = &curwin->w_cursor;
12088 else
12089 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012090 if (fnum != -1)
12091 list_append_number(l, (varnumber_T)fnum);
12092 else
12093 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012094 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12095 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012096 list_append_number(l, (fp != NULL)
12097 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012098 : (varnumber_T)0);
12099 list_append_number(l,
12100#ifdef FEAT_VIRTUALEDIT
12101 (fp != NULL) ? (varnumber_T)fp->coladd :
12102#endif
12103 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012104 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012105 list_append_number(l, curwin->w_curswant == MAXCOL ?
12106 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012107 }
12108 else
12109 rettv->vval.v_number = FALSE;
12110}
12111
12112/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012113 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012114 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012115 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012116f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012117 typval_T *argvars UNUSED;
12118 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012119{
12120#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012121 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012122#endif
12123
Bram Moolenaar2641f772005-03-25 21:58:17 +000012124#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012125 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012126 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012127 wp = NULL;
12128 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12129 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012130 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012131 if (wp == NULL)
12132 return;
12133 }
12134
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012135 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012136 }
12137#endif
12138}
12139
12140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 * "getreg()" function
12142 */
12143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012145 typval_T *argvars;
12146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147{
12148 char_u *strregname;
12149 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012150 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012151 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012154 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012156 strregname = get_tv_string_chk(&argvars[0]);
12157 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012158 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012160 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012161 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12162 return_list = get_tv_number_chk(&argvars[2], &error);
12163 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012166 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012167
12168 if (error)
12169 return;
12170
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 regname = (strregname == NULL ? '"' : *strregname);
12172 if (regname == 0)
12173 regname = '"';
12174
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012175 if (return_list)
12176 {
12177 rettv->v_type = VAR_LIST;
12178 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12179 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012180 if (rettv->vval.v_list != NULL)
12181 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012182 }
12183 else
12184 {
12185 rettv->v_type = VAR_STRING;
12186 rettv->vval.v_string = get_reg_contents(regname,
12187 arg2 ? GREG_EXPR_SRC : 0);
12188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189}
12190
12191/*
12192 * "getregtype()" function
12193 */
12194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012195f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012196 typval_T *argvars;
12197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199 char_u *strregname;
12200 int regname;
12201 char_u buf[NUMBUFLEN + 2];
12202 long reglen = 0;
12203
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012204 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012205 {
12206 strregname = get_tv_string_chk(&argvars[0]);
12207 if (strregname == NULL) /* type error; errmsg already given */
12208 {
12209 rettv->v_type = VAR_STRING;
12210 rettv->vval.v_string = NULL;
12211 return;
12212 }
12213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 else
12215 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012216 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217
12218 regname = (strregname == NULL ? '"' : *strregname);
12219 if (regname == 0)
12220 regname = '"';
12221
12222 buf[0] = NUL;
12223 buf[1] = NUL;
12224 switch (get_reg_type(regname, &reglen))
12225 {
12226 case MLINE: buf[0] = 'V'; break;
12227 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 case MBLOCK:
12229 buf[0] = Ctrl_V;
12230 sprintf((char *)buf + 1, "%ld", reglen + 1);
12231 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->v_type = VAR_STRING;
12234 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235}
12236
12237/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012238 * "gettabvar()" function
12239 */
12240 static void
12241f_gettabvar(argvars, rettv)
12242 typval_T *argvars;
12243 typval_T *rettv;
12244{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012245 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012246 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012247 dictitem_T *v;
12248 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012249 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012250
12251 rettv->v_type = VAR_STRING;
12252 rettv->vval.v_string = NULL;
12253
12254 varname = get_tv_string_chk(&argvars[1]);
12255 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12256 if (tp != NULL && varname != NULL)
12257 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012258 /* Set tp to be our tabpage, temporarily. Also set the window to the
12259 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012260 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12261 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012262 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012263 /* look up the variable */
12264 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12265 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12266 if (v != NULL)
12267 {
12268 copy_tv(&v->di_tv, rettv);
12269 done = TRUE;
12270 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012271 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012272
12273 /* restore previous notion of curwin */
12274 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012275 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012276
12277 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012278 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012279}
12280
12281/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012282 * "gettabwinvar()" function
12283 */
12284 static void
12285f_gettabwinvar(argvars, rettv)
12286 typval_T *argvars;
12287 typval_T *rettv;
12288{
12289 getwinvar(argvars, rettv, 1);
12290}
12291
12292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 * "getwinposx()" function
12294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012297 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301#ifdef FEAT_GUI
12302 if (gui.in_use)
12303 {
12304 int x, y;
12305
12306 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 }
12309#endif
12310}
12311
12312/*
12313 * "getwinposy()" function
12314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012317 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321#ifdef FEAT_GUI
12322 if (gui.in_use)
12323 {
12324 int x, y;
12325
12326 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 }
12329#endif
12330}
12331
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012332/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012333 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012334 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012335 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012336find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012337 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012338 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012339{
12340#ifdef FEAT_WINDOWS
12341 win_T *wp;
12342#endif
12343 int nr;
12344
12345 nr = get_tv_number_chk(vp, NULL);
12346
12347#ifdef FEAT_WINDOWS
12348 if (nr < 0)
12349 return NULL;
12350 if (nr == 0)
12351 return curwin;
12352
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012353 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12354 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012355 if (--nr <= 0)
12356 break;
12357 return wp;
12358#else
12359 if (nr == 0 || nr == 1)
12360 return curwin;
12361 return NULL;
12362#endif
12363}
12364
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365/*
12366 * "getwinvar()" function
12367 */
12368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012370 typval_T *argvars;
12371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012373 getwinvar(argvars, rettv, 0);
12374}
12375
12376/*
12377 * getwinvar() and gettabwinvar()
12378 */
12379 static void
12380getwinvar(argvars, rettv, off)
12381 typval_T *argvars;
12382 typval_T *rettv;
12383 int off; /* 1 for gettabwinvar() */
12384{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 win_T *win, *oldcurwin;
12386 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012387 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012388 tabpage_T *tp = NULL;
12389 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012390 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012392#ifdef FEAT_WINDOWS
12393 if (off == 1)
12394 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12395 else
12396 tp = curtab;
12397#endif
12398 win = find_win_by_nr(&argvars[off], tp);
12399 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012400 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012402 rettv->v_type = VAR_STRING;
12403 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404
12405 if (win != NULL && varname != NULL)
12406 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012407 /* Set curwin to be our win, temporarily. Also set the tabpage,
12408 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012409 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012410 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012411 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012412 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012413 if (get_option_tv(&varname, rettv, 1) == OK)
12414 done = TRUE;
12415 }
12416 else
12417 {
12418 /* Look up the variable. */
12419 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12420 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12421 varname, FALSE);
12422 if (v != NULL)
12423 {
12424 copy_tv(&v->di_tv, rettv);
12425 done = TRUE;
12426 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012429
12430 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012431 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 }
12433
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012434 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12435 /* use the default return value */
12436 copy_tv(&argvars[off + 2], rettv);
12437
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 --emsg_off;
12439}
12440
12441/*
12442 * "glob()" function
12443 */
12444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012446 typval_T *argvars;
12447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012449 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012451 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012453 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012454 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012456 if (argvars[1].v_type != VAR_UNKNOWN)
12457 {
12458 if (get_tv_number_chk(&argvars[1], &error))
12459 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012460 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012461 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012462 if (get_tv_number_chk(&argvars[2], &error))
12463 {
12464 rettv->v_type = VAR_LIST;
12465 rettv->vval.v_list = NULL;
12466 }
12467 if (argvars[3].v_type != VAR_UNKNOWN
12468 && get_tv_number_chk(&argvars[3], &error))
12469 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012470 }
12471 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012472 if (!error)
12473 {
12474 ExpandInit(&xpc);
12475 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012476 if (p_wic)
12477 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012478 if (rettv->v_type == VAR_STRING)
12479 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012480 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012481 else if (rettv_list_alloc(rettv) != FAIL)
12482 {
12483 int i;
12484
12485 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12486 NULL, options, WILD_ALL_KEEP);
12487 for (i = 0; i < xpc.xp_numfiles; i++)
12488 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12489
12490 ExpandCleanup(&xpc);
12491 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012492 }
12493 else
12494 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495}
12496
12497/*
12498 * "globpath()" function
12499 */
12500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *argvars;
12503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012505 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012507 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012508 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012509 garray_T ga;
12510 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012512 /* When the optional second argument is non-zero, don't remove matches
12513 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012514 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012515 if (argvars[2].v_type != VAR_UNKNOWN)
12516 {
12517 if (get_tv_number_chk(&argvars[2], &error))
12518 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012519 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012520 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012521 if (get_tv_number_chk(&argvars[3], &error))
12522 {
12523 rettv->v_type = VAR_LIST;
12524 rettv->vval.v_list = NULL;
12525 }
12526 if (argvars[4].v_type != VAR_UNKNOWN
12527 && get_tv_number_chk(&argvars[4], &error))
12528 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012529 }
12530 }
12531 if (file != NULL && !error)
12532 {
12533 ga_init2(&ga, (int)sizeof(char_u *), 10);
12534 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12535 if (rettv->v_type == VAR_STRING)
12536 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12537 else if (rettv_list_alloc(rettv) != FAIL)
12538 for (i = 0; i < ga.ga_len; ++i)
12539 list_append_string(rettv->vval.v_list,
12540 ((char_u **)(ga.ga_data))[i], -1);
12541 ga_clear_strings(&ga);
12542 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012543 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545}
12546
12547/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012548 * "glob2regpat()" function
12549 */
12550 static void
12551f_glob2regpat(argvars, rettv)
12552 typval_T *argvars;
12553 typval_T *rettv;
12554{
12555 char_u *pat = get_tv_string_chk(&argvars[0]);
12556
12557 rettv->v_type = VAR_STRING;
12558 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12559}
12560
12561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 * "has()" function
12563 */
12564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012565f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *argvars;
12567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568{
12569 int i;
12570 char_u *name;
12571 int n = FALSE;
12572 static char *(has_list[]) =
12573 {
12574#ifdef AMIGA
12575 "amiga",
12576# ifdef FEAT_ARP
12577 "arp",
12578# endif
12579#endif
12580#ifdef __BEOS__
12581 "beos",
12582#endif
12583#ifdef MSDOS
12584# ifdef DJGPP
12585 "dos32",
12586# else
12587 "dos16",
12588# endif
12589#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012590#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591 "mac",
12592#endif
12593#if defined(MACOS_X_UNIX)
12594 "macunix",
12595#endif
12596#ifdef OS2
12597 "os2",
12598#endif
12599#ifdef __QNX__
12600 "qnx",
12601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602#ifdef UNIX
12603 "unix",
12604#endif
12605#ifdef VMS
12606 "vms",
12607#endif
12608#ifdef WIN16
12609 "win16",
12610#endif
12611#ifdef WIN32
12612 "win32",
12613#endif
12614#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12615 "win32unix",
12616#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012617#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 "win64",
12619#endif
12620#ifdef EBCDIC
12621 "ebcdic",
12622#endif
12623#ifndef CASE_INSENSITIVE_FILENAME
12624 "fname_case",
12625#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012626#ifdef HAVE_ACL
12627 "acl",
12628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629#ifdef FEAT_ARABIC
12630 "arabic",
12631#endif
12632#ifdef FEAT_AUTOCMD
12633 "autocmd",
12634#endif
12635#ifdef FEAT_BEVAL
12636 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012637# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12638 "balloon_multiline",
12639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640#endif
12641#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12642 "builtin_terms",
12643# ifdef ALL_BUILTIN_TCAPS
12644 "all_builtin_terms",
12645# endif
12646#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012647#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12648 || defined(FEAT_GUI_W32) \
12649 || defined(FEAT_GUI_MOTIF))
12650 "browsefilter",
12651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#ifdef FEAT_BYTEOFF
12653 "byte_offset",
12654#endif
12655#ifdef FEAT_CINDENT
12656 "cindent",
12657#endif
12658#ifdef FEAT_CLIENTSERVER
12659 "clientserver",
12660#endif
12661#ifdef FEAT_CLIPBOARD
12662 "clipboard",
12663#endif
12664#ifdef FEAT_CMDL_COMPL
12665 "cmdline_compl",
12666#endif
12667#ifdef FEAT_CMDHIST
12668 "cmdline_hist",
12669#endif
12670#ifdef FEAT_COMMENTS
12671 "comments",
12672#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012673#ifdef FEAT_CONCEAL
12674 "conceal",
12675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676#ifdef FEAT_CRYPT
12677 "cryptv",
12678#endif
12679#ifdef FEAT_CSCOPE
12680 "cscope",
12681#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012682#ifdef FEAT_CURSORBIND
12683 "cursorbind",
12684#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012685#ifdef CURSOR_SHAPE
12686 "cursorshape",
12687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688#ifdef DEBUG
12689 "debug",
12690#endif
12691#ifdef FEAT_CON_DIALOG
12692 "dialog_con",
12693#endif
12694#ifdef FEAT_GUI_DIALOG
12695 "dialog_gui",
12696#endif
12697#ifdef FEAT_DIFF
12698 "diff",
12699#endif
12700#ifdef FEAT_DIGRAPHS
12701 "digraphs",
12702#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012703#ifdef FEAT_DIRECTX
12704 "directx",
12705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706#ifdef FEAT_DND
12707 "dnd",
12708#endif
12709#ifdef FEAT_EMACS_TAGS
12710 "emacs_tags",
12711#endif
12712 "eval", /* always present, of course! */
12713#ifdef FEAT_EX_EXTRA
12714 "ex_extra",
12715#endif
12716#ifdef FEAT_SEARCH_EXTRA
12717 "extra_search",
12718#endif
12719#ifdef FEAT_FKMAP
12720 "farsi",
12721#endif
12722#ifdef FEAT_SEARCHPATH
12723 "file_in_path",
12724#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012725#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012726 "filterpipe",
12727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728#ifdef FEAT_FIND_ID
12729 "find_in_path",
12730#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012731#ifdef FEAT_FLOAT
12732 "float",
12733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734#ifdef FEAT_FOLDING
12735 "folding",
12736#endif
12737#ifdef FEAT_FOOTER
12738 "footer",
12739#endif
12740#if !defined(USE_SYSTEM) && defined(UNIX)
12741 "fork",
12742#endif
12743#ifdef FEAT_GETTEXT
12744 "gettext",
12745#endif
12746#ifdef FEAT_GUI
12747 "gui",
12748#endif
12749#ifdef FEAT_GUI_ATHENA
12750# ifdef FEAT_GUI_NEXTAW
12751 "gui_neXtaw",
12752# else
12753 "gui_athena",
12754# endif
12755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756#ifdef FEAT_GUI_GTK
12757 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012760#ifdef FEAT_GUI_GNOME
12761 "gui_gnome",
12762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763#ifdef FEAT_GUI_MAC
12764 "gui_mac",
12765#endif
12766#ifdef FEAT_GUI_MOTIF
12767 "gui_motif",
12768#endif
12769#ifdef FEAT_GUI_PHOTON
12770 "gui_photon",
12771#endif
12772#ifdef FEAT_GUI_W16
12773 "gui_win16",
12774#endif
12775#ifdef FEAT_GUI_W32
12776 "gui_win32",
12777#endif
12778#ifdef FEAT_HANGULIN
12779 "hangul_input",
12780#endif
12781#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12782 "iconv",
12783#endif
12784#ifdef FEAT_INS_EXPAND
12785 "insert_expand",
12786#endif
12787#ifdef FEAT_JUMPLIST
12788 "jumplist",
12789#endif
12790#ifdef FEAT_KEYMAP
12791 "keymap",
12792#endif
12793#ifdef FEAT_LANGMAP
12794 "langmap",
12795#endif
12796#ifdef FEAT_LIBCALL
12797 "libcall",
12798#endif
12799#ifdef FEAT_LINEBREAK
12800 "linebreak",
12801#endif
12802#ifdef FEAT_LISP
12803 "lispindent",
12804#endif
12805#ifdef FEAT_LISTCMDS
12806 "listcmds",
12807#endif
12808#ifdef FEAT_LOCALMAP
12809 "localmap",
12810#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012811#ifdef FEAT_LUA
12812# ifndef DYNAMIC_LUA
12813 "lua",
12814# endif
12815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816#ifdef FEAT_MENU
12817 "menu",
12818#endif
12819#ifdef FEAT_SESSION
12820 "mksession",
12821#endif
12822#ifdef FEAT_MODIFY_FNAME
12823 "modify_fname",
12824#endif
12825#ifdef FEAT_MOUSE
12826 "mouse",
12827#endif
12828#ifdef FEAT_MOUSESHAPE
12829 "mouseshape",
12830#endif
12831#if defined(UNIX) || defined(VMS)
12832# ifdef FEAT_MOUSE_DEC
12833 "mouse_dec",
12834# endif
12835# ifdef FEAT_MOUSE_GPM
12836 "mouse_gpm",
12837# endif
12838# ifdef FEAT_MOUSE_JSB
12839 "mouse_jsbterm",
12840# endif
12841# ifdef FEAT_MOUSE_NET
12842 "mouse_netterm",
12843# endif
12844# ifdef FEAT_MOUSE_PTERM
12845 "mouse_pterm",
12846# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012847# ifdef FEAT_MOUSE_SGR
12848 "mouse_sgr",
12849# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012850# ifdef FEAT_SYSMOUSE
12851 "mouse_sysmouse",
12852# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012853# ifdef FEAT_MOUSE_URXVT
12854 "mouse_urxvt",
12855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856# ifdef FEAT_MOUSE_XTERM
12857 "mouse_xterm",
12858# endif
12859#endif
12860#ifdef FEAT_MBYTE
12861 "multi_byte",
12862#endif
12863#ifdef FEAT_MBYTE_IME
12864 "multi_byte_ime",
12865#endif
12866#ifdef FEAT_MULTI_LANG
12867 "multi_lang",
12868#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012869#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012870#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012871 "mzscheme",
12872#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874#ifdef FEAT_OLE
12875 "ole",
12876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877#ifdef FEAT_PATH_EXTRA
12878 "path_extra",
12879#endif
12880#ifdef FEAT_PERL
12881#ifndef DYNAMIC_PERL
12882 "perl",
12883#endif
12884#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012885#ifdef FEAT_PERSISTENT_UNDO
12886 "persistent_undo",
12887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888#ifdef FEAT_PYTHON
12889#ifndef DYNAMIC_PYTHON
12890 "python",
12891#endif
12892#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012893#ifdef FEAT_PYTHON3
12894#ifndef DYNAMIC_PYTHON3
12895 "python3",
12896#endif
12897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898#ifdef FEAT_POSTSCRIPT
12899 "postscript",
12900#endif
12901#ifdef FEAT_PRINTER
12902 "printer",
12903#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012904#ifdef FEAT_PROFILE
12905 "profile",
12906#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012907#ifdef FEAT_RELTIME
12908 "reltime",
12909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910#ifdef FEAT_QUICKFIX
12911 "quickfix",
12912#endif
12913#ifdef FEAT_RIGHTLEFT
12914 "rightleft",
12915#endif
12916#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12917 "ruby",
12918#endif
12919#ifdef FEAT_SCROLLBIND
12920 "scrollbind",
12921#endif
12922#ifdef FEAT_CMDL_INFO
12923 "showcmd",
12924 "cmdline_info",
12925#endif
12926#ifdef FEAT_SIGNS
12927 "signs",
12928#endif
12929#ifdef FEAT_SMARTINDENT
12930 "smartindent",
12931#endif
12932#ifdef FEAT_SNIFF
12933 "sniff",
12934#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012935#ifdef STARTUPTIME
12936 "startuptime",
12937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938#ifdef FEAT_STL_OPT
12939 "statusline",
12940#endif
12941#ifdef FEAT_SUN_WORKSHOP
12942 "sun_workshop",
12943#endif
12944#ifdef FEAT_NETBEANS_INTG
12945 "netbeans_intg",
12946#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012947#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012948 "spell",
12949#endif
12950#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 "syntax",
12952#endif
12953#if defined(USE_SYSTEM) || !defined(UNIX)
12954 "system",
12955#endif
12956#ifdef FEAT_TAG_BINS
12957 "tag_binary",
12958#endif
12959#ifdef FEAT_TAG_OLDSTATIC
12960 "tag_old_static",
12961#endif
12962#ifdef FEAT_TAG_ANYWHITE
12963 "tag_any_white",
12964#endif
12965#ifdef FEAT_TCL
12966# ifndef DYNAMIC_TCL
12967 "tcl",
12968# endif
12969#endif
12970#ifdef TERMINFO
12971 "terminfo",
12972#endif
12973#ifdef FEAT_TERMRESPONSE
12974 "termresponse",
12975#endif
12976#ifdef FEAT_TEXTOBJ
12977 "textobjects",
12978#endif
12979#ifdef HAVE_TGETENT
12980 "tgetent",
12981#endif
12982#ifdef FEAT_TITLE
12983 "title",
12984#endif
12985#ifdef FEAT_TOOLBAR
12986 "toolbar",
12987#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012988#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12989 "unnamedplus",
12990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991#ifdef FEAT_USR_CMDS
12992 "user-commands", /* was accidentally included in 5.4 */
12993 "user_commands",
12994#endif
12995#ifdef FEAT_VIMINFO
12996 "viminfo",
12997#endif
12998#ifdef FEAT_VERTSPLIT
12999 "vertsplit",
13000#endif
13001#ifdef FEAT_VIRTUALEDIT
13002 "virtualedit",
13003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005#ifdef FEAT_VISUALEXTRA
13006 "visualextra",
13007#endif
13008#ifdef FEAT_VREPLACE
13009 "vreplace",
13010#endif
13011#ifdef FEAT_WILDIGN
13012 "wildignore",
13013#endif
13014#ifdef FEAT_WILDMENU
13015 "wildmenu",
13016#endif
13017#ifdef FEAT_WINDOWS
13018 "windows",
13019#endif
13020#ifdef FEAT_WAK
13021 "winaltkeys",
13022#endif
13023#ifdef FEAT_WRITEBACKUP
13024 "writebackup",
13025#endif
13026#ifdef FEAT_XIM
13027 "xim",
13028#endif
13029#ifdef FEAT_XFONTSET
13030 "xfontset",
13031#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013032#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013033 "xpm",
13034 "xpm_w32", /* for backward compatibility */
13035#else
13036# if defined(HAVE_XPM)
13037 "xpm",
13038# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040#ifdef USE_XSMP
13041 "xsmp",
13042#endif
13043#ifdef USE_XSMP_INTERACT
13044 "xsmp_interact",
13045#endif
13046#ifdef FEAT_XCLIPBOARD
13047 "xterm_clipboard",
13048#endif
13049#ifdef FEAT_XTERM_SAVE
13050 "xterm_save",
13051#endif
13052#if defined(UNIX) && defined(FEAT_X11)
13053 "X11",
13054#endif
13055 NULL
13056 };
13057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 for (i = 0; has_list[i] != NULL; ++i)
13060 if (STRICMP(name, has_list[i]) == 0)
13061 {
13062 n = TRUE;
13063 break;
13064 }
13065
13066 if (n == FALSE)
13067 {
13068 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013069 {
13070 if (name[5] == '-'
13071 && STRLEN(name) > 11
13072 && vim_isdigit(name[6])
13073 && vim_isdigit(name[8])
13074 && vim_isdigit(name[10]))
13075 {
13076 int major = atoi((char *)name + 6);
13077 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013078
13079 /* Expect "patch-9.9.01234". */
13080 n = (major < VIM_VERSION_MAJOR
13081 || (major == VIM_VERSION_MAJOR
13082 && (minor < VIM_VERSION_MINOR
13083 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013084 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013085 }
13086 else
13087 n = has_patch(atoi((char *)name + 5));
13088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 else if (STRICMP(name, "vim_starting") == 0)
13090 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013091#ifdef FEAT_MBYTE
13092 else if (STRICMP(name, "multi_byte_encoding") == 0)
13093 n = has_mbyte;
13094#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013095#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13096 else if (STRICMP(name, "balloon_multiline") == 0)
13097 n = multiline_balloon_available();
13098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099#ifdef DYNAMIC_TCL
13100 else if (STRICMP(name, "tcl") == 0)
13101 n = tcl_enabled(FALSE);
13102#endif
13103#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13104 else if (STRICMP(name, "iconv") == 0)
13105 n = iconv_enabled(FALSE);
13106#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013107#ifdef DYNAMIC_LUA
13108 else if (STRICMP(name, "lua") == 0)
13109 n = lua_enabled(FALSE);
13110#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013111#ifdef DYNAMIC_MZSCHEME
13112 else if (STRICMP(name, "mzscheme") == 0)
13113 n = mzscheme_enabled(FALSE);
13114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115#ifdef DYNAMIC_RUBY
13116 else if (STRICMP(name, "ruby") == 0)
13117 n = ruby_enabled(FALSE);
13118#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013119#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120#ifdef DYNAMIC_PYTHON
13121 else if (STRICMP(name, "python") == 0)
13122 n = python_enabled(FALSE);
13123#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013124#endif
13125#ifdef FEAT_PYTHON3
13126#ifdef DYNAMIC_PYTHON3
13127 else if (STRICMP(name, "python3") == 0)
13128 n = python3_enabled(FALSE);
13129#endif
13130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131#ifdef DYNAMIC_PERL
13132 else if (STRICMP(name, "perl") == 0)
13133 n = perl_enabled(FALSE);
13134#endif
13135#ifdef FEAT_GUI
13136 else if (STRICMP(name, "gui_running") == 0)
13137 n = (gui.in_use || gui.starting);
13138# ifdef FEAT_GUI_W32
13139 else if (STRICMP(name, "gui_win32s") == 0)
13140 n = gui_is_win32s();
13141# endif
13142# ifdef FEAT_BROWSE
13143 else if (STRICMP(name, "browse") == 0)
13144 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13145# endif
13146#endif
13147#ifdef FEAT_SYN_HL
13148 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013149 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#endif
13151#if defined(WIN3264)
13152 else if (STRICMP(name, "win95") == 0)
13153 n = mch_windows95();
13154#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013155#ifdef FEAT_NETBEANS_INTG
13156 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013157 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 }
13160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162}
13163
13164/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013165 * "has_key()" function
13166 */
13167 static void
13168f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013169 typval_T *argvars;
13170 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013171{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013172 if (argvars[0].v_type != VAR_DICT)
13173 {
13174 EMSG(_(e_dictreq));
13175 return;
13176 }
13177 if (argvars[0].vval.v_dict == NULL)
13178 return;
13179
13180 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013181 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013182}
13183
13184/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013185 * "haslocaldir()" function
13186 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013187 static void
13188f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013189 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013190 typval_T *rettv;
13191{
13192 rettv->vval.v_number = (curwin->w_localdir != NULL);
13193}
13194
13195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 * "hasmapto()" function
13197 */
13198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 typval_T *argvars;
13201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202{
13203 char_u *name;
13204 char_u *mode;
13205 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013206 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013209 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 mode = (char_u *)"nvo";
13211 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013214 if (argvars[2].v_type != VAR_UNKNOWN)
13215 abbr = get_tv_number(&argvars[2]);
13216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217
Bram Moolenaar2c932302006-03-18 21:42:09 +000013218 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013219 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222}
13223
13224/*
13225 * "histadd()" function
13226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013229 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231{
13232#ifdef FEAT_CMDHIST
13233 int histype;
13234 char_u *str;
13235 char_u buf[NUMBUFLEN];
13236#endif
13237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 if (check_restricted() || check_secure())
13240 return;
13241#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013242 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13243 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 if (histype >= 0)
13245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 if (*str != NUL)
13248 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013249 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 return;
13253 }
13254 }
13255#endif
13256}
13257
13258/*
13259 * "histdel()" function
13260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013263 typval_T *argvars UNUSED;
13264 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265{
13266#ifdef FEAT_CMDHIST
13267 int n;
13268 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013269 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013271 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13272 if (str == NULL)
13273 n = 0;
13274 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013276 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013277 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013279 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013280 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 else
13282 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284 get_tv_string_buf(&argvars[1], buf));
13285 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286#endif
13287}
13288
13289/*
13290 * "histget()" function
13291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013294 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296{
13297#ifdef FEAT_CMDHIST
13298 int type;
13299 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013300 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13303 if (str == NULL)
13304 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306 {
13307 type = get_histtype(str);
13308 if (argvars[1].v_type == VAR_UNKNOWN)
13309 idx = get_history_idx(type);
13310 else
13311 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13312 /* -1 on type error */
13313 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013316 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319}
13320
13321/*
13322 * "histnr()" function
13323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013325f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013326 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328{
13329 int i;
13330
13331#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 char_u *history = get_tv_string_chk(&argvars[0]);
13333
13334 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 if (i >= HIST_CMD && i < HIST_COUNT)
13336 i = get_history_idx(i);
13337 else
13338#endif
13339 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341}
13342
13343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344 * "highlightID(name)" function
13345 */
13346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013347f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013348 typval_T *argvars;
13349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013351 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352}
13353
13354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013355 * "highlight_exists()" function
13356 */
13357 static void
13358f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013359 typval_T *argvars;
13360 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013361{
13362 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13363}
13364
13365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 * "hostname()" function
13367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013369f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013370 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372{
13373 char_u hostname[256];
13374
13375 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376 rettv->v_type = VAR_STRING;
13377 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378}
13379
13380/*
13381 * iconv() function
13382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013385 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387{
13388#ifdef FEAT_MBYTE
13389 char_u buf1[NUMBUFLEN];
13390 char_u buf2[NUMBUFLEN];
13391 char_u *from, *to, *str;
13392 vimconv_T vimconv;
13393#endif
13394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013395 rettv->v_type = VAR_STRING;
13396 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397
13398#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 str = get_tv_string(&argvars[0]);
13400 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13401 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 vimconv.vc_type = CONV_NONE;
13403 convert_setup(&vimconv, from, to);
13404
13405 /* If the encodings are equal, no conversion needed. */
13406 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013409 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410
13411 convert_setup(&vimconv, NULL, NULL);
13412 vim_free(from);
13413 vim_free(to);
13414#endif
13415}
13416
13417/*
13418 * "indent()" function
13419 */
13420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013422 typval_T *argvars;
13423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424{
13425 linenr_T lnum;
13426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013427 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013431 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432}
13433
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013434/*
13435 * "index()" function
13436 */
13437 static void
13438f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013441{
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 list_T *l;
13443 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013444 long idx = 0;
13445 int ic = FALSE;
13446
13447 rettv->vval.v_number = -1;
13448 if (argvars[0].v_type != VAR_LIST)
13449 {
13450 EMSG(_(e_listreq));
13451 return;
13452 }
13453 l = argvars[0].vval.v_list;
13454 if (l != NULL)
13455 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013456 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013457 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013459 int error = FALSE;
13460
Bram Moolenaar758711c2005-02-02 23:11:38 +000013461 /* Start at specified item. Use the cached index that list_find()
13462 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013464 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013465 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466 ic = get_tv_number_chk(&argvars[3], &error);
13467 if (error)
13468 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013469 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013470
Bram Moolenaar758711c2005-02-02 23:11:38 +000013471 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013472 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013473 {
13474 rettv->vval.v_number = idx;
13475 break;
13476 }
13477 }
13478}
13479
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480static int inputsecret_flag = 0;
13481
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013482static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13483
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013485 * This function is used by f_input() and f_inputdialog() functions. The third
13486 * argument to f_input() specifies the type of completion to use at the
13487 * prompt. The third argument to f_inputdialog() specifies the value to return
13488 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 */
13490 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013491get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013492 typval_T *argvars;
13493 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013494 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497 char_u *p = NULL;
13498 int c;
13499 char_u buf[NUMBUFLEN];
13500 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013502 int xp_type = EXPAND_NOTHING;
13503 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013506 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507
13508#ifdef NO_CONSOLE_INPUT
13509 /* While starting up, there is no place to enter text. */
13510 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512#endif
13513
13514 cmd_silent = FALSE; /* Want to see the prompt. */
13515 if (prompt != NULL)
13516 {
13517 /* Only the part of the message after the last NL is considered as
13518 * prompt for the command line */
13519 p = vim_strrchr(prompt, '\n');
13520 if (p == NULL)
13521 p = prompt;
13522 else
13523 {
13524 ++p;
13525 c = *p;
13526 *p = NUL;
13527 msg_start();
13528 msg_clr_eos();
13529 msg_puts_attr(prompt, echo_attr);
13530 msg_didout = FALSE;
13531 msg_starthere();
13532 *p = c;
13533 }
13534 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 if (argvars[1].v_type != VAR_UNKNOWN)
13537 {
13538 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13539 if (defstr != NULL)
13540 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013542 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013543 {
13544 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013545 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013546 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013547
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013548 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013549 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013550
Bram Moolenaar4463f292005-09-25 22:20:24 +000013551 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13552 if (xp_name == NULL)
13553 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013554
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013555 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013556
Bram Moolenaar4463f292005-09-25 22:20:24 +000013557 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13558 &xp_arg) == FAIL)
13559 return;
13560 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013561 }
13562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013564 {
13565# ifdef FEAT_EX_EXTRA
13566 int save_ex_normal_busy = ex_normal_busy;
13567 ex_normal_busy = 0;
13568# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013570 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13571 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013572# ifdef FEAT_EX_EXTRA
13573 ex_normal_busy = save_ex_normal_busy;
13574# endif
13575 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013576 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013577 && argvars[1].v_type != VAR_UNKNOWN
13578 && argvars[2].v_type != VAR_UNKNOWN)
13579 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13580 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013581
13582 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013584 /* since the user typed this, no need to wait for return */
13585 need_wait_return = FALSE;
13586 msg_didout = FALSE;
13587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 cmd_silent = cmd_silent_save;
13589}
13590
13591/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013592 * "input()" function
13593 * Also handles inputsecret() when inputsecret is set.
13594 */
13595 static void
13596f_input(argvars, rettv)
13597 typval_T *argvars;
13598 typval_T *rettv;
13599{
13600 get_user_input(argvars, rettv, FALSE);
13601}
13602
13603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 * "inputdialog()" function
13605 */
13606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013608 typval_T *argvars;
13609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610{
13611#if defined(FEAT_GUI_TEXTDIALOG)
13612 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13613 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13614 {
13615 char_u *message;
13616 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013619 message = get_tv_string_chk(&argvars[0]);
13620 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013621 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013622 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 else
13624 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 if (message != NULL && defstr != NULL
13626 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013627 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 else
13630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 if (message != NULL && defstr != NULL
13632 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013633 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 rettv->vval.v_string = vim_strsave(
13635 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 }
13641 else
13642#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013643 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644}
13645
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013646/*
13647 * "inputlist()" function
13648 */
13649 static void
13650f_inputlist(argvars, rettv)
13651 typval_T *argvars;
13652 typval_T *rettv;
13653{
13654 listitem_T *li;
13655 int selected;
13656 int mouse_used;
13657
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013658#ifdef NO_CONSOLE_INPUT
13659 /* While starting up, there is no place to enter text. */
13660 if (no_console_input())
13661 return;
13662#endif
13663 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13664 {
13665 EMSG2(_(e_listarg), "inputlist()");
13666 return;
13667 }
13668
13669 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013670 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013671 lines_left = Rows; /* avoid more prompt */
13672 msg_scroll = TRUE;
13673 msg_clr_eos();
13674
13675 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13676 {
13677 msg_puts(get_tv_string(&li->li_tv));
13678 msg_putchar('\n');
13679 }
13680
13681 /* Ask for choice. */
13682 selected = prompt_for_number(&mouse_used);
13683 if (mouse_used)
13684 selected -= lines_left;
13685
13686 rettv->vval.v_number = selected;
13687}
13688
13689
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13691
13692/*
13693 * "inputrestore()" function
13694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013696f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013697 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699{
13700 if (ga_userinput.ga_len > 0)
13701 {
13702 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13704 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013705 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 }
13707 else if (p_verbose > 1)
13708 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013709 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 }
13712}
13713
13714/*
13715 * "inputsave()" function
13716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013719 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013722 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 if (ga_grow(&ga_userinput, 1) == OK)
13724 {
13725 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13726 + ga_userinput.ga_len);
13727 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013728 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 }
13730 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732}
13733
13734/*
13735 * "inputsecret()" function
13736 */
13737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013739 typval_T *argvars;
13740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741{
13742 ++cmdline_star;
13743 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 --cmdline_star;
13746 --inputsecret_flag;
13747}
13748
13749/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013750 * "insert()" function
13751 */
13752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 typval_T *argvars;
13755 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013756{
13757 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 listitem_T *item;
13759 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013761
13762 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013763 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013764 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013765 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013766 {
13767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013768 before = get_tv_number_chk(&argvars[2], &error);
13769 if (error)
13770 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013771
Bram Moolenaar758711c2005-02-02 23:11:38 +000013772 if (before == l->lv_len)
13773 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013774 else
13775 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013776 item = list_find(l, before);
13777 if (item == NULL)
13778 {
13779 EMSGN(_(e_listidx), before);
13780 l = NULL;
13781 }
13782 }
13783 if (l != NULL)
13784 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013785 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013786 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013787 }
13788 }
13789}
13790
13791/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013792 * "invert(expr)" function
13793 */
13794 static void
13795f_invert(argvars, rettv)
13796 typval_T *argvars;
13797 typval_T *rettv;
13798{
13799 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13800}
13801
13802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 * "isdirectory()" function
13804 */
13805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *argvars;
13808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013810 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811}
13812
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013813/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013814 * "islocked()" function
13815 */
13816 static void
13817f_islocked(argvars, rettv)
13818 typval_T *argvars;
13819 typval_T *rettv;
13820{
13821 lval_T lv;
13822 char_u *end;
13823 dictitem_T *di;
13824
13825 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013826 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13827 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013828 if (end != NULL && lv.ll_name != NULL)
13829 {
13830 if (*end != NUL)
13831 EMSG(_(e_trailing));
13832 else
13833 {
13834 if (lv.ll_tv == NULL)
13835 {
13836 if (check_changedtick(lv.ll_name))
13837 rettv->vval.v_number = 1; /* always locked */
13838 else
13839 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013840 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013841 if (di != NULL)
13842 {
13843 /* Consider a variable locked when:
13844 * 1. the variable itself is locked
13845 * 2. the value of the variable is locked.
13846 * 3. the List or Dict value is locked.
13847 */
13848 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13849 || tv_islocked(&di->di_tv));
13850 }
13851 }
13852 }
13853 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013854 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013855 else if (lv.ll_newkey != NULL)
13856 EMSG2(_(e_dictkey), lv.ll_newkey);
13857 else if (lv.ll_list != NULL)
13858 /* List item. */
13859 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13860 else
13861 /* Dictionary item. */
13862 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13863 }
13864 }
13865
13866 clear_lval(&lv);
13867}
13868
Bram Moolenaar33570922005-01-25 22:26:29 +000013869static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013870
13871/*
13872 * Turn a dict into a list:
13873 * "what" == 0: list of keys
13874 * "what" == 1: list of values
13875 * "what" == 2: list of items
13876 */
13877 static void
13878dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013879 typval_T *argvars;
13880 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013881 int what;
13882{
Bram Moolenaar33570922005-01-25 22:26:29 +000013883 list_T *l2;
13884 dictitem_T *di;
13885 hashitem_T *hi;
13886 listitem_T *li;
13887 listitem_T *li2;
13888 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013889 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013890
Bram Moolenaar8c711452005-01-14 21:53:12 +000013891 if (argvars[0].v_type != VAR_DICT)
13892 {
13893 EMSG(_(e_dictreq));
13894 return;
13895 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013896 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013897 return;
13898
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013899 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013900 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013901
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013902 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013903 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013904 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013905 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013906 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013907 --todo;
13908 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013909
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013910 li = listitem_alloc();
13911 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013912 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013913 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013914
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013915 if (what == 0)
13916 {
13917 /* keys() */
13918 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013919 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013920 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13921 }
13922 else if (what == 1)
13923 {
13924 /* values() */
13925 copy_tv(&di->di_tv, &li->li_tv);
13926 }
13927 else
13928 {
13929 /* items() */
13930 l2 = list_alloc();
13931 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013932 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013933 li->li_tv.vval.v_list = l2;
13934 if (l2 == NULL)
13935 break;
13936 ++l2->lv_refcount;
13937
13938 li2 = listitem_alloc();
13939 if (li2 == NULL)
13940 break;
13941 list_append(l2, li2);
13942 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013943 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013944 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13945
13946 li2 = listitem_alloc();
13947 if (li2 == NULL)
13948 break;
13949 list_append(l2, li2);
13950 copy_tv(&di->di_tv, &li2->li_tv);
13951 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013952 }
13953 }
13954}
13955
13956/*
13957 * "items(dict)" function
13958 */
13959 static void
13960f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013963{
13964 dict_list(argvars, rettv, 2);
13965}
13966
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968 * "join()" function
13969 */
13970 static void
13971f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013972 typval_T *argvars;
13973 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013974{
13975 garray_T ga;
13976 char_u *sep;
13977
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013978 if (argvars[0].v_type != VAR_LIST)
13979 {
13980 EMSG(_(e_listreq));
13981 return;
13982 }
13983 if (argvars[0].vval.v_list == NULL)
13984 return;
13985 if (argvars[1].v_type == VAR_UNKNOWN)
13986 sep = (char_u *)" ";
13987 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013989
13990 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991
13992 if (sep != NULL)
13993 {
13994 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013995 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013996 ga_append(&ga, NUL);
13997 rettv->vval.v_string = (char_u *)ga.ga_data;
13998 }
13999 else
14000 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014001}
14002
14003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014004 * "keys()" function
14005 */
14006 static void
14007f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014008 typval_T *argvars;
14009 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014010{
14011 dict_list(argvars, rettv, 0);
14012}
14013
14014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 * "last_buffer_nr()" function.
14016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014018f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014019 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021{
14022 int n = 0;
14023 buf_T *buf;
14024
14025 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14026 if (n < buf->b_fnum)
14027 n = buf->b_fnum;
14028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014029 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014030}
14031
14032/*
14033 * "len()" function
14034 */
14035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014037 typval_T *argvars;
14038 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014039{
14040 switch (argvars[0].v_type)
14041 {
14042 case VAR_STRING:
14043 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 rettv->vval.v_number = (varnumber_T)STRLEN(
14045 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014046 break;
14047 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014048 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014049 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014050 case VAR_DICT:
14051 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14052 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014053 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014054 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014055 break;
14056 }
14057}
14058
Bram Moolenaar33570922005-01-25 22:26:29 +000014059static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014060
14061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014062libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014063 typval_T *argvars;
14064 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014065 int type;
14066{
14067#ifdef FEAT_LIBCALL
14068 char_u *string_in;
14069 char_u **string_result;
14070 int nr_result;
14071#endif
14072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014073 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014074 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014076
14077 if (check_restricted() || check_secure())
14078 return;
14079
14080#ifdef FEAT_LIBCALL
14081 /* The first two args must be strings, otherwise its meaningless */
14082 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14083 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014084 string_in = NULL;
14085 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014086 string_in = argvars[2].vval.v_string;
14087 if (type == VAR_NUMBER)
14088 string_result = NULL;
14089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014091 if (mch_libcall(argvars[0].vval.v_string,
14092 argvars[1].vval.v_string,
14093 string_in,
14094 argvars[2].vval.v_number,
14095 string_result,
14096 &nr_result) == OK
14097 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014098 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014099 }
14100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101}
14102
14103/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014104 * "libcall()" function
14105 */
14106 static void
14107f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014108 typval_T *argvars;
14109 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014110{
14111 libcall_common(argvars, rettv, VAR_STRING);
14112}
14113
14114/*
14115 * "libcallnr()" function
14116 */
14117 static void
14118f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014119 typval_T *argvars;
14120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121{
14122 libcall_common(argvars, rettv, VAR_NUMBER);
14123}
14124
14125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126 * "line(string)" function
14127 */
14128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 typval_T *argvars;
14131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132{
14133 linenr_T lnum = 0;
14134 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014135 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014137 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 if (fp != NULL)
14139 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141}
14142
14143/*
14144 * "line2byte(lnum)" function
14145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014147f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014148 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150{
14151#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153#else
14154 linenr_T lnum;
14155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014156 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14161 if (rettv->vval.v_number >= 0)
14162 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163#endif
14164}
14165
14166/*
14167 * "lispindent(lnum)" function
14168 */
14169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014170f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014171 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173{
14174#ifdef FEAT_LISP
14175 pos_T pos;
14176 linenr_T lnum;
14177
14178 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14181 {
14182 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 curwin->w_cursor = pos;
14185 }
14186 else
14187#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014188 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189}
14190
14191/*
14192 * "localtime()" function
14193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014195f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014196 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014199 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200}
14201
Bram Moolenaar33570922005-01-25 22:26:29 +000014202static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203
14204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014205get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *argvars;
14207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 int exact;
14209{
14210 char_u *keys;
14211 char_u *which;
14212 char_u buf[NUMBUFLEN];
14213 char_u *keys_buf = NULL;
14214 char_u *rhs;
14215 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014216 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014217 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014218 mapblock_T *mp;
14219 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220
14221 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014222 rettv->v_type = VAR_STRING;
14223 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014225 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 if (*keys == NUL)
14227 return;
14228
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014229 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014232 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014233 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014234 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014235 if (argvars[3].v_type != VAR_UNKNOWN)
14236 get_dict = get_tv_number(&argvars[3]);
14237 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 else
14240 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 if (which == NULL)
14242 return;
14243
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 mode = get_map_mode(&which, 0);
14245
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014246 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014247 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014249
14250 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014252 /* Return a string. */
14253 if (rhs != NULL)
14254 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255
Bram Moolenaarbd743252010-10-20 21:23:33 +020014256 }
14257 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14258 {
14259 /* Return a dictionary. */
14260 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14261 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14262 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263
Bram Moolenaarbd743252010-10-20 21:23:33 +020014264 dict_add_nr_str(dict, "lhs", 0L, lhs);
14265 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14266 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14267 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14268 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14269 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14270 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014271 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014272 dict_add_nr_str(dict, "mode", 0L, mapmode);
14273
14274 vim_free(lhs);
14275 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 }
14277}
14278
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014279#ifdef FEAT_FLOAT
14280/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014281 * "log()" function
14282 */
14283 static void
14284f_log(argvars, rettv)
14285 typval_T *argvars;
14286 typval_T *rettv;
14287{
14288 float_T f;
14289
14290 rettv->v_type = VAR_FLOAT;
14291 if (get_float_arg(argvars, &f) == OK)
14292 rettv->vval.v_float = log(f);
14293 else
14294 rettv->vval.v_float = 0.0;
14295}
14296
14297/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014298 * "log10()" function
14299 */
14300 static void
14301f_log10(argvars, rettv)
14302 typval_T *argvars;
14303 typval_T *rettv;
14304{
14305 float_T f;
14306
14307 rettv->v_type = VAR_FLOAT;
14308 if (get_float_arg(argvars, &f) == OK)
14309 rettv->vval.v_float = log10(f);
14310 else
14311 rettv->vval.v_float = 0.0;
14312}
14313#endif
14314
Bram Moolenaar1dced572012-04-05 16:54:08 +020014315#ifdef FEAT_LUA
14316/*
14317 * "luaeval()" function
14318 */
14319 static void
14320f_luaeval(argvars, rettv)
14321 typval_T *argvars;
14322 typval_T *rettv;
14323{
14324 char_u *str;
14325 char_u buf[NUMBUFLEN];
14326
14327 str = get_tv_string_buf(&argvars[0], buf);
14328 do_luaeval(str, argvars + 1, rettv);
14329}
14330#endif
14331
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014333 * "map()" function
14334 */
14335 static void
14336f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014337 typval_T *argvars;
14338 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014339{
14340 filter_map(argvars, rettv, TRUE);
14341}
14342
14343/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014344 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 */
14346 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014348 typval_T *argvars;
14349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014351 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352}
14353
14354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014355 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 */
14357 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014358f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014359 typval_T *argvars;
14360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014362 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363}
14364
Bram Moolenaar33570922005-01-25 22:26:29 +000014365static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366
14367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014369 typval_T *argvars;
14370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 int type;
14372{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014373 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014374 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014375 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 char_u *pat;
14377 regmatch_T regmatch;
14378 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014379 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 char_u *save_cpo;
14381 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014382 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014383 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014384 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014385 list_T *l = NULL;
14386 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014387 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014388 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389
14390 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14391 save_cpo = p_cpo;
14392 p_cpo = (char_u *)"";
14393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014394 rettv->vval.v_number = -1;
14395 if (type == 3)
14396 {
14397 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014398 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014399 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014400 }
14401 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403 rettv->v_type = VAR_STRING;
14404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014407 if (argvars[0].v_type == VAR_LIST)
14408 {
14409 if ((l = argvars[0].vval.v_list) == NULL)
14410 goto theend;
14411 li = l->lv_first;
14412 }
14413 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014414 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014415 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014416 len = (long)STRLEN(str);
14417 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14420 if (pat == NULL)
14421 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014422
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014423 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425 int error = FALSE;
14426
14427 start = get_tv_number_chk(&argvars[2], &error);
14428 if (error)
14429 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014430 if (l != NULL)
14431 {
14432 li = list_find(l, start);
14433 if (li == NULL)
14434 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014435 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014436 }
14437 else
14438 {
14439 if (start < 0)
14440 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014441 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014442 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014443 /* When "count" argument is there ignore matches before "start",
14444 * otherwise skip part of the string. Differs when pattern is "^"
14445 * or "\<". */
14446 if (argvars[3].v_type != VAR_UNKNOWN)
14447 startcol = start;
14448 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014449 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014450 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014451 len -= start;
14452 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014453 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014454
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014455 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014456 nth = get_tv_number_chk(&argvars[3], &error);
14457 if (error)
14458 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 }
14460
14461 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14462 if (regmatch.regprog != NULL)
14463 {
14464 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014465
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014466 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014467 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014468 if (l != NULL)
14469 {
14470 if (li == NULL)
14471 {
14472 match = FALSE;
14473 break;
14474 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014475 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014476 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014477 if (str == NULL)
14478 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014479 }
14480
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014481 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014482
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014483 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014484 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014485 if (l == NULL && !match)
14486 break;
14487
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014488 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014489 if (l != NULL)
14490 {
14491 li = li->li_next;
14492 ++idx;
14493 }
14494 else
14495 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014496#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014497 startcol = (colnr_T)(regmatch.startp[0]
14498 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014499#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014500 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014501#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014502 if (startcol > (colnr_T)len
14503 || str + startcol <= regmatch.startp[0])
14504 {
14505 match = FALSE;
14506 break;
14507 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014508 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014509 }
14510
14511 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014513 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014514 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014515 int i;
14516
14517 /* return list with matched string and submatches */
14518 for (i = 0; i < NSUBEXP; ++i)
14519 {
14520 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014521 {
14522 if (list_append_string(rettv->vval.v_list,
14523 (char_u *)"", 0) == FAIL)
14524 break;
14525 }
14526 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014527 regmatch.startp[i],
14528 (int)(regmatch.endp[i] - regmatch.startp[i]))
14529 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014530 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014531 }
14532 }
14533 else if (type == 2)
14534 {
14535 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014536 if (l != NULL)
14537 copy_tv(&li->li_tv, rettv);
14538 else
14539 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014541 }
14542 else if (l != NULL)
14543 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 else
14545 {
14546 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 (varnumber_T)(regmatch.startp[0] - str);
14549 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014552 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 }
14554 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014555 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 }
14557
14558theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014559 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 p_cpo = save_cpo;
14561}
14562
14563/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014564 * "match()" function
14565 */
14566 static void
14567f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014568 typval_T *argvars;
14569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570{
14571 find_some_match(argvars, rettv, 1);
14572}
14573
14574/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014575 * "matchadd()" function
14576 */
14577 static void
14578f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014579 typval_T *argvars UNUSED;
14580 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014581{
14582#ifdef FEAT_SEARCH_EXTRA
14583 char_u buf[NUMBUFLEN];
14584 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14585 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14586 int prio = 10; /* default priority */
14587 int id = -1;
14588 int error = FALSE;
14589
14590 rettv->vval.v_number = -1;
14591
14592 if (grp == NULL || pat == NULL)
14593 return;
14594 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014595 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014596 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014597 if (argvars[3].v_type != VAR_UNKNOWN)
14598 id = get_tv_number_chk(&argvars[3], &error);
14599 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014600 if (error == TRUE)
14601 return;
14602 if (id >= 1 && id <= 3)
14603 {
14604 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14605 return;
14606 }
14607
Bram Moolenaarb3414592014-06-17 17:48:32 +020014608 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14609#endif
14610}
14611
14612/*
14613 * "matchaddpos()" function
14614 */
14615 static void
14616f_matchaddpos(argvars, rettv)
14617 typval_T *argvars UNUSED;
14618 typval_T *rettv UNUSED;
14619{
14620#ifdef FEAT_SEARCH_EXTRA
14621 char_u buf[NUMBUFLEN];
14622 char_u *group;
14623 int prio = 10;
14624 int id = -1;
14625 int error = FALSE;
14626 list_T *l;
14627
14628 rettv->vval.v_number = -1;
14629
14630 group = get_tv_string_buf_chk(&argvars[0], buf);
14631 if (group == NULL)
14632 return;
14633
14634 if (argvars[1].v_type != VAR_LIST)
14635 {
14636 EMSG2(_(e_listarg), "matchaddpos()");
14637 return;
14638 }
14639 l = argvars[1].vval.v_list;
14640 if (l == NULL)
14641 return;
14642
14643 if (argvars[2].v_type != VAR_UNKNOWN)
14644 {
14645 prio = get_tv_number_chk(&argvars[2], &error);
14646 if (argvars[3].v_type != VAR_UNKNOWN)
14647 id = get_tv_number_chk(&argvars[3], &error);
14648 }
14649 if (error == TRUE)
14650 return;
14651
14652 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14653 if (id == 1 || id == 2)
14654 {
14655 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14656 return;
14657 }
14658
14659 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014660#endif
14661}
14662
14663/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014664 * "matcharg()" function
14665 */
14666 static void
14667f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014668 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014669 typval_T *rettv;
14670{
14671 if (rettv_list_alloc(rettv) == OK)
14672 {
14673#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014674 int id = get_tv_number(&argvars[0]);
14675 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014676
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014677 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014678 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014679 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14680 {
14681 list_append_string(rettv->vval.v_list,
14682 syn_id2name(m->hlg_id), -1);
14683 list_append_string(rettv->vval.v_list, m->pattern, -1);
14684 }
14685 else
14686 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014687 list_append_string(rettv->vval.v_list, NULL, -1);
14688 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014689 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014690 }
14691#endif
14692 }
14693}
14694
14695/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014696 * "matchdelete()" function
14697 */
14698 static void
14699f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014700 typval_T *argvars UNUSED;
14701 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014702{
14703#ifdef FEAT_SEARCH_EXTRA
14704 rettv->vval.v_number = match_delete(curwin,
14705 (int)get_tv_number(&argvars[0]), TRUE);
14706#endif
14707}
14708
14709/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014710 * "matchend()" function
14711 */
14712 static void
14713f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014714 typval_T *argvars;
14715 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716{
14717 find_some_match(argvars, rettv, 0);
14718}
14719
14720/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014721 * "matchlist()" function
14722 */
14723 static void
14724f_matchlist(argvars, rettv)
14725 typval_T *argvars;
14726 typval_T *rettv;
14727{
14728 find_some_match(argvars, rettv, 3);
14729}
14730
14731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014732 * "matchstr()" function
14733 */
14734 static void
14735f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
14737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738{
14739 find_some_match(argvars, rettv, 2);
14740}
14741
Bram Moolenaar33570922005-01-25 22:26:29 +000014742static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014743
14744 static void
14745max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014746 typval_T *argvars;
14747 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014748 int domax;
14749{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014750 long n = 0;
14751 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014752 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014753
14754 if (argvars[0].v_type == VAR_LIST)
14755 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014756 list_T *l;
14757 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014758
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014759 l = argvars[0].vval.v_list;
14760 if (l != NULL)
14761 {
14762 li = l->lv_first;
14763 if (li != NULL)
14764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014765 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014766 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014767 {
14768 li = li->li_next;
14769 if (li == NULL)
14770 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014771 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014772 if (domax ? i > n : i < n)
14773 n = i;
14774 }
14775 }
14776 }
14777 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014778 else if (argvars[0].v_type == VAR_DICT)
14779 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014781 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014782 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014783 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014784
14785 d = argvars[0].vval.v_dict;
14786 if (d != NULL)
14787 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014788 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014789 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014790 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014791 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014792 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014793 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014795 if (first)
14796 {
14797 n = i;
14798 first = FALSE;
14799 }
14800 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014801 n = i;
14802 }
14803 }
14804 }
14805 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014806 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014807 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014809}
14810
14811/*
14812 * "max()" function
14813 */
14814 static void
14815f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014816 typval_T *argvars;
14817 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014818{
14819 max_min(argvars, rettv, TRUE);
14820}
14821
14822/*
14823 * "min()" function
14824 */
14825 static void
14826f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014827 typval_T *argvars;
14828 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014829{
14830 max_min(argvars, rettv, FALSE);
14831}
14832
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014833static int mkdir_recurse __ARGS((char_u *dir, int prot));
14834
14835/*
14836 * Create the directory in which "dir" is located, and higher levels when
14837 * needed.
14838 */
14839 static int
14840mkdir_recurse(dir, prot)
14841 char_u *dir;
14842 int prot;
14843{
14844 char_u *p;
14845 char_u *updir;
14846 int r = FAIL;
14847
14848 /* Get end of directory name in "dir".
14849 * We're done when it's "/" or "c:/". */
14850 p = gettail_sep(dir);
14851 if (p <= get_past_head(dir))
14852 return OK;
14853
14854 /* If the directory exists we're done. Otherwise: create it.*/
14855 updir = vim_strnsave(dir, (int)(p - dir));
14856 if (updir == NULL)
14857 return FAIL;
14858 if (mch_isdir(updir))
14859 r = OK;
14860 else if (mkdir_recurse(updir, prot) == OK)
14861 r = vim_mkdir_emsg(updir, prot);
14862 vim_free(updir);
14863 return r;
14864}
14865
14866#ifdef vim_mkdir
14867/*
14868 * "mkdir()" function
14869 */
14870 static void
14871f_mkdir(argvars, rettv)
14872 typval_T *argvars;
14873 typval_T *rettv;
14874{
14875 char_u *dir;
14876 char_u buf[NUMBUFLEN];
14877 int prot = 0755;
14878
14879 rettv->vval.v_number = FAIL;
14880 if (check_restricted() || check_secure())
14881 return;
14882
14883 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014884 if (*dir == NUL)
14885 rettv->vval.v_number = FAIL;
14886 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014887 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014888 if (*gettail(dir) == NUL)
14889 /* remove trailing slashes */
14890 *gettail_sep(dir) = NUL;
14891
14892 if (argvars[1].v_type != VAR_UNKNOWN)
14893 {
14894 if (argvars[2].v_type != VAR_UNKNOWN)
14895 prot = get_tv_number_chk(&argvars[2], NULL);
14896 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14897 mkdir_recurse(dir, prot);
14898 }
14899 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014900 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014901}
14902#endif
14903
Bram Moolenaar0d660222005-01-07 21:51:51 +000014904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 * "mode()" function
14906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014909 typval_T *argvars;
14910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014912 char_u buf[3];
14913
14914 buf[1] = NUL;
14915 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 if (VIsual_active)
14918 {
14919 if (VIsual_select)
14920 buf[0] = VIsual_mode + 's' - 'v';
14921 else
14922 buf[0] = VIsual_mode;
14923 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014924 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014925 || State == CONFIRM)
14926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014928 if (State == ASKMORE)
14929 buf[1] = 'm';
14930 else if (State == CONFIRM)
14931 buf[1] = '?';
14932 }
14933 else if (State == EXTERNCMD)
14934 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 else if (State & INSERT)
14936 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014937#ifdef FEAT_VREPLACE
14938 if (State & VREPLACE_FLAG)
14939 {
14940 buf[0] = 'R';
14941 buf[1] = 'v';
14942 }
14943 else
14944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 if (State & REPLACE_FLAG)
14946 buf[0] = 'R';
14947 else
14948 buf[0] = 'i';
14949 }
14950 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014953 if (exmode_active)
14954 buf[1] = 'v';
14955 }
14956 else if (exmode_active)
14957 {
14958 buf[0] = 'c';
14959 buf[1] = 'e';
14960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014964 if (finish_op)
14965 buf[1] = 'o';
14966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014968 /* Clear out the minor mode when the argument is not a non-zero number or
14969 * non-empty string. */
14970 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014971 buf[1] = NUL;
14972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014973 rettv->vval.v_string = vim_strsave(buf);
14974 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975}
14976
Bram Moolenaar429fa852013-04-15 12:27:36 +020014977#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014978/*
14979 * "mzeval()" function
14980 */
14981 static void
14982f_mzeval(argvars, rettv)
14983 typval_T *argvars;
14984 typval_T *rettv;
14985{
14986 char_u *str;
14987 char_u buf[NUMBUFLEN];
14988
14989 str = get_tv_string_buf(&argvars[0], buf);
14990 do_mzeval(str, rettv);
14991}
Bram Moolenaar75676462013-01-30 14:55:42 +010014992
14993 void
14994mzscheme_call_vim(name, args, rettv)
14995 char_u *name;
14996 typval_T *args;
14997 typval_T *rettv;
14998{
14999 typval_T argvars[3];
15000
15001 argvars[0].v_type = VAR_STRING;
15002 argvars[0].vval.v_string = name;
15003 copy_tv(args, &argvars[1]);
15004 argvars[2].v_type = VAR_UNKNOWN;
15005 f_call(argvars, rettv);
15006 clear_tv(&argvars[1]);
15007}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015008#endif
15009
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 * "nextnonblank()" function
15012 */
15013 static void
15014f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 typval_T *argvars;
15016 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017{
15018 linenr_T lnum;
15019
15020 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015022 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015023 {
15024 lnum = 0;
15025 break;
15026 }
15027 if (*skipwhite(ml_get(lnum)) != NUL)
15028 break;
15029 }
15030 rettv->vval.v_number = lnum;
15031}
15032
15033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 * "nr2char()" function
15035 */
15036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015037f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015038 typval_T *argvars;
15039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040{
15041 char_u buf[NUMBUFLEN];
15042
15043#ifdef FEAT_MBYTE
15044 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015045 {
15046 int utf8 = 0;
15047
15048 if (argvars[1].v_type != VAR_UNKNOWN)
15049 utf8 = get_tv_number_chk(&argvars[1], NULL);
15050 if (utf8)
15051 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15052 else
15053 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 else
15056#endif
15057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015058 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 buf[1] = NUL;
15060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015061 rettv->v_type = VAR_STRING;
15062 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015063}
15064
15065/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015066 * "or(expr, expr)" function
15067 */
15068 static void
15069f_or(argvars, rettv)
15070 typval_T *argvars;
15071 typval_T *rettv;
15072{
15073 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15074 | get_tv_number_chk(&argvars[1], NULL);
15075}
15076
15077/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015078 * "pathshorten()" function
15079 */
15080 static void
15081f_pathshorten(argvars, rettv)
15082 typval_T *argvars;
15083 typval_T *rettv;
15084{
15085 char_u *p;
15086
15087 rettv->v_type = VAR_STRING;
15088 p = get_tv_string_chk(&argvars[0]);
15089 if (p == NULL)
15090 rettv->vval.v_string = NULL;
15091 else
15092 {
15093 p = vim_strsave(p);
15094 rettv->vval.v_string = p;
15095 if (p != NULL)
15096 shorten_dir(p);
15097 }
15098}
15099
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015100#ifdef FEAT_FLOAT
15101/*
15102 * "pow()" function
15103 */
15104 static void
15105f_pow(argvars, rettv)
15106 typval_T *argvars;
15107 typval_T *rettv;
15108{
15109 float_T fx, fy;
15110
15111 rettv->v_type = VAR_FLOAT;
15112 if (get_float_arg(argvars, &fx) == OK
15113 && get_float_arg(&argvars[1], &fy) == OK)
15114 rettv->vval.v_float = pow(fx, fy);
15115 else
15116 rettv->vval.v_float = 0.0;
15117}
15118#endif
15119
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015120/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121 * "prevnonblank()" function
15122 */
15123 static void
15124f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015125 typval_T *argvars;
15126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127{
15128 linenr_T lnum;
15129
15130 lnum = get_tv_lnum(argvars);
15131 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15132 lnum = 0;
15133 else
15134 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15135 --lnum;
15136 rettv->vval.v_number = lnum;
15137}
15138
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015139#ifdef HAVE_STDARG_H
15140/* This dummy va_list is here because:
15141 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15142 * - locally in the function results in a "used before set" warning
15143 * - using va_start() to initialize it gives "function with fixed args" error */
15144static va_list ap;
15145#endif
15146
Bram Moolenaar8c711452005-01-14 21:53:12 +000015147/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015148 * "printf()" function
15149 */
15150 static void
15151f_printf(argvars, rettv)
15152 typval_T *argvars;
15153 typval_T *rettv;
15154{
15155 rettv->v_type = VAR_STRING;
15156 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015157#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015158 {
15159 char_u buf[NUMBUFLEN];
15160 int len;
15161 char_u *s;
15162 int saved_did_emsg = did_emsg;
15163 char *fmt;
15164
15165 /* Get the required length, allocate the buffer and do it for real. */
15166 did_emsg = FALSE;
15167 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015168 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015169 if (!did_emsg)
15170 {
15171 s = alloc(len + 1);
15172 if (s != NULL)
15173 {
15174 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015175 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015176 }
15177 }
15178 did_emsg |= saved_did_emsg;
15179 }
15180#endif
15181}
15182
15183/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015184 * "pumvisible()" function
15185 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015186 static void
15187f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015188 typval_T *argvars UNUSED;
15189 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015190{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015191#ifdef FEAT_INS_EXPAND
15192 if (pum_visible())
15193 rettv->vval.v_number = 1;
15194#endif
15195}
15196
Bram Moolenaardb913952012-06-29 12:54:53 +020015197#ifdef FEAT_PYTHON3
15198/*
15199 * "py3eval()" function
15200 */
15201 static void
15202f_py3eval(argvars, rettv)
15203 typval_T *argvars;
15204 typval_T *rettv;
15205{
15206 char_u *str;
15207 char_u buf[NUMBUFLEN];
15208
15209 str = get_tv_string_buf(&argvars[0], buf);
15210 do_py3eval(str, rettv);
15211}
15212#endif
15213
15214#ifdef FEAT_PYTHON
15215/*
15216 * "pyeval()" function
15217 */
15218 static void
15219f_pyeval(argvars, rettv)
15220 typval_T *argvars;
15221 typval_T *rettv;
15222{
15223 char_u *str;
15224 char_u buf[NUMBUFLEN];
15225
15226 str = get_tv_string_buf(&argvars[0], buf);
15227 do_pyeval(str, rettv);
15228}
15229#endif
15230
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015232 * "range()" function
15233 */
15234 static void
15235f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015236 typval_T *argvars;
15237 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015238{
15239 long start;
15240 long end;
15241 long stride = 1;
15242 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015243 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015245 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015246 if (argvars[1].v_type == VAR_UNKNOWN)
15247 {
15248 end = start - 1;
15249 start = 0;
15250 }
15251 else
15252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015253 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015254 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015255 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015256 }
15257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015258 if (error)
15259 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015260 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015261 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015262 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015263 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015264 else
15265 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015266 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015267 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015268 if (list_append_number(rettv->vval.v_list,
15269 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015270 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015271 }
15272}
15273
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015274/*
15275 * "readfile()" function
15276 */
15277 static void
15278f_readfile(argvars, rettv)
15279 typval_T *argvars;
15280 typval_T *rettv;
15281{
15282 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015283 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015284 char_u *fname;
15285 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015286 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15287 int io_size = sizeof(buf);
15288 int readlen; /* size of last fread() */
15289 char_u *prev = NULL; /* previously read bytes, if any */
15290 long prevlen = 0; /* length of data in prev */
15291 long prevsize = 0; /* size of prev buffer */
15292 long maxline = MAXLNUM;
15293 long cnt = 0;
15294 char_u *p; /* position in buf */
15295 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015296
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015297 if (argvars[1].v_type != VAR_UNKNOWN)
15298 {
15299 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15300 binary = TRUE;
15301 if (argvars[2].v_type != VAR_UNKNOWN)
15302 maxline = get_tv_number(&argvars[2]);
15303 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015304
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015305 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015306 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015307
15308 /* Always open the file in binary mode, library functions have a mind of
15309 * their own about CR-LF conversion. */
15310 fname = get_tv_string(&argvars[0]);
15311 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15312 {
15313 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15314 return;
15315 }
15316
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015317 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015318 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015319 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015320
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015321 /* This for loop processes what was read, but is also entered at end
15322 * of file so that either:
15323 * - an incomplete line gets written
15324 * - a "binary" file gets an empty line at the end if it ends in a
15325 * newline. */
15326 for (p = buf, start = buf;
15327 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15328 ++p)
15329 {
15330 if (*p == '\n' || readlen <= 0)
15331 {
15332 listitem_T *li;
15333 char_u *s = NULL;
15334 long_u len = p - start;
15335
15336 /* Finished a line. Remove CRs before NL. */
15337 if (readlen > 0 && !binary)
15338 {
15339 while (len > 0 && start[len - 1] == '\r')
15340 --len;
15341 /* removal may cross back to the "prev" string */
15342 if (len == 0)
15343 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15344 --prevlen;
15345 }
15346 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015347 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015348 else
15349 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015350 /* Change "prev" buffer to be the right size. This way
15351 * the bytes are only copied once, and very long lines are
15352 * allocated only once. */
15353 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015354 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015355 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015356 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015357 prev = NULL; /* the list will own the string */
15358 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015359 }
15360 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015361 if (s == NULL)
15362 {
15363 do_outofmem_msg((long_u) prevlen + len + 1);
15364 failed = TRUE;
15365 break;
15366 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015367
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015368 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015369 {
15370 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015371 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015372 break;
15373 }
15374 li->li_tv.v_type = VAR_STRING;
15375 li->li_tv.v_lock = 0;
15376 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015377 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015379 start = p + 1; /* step over newline */
15380 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015381 break;
15382 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015383 else if (*p == NUL)
15384 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015385#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015386 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15387 * when finding the BF and check the previous two bytes. */
15388 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015389 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015390 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15391 * + 1, these may be in the "prev" string. */
15392 char_u back1 = p >= buf + 1 ? p[-1]
15393 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15394 char_u back2 = p >= buf + 2 ? p[-2]
15395 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15396 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015397
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015398 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015399 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015400 char_u *dest = p - 2;
15401
15402 /* Usually a BOM is at the beginning of a file, and so at
15403 * the beginning of a line; then we can just step over it.
15404 */
15405 if (start == dest)
15406 start = p + 1;
15407 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015408 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015409 /* have to shuffle buf to close gap */
15410 int adjust_prevlen = 0;
15411
15412 if (dest < buf)
15413 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015414 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015415 dest = buf;
15416 }
15417 if (readlen > p - buf + 1)
15418 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15419 readlen -= 3 - adjust_prevlen;
15420 prevlen -= adjust_prevlen;
15421 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015422 }
15423 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015424 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015425#endif
15426 } /* for */
15427
15428 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15429 break;
15430 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015431 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015432 /* There's part of a line in buf, store it in "prev". */
15433 if (p - start + prevlen >= prevsize)
15434 {
15435 /* need bigger "prev" buffer */
15436 char_u *newprev;
15437
15438 /* A common use case is ordinary text files and "prev" gets a
15439 * fragment of a line, so the first allocation is made
15440 * small, to avoid repeatedly 'allocing' large and
15441 * 'reallocing' small. */
15442 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015443 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015444 else
15445 {
15446 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015447 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015448 prevsize = grow50pc > growmin ? grow50pc : growmin;
15449 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015450 newprev = prev == NULL ? alloc(prevsize)
15451 : vim_realloc(prev, prevsize);
15452 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015453 {
15454 do_outofmem_msg((long_u)prevsize);
15455 failed = TRUE;
15456 break;
15457 }
15458 prev = newprev;
15459 }
15460 /* Add the line part to end of "prev". */
15461 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015462 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015463 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015464 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015465
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015466 /*
15467 * For a negative line count use only the lines at the end of the file,
15468 * free the rest.
15469 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015470 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015471 while (cnt > -maxline)
15472 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015473 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015474 --cnt;
15475 }
15476
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015477 if (failed)
15478 {
15479 list_free(rettv->vval.v_list, TRUE);
15480 /* readfile doc says an empty list is returned on error */
15481 rettv->vval.v_list = list_alloc();
15482 }
15483
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015484 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015485 fclose(fd);
15486}
15487
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015488#if defined(FEAT_RELTIME)
15489static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15490
15491/*
15492 * Convert a List to proftime_T.
15493 * Return FAIL when there is something wrong.
15494 */
15495 static int
15496list2proftime(arg, tm)
15497 typval_T *arg;
15498 proftime_T *tm;
15499{
15500 long n1, n2;
15501 int error = FALSE;
15502
15503 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15504 || arg->vval.v_list->lv_len != 2)
15505 return FAIL;
15506 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15507 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15508# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015509 tm->HighPart = n1;
15510 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015511# else
15512 tm->tv_sec = n1;
15513 tm->tv_usec = n2;
15514# endif
15515 return error ? FAIL : OK;
15516}
15517#endif /* FEAT_RELTIME */
15518
15519/*
15520 * "reltime()" function
15521 */
15522 static void
15523f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015524 typval_T *argvars UNUSED;
15525 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015526{
15527#ifdef FEAT_RELTIME
15528 proftime_T res;
15529 proftime_T start;
15530
15531 if (argvars[0].v_type == VAR_UNKNOWN)
15532 {
15533 /* No arguments: get current time. */
15534 profile_start(&res);
15535 }
15536 else if (argvars[1].v_type == VAR_UNKNOWN)
15537 {
15538 if (list2proftime(&argvars[0], &res) == FAIL)
15539 return;
15540 profile_end(&res);
15541 }
15542 else
15543 {
15544 /* Two arguments: compute the difference. */
15545 if (list2proftime(&argvars[0], &start) == FAIL
15546 || list2proftime(&argvars[1], &res) == FAIL)
15547 return;
15548 profile_sub(&res, &start);
15549 }
15550
15551 if (rettv_list_alloc(rettv) == OK)
15552 {
15553 long n1, n2;
15554
15555# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015556 n1 = res.HighPart;
15557 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015558# else
15559 n1 = res.tv_sec;
15560 n2 = res.tv_usec;
15561# endif
15562 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15563 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15564 }
15565#endif
15566}
15567
15568/*
15569 * "reltimestr()" function
15570 */
15571 static void
15572f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015573 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015574 typval_T *rettv;
15575{
15576#ifdef FEAT_RELTIME
15577 proftime_T tm;
15578#endif
15579
15580 rettv->v_type = VAR_STRING;
15581 rettv->vval.v_string = NULL;
15582#ifdef FEAT_RELTIME
15583 if (list2proftime(&argvars[0], &tm) == OK)
15584 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15585#endif
15586}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015587
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15589static void make_connection __ARGS((void));
15590static int check_connection __ARGS((void));
15591
15592 static void
15593make_connection()
15594{
15595 if (X_DISPLAY == NULL
15596# ifdef FEAT_GUI
15597 && !gui.in_use
15598# endif
15599 )
15600 {
15601 x_force_connect = TRUE;
15602 setup_term_clip();
15603 x_force_connect = FALSE;
15604 }
15605}
15606
15607 static int
15608check_connection()
15609{
15610 make_connection();
15611 if (X_DISPLAY == NULL)
15612 {
15613 EMSG(_("E240: No connection to Vim server"));
15614 return FAIL;
15615 }
15616 return OK;
15617}
15618#endif
15619
15620#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015621static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015622
15623 static void
15624remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015625 typval_T *argvars;
15626 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015627 int expr;
15628{
15629 char_u *server_name;
15630 char_u *keys;
15631 char_u *r = NULL;
15632 char_u buf[NUMBUFLEN];
15633# ifdef WIN32
15634 HWND w;
15635# else
15636 Window w;
15637# endif
15638
15639 if (check_restricted() || check_secure())
15640 return;
15641
15642# ifdef FEAT_X11
15643 if (check_connection() == FAIL)
15644 return;
15645# endif
15646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015647 server_name = get_tv_string_chk(&argvars[0]);
15648 if (server_name == NULL)
15649 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015650 keys = get_tv_string_buf(&argvars[1], buf);
15651# ifdef WIN32
15652 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15653# else
15654 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15655 < 0)
15656# endif
15657 {
15658 if (r != NULL)
15659 EMSG(r); /* sending worked but evaluation failed */
15660 else
15661 EMSG2(_("E241: Unable to send to %s"), server_name);
15662 return;
15663 }
15664
15665 rettv->vval.v_string = r;
15666
15667 if (argvars[2].v_type != VAR_UNKNOWN)
15668 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015669 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015670 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015671 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015672
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015673 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015674 v.di_tv.v_type = VAR_STRING;
15675 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015676 idvar = get_tv_string_chk(&argvars[2]);
15677 if (idvar != NULL)
15678 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015679 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680 }
15681}
15682#endif
15683
15684/*
15685 * "remote_expr()" function
15686 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015687 static void
15688f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015690 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015691{
15692 rettv->v_type = VAR_STRING;
15693 rettv->vval.v_string = NULL;
15694#ifdef FEAT_CLIENTSERVER
15695 remote_common(argvars, rettv, TRUE);
15696#endif
15697}
15698
15699/*
15700 * "remote_foreground()" function
15701 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702 static void
15703f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015704 typval_T *argvars UNUSED;
15705 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015707#ifdef FEAT_CLIENTSERVER
15708# ifdef WIN32
15709 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015710 {
15711 char_u *server_name = get_tv_string_chk(&argvars[0]);
15712
15713 if (server_name != NULL)
15714 serverForeground(server_name);
15715 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716# else
15717 /* Send a foreground() expression to the server. */
15718 argvars[1].v_type = VAR_STRING;
15719 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15720 argvars[2].v_type = VAR_UNKNOWN;
15721 remote_common(argvars, rettv, TRUE);
15722 vim_free(argvars[1].vval.v_string);
15723# endif
15724#endif
15725}
15726
Bram Moolenaar0d660222005-01-07 21:51:51 +000015727 static void
15728f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731{
15732#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015733 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734 char_u *s = NULL;
15735# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015736 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015737# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015738 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739
15740 if (check_restricted() || check_secure())
15741 {
15742 rettv->vval.v_number = -1;
15743 return;
15744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015745 serverid = get_tv_string_chk(&argvars[0]);
15746 if (serverid == NULL)
15747 {
15748 rettv->vval.v_number = -1;
15749 return; /* type error; errmsg already given */
15750 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015752 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 if (n == 0)
15754 rettv->vval.v_number = -1;
15755 else
15756 {
15757 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15758 rettv->vval.v_number = (s != NULL);
15759 }
15760# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015761 if (check_connection() == FAIL)
15762 return;
15763
15764 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015765 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015766# endif
15767
15768 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015770 char_u *retvar;
15771
Bram Moolenaar33570922005-01-25 22:26:29 +000015772 v.di_tv.v_type = VAR_STRING;
15773 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015774 retvar = get_tv_string_chk(&argvars[1]);
15775 if (retvar != NULL)
15776 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778 }
15779#else
15780 rettv->vval.v_number = -1;
15781#endif
15782}
15783
Bram Moolenaar0d660222005-01-07 21:51:51 +000015784 static void
15785f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015786 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015787 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788{
15789 char_u *r = NULL;
15790
15791#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015792 char_u *serverid = get_tv_string_chk(&argvars[0]);
15793
15794 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015795 {
15796# ifdef WIN32
15797 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015798 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015799
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015800 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015801 if (n != 0)
15802 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15803 if (r == NULL)
15804# else
15805 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015806 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807# endif
15808 EMSG(_("E277: Unable to read a server reply"));
15809 }
15810#endif
15811 rettv->v_type = VAR_STRING;
15812 rettv->vval.v_string = r;
15813}
15814
15815/*
15816 * "remote_send()" function
15817 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818 static void
15819f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015820 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015821 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822{
15823 rettv->v_type = VAR_STRING;
15824 rettv->vval.v_string = NULL;
15825#ifdef FEAT_CLIENTSERVER
15826 remote_common(argvars, rettv, FALSE);
15827#endif
15828}
15829
15830/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015831 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015832 */
15833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015834f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015835 typval_T *argvars;
15836 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015837{
Bram Moolenaar33570922005-01-25 22:26:29 +000015838 list_T *l;
15839 listitem_T *item, *item2;
15840 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015841 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015842 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015843 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015844 dict_T *d;
15845 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015846 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015847
Bram Moolenaar8c711452005-01-14 21:53:12 +000015848 if (argvars[0].v_type == VAR_DICT)
15849 {
15850 if (argvars[2].v_type != VAR_UNKNOWN)
15851 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015852 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015853 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015855 key = get_tv_string_chk(&argvars[1]);
15856 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015858 di = dict_find(d, key, -1);
15859 if (di == NULL)
15860 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015861 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15862 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015863 {
15864 *rettv = di->di_tv;
15865 init_tv(&di->di_tv);
15866 dictitem_remove(d, di);
15867 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015869 }
15870 }
15871 else if (argvars[0].v_type != VAR_LIST)
15872 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015873 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015874 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015876 int error = FALSE;
15877
15878 idx = get_tv_number_chk(&argvars[1], &error);
15879 if (error)
15880 ; /* type error: do nothing, errmsg already given */
15881 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015882 EMSGN(_(e_listidx), idx);
15883 else
15884 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015885 if (argvars[2].v_type == VAR_UNKNOWN)
15886 {
15887 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015888 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015889 *rettv = item->li_tv;
15890 vim_free(item);
15891 }
15892 else
15893 {
15894 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 end = get_tv_number_chk(&argvars[2], &error);
15896 if (error)
15897 ; /* type error: do nothing */
15898 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015899 EMSGN(_(e_listidx), end);
15900 else
15901 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015902 int cnt = 0;
15903
15904 for (li = item; li != NULL; li = li->li_next)
15905 {
15906 ++cnt;
15907 if (li == item2)
15908 break;
15909 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015910 if (li == NULL) /* didn't find "item2" after "item" */
15911 EMSG(_(e_invrange));
15912 else
15913 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015914 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015915 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015916 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015917 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015918 l->lv_first = item;
15919 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015920 item->li_prev = NULL;
15921 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015922 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015923 }
15924 }
15925 }
15926 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015927 }
15928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929}
15930
15931/*
15932 * "rename({from}, {to})" function
15933 */
15934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015935f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015936 typval_T *argvars;
15937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938{
15939 char_u buf[NUMBUFLEN];
15940
15941 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015942 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015944 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15945 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946}
15947
15948/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015949 * "repeat()" function
15950 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015951 static void
15952f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015953 typval_T *argvars;
15954 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015955{
15956 char_u *p;
15957 int n;
15958 int slen;
15959 int len;
15960 char_u *r;
15961 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015962
15963 n = get_tv_number(&argvars[1]);
15964 if (argvars[0].v_type == VAR_LIST)
15965 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015966 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015967 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015968 if (list_extend(rettv->vval.v_list,
15969 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015970 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015971 }
15972 else
15973 {
15974 p = get_tv_string(&argvars[0]);
15975 rettv->v_type = VAR_STRING;
15976 rettv->vval.v_string = NULL;
15977
15978 slen = (int)STRLEN(p);
15979 len = slen * n;
15980 if (len <= 0)
15981 return;
15982
15983 r = alloc(len + 1);
15984 if (r != NULL)
15985 {
15986 for (i = 0; i < n; i++)
15987 mch_memmove(r + i * slen, p, (size_t)slen);
15988 r[len] = NUL;
15989 }
15990
15991 rettv->vval.v_string = r;
15992 }
15993}
15994
15995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996 * "resolve()" function
15997 */
15998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015999f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 typval_T *argvars;
16001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002{
16003 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016004#ifdef HAVE_READLINK
16005 char_u *buf = NULL;
16006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016008 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009#ifdef FEAT_SHORTCUT
16010 {
16011 char_u *v = NULL;
16012
16013 v = mch_resolve_shortcut(p);
16014 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016015 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016017 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 }
16019#else
16020# ifdef HAVE_READLINK
16021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 char_u *cpy;
16023 int len;
16024 char_u *remain = NULL;
16025 char_u *q;
16026 int is_relative_to_current = FALSE;
16027 int has_trailing_pathsep = FALSE;
16028 int limit = 100;
16029
16030 p = vim_strsave(p);
16031
16032 if (p[0] == '.' && (vim_ispathsep(p[1])
16033 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16034 is_relative_to_current = TRUE;
16035
16036 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016037 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016040 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042
16043 q = getnextcomp(p);
16044 if (*q != NUL)
16045 {
16046 /* Separate the first path component in "p", and keep the
16047 * remainder (beginning with the path separator). */
16048 remain = vim_strsave(q - 1);
16049 q[-1] = NUL;
16050 }
16051
Bram Moolenaard9462e32011-04-11 21:35:11 +020016052 buf = alloc(MAXPATHL + 1);
16053 if (buf == NULL)
16054 goto fail;
16055
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 for (;;)
16057 {
16058 for (;;)
16059 {
16060 len = readlink((char *)p, (char *)buf, MAXPATHL);
16061 if (len <= 0)
16062 break;
16063 buf[len] = NUL;
16064
16065 if (limit-- == 0)
16066 {
16067 vim_free(p);
16068 vim_free(remain);
16069 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 goto fail;
16072 }
16073
16074 /* Ensure that the result will have a trailing path separator
16075 * if the argument has one. */
16076 if (remain == NULL && has_trailing_pathsep)
16077 add_pathsep(buf);
16078
16079 /* Separate the first path component in the link value and
16080 * concatenate the remainders. */
16081 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16082 if (*q != NUL)
16083 {
16084 if (remain == NULL)
16085 remain = vim_strsave(q - 1);
16086 else
16087 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016088 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 if (cpy != NULL)
16090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 vim_free(remain);
16092 remain = cpy;
16093 }
16094 }
16095 q[-1] = NUL;
16096 }
16097
16098 q = gettail(p);
16099 if (q > p && *q == NUL)
16100 {
16101 /* Ignore trailing path separator. */
16102 q[-1] = NUL;
16103 q = gettail(p);
16104 }
16105 if (q > p && !mch_isFullName(buf))
16106 {
16107 /* symlink is relative to directory of argument */
16108 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16109 if (cpy != NULL)
16110 {
16111 STRCPY(cpy, p);
16112 STRCPY(gettail(cpy), buf);
16113 vim_free(p);
16114 p = cpy;
16115 }
16116 }
16117 else
16118 {
16119 vim_free(p);
16120 p = vim_strsave(buf);
16121 }
16122 }
16123
16124 if (remain == NULL)
16125 break;
16126
16127 /* Append the first path component of "remain" to "p". */
16128 q = getnextcomp(remain + 1);
16129 len = q - remain - (*q != NUL);
16130 cpy = vim_strnsave(p, STRLEN(p) + len);
16131 if (cpy != NULL)
16132 {
16133 STRNCAT(cpy, remain, len);
16134 vim_free(p);
16135 p = cpy;
16136 }
16137 /* Shorten "remain". */
16138 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016139 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 else
16141 {
16142 vim_free(remain);
16143 remain = NULL;
16144 }
16145 }
16146
16147 /* If the result is a relative path name, make it explicitly relative to
16148 * the current directory if and only if the argument had this form. */
16149 if (!vim_ispathsep(*p))
16150 {
16151 if (is_relative_to_current
16152 && *p != NUL
16153 && !(p[0] == '.'
16154 && (p[1] == NUL
16155 || vim_ispathsep(p[1])
16156 || (p[1] == '.'
16157 && (p[2] == NUL
16158 || vim_ispathsep(p[2]))))))
16159 {
16160 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016161 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 if (cpy != NULL)
16163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 vim_free(p);
16165 p = cpy;
16166 }
16167 }
16168 else if (!is_relative_to_current)
16169 {
16170 /* Strip leading "./". */
16171 q = p;
16172 while (q[0] == '.' && vim_ispathsep(q[1]))
16173 q += 2;
16174 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016175 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 }
16177 }
16178
16179 /* Ensure that the result will have no trailing path separator
16180 * if the argument had none. But keep "/" or "//". */
16181 if (!has_trailing_pathsep)
16182 {
16183 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016184 if (after_pathsep(p, q))
16185 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 }
16187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016188 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 }
16190# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016191 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192# endif
16193#endif
16194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016195 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196
16197#ifdef HAVE_READLINK
16198fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016199 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016201 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202}
16203
16204/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 */
16207 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016208f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016209 typval_T *argvars;
16210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211{
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 list_T *l;
16213 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215 if (argvars[0].v_type != VAR_LIST)
16216 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016217 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016218 && !tv_check_lock(l->lv_lock,
16219 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016220 {
16221 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016222 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016223 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224 while (li != NULL)
16225 {
16226 ni = li->li_prev;
16227 list_append(l, li);
16228 li = ni;
16229 }
16230 rettv->vval.v_list = l;
16231 rettv->v_type = VAR_LIST;
16232 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016233 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235}
16236
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016237#define SP_NOMOVE 0x01 /* don't move cursor */
16238#define SP_REPEAT 0x02 /* repeat to find outer pair */
16239#define SP_RETCOUNT 0x04 /* return matchcount */
16240#define SP_SETPCMARK 0x08 /* set previous context mark */
16241#define SP_START 0x10 /* accept match at start position */
16242#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16243#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016244
Bram Moolenaar33570922005-01-25 22:26:29 +000016245static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246
16247/*
16248 * Get flags for a search function.
16249 * Possibly sets "p_ws".
16250 * Returns BACKWARD, FORWARD or zero (for an error).
16251 */
16252 static int
16253get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 int *flagsp;
16256{
16257 int dir = FORWARD;
16258 char_u *flags;
16259 char_u nbuf[NUMBUFLEN];
16260 int mask;
16261
16262 if (varp->v_type != VAR_UNKNOWN)
16263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 flags = get_tv_string_buf_chk(varp, nbuf);
16265 if (flags == NULL)
16266 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016267 while (*flags != NUL)
16268 {
16269 switch (*flags)
16270 {
16271 case 'b': dir = BACKWARD; break;
16272 case 'w': p_ws = TRUE; break;
16273 case 'W': p_ws = FALSE; break;
16274 default: mask = 0;
16275 if (flagsp != NULL)
16276 switch (*flags)
16277 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016278 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016279 case 'e': mask = SP_END; break;
16280 case 'm': mask = SP_RETCOUNT; break;
16281 case 'n': mask = SP_NOMOVE; break;
16282 case 'p': mask = SP_SUBPAT; break;
16283 case 'r': mask = SP_REPEAT; break;
16284 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016285 }
16286 if (mask == 0)
16287 {
16288 EMSG2(_(e_invarg2), flags);
16289 dir = 0;
16290 }
16291 else
16292 *flagsp |= mask;
16293 }
16294 if (dir == 0)
16295 break;
16296 ++flags;
16297 }
16298 }
16299 return dir;
16300}
16301
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016303 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016305 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016306search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016307 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016308 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016309 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016311 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 char_u *pat;
16313 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016314 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 int save_p_ws = p_ws;
16316 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016317 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016318 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016319 proftime_T tm;
16320#ifdef FEAT_RELTIME
16321 long time_limit = 0;
16322#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016323 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016324 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016326 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016327 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016328 if (dir == 0)
16329 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016330 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016331 if (flags & SP_START)
16332 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016333 if (flags & SP_END)
16334 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016335
Bram Moolenaar76929292008-01-06 19:07:36 +000016336 /* Optional arguments: line number to stop searching and timeout. */
16337 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016338 {
16339 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16340 if (lnum_stop < 0)
16341 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016342#ifdef FEAT_RELTIME
16343 if (argvars[3].v_type != VAR_UNKNOWN)
16344 {
16345 time_limit = get_tv_number_chk(&argvars[3], NULL);
16346 if (time_limit < 0)
16347 goto theend;
16348 }
16349#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016350 }
16351
Bram Moolenaar76929292008-01-06 19:07:36 +000016352#ifdef FEAT_RELTIME
16353 /* Set the time limit, if there is one. */
16354 profile_setlimit(time_limit, &tm);
16355#endif
16356
Bram Moolenaar231334e2005-07-25 20:46:57 +000016357 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016358 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016359 * Check to make sure only those flags are set.
16360 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16361 * flags cannot be set. Check for that condition also.
16362 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016363 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016364 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016367 goto theend;
16368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016370 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016371 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016372 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016373 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016375 if (flags & SP_SUBPAT)
16376 retval = subpatnum;
16377 else
16378 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016379 if (flags & SP_SETPCMARK)
16380 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016382 if (match_pos != NULL)
16383 {
16384 /* Store the match cursor position */
16385 match_pos->lnum = pos.lnum;
16386 match_pos->col = pos.col + 1;
16387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 /* "/$" will put the cursor after the end of the line, may need to
16389 * correct that here */
16390 check_cursor();
16391 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016392
16393 /* If 'n' flag is used: restore cursor position. */
16394 if (flags & SP_NOMOVE)
16395 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016396 else
16397 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016398theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016400
16401 return retval;
16402}
16403
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016404#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016405
16406/*
16407 * round() is not in C90, use ceil() or floor() instead.
16408 */
16409 float_T
16410vim_round(f)
16411 float_T f;
16412{
16413 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16414}
16415
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016416/*
16417 * "round({float})" function
16418 */
16419 static void
16420f_round(argvars, rettv)
16421 typval_T *argvars;
16422 typval_T *rettv;
16423{
16424 float_T f;
16425
16426 rettv->v_type = VAR_FLOAT;
16427 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016428 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016429 else
16430 rettv->vval.v_float = 0.0;
16431}
16432#endif
16433
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016434/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016435 * "screenattr()" function
16436 */
16437 static void
16438f_screenattr(argvars, rettv)
16439 typval_T *argvars UNUSED;
16440 typval_T *rettv;
16441{
16442 int row;
16443 int col;
16444 int c;
16445
16446 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16447 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16448 if (row < 0 || row >= screen_Rows
16449 || col < 0 || col >= screen_Columns)
16450 c = -1;
16451 else
16452 c = ScreenAttrs[LineOffset[row] + col];
16453 rettv->vval.v_number = c;
16454}
16455
16456/*
16457 * "screenchar()" function
16458 */
16459 static void
16460f_screenchar(argvars, rettv)
16461 typval_T *argvars UNUSED;
16462 typval_T *rettv;
16463{
16464 int row;
16465 int col;
16466 int off;
16467 int c;
16468
16469 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16470 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16471 if (row < 0 || row >= screen_Rows
16472 || col < 0 || col >= screen_Columns)
16473 c = -1;
16474 else
16475 {
16476 off = LineOffset[row] + col;
16477#ifdef FEAT_MBYTE
16478 if (enc_utf8 && ScreenLinesUC[off] != 0)
16479 c = ScreenLinesUC[off];
16480 else
16481#endif
16482 c = ScreenLines[off];
16483 }
16484 rettv->vval.v_number = c;
16485}
16486
16487/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016488 * "screencol()" function
16489 *
16490 * First column is 1 to be consistent with virtcol().
16491 */
16492 static void
16493f_screencol(argvars, rettv)
16494 typval_T *argvars UNUSED;
16495 typval_T *rettv;
16496{
16497 rettv->vval.v_number = screen_screencol() + 1;
16498}
16499
16500/*
16501 * "screenrow()" function
16502 */
16503 static void
16504f_screenrow(argvars, rettv)
16505 typval_T *argvars UNUSED;
16506 typval_T *rettv;
16507{
16508 rettv->vval.v_number = screen_screenrow() + 1;
16509}
16510
16511/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016512 * "search()" function
16513 */
16514 static void
16515f_search(argvars, rettv)
16516 typval_T *argvars;
16517 typval_T *rettv;
16518{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016519 int flags = 0;
16520
16521 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522}
16523
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016525 * "searchdecl()" function
16526 */
16527 static void
16528f_searchdecl(argvars, rettv)
16529 typval_T *argvars;
16530 typval_T *rettv;
16531{
16532 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016533 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016534 int error = FALSE;
16535 char_u *name;
16536
16537 rettv->vval.v_number = 1; /* default: FAIL */
16538
16539 name = get_tv_string_chk(&argvars[0]);
16540 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016541 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016542 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016543 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16544 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16545 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016546 if (!error && name != NULL)
16547 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016548 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016549}
16550
16551/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016552 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016554 static int
16555searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016556 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016557 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558{
16559 char_u *spat, *mpat, *epat;
16560 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 int dir;
16563 int flags = 0;
16564 char_u nbuf1[NUMBUFLEN];
16565 char_u nbuf2[NUMBUFLEN];
16566 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016567 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016568 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016569 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016572 spat = get_tv_string_chk(&argvars[0]);
16573 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16574 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16575 if (spat == NULL || mpat == NULL || epat == NULL)
16576 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016577
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 /* Handle the optional fourth argument: flags */
16579 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016580 if (dir == 0)
16581 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016582
16583 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016584 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16585 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016586 if ((flags & (SP_END | SP_SUBPAT)) != 0
16587 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016588 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016589 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016590 goto theend;
16591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016593 /* Using 'r' implies 'W', otherwise it doesn't work. */
16594 if (flags & SP_REPEAT)
16595 p_ws = FALSE;
16596
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016597 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016598 if (argvars[3].v_type == VAR_UNKNOWN
16599 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 skip = (char_u *)"";
16601 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016603 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016604 if (argvars[5].v_type != VAR_UNKNOWN)
16605 {
16606 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16607 if (lnum_stop < 0)
16608 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016609#ifdef FEAT_RELTIME
16610 if (argvars[6].v_type != VAR_UNKNOWN)
16611 {
16612 time_limit = get_tv_number_chk(&argvars[6], NULL);
16613 if (time_limit < 0)
16614 goto theend;
16615 }
16616#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016617 }
16618 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016619 if (skip == NULL)
16620 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016622 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016623 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016624
16625theend:
16626 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016627
16628 return retval;
16629}
16630
16631/*
16632 * "searchpair()" function
16633 */
16634 static void
16635f_searchpair(argvars, rettv)
16636 typval_T *argvars;
16637 typval_T *rettv;
16638{
16639 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16640}
16641
16642/*
16643 * "searchpairpos()" function
16644 */
16645 static void
16646f_searchpairpos(argvars, rettv)
16647 typval_T *argvars;
16648 typval_T *rettv;
16649{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016650 pos_T match_pos;
16651 int lnum = 0;
16652 int col = 0;
16653
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016654 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016655 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016656
16657 if (searchpair_cmn(argvars, &match_pos) > 0)
16658 {
16659 lnum = match_pos.lnum;
16660 col = match_pos.col;
16661 }
16662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016663 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16664 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016665}
16666
16667/*
16668 * Search for a start/middle/end thing.
16669 * Used by searchpair(), see its documentation for the details.
16670 * Returns 0 or -1 for no match,
16671 */
16672 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016673do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16674 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016675 char_u *spat; /* start pattern */
16676 char_u *mpat; /* middle pattern */
16677 char_u *epat; /* end pattern */
16678 int dir; /* BACKWARD or FORWARD */
16679 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016680 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016681 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016682 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016683 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016684{
16685 char_u *save_cpo;
16686 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16687 long retval = 0;
16688 pos_T pos;
16689 pos_T firstpos;
16690 pos_T foundpos;
16691 pos_T save_cursor;
16692 pos_T save_pos;
16693 int n;
16694 int r;
16695 int nest = 1;
16696 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016697 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016698 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016699
16700 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16701 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016702 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016703
Bram Moolenaar76929292008-01-06 19:07:36 +000016704#ifdef FEAT_RELTIME
16705 /* Set the time limit, if there is one. */
16706 profile_setlimit(time_limit, &tm);
16707#endif
16708
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016709 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16710 * start/middle/end (pat3, for the top pair). */
16711 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16712 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16713 if (pat2 == NULL || pat3 == NULL)
16714 goto theend;
16715 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16716 if (*mpat == NUL)
16717 STRCPY(pat3, pat2);
16718 else
16719 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16720 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016721 if (flags & SP_START)
16722 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016723
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 save_cursor = curwin->w_cursor;
16725 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016726 clearpos(&firstpos);
16727 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 pat = pat3;
16729 for (;;)
16730 {
16731 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016732 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16734 /* didn't find it or found the first match again: FAIL */
16735 break;
16736
16737 if (firstpos.lnum == 0)
16738 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016739 if (equalpos(pos, foundpos))
16740 {
16741 /* Found the same position again. Can happen with a pattern that
16742 * has "\zs" at the end and searching backwards. Advance one
16743 * character and try again. */
16744 if (dir == BACKWARD)
16745 decl(&pos);
16746 else
16747 incl(&pos);
16748 }
16749 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016751 /* clear the start flag to avoid getting stuck here */
16752 options &= ~SEARCH_START;
16753
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 /* If the skip pattern matches, ignore this match. */
16755 if (*skip != NUL)
16756 {
16757 save_pos = curwin->w_cursor;
16758 curwin->w_cursor = pos;
16759 r = eval_to_bool(skip, &err, NULL, FALSE);
16760 curwin->w_cursor = save_pos;
16761 if (err)
16762 {
16763 /* Evaluating {skip} caused an error, break here. */
16764 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016765 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 break;
16767 }
16768 if (r)
16769 continue;
16770 }
16771
16772 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16773 {
16774 /* Found end when searching backwards or start when searching
16775 * forward: nested pair. */
16776 ++nest;
16777 pat = pat2; /* nested, don't search for middle */
16778 }
16779 else
16780 {
16781 /* Found end when searching forward or start when searching
16782 * backward: end of (nested) pair; or found middle in outer pair. */
16783 if (--nest == 1)
16784 pat = pat3; /* outer level, search for middle */
16785 }
16786
16787 if (nest == 0)
16788 {
16789 /* Found the match: return matchcount or line number. */
16790 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016791 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016793 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016794 if (flags & SP_SETPCMARK)
16795 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 curwin->w_cursor = pos;
16797 if (!(flags & SP_REPEAT))
16798 break;
16799 nest = 1; /* search for next unmatched */
16800 }
16801 }
16802
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016803 if (match_pos != NULL)
16804 {
16805 /* Store the match cursor position */
16806 match_pos->lnum = curwin->w_cursor.lnum;
16807 match_pos->col = curwin->w_cursor.col + 1;
16808 }
16809
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016811 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 curwin->w_cursor = save_cursor;
16813
16814theend:
16815 vim_free(pat2);
16816 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016817 if (p_cpo == empty_option)
16818 p_cpo = save_cpo;
16819 else
16820 /* Darn, evaluating the {skip} expression changed the value. */
16821 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016822
16823 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824}
16825
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016826/*
16827 * "searchpos()" function
16828 */
16829 static void
16830f_searchpos(argvars, rettv)
16831 typval_T *argvars;
16832 typval_T *rettv;
16833{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016834 pos_T match_pos;
16835 int lnum = 0;
16836 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016837 int n;
16838 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016839
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016840 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016841 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016842
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016843 n = search_cmn(argvars, &match_pos, &flags);
16844 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016845 {
16846 lnum = match_pos.lnum;
16847 col = match_pos.col;
16848 }
16849
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016850 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16851 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016852 if (flags & SP_SUBPAT)
16853 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016854}
16855
16856
Bram Moolenaar0d660222005-01-07 21:51:51 +000016857 static void
16858f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016859 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862#ifdef FEAT_CLIENTSERVER
16863 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016864 char_u *server = get_tv_string_chk(&argvars[0]);
16865 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016868 if (server == NULL || reply == NULL)
16869 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016870 if (check_restricted() || check_secure())
16871 return;
16872# ifdef FEAT_X11
16873 if (check_connection() == FAIL)
16874 return;
16875# endif
16876
16877 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016879 EMSG(_("E258: Unable to send to client"));
16880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882 rettv->vval.v_number = 0;
16883#else
16884 rettv->vval.v_number = -1;
16885#endif
16886}
16887
Bram Moolenaar0d660222005-01-07 21:51:51 +000016888 static void
16889f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016890 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016892{
16893 char_u *r = NULL;
16894
16895#ifdef FEAT_CLIENTSERVER
16896# ifdef WIN32
16897 r = serverGetVimNames();
16898# else
16899 make_connection();
16900 if (X_DISPLAY != NULL)
16901 r = serverGetVimNames(X_DISPLAY);
16902# endif
16903#endif
16904 rettv->v_type = VAR_STRING;
16905 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906}
16907
16908/*
16909 * "setbufvar()" function
16910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016912f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016914 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915{
16916 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016919 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 char_u nbuf[NUMBUFLEN];
16921
16922 if (check_restricted() || check_secure())
16923 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016924 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16925 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016926 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927 varp = &argvars[2];
16928
16929 if (buf != NULL && varname != NULL && varp != NULL)
16930 {
16931 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933
16934 if (*varname == '&')
16935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016936 long numval;
16937 char_u *strval;
16938 int error = FALSE;
16939
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016941 numval = get_tv_number_chk(varp, &error);
16942 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016943 if (!error && strval != NULL)
16944 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945 }
16946 else
16947 {
16948 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16949 if (bufvarname != NULL)
16950 {
16951 STRCPY(bufvarname, "b:");
16952 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016953 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 vim_free(bufvarname);
16955 }
16956 }
16957
16958 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961}
16962
16963/*
16964 * "setcmdpos()" function
16965 */
16966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016967f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016968 typval_T *argvars;
16969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016971 int pos = (int)get_tv_number(&argvars[0]) - 1;
16972
16973 if (pos >= 0)
16974 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975}
16976
16977/*
16978 * "setline()" function
16979 */
16980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016981f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 typval_T *argvars;
16983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984{
16985 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016986 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016987 list_T *l = NULL;
16988 listitem_T *li = NULL;
16989 long added = 0;
16990 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016992 lnum = get_tv_lnum(&argvars[0]);
16993 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016995 l = argvars[1].vval.v_list;
16996 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016998 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017000
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017001 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017002 for (;;)
17003 {
17004 if (l != NULL)
17005 {
17006 /* list argument, get next string */
17007 if (li == NULL)
17008 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017010 li = li->li_next;
17011 }
17012
17013 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017014 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017015 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017016
17017 /* When coming here from Insert mode, sync undo, so that this can be
17018 * undone separately from what was previously inserted. */
17019 if (u_sync_once == 2)
17020 {
17021 u_sync_once = 1; /* notify that u_sync() was called */
17022 u_sync(TRUE);
17023 }
17024
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017025 if (lnum <= curbuf->b_ml.ml_line_count)
17026 {
17027 /* existing line, replace it */
17028 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17029 {
17030 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017031 if (lnum == curwin->w_cursor.lnum)
17032 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017033 rettv->vval.v_number = 0; /* OK */
17034 }
17035 }
17036 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17037 {
17038 /* lnum is one past the last line, append the line */
17039 ++added;
17040 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17041 rettv->vval.v_number = 0; /* OK */
17042 }
17043
17044 if (l == NULL) /* only one string argument */
17045 break;
17046 ++lnum;
17047 }
17048
17049 if (added > 0)
17050 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051}
17052
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017053static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17054
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017056 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017057 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017058 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017059set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017060 win_T *wp UNUSED;
17061 typval_T *list_arg UNUSED;
17062 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017063 typval_T *rettv;
17064{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017065#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017066 char_u *act;
17067 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017068#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017069
Bram Moolenaar2641f772005-03-25 21:58:17 +000017070 rettv->vval.v_number = -1;
17071
17072#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017073 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017074 EMSG(_(e_listreq));
17075 else
17076 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017077 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017078
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017079 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017080 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017081 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 if (act == NULL)
17083 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017084 if (*act == 'a' || *act == 'r')
17085 action = *act;
17086 }
17087
Bram Moolenaar81484f42012-12-05 15:16:47 +010017088 if (l != NULL && set_errorlist(wp, l, action,
17089 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017090 rettv->vval.v_number = 0;
17091 }
17092#endif
17093}
17094
17095/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017096 * "setloclist()" function
17097 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017098 static void
17099f_setloclist(argvars, rettv)
17100 typval_T *argvars;
17101 typval_T *rettv;
17102{
17103 win_T *win;
17104
17105 rettv->vval.v_number = -1;
17106
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017107 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017108 if (win != NULL)
17109 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17110}
17111
17112/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017113 * "setmatches()" function
17114 */
17115 static void
17116f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017117 typval_T *argvars UNUSED;
17118 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017119{
17120#ifdef FEAT_SEARCH_EXTRA
17121 list_T *l;
17122 listitem_T *li;
17123 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017124 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017125
17126 rettv->vval.v_number = -1;
17127 if (argvars[0].v_type != VAR_LIST)
17128 {
17129 EMSG(_(e_listreq));
17130 return;
17131 }
17132 if ((l = argvars[0].vval.v_list) != NULL)
17133 {
17134
17135 /* To some extent make sure that we are dealing with a list from
17136 * "getmatches()". */
17137 li = l->lv_first;
17138 while (li != NULL)
17139 {
17140 if (li->li_tv.v_type != VAR_DICT
17141 || (d = li->li_tv.vval.v_dict) == NULL)
17142 {
17143 EMSG(_(e_invarg));
17144 return;
17145 }
17146 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017147 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17148 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017149 && dict_find(d, (char_u *)"priority", -1) != NULL
17150 && dict_find(d, (char_u *)"id", -1) != NULL))
17151 {
17152 EMSG(_(e_invarg));
17153 return;
17154 }
17155 li = li->li_next;
17156 }
17157
17158 clear_matches(curwin);
17159 li = l->lv_first;
17160 while (li != NULL)
17161 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017162 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017163 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017164 dictitem_T *di;
17165
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017166 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017167
17168 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17169 {
17170 if (s == NULL)
17171 {
17172 s = list_alloc();
17173 if (s == NULL)
17174 return;
17175 }
17176
17177 /* match from matchaddpos() */
17178 for (i = 1; i < 9; i++)
17179 {
17180 sprintf((char *)buf, (char *)"pos%d", i);
17181 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17182 {
17183 if (di->di_tv.v_type != VAR_LIST)
17184 return;
17185
17186 list_append_tv(s, &di->di_tv);
17187 s->lv_refcount++;
17188 }
17189 else
17190 break;
17191 }
17192 }
17193 if (i == 0)
17194 {
17195 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017196 get_dict_string(d, (char_u *)"pattern", FALSE),
17197 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017198 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017199 }
17200 else
17201 {
17202 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17203 NULL, (int)get_dict_number(d, (char_u *)"priority"),
17204 (int)get_dict_number(d, (char_u *)"id"), s);
17205 list_unref(s);
17206 s = NULL;
17207 }
17208
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017209 li = li->li_next;
17210 }
17211 rettv->vval.v_number = 0;
17212 }
17213#endif
17214}
17215
17216/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017217 * "setpos()" function
17218 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017219 static void
17220f_setpos(argvars, rettv)
17221 typval_T *argvars;
17222 typval_T *rettv;
17223{
17224 pos_T pos;
17225 int fnum;
17226 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017227 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017228
Bram Moolenaar08250432008-02-13 11:42:46 +000017229 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017230 name = get_tv_string_chk(argvars);
17231 if (name != NULL)
17232 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017233 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017234 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017235 if (--pos.col < 0)
17236 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017237 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017238 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017239 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017240 if (fnum == curbuf->b_fnum)
17241 {
17242 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017243 if (curswant >= 0)
17244 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017245 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017246 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017247 }
17248 else
17249 EMSG(_(e_invarg));
17250 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017251 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17252 {
17253 /* set mark */
17254 if (setmark_pos(name[1], &pos, fnum) == OK)
17255 rettv->vval.v_number = 0;
17256 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017257 else
17258 EMSG(_(e_invarg));
17259 }
17260 }
17261}
17262
17263/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017264 * "setqflist()" function
17265 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017266 static void
17267f_setqflist(argvars, rettv)
17268 typval_T *argvars;
17269 typval_T *rettv;
17270{
17271 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17272}
17273
17274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 * "setreg()" function
17276 */
17277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017279 typval_T *argvars;
17280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281{
17282 int regname;
17283 char_u *strregname;
17284 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017285 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 int append;
17287 char_u yank_type;
17288 long block_len;
17289
17290 block_len = -1;
17291 yank_type = MAUTO;
17292 append = FALSE;
17293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017294 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017295 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017297 if (strregname == NULL)
17298 return; /* type error; errmsg already given */
17299 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 if (regname == 0 || regname == '@')
17301 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017303 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017305 stropt = get_tv_string_chk(&argvars[2]);
17306 if (stropt == NULL)
17307 return; /* type error */
17308 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 switch (*stropt)
17310 {
17311 case 'a': case 'A': /* append */
17312 append = TRUE;
17313 break;
17314 case 'v': case 'c': /* character-wise selection */
17315 yank_type = MCHAR;
17316 break;
17317 case 'V': case 'l': /* line-wise selection */
17318 yank_type = MLINE;
17319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 case 'b': case Ctrl_V: /* block-wise selection */
17321 yank_type = MBLOCK;
17322 if (VIM_ISDIGIT(stropt[1]))
17323 {
17324 ++stropt;
17325 block_len = getdigits(&stropt) - 1;
17326 --stropt;
17327 }
17328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 }
17330 }
17331
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017332 if (argvars[1].v_type == VAR_LIST)
17333 {
17334 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017335 char_u **allocval;
17336 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017337 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017338 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017339 int len = argvars[1].vval.v_list->lv_len;
17340 listitem_T *li;
17341
Bram Moolenaar7d647822014-04-05 21:28:56 +020017342 /* First half: use for pointers to result lines; second half: use for
17343 * pointers to allocated copies. */
17344 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017345 if (lstval == NULL)
17346 return;
17347 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017348 allocval = lstval + len + 2;
17349 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017350
17351 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17352 li = li->li_next)
17353 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017354 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017355 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017356 goto free_lstval;
17357 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017358 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017359 /* Need to make a copy, next get_tv_string_buf_chk() will
17360 * overwrite the string. */
17361 strval = vim_strsave(buf);
17362 if (strval == NULL)
17363 goto free_lstval;
17364 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017365 }
17366 *curval++ = strval;
17367 }
17368 *curval++ = NULL;
17369
17370 write_reg_contents_lst(regname, lstval, -1,
17371 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017372free_lstval:
17373 while (curallocval > allocval)
17374 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017375 vim_free(lstval);
17376 }
17377 else
17378 {
17379 strval = get_tv_string_chk(&argvars[1]);
17380 if (strval == NULL)
17381 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017382 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017385 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386}
17387
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017388/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017389 * "settabvar()" function
17390 */
17391 static void
17392f_settabvar(argvars, rettv)
17393 typval_T *argvars;
17394 typval_T *rettv;
17395{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017396#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017397 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017398 tabpage_T *tp;
17399#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017400 char_u *varname, *tabvarname;
17401 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017402
17403 rettv->vval.v_number = 0;
17404
17405 if (check_restricted() || check_secure())
17406 return;
17407
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017408#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017409 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017410#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017411 varname = get_tv_string_chk(&argvars[1]);
17412 varp = &argvars[2];
17413
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017414 if (varname != NULL && varp != NULL
17415#ifdef FEAT_WINDOWS
17416 && tp != NULL
17417#endif
17418 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017419 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017420#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017421 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017422 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017423#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017424
17425 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17426 if (tabvarname != NULL)
17427 {
17428 STRCPY(tabvarname, "t:");
17429 STRCPY(tabvarname + 2, varname);
17430 set_var(tabvarname, varp, TRUE);
17431 vim_free(tabvarname);
17432 }
17433
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017434#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017435 /* Restore current tabpage */
17436 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017437 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017438#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017439 }
17440}
17441
17442/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017443 * "settabwinvar()" function
17444 */
17445 static void
17446f_settabwinvar(argvars, rettv)
17447 typval_T *argvars;
17448 typval_T *rettv;
17449{
17450 setwinvar(argvars, rettv, 1);
17451}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452
17453/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017454 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017457f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017458 typval_T *argvars;
17459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017461 setwinvar(argvars, rettv, 0);
17462}
17463
17464/*
17465 * "setwinvar()" and "settabwinvar()" functions
17466 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017467
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017468 static void
17469setwinvar(argvars, rettv, off)
17470 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017471 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017472 int off;
17473{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 win_T *win;
17475#ifdef FEAT_WINDOWS
17476 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017477 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478#endif
17479 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017480 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017482 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483
17484 if (check_restricted() || check_secure())
17485 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017486
17487#ifdef FEAT_WINDOWS
17488 if (off == 1)
17489 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17490 else
17491 tp = curtab;
17492#endif
17493 win = find_win_by_nr(&argvars[off], tp);
17494 varname = get_tv_string_chk(&argvars[off + 1]);
17495 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496
17497 if (win != NULL && varname != NULL && varp != NULL)
17498 {
17499#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017500 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017503 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017505 long numval;
17506 char_u *strval;
17507 int error = FALSE;
17508
17509 ++varname;
17510 numval = get_tv_number_chk(varp, &error);
17511 strval = get_tv_string_buf_chk(varp, nbuf);
17512 if (!error && strval != NULL)
17513 set_option_value(varname, numval, strval, OPT_LOCAL);
17514 }
17515 else
17516 {
17517 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17518 if (winvarname != NULL)
17519 {
17520 STRCPY(winvarname, "w:");
17521 STRCPY(winvarname + 2, varname);
17522 set_var(winvarname, varp, TRUE);
17523 vim_free(winvarname);
17524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 }
17526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017528 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529#endif
17530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531}
17532
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017533#ifdef FEAT_CRYPT
17534/*
17535 * "sha256({string})" function
17536 */
17537 static void
17538f_sha256(argvars, rettv)
17539 typval_T *argvars;
17540 typval_T *rettv;
17541{
17542 char_u *p;
17543
17544 p = get_tv_string(&argvars[0]);
17545 rettv->vval.v_string = vim_strsave(
17546 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17547 rettv->v_type = VAR_STRING;
17548}
17549#endif /* FEAT_CRYPT */
17550
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017552 * "shellescape({string})" function
17553 */
17554 static void
17555f_shellescape(argvars, rettv)
17556 typval_T *argvars;
17557 typval_T *rettv;
17558{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017559 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017560 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017561 rettv->v_type = VAR_STRING;
17562}
17563
17564/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017565 * shiftwidth() function
17566 */
17567 static void
17568f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017569 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017570 typval_T *rettv;
17571{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017572 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017573}
17574
17575/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 */
17578 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 typval_T *argvars;
17581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017583 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585 p = get_tv_string(&argvars[0]);
17586 rettv->vval.v_string = vim_strsave(p);
17587 simplify_filename(rettv->vval.v_string); /* simplify in place */
17588 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589}
17590
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017591#ifdef FEAT_FLOAT
17592/*
17593 * "sin()" function
17594 */
17595 static void
17596f_sin(argvars, rettv)
17597 typval_T *argvars;
17598 typval_T *rettv;
17599{
17600 float_T f;
17601
17602 rettv->v_type = VAR_FLOAT;
17603 if (get_float_arg(argvars, &f) == OK)
17604 rettv->vval.v_float = sin(f);
17605 else
17606 rettv->vval.v_float = 0.0;
17607}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017608
17609/*
17610 * "sinh()" function
17611 */
17612 static void
17613f_sinh(argvars, rettv)
17614 typval_T *argvars;
17615 typval_T *rettv;
17616{
17617 float_T f;
17618
17619 rettv->v_type = VAR_FLOAT;
17620 if (get_float_arg(argvars, &f) == OK)
17621 rettv->vval.v_float = sinh(f);
17622 else
17623 rettv->vval.v_float = 0.0;
17624}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017625#endif
17626
Bram Moolenaar0d660222005-01-07 21:51:51 +000017627static int
17628#ifdef __BORLANDC__
17629 _RTLENTRYF
17630#endif
17631 item_compare __ARGS((const void *s1, const void *s2));
17632static int
17633#ifdef __BORLANDC__
17634 _RTLENTRYF
17635#endif
17636 item_compare2 __ARGS((const void *s1, const void *s2));
17637
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017638/* struct used in the array that's given to qsort() */
17639typedef struct
17640{
17641 listitem_T *item;
17642 int idx;
17643} sortItem_T;
17644
Bram Moolenaar0d660222005-01-07 21:51:51 +000017645static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017646static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017647static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017648static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017649static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017650static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017651static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017652#define ITEM_COMPARE_FAIL 999
17653
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017655 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017657 static int
17658#ifdef __BORLANDC__
17659_RTLENTRYF
17660#endif
17661item_compare(s1, s2)
17662 const void *s1;
17663 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017665 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017666 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017667 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017668 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017669 int res;
17670 char_u numbuf1[NUMBUFLEN];
17671 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017673 si1 = (sortItem_T *)s1;
17674 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017675 tv1 = &si1->item->li_tv;
17676 tv2 = &si2->item->li_tv;
17677 /* tv2string() puts quotes around a string and allocates memory. Don't do
17678 * that for string variables. Use a single quote when comparing with a
17679 * non-string to do what the docs promise. */
17680 if (tv1->v_type == VAR_STRING)
17681 {
17682 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17683 p1 = (char_u *)"'";
17684 else
17685 p1 = tv1->vval.v_string;
17686 }
17687 else
17688 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17689 if (tv2->v_type == VAR_STRING)
17690 {
17691 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17692 p2 = (char_u *)"'";
17693 else
17694 p2 = tv2->vval.v_string;
17695 }
17696 else
17697 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017698 if (p1 == NULL)
17699 p1 = (char_u *)"";
17700 if (p2 == NULL)
17701 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017702 if (!item_compare_numeric)
17703 {
17704 if (item_compare_ic)
17705 res = STRICMP(p1, p2);
17706 else
17707 res = STRCMP(p1, p2);
17708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017710 {
17711 double n1, n2;
17712 n1 = strtod((char *)p1, (char **)&p1);
17713 n2 = strtod((char *)p2, (char **)&p2);
17714 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17715 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017716
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017717 /* When the result would be zero, compare the item indexes. Makes the
17718 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017719 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017720 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017721
Bram Moolenaar0d660222005-01-07 21:51:51 +000017722 vim_free(tofree1);
17723 vim_free(tofree2);
17724 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725}
17726
17727 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017728#ifdef __BORLANDC__
17729_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017731item_compare2(s1, s2)
17732 const void *s1;
17733 const void *s2;
17734{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017735 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017736 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017737 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017738 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017741 /* shortcut after failure in previous call; compare all items equal */
17742 if (item_compare_func_err)
17743 return 0;
17744
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017745 si1 = (sortItem_T *)s1;
17746 si2 = (sortItem_T *)s2;
17747
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017748 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017749 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017750 copy_tv(&si1->item->li_tv, &argv[0]);
17751 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017752
17753 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017754 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017755 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17756 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017757 clear_tv(&argv[0]);
17758 clear_tv(&argv[1]);
17759
17760 if (res == FAIL)
17761 res = ITEM_COMPARE_FAIL;
17762 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017763 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17764 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017765 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017767
17768 /* When the result would be zero, compare the pointers themselves. Makes
17769 * the sort stable. */
17770 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017771 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017772
Bram Moolenaar0d660222005-01-07 21:51:51 +000017773 return res;
17774}
17775
17776/*
17777 * "sort({list})" function
17778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017780do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 typval_T *argvars;
17782 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017783 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784{
Bram Moolenaar33570922005-01-25 22:26:29 +000017785 list_T *l;
17786 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017787 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017788 long len;
17789 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790
Bram Moolenaar0d660222005-01-07 21:51:51 +000017791 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017792 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 else
17794 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017795 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017796 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017797 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17798 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017799 return;
17800 rettv->vval.v_list = l;
17801 rettv->v_type = VAR_LIST;
17802 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803
Bram Moolenaar0d660222005-01-07 21:51:51 +000017804 len = list_len(l);
17805 if (len <= 1)
17806 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807
Bram Moolenaar0d660222005-01-07 21:51:51 +000017808 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017809 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017810 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017811 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017812 if (argvars[1].v_type != VAR_UNKNOWN)
17813 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017814 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017815 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017816 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017817 else
17818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017819 int error = FALSE;
17820
17821 i = get_tv_number_chk(&argvars[1], &error);
17822 if (error)
17823 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017824 if (i == 1)
17825 item_compare_ic = TRUE;
17826 else
17827 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017828 if (item_compare_func != NULL)
17829 {
17830 if (STRCMP(item_compare_func, "n") == 0)
17831 {
17832 item_compare_func = NULL;
17833 item_compare_numeric = TRUE;
17834 }
17835 else if (STRCMP(item_compare_func, "i") == 0)
17836 {
17837 item_compare_func = NULL;
17838 item_compare_ic = TRUE;
17839 }
17840 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017841 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017842
17843 if (argvars[2].v_type != VAR_UNKNOWN)
17844 {
17845 /* optional third argument: {dict} */
17846 if (argvars[2].v_type != VAR_DICT)
17847 {
17848 EMSG(_(e_dictreq));
17849 return;
17850 }
17851 item_compare_selfdict = argvars[2].vval.v_dict;
17852 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854
Bram Moolenaar0d660222005-01-07 21:51:51 +000017855 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017856 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017857 if (ptrs == NULL)
17858 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859
Bram Moolenaar327aa022014-03-25 18:24:23 +010017860 i = 0;
17861 if (sort)
17862 {
17863 /* sort(): ptrs will be the list to sort */
17864 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017865 {
17866 ptrs[i].item = li;
17867 ptrs[i].idx = i;
17868 ++i;
17869 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017870
17871 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017872 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017873 /* test the compare function */
17874 if (item_compare_func != NULL
17875 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017876 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017877 EMSG(_("E702: Sort compare function failed"));
17878 else
17879 {
17880 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017881 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017882 item_compare_func == NULL ? item_compare : item_compare2);
17883
17884 if (!item_compare_func_err)
17885 {
17886 /* Clear the List and append the items in sorted order. */
17887 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17888 l->lv_len = 0;
17889 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017890 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017891 }
17892 }
17893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017895 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017896 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17897
17898 /* f_uniq(): ptrs will be a stack of items to remove */
17899 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017900 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017901 item_compare_func_ptr = item_compare_func
17902 ? item_compare2 : item_compare;
17903
17904 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17905 li = li->li_next)
17906 {
17907 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17908 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017909 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017910 if (item_compare_func_err)
17911 {
17912 EMSG(_("E882: Uniq compare function failed"));
17913 break;
17914 }
17915 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017917 if (!item_compare_func_err)
17918 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017919 while (--i >= 0)
17920 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017921 li = ptrs[i].item->li_next;
17922 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017923 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017924 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017925 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017926 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017927 list_fix_watch(l, li);
17928 listitem_free(li);
17929 l->lv_len--;
17930 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017931 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017932 }
17933
17934 vim_free(ptrs);
17935 }
17936}
17937
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017938/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017939 * "sort({list})" function
17940 */
17941 static void
17942f_sort(argvars, rettv)
17943 typval_T *argvars;
17944 typval_T *rettv;
17945{
17946 do_sort_uniq(argvars, rettv, TRUE);
17947}
17948
17949/*
17950 * "uniq({list})" function
17951 */
17952 static void
17953f_uniq(argvars, rettv)
17954 typval_T *argvars;
17955 typval_T *rettv;
17956{
17957 do_sort_uniq(argvars, rettv, FALSE);
17958}
17959
17960/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017961 * "soundfold({word})" function
17962 */
17963 static void
17964f_soundfold(argvars, rettv)
17965 typval_T *argvars;
17966 typval_T *rettv;
17967{
17968 char_u *s;
17969
17970 rettv->v_type = VAR_STRING;
17971 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017972#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017973 rettv->vval.v_string = eval_soundfold(s);
17974#else
17975 rettv->vval.v_string = vim_strsave(s);
17976#endif
17977}
17978
17979/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017980 * "spellbadword()" function
17981 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017982 static void
17983f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017984 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017985 typval_T *rettv;
17986{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017987 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017988 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017989 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017990
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017991 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017992 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017993
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017994#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017995 if (argvars[0].v_type == VAR_UNKNOWN)
17996 {
17997 /* Find the start and length of the badly spelled word. */
17998 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17999 if (len != 0)
18000 word = ml_get_cursor();
18001 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018002 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018003 {
18004 char_u *str = get_tv_string_chk(&argvars[0]);
18005 int capcol = -1;
18006
18007 if (str != NULL)
18008 {
18009 /* Check the argument for spelling. */
18010 while (*str != NUL)
18011 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018012 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018013 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018014 {
18015 word = str;
18016 break;
18017 }
18018 str += len;
18019 }
18020 }
18021 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018022#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018023
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018024 list_append_string(rettv->vval.v_list, word, len);
18025 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018026 attr == HLF_SPB ? "bad" :
18027 attr == HLF_SPR ? "rare" :
18028 attr == HLF_SPL ? "local" :
18029 attr == HLF_SPC ? "caps" :
18030 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018031}
18032
18033/*
18034 * "spellsuggest()" function
18035 */
18036 static void
18037f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018038 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018039 typval_T *rettv;
18040{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018041#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018042 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018043 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018044 int maxcount;
18045 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018046 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018047 listitem_T *li;
18048 int need_capital = FALSE;
18049#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018050
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018051 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018052 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018053
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018054#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018055 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018056 {
18057 str = get_tv_string(&argvars[0]);
18058 if (argvars[1].v_type != VAR_UNKNOWN)
18059 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018060 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018061 if (maxcount <= 0)
18062 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018063 if (argvars[2].v_type != VAR_UNKNOWN)
18064 {
18065 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18066 if (typeerr)
18067 return;
18068 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018069 }
18070 else
18071 maxcount = 25;
18072
Bram Moolenaar4770d092006-01-12 23:22:24 +000018073 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018074
18075 for (i = 0; i < ga.ga_len; ++i)
18076 {
18077 str = ((char_u **)ga.ga_data)[i];
18078
18079 li = listitem_alloc();
18080 if (li == NULL)
18081 vim_free(str);
18082 else
18083 {
18084 li->li_tv.v_type = VAR_STRING;
18085 li->li_tv.v_lock = 0;
18086 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018087 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018088 }
18089 }
18090 ga_clear(&ga);
18091 }
18092#endif
18093}
18094
Bram Moolenaar0d660222005-01-07 21:51:51 +000018095 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018096f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018097 typval_T *argvars;
18098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099{
18100 char_u *str;
18101 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018102 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018103 regmatch_T regmatch;
18104 char_u patbuf[NUMBUFLEN];
18105 char_u *save_cpo;
18106 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018107 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018108 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018109 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018110
18111 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18112 save_cpo = p_cpo;
18113 p_cpo = (char_u *)"";
18114
18115 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018116 if (argvars[1].v_type != VAR_UNKNOWN)
18117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018118 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18119 if (pat == NULL)
18120 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018121 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018122 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018123 }
18124 if (pat == NULL || *pat == NUL)
18125 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018126
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018127 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018129 if (typeerr)
18130 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131
Bram Moolenaar0d660222005-01-07 21:51:51 +000018132 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18133 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018135 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018136 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018137 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018138 if (*str == NUL)
18139 match = FALSE; /* empty item at the end */
18140 else
18141 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018142 if (match)
18143 end = regmatch.startp[0];
18144 else
18145 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018146 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18147 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018148 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018149 if (list_append_string(rettv->vval.v_list, str,
18150 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018152 }
18153 if (!match)
18154 break;
18155 /* Advance to just after the match. */
18156 if (regmatch.endp[0] > str)
18157 col = 0;
18158 else
18159 {
18160 /* Don't get stuck at the same match. */
18161#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018162 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018163#else
18164 col = 1;
18165#endif
18166 }
18167 str = regmatch.endp[0];
18168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaar473de612013-06-08 18:19:48 +020018170 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172
Bram Moolenaar0d660222005-01-07 21:51:51 +000018173 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174}
18175
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018176#ifdef FEAT_FLOAT
18177/*
18178 * "sqrt()" function
18179 */
18180 static void
18181f_sqrt(argvars, rettv)
18182 typval_T *argvars;
18183 typval_T *rettv;
18184{
18185 float_T f;
18186
18187 rettv->v_type = VAR_FLOAT;
18188 if (get_float_arg(argvars, &f) == OK)
18189 rettv->vval.v_float = sqrt(f);
18190 else
18191 rettv->vval.v_float = 0.0;
18192}
18193
18194/*
18195 * "str2float()" function
18196 */
18197 static void
18198f_str2float(argvars, rettv)
18199 typval_T *argvars;
18200 typval_T *rettv;
18201{
18202 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18203
18204 if (*p == '+')
18205 p = skipwhite(p + 1);
18206 (void)string2float(p, &rettv->vval.v_float);
18207 rettv->v_type = VAR_FLOAT;
18208}
18209#endif
18210
Bram Moolenaar2c932302006-03-18 21:42:09 +000018211/*
18212 * "str2nr()" function
18213 */
18214 static void
18215f_str2nr(argvars, rettv)
18216 typval_T *argvars;
18217 typval_T *rettv;
18218{
18219 int base = 10;
18220 char_u *p;
18221 long n;
18222
18223 if (argvars[1].v_type != VAR_UNKNOWN)
18224 {
18225 base = get_tv_number(&argvars[1]);
18226 if (base != 8 && base != 10 && base != 16)
18227 {
18228 EMSG(_(e_invarg));
18229 return;
18230 }
18231 }
18232
18233 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018234 if (*p == '+')
18235 p = skipwhite(p + 1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020018236 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018237 rettv->vval.v_number = n;
18238}
18239
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240#ifdef HAVE_STRFTIME
18241/*
18242 * "strftime({format}[, {time}])" function
18243 */
18244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018245f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018246 typval_T *argvars;
18247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248{
18249 char_u result_buf[256];
18250 struct tm *curtime;
18251 time_t seconds;
18252 char_u *p;
18253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018254 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018256 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018257 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 seconds = time(NULL);
18259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018260 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 curtime = localtime(&seconds);
18262 /* MSVC returns NULL for an invalid value of seconds. */
18263 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018264 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 else
18266 {
18267# ifdef FEAT_MBYTE
18268 vimconv_T conv;
18269 char_u *enc;
18270
18271 conv.vc_type = CONV_NONE;
18272 enc = enc_locale();
18273 convert_setup(&conv, p_enc, enc);
18274 if (conv.vc_type != CONV_NONE)
18275 p = string_convert(&conv, p, NULL);
18276# endif
18277 if (p != NULL)
18278 (void)strftime((char *)result_buf, sizeof(result_buf),
18279 (char *)p, curtime);
18280 else
18281 result_buf[0] = NUL;
18282
18283# ifdef FEAT_MBYTE
18284 if (conv.vc_type != CONV_NONE)
18285 vim_free(p);
18286 convert_setup(&conv, enc, p_enc);
18287 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018288 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 else
18290# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018291 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292
18293# ifdef FEAT_MBYTE
18294 /* Release conversion descriptors */
18295 convert_setup(&conv, NULL, NULL);
18296 vim_free(enc);
18297# endif
18298 }
18299}
18300#endif
18301
18302/*
18303 * "stridx()" function
18304 */
18305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 typval_T *argvars;
18308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309{
18310 char_u buf[NUMBUFLEN];
18311 char_u *needle;
18312 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018313 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018315 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018317 needle = get_tv_string_chk(&argvars[1]);
18318 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018319 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018320 if (needle == NULL || haystack == NULL)
18321 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322
Bram Moolenaar33570922005-01-25 22:26:29 +000018323 if (argvars[2].v_type != VAR_UNKNOWN)
18324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325 int error = FALSE;
18326
18327 start_idx = get_tv_number_chk(&argvars[2], &error);
18328 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018329 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018330 if (start_idx >= 0)
18331 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018332 }
18333
18334 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18335 if (pos != NULL)
18336 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337}
18338
18339/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018340 * "string()" function
18341 */
18342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018343f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018344 typval_T *argvars;
18345 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018346{
18347 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018350 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018351 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018352 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018353 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018354 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355}
18356
18357/*
18358 * "strlen()" function
18359 */
18360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018361f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018362 typval_T *argvars;
18363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018365 rettv->vval.v_number = (varnumber_T)(STRLEN(
18366 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367}
18368
18369/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018370 * "strchars()" function
18371 */
18372 static void
18373f_strchars(argvars, rettv)
18374 typval_T *argvars;
18375 typval_T *rettv;
18376{
18377 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018378 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018379#ifdef FEAT_MBYTE
18380 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018381 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018382#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018383
18384 if (argvars[1].v_type != VAR_UNKNOWN)
18385 skipcc = get_tv_number_chk(&argvars[1], NULL);
18386 if (skipcc < 0 || skipcc > 1)
18387 EMSG(_(e_invarg));
18388 else
18389 {
18390#ifdef FEAT_MBYTE
18391 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18392 while (*s != NUL)
18393 {
18394 func_mb_ptr2char_adv(&s);
18395 ++len;
18396 }
18397 rettv->vval.v_number = len;
18398#else
18399 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18400#endif
18401 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018402}
18403
18404/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018405 * "strdisplaywidth()" function
18406 */
18407 static void
18408f_strdisplaywidth(argvars, rettv)
18409 typval_T *argvars;
18410 typval_T *rettv;
18411{
18412 char_u *s = get_tv_string(&argvars[0]);
18413 int col = 0;
18414
18415 if (argvars[1].v_type != VAR_UNKNOWN)
18416 col = get_tv_number(&argvars[1]);
18417
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018418 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018419}
18420
18421/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018422 * "strwidth()" function
18423 */
18424 static void
18425f_strwidth(argvars, rettv)
18426 typval_T *argvars;
18427 typval_T *rettv;
18428{
18429 char_u *s = get_tv_string(&argvars[0]);
18430
18431 rettv->vval.v_number = (varnumber_T)(
18432#ifdef FEAT_MBYTE
18433 mb_string2cells(s, -1)
18434#else
18435 STRLEN(s)
18436#endif
18437 );
18438}
18439
18440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 * "strpart()" function
18442 */
18443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018444f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018445 typval_T *argvars;
18446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447{
18448 char_u *p;
18449 int n;
18450 int len;
18451 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018452 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018454 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 slen = (int)STRLEN(p);
18456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018457 n = get_tv_number_chk(&argvars[1], &error);
18458 if (error)
18459 len = 0;
18460 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018461 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 else
18463 len = slen - n; /* default len: all bytes that are available. */
18464
18465 /*
18466 * Only return the overlap between the specified part and the actual
18467 * string.
18468 */
18469 if (n < 0)
18470 {
18471 len += n;
18472 n = 0;
18473 }
18474 else if (n > slen)
18475 n = slen;
18476 if (len < 0)
18477 len = 0;
18478 else if (n + len > slen)
18479 len = slen - n;
18480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481 rettv->v_type = VAR_STRING;
18482 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483}
18484
18485/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018486 * "strridx()" function
18487 */
18488 static void
18489f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018490 typval_T *argvars;
18491 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018492{
18493 char_u buf[NUMBUFLEN];
18494 char_u *needle;
18495 char_u *haystack;
18496 char_u *rest;
18497 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018498 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018500 needle = get_tv_string_chk(&argvars[1]);
18501 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018502
18503 rettv->vval.v_number = -1;
18504 if (needle == NULL || haystack == NULL)
18505 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018506
18507 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018508 if (argvars[2].v_type != VAR_UNKNOWN)
18509 {
18510 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018511 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018512 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018513 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018514 }
18515 else
18516 end_idx = haystack_len;
18517
Bram Moolenaar0d660222005-01-07 21:51:51 +000018518 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018519 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018520 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018521 lastmatch = haystack + end_idx;
18522 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018523 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018524 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018525 for (rest = haystack; *rest != '\0'; ++rest)
18526 {
18527 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018528 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018529 break;
18530 lastmatch = rest;
18531 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018532 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018533
18534 if (lastmatch == NULL)
18535 rettv->vval.v_number = -1;
18536 else
18537 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18538}
18539
18540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 * "strtrans()" function
18542 */
18543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018544f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018545 typval_T *argvars;
18546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548 rettv->v_type = VAR_STRING;
18549 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550}
18551
18552/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018553 * "submatch()" function
18554 */
18555 static void
18556f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018557 typval_T *argvars;
18558 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018559{
Bram Moolenaar41571762014-04-02 19:00:58 +020018560 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018561 int no;
18562 int retList = 0;
18563
18564 no = (int)get_tv_number_chk(&argvars[0], &error);
18565 if (error)
18566 return;
18567 error = FALSE;
18568 if (argvars[1].v_type != VAR_UNKNOWN)
18569 retList = get_tv_number_chk(&argvars[1], &error);
18570 if (error)
18571 return;
18572
18573 if (retList == 0)
18574 {
18575 rettv->v_type = VAR_STRING;
18576 rettv->vval.v_string = reg_submatch(no);
18577 }
18578 else
18579 {
18580 rettv->v_type = VAR_LIST;
18581 rettv->vval.v_list = reg_submatch_list(no);
18582 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018583}
18584
18585/*
18586 * "substitute()" function
18587 */
18588 static void
18589f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018590 typval_T *argvars;
18591 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018592{
18593 char_u patbuf[NUMBUFLEN];
18594 char_u subbuf[NUMBUFLEN];
18595 char_u flagsbuf[NUMBUFLEN];
18596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018597 char_u *str = get_tv_string_chk(&argvars[0]);
18598 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18599 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18600 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18601
Bram Moolenaar0d660222005-01-07 21:51:51 +000018602 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018603 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18604 rettv->vval.v_string = NULL;
18605 else
18606 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018607}
18608
18609/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018610 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018614 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616{
18617 int id = 0;
18618#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018619 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 long col;
18621 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018622 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018624 lnum = get_tv_lnum(argvars); /* -1 on type error */
18625 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18626 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018628 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018629 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018630 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631#endif
18632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634}
18635
18636/*
18637 * "synIDattr(id, what [, mode])" function
18638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643{
18644 char_u *p = NULL;
18645#ifdef FEAT_SYN_HL
18646 int id;
18647 char_u *what;
18648 char_u *mode;
18649 char_u modebuf[NUMBUFLEN];
18650 int modec;
18651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 id = get_tv_number(&argvars[0]);
18653 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018654 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018658 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 modec = 0; /* replace invalid with current */
18660 }
18661 else
18662 {
18663#ifdef FEAT_GUI
18664 if (gui.in_use)
18665 modec = 'g';
18666 else
18667#endif
18668 if (t_colors > 1)
18669 modec = 'c';
18670 else
18671 modec = 't';
18672 }
18673
18674
18675 switch (TOLOWER_ASC(what[0]))
18676 {
18677 case 'b':
18678 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18679 p = highlight_color(id, what, modec);
18680 else /* bold */
18681 p = highlight_has_attr(id, HL_BOLD, modec);
18682 break;
18683
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018684 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 p = highlight_color(id, what, modec);
18686 break;
18687
18688 case 'i':
18689 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18690 p = highlight_has_attr(id, HL_INVERSE, modec);
18691 else /* italic */
18692 p = highlight_has_attr(id, HL_ITALIC, modec);
18693 break;
18694
18695 case 'n': /* name */
18696 p = get_highlight_name(NULL, id - 1);
18697 break;
18698
18699 case 'r': /* reverse */
18700 p = highlight_has_attr(id, HL_INVERSE, modec);
18701 break;
18702
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018703 case 's':
18704 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18705 p = highlight_color(id, what, modec);
18706 else /* standout */
18707 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 break;
18709
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018710 case 'u':
18711 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18712 /* underline */
18713 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18714 else
18715 /* undercurl */
18716 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717 break;
18718 }
18719
18720 if (p != NULL)
18721 p = vim_strsave(p);
18722#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723 rettv->v_type = VAR_STRING;
18724 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725}
18726
18727/*
18728 * "synIDtrans(id)" function
18729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734{
18735 int id;
18736
18737#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018738 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739
18740 if (id > 0)
18741 id = syn_get_final_id(id);
18742 else
18743#endif
18744 id = 0;
18745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018746 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747}
18748
18749/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018750 * "synconcealed(lnum, col)" function
18751 */
18752 static void
18753f_synconcealed(argvars, rettv)
18754 typval_T *argvars UNUSED;
18755 typval_T *rettv;
18756{
18757#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18758 long lnum;
18759 long col;
18760 int syntax_flags = 0;
18761 int cchar;
18762 int matchid = 0;
18763 char_u str[NUMBUFLEN];
18764#endif
18765
18766 rettv->v_type = VAR_LIST;
18767 rettv->vval.v_list = NULL;
18768
18769#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18770 lnum = get_tv_lnum(argvars); /* -1 on type error */
18771 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18772
18773 vim_memset(str, NUL, sizeof(str));
18774
18775 if (rettv_list_alloc(rettv) != FAIL)
18776 {
18777 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18778 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18779 && curwin->w_p_cole > 0)
18780 {
18781 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18782 syntax_flags = get_syntax_info(&matchid);
18783
18784 /* get the conceal character */
18785 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18786 {
18787 cchar = syn_get_sub_char();
18788 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18789 cchar = lcs_conceal;
18790 if (cchar != NUL)
18791 {
18792# ifdef FEAT_MBYTE
18793 if (has_mbyte)
18794 (*mb_char2bytes)(cchar, str);
18795 else
18796# endif
18797 str[0] = cchar;
18798 }
18799 }
18800 }
18801
18802 list_append_number(rettv->vval.v_list,
18803 (syntax_flags & HL_CONCEAL) != 0);
18804 /* -1 to auto-determine strlen */
18805 list_append_string(rettv->vval.v_list, str, -1);
18806 list_append_number(rettv->vval.v_list, matchid);
18807 }
18808#endif
18809}
18810
18811/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018812 * "synstack(lnum, col)" function
18813 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018814 static void
18815f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018816 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018817 typval_T *rettv;
18818{
18819#ifdef FEAT_SYN_HL
18820 long lnum;
18821 long col;
18822 int i;
18823 int id;
18824#endif
18825
18826 rettv->v_type = VAR_LIST;
18827 rettv->vval.v_list = NULL;
18828
18829#ifdef FEAT_SYN_HL
18830 lnum = get_tv_lnum(argvars); /* -1 on type error */
18831 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18832
18833 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018834 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018835 && rettv_list_alloc(rettv) != FAIL)
18836 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018837 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018838 for (i = 0; ; ++i)
18839 {
18840 id = syn_get_stack_item(i);
18841 if (id < 0)
18842 break;
18843 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18844 break;
18845 }
18846 }
18847#endif
18848}
18849
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018851get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018852 typval_T *argvars;
18853 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018854 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018856 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018858 char_u *infile = NULL;
18859 char_u buf[NUMBUFLEN];
18860 int err = FALSE;
18861 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018862 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018863 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018865 rettv->v_type = VAR_STRING;
18866 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018867 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018868 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018869
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018870 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018871 {
18872 /*
18873 * Write the string to a temp file, to be used for input of the shell
18874 * command.
18875 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018876 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018877 {
18878 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018879 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018880 }
18881
18882 fd = mch_fopen((char *)infile, WRITEBIN);
18883 if (fd == NULL)
18884 {
18885 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018886 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018887 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018888 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018889 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018890 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18891 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018892 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018893 else
18894 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018895 size_t len;
18896
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018897 p = get_tv_string_buf_chk(&argvars[1], buf);
18898 if (p == NULL)
18899 {
18900 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018901 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018902 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018903 len = STRLEN(p);
18904 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018905 err = TRUE;
18906 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018907 if (fclose(fd) != 0)
18908 err = TRUE;
18909 if (err)
18910 {
18911 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018912 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018913 }
18914 }
18915
Bram Moolenaar52a72462014-08-29 15:53:52 +020018916 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18917 * echoes typeahead, that messes up the display. */
18918 if (!msg_silent)
18919 flags += SHELL_COOKED;
18920
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018921 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018923 int len;
18924 listitem_T *li;
18925 char_u *s = NULL;
18926 char_u *start;
18927 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018928 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929
Bram Moolenaar52a72462014-08-29 15:53:52 +020018930 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018931 if (res == NULL)
18932 goto errret;
18933
18934 list = list_alloc();
18935 if (list == NULL)
18936 goto errret;
18937
18938 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018940 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018941 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018942 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018943 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018944
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018945 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018946 if (s == NULL)
18947 goto errret;
18948
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018949 for (p = s; start < end; ++p, ++start)
18950 *p = *start == NUL ? NL : *start;
18951 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018952
18953 li = listitem_alloc();
18954 if (li == NULL)
18955 {
18956 vim_free(s);
18957 goto errret;
18958 }
18959 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018960 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018961 li->li_tv.vval.v_string = s;
18962 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018964
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018965 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018966 rettv->v_type = VAR_LIST;
18967 rettv->vval.v_list = list;
18968 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018970 else
18971 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018972 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018973#ifdef USE_CR
18974 /* translate <CR> into <NL> */
18975 if (res != NULL)
18976 {
18977 char_u *s;
18978
18979 for (s = res; *s; ++s)
18980 {
18981 if (*s == CAR)
18982 *s = NL;
18983 }
18984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985#else
18986# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018987 /* translate <CR><NL> into <NL> */
18988 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018990 char_u *s, *d;
18991
18992 d = res;
18993 for (s = res; *s; ++s)
18994 {
18995 if (s[0] == CAR && s[1] == NL)
18996 ++s;
18997 *d++ = *s;
18998 }
18999 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001# endif
19002#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019003 rettv->vval.v_string = res;
19004 res = NULL;
19005 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019006
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019007errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019008 if (infile != NULL)
19009 {
19010 mch_remove(infile);
19011 vim_free(infile);
19012 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019013 if (res != NULL)
19014 vim_free(res);
19015 if (list != NULL)
19016 list_free(list, TRUE);
19017}
19018
19019/*
19020 * "system()" function
19021 */
19022 static void
19023f_system(argvars, rettv)
19024 typval_T *argvars;
19025 typval_T *rettv;
19026{
19027 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19028}
19029
19030/*
19031 * "systemlist()" function
19032 */
19033 static void
19034f_systemlist(argvars, rettv)
19035 typval_T *argvars;
19036 typval_T *rettv;
19037{
19038 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039}
19040
19041/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019042 * "tabpagebuflist()" function
19043 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019044 static void
19045f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019046 typval_T *argvars UNUSED;
19047 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019048{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019049#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019050 tabpage_T *tp;
19051 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019052
19053 if (argvars[0].v_type == VAR_UNKNOWN)
19054 wp = firstwin;
19055 else
19056 {
19057 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19058 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019059 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019060 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019061 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019062 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019063 for (; wp != NULL; wp = wp->w_next)
19064 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019065 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019066 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019067 }
19068#endif
19069}
19070
19071
19072/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019073 * "tabpagenr()" function
19074 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019075 static void
19076f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019077 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019078 typval_T *rettv;
19079{
19080 int nr = 1;
19081#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019082 char_u *arg;
19083
19084 if (argvars[0].v_type != VAR_UNKNOWN)
19085 {
19086 arg = get_tv_string_chk(&argvars[0]);
19087 nr = 0;
19088 if (arg != NULL)
19089 {
19090 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019091 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019092 else
19093 EMSG2(_(e_invexpr2), arg);
19094 }
19095 }
19096 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019097 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019098#endif
19099 rettv->vval.v_number = nr;
19100}
19101
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019102
19103#ifdef FEAT_WINDOWS
19104static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19105
19106/*
19107 * Common code for tabpagewinnr() and winnr().
19108 */
19109 static int
19110get_winnr(tp, argvar)
19111 tabpage_T *tp;
19112 typval_T *argvar;
19113{
19114 win_T *twin;
19115 int nr = 1;
19116 win_T *wp;
19117 char_u *arg;
19118
19119 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19120 if (argvar->v_type != VAR_UNKNOWN)
19121 {
19122 arg = get_tv_string_chk(argvar);
19123 if (arg == NULL)
19124 nr = 0; /* type error; errmsg already given */
19125 else if (STRCMP(arg, "$") == 0)
19126 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19127 else if (STRCMP(arg, "#") == 0)
19128 {
19129 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19130 if (twin == NULL)
19131 nr = 0;
19132 }
19133 else
19134 {
19135 EMSG2(_(e_invexpr2), arg);
19136 nr = 0;
19137 }
19138 }
19139
19140 if (nr > 0)
19141 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19142 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019143 {
19144 if (wp == NULL)
19145 {
19146 /* didn't find it in this tabpage */
19147 nr = 0;
19148 break;
19149 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019150 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019151 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019152 return nr;
19153}
19154#endif
19155
19156/*
19157 * "tabpagewinnr()" function
19158 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019159 static void
19160f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019161 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019162 typval_T *rettv;
19163{
19164 int nr = 1;
19165#ifdef FEAT_WINDOWS
19166 tabpage_T *tp;
19167
19168 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19169 if (tp == NULL)
19170 nr = 0;
19171 else
19172 nr = get_winnr(tp, &argvars[1]);
19173#endif
19174 rettv->vval.v_number = nr;
19175}
19176
19177
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019178/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019179 * "tagfiles()" function
19180 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019181 static void
19182f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019183 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019184 typval_T *rettv;
19185{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019186 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019187 tagname_T tn;
19188 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019189
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019190 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019191 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019192 fname = alloc(MAXPATHL);
19193 if (fname == NULL)
19194 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019195
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019196 for (first = TRUE; ; first = FALSE)
19197 if (get_tagfname(&tn, first, fname) == FAIL
19198 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019199 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019200 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019201 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019202}
19203
19204/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019205 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019206 */
19207 static void
19208f_taglist(argvars, rettv)
19209 typval_T *argvars;
19210 typval_T *rettv;
19211{
19212 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019213
19214 tag_pattern = get_tv_string(&argvars[0]);
19215
19216 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019217 if (*tag_pattern == NUL)
19218 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019219
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019220 if (rettv_list_alloc(rettv) == OK)
19221 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019222}
19223
19224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 * "tempname()" function
19226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019228f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019229 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231{
19232 static int x = 'A';
19233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019234 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019235 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236
19237 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19238 * names. Skip 'I' and 'O', they are used for shell redirection. */
19239 do
19240 {
19241 if (x == 'Z')
19242 x = '0';
19243 else if (x == '9')
19244 x = 'A';
19245 else
19246 {
19247#ifdef EBCDIC
19248 if (x == 'I')
19249 x = 'J';
19250 else if (x == 'R')
19251 x = 'S';
19252 else
19253#endif
19254 ++x;
19255 }
19256 } while (x == 'I' || x == 'O');
19257}
19258
19259/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019260 * "test(list)" function: Just checking the walls...
19261 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019262 static void
19263f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019264 typval_T *argvars UNUSED;
19265 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019266{
19267 /* Used for unit testing. Change the code below to your liking. */
19268#if 0
19269 listitem_T *li;
19270 list_T *l;
19271 char_u *bad, *good;
19272
19273 if (argvars[0].v_type != VAR_LIST)
19274 return;
19275 l = argvars[0].vval.v_list;
19276 if (l == NULL)
19277 return;
19278 li = l->lv_first;
19279 if (li == NULL)
19280 return;
19281 bad = get_tv_string(&li->li_tv);
19282 li = li->li_next;
19283 if (li == NULL)
19284 return;
19285 good = get_tv_string(&li->li_tv);
19286 rettv->vval.v_number = test_edit_score(bad, good);
19287#endif
19288}
19289
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019290#ifdef FEAT_FLOAT
19291/*
19292 * "tan()" function
19293 */
19294 static void
19295f_tan(argvars, rettv)
19296 typval_T *argvars;
19297 typval_T *rettv;
19298{
19299 float_T f;
19300
19301 rettv->v_type = VAR_FLOAT;
19302 if (get_float_arg(argvars, &f) == OK)
19303 rettv->vval.v_float = tan(f);
19304 else
19305 rettv->vval.v_float = 0.0;
19306}
19307
19308/*
19309 * "tanh()" function
19310 */
19311 static void
19312f_tanh(argvars, rettv)
19313 typval_T *argvars;
19314 typval_T *rettv;
19315{
19316 float_T f;
19317
19318 rettv->v_type = VAR_FLOAT;
19319 if (get_float_arg(argvars, &f) == OK)
19320 rettv->vval.v_float = tanh(f);
19321 else
19322 rettv->vval.v_float = 0.0;
19323}
19324#endif
19325
Bram Moolenaard52d9742005-08-21 22:20:28 +000019326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 * "tolower(string)" function
19328 */
19329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019330f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019331 typval_T *argvars;
19332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333{
19334 char_u *p;
19335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019336 p = vim_strsave(get_tv_string(&argvars[0]));
19337 rettv->v_type = VAR_STRING;
19338 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339
19340 if (p != NULL)
19341 while (*p != NUL)
19342 {
19343#ifdef FEAT_MBYTE
19344 int l;
19345
19346 if (enc_utf8)
19347 {
19348 int c, lc;
19349
19350 c = utf_ptr2char(p);
19351 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019352 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 /* TODO: reallocate string when byte count changes. */
19354 if (utf_char2len(lc) == l)
19355 utf_char2bytes(lc, p);
19356 p += l;
19357 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019358 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 p += l; /* skip multi-byte character */
19360 else
19361#endif
19362 {
19363 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19364 ++p;
19365 }
19366 }
19367}
19368
19369/*
19370 * "toupper(string)" function
19371 */
19372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019373f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019374 typval_T *argvars;
19375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019378 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379}
19380
19381/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019382 * "tr(string, fromstr, tostr)" function
19383 */
19384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019385f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019386 typval_T *argvars;
19387 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019388{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019389 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019390 char_u *fromstr;
19391 char_u *tostr;
19392 char_u *p;
19393#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019394 int inlen;
19395 int fromlen;
19396 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019397 int idx;
19398 char_u *cpstr;
19399 int cplen;
19400 int first = TRUE;
19401#endif
19402 char_u buf[NUMBUFLEN];
19403 char_u buf2[NUMBUFLEN];
19404 garray_T ga;
19405
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019406 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019407 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19408 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019409
19410 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019411 rettv->v_type = VAR_STRING;
19412 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019413 if (fromstr == NULL || tostr == NULL)
19414 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019415 ga_init2(&ga, (int)sizeof(char), 80);
19416
19417#ifdef FEAT_MBYTE
19418 if (!has_mbyte)
19419#endif
19420 /* not multi-byte: fromstr and tostr must be the same length */
19421 if (STRLEN(fromstr) != STRLEN(tostr))
19422 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019423#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019424error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019425#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019426 EMSG2(_(e_invarg2), fromstr);
19427 ga_clear(&ga);
19428 return;
19429 }
19430
19431 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019432 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019433 {
19434#ifdef FEAT_MBYTE
19435 if (has_mbyte)
19436 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019437 inlen = (*mb_ptr2len)(in_str);
19438 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019439 cplen = inlen;
19440 idx = 0;
19441 for (p = fromstr; *p != NUL; p += fromlen)
19442 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019443 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019444 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019445 {
19446 for (p = tostr; *p != NUL; p += tolen)
19447 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019448 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019449 if (idx-- == 0)
19450 {
19451 cplen = tolen;
19452 cpstr = p;
19453 break;
19454 }
19455 }
19456 if (*p == NUL) /* tostr is shorter than fromstr */
19457 goto error;
19458 break;
19459 }
19460 ++idx;
19461 }
19462
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019463 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019464 {
19465 /* Check that fromstr and tostr have the same number of
19466 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019467 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019468 first = FALSE;
19469 for (p = tostr; *p != NUL; p += tolen)
19470 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019471 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019472 --idx;
19473 }
19474 if (idx != 0)
19475 goto error;
19476 }
19477
19478 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019479 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019480 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019481
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019482 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019483 }
19484 else
19485#endif
19486 {
19487 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019488 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019489 if (p != NULL)
19490 ga_append(&ga, tostr[p - fromstr]);
19491 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019492 ga_append(&ga, *in_str);
19493 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019494 }
19495 }
19496
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019497 /* add a terminating NUL */
19498 ga_grow(&ga, 1);
19499 ga_append(&ga, NUL);
19500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019501 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019502}
19503
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019504#ifdef FEAT_FLOAT
19505/*
19506 * "trunc({float})" function
19507 */
19508 static void
19509f_trunc(argvars, rettv)
19510 typval_T *argvars;
19511 typval_T *rettv;
19512{
19513 float_T f;
19514
19515 rettv->v_type = VAR_FLOAT;
19516 if (get_float_arg(argvars, &f) == OK)
19517 /* trunc() is not in C90, use floor() or ceil() instead. */
19518 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19519 else
19520 rettv->vval.v_float = 0.0;
19521}
19522#endif
19523
Bram Moolenaar8299df92004-07-10 09:47:34 +000019524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 * "type(expr)" function
19526 */
19527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019528f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019529 typval_T *argvars;
19530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019532 int n;
19533
19534 switch (argvars[0].v_type)
19535 {
19536 case VAR_NUMBER: n = 0; break;
19537 case VAR_STRING: n = 1; break;
19538 case VAR_FUNC: n = 2; break;
19539 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019540 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019541#ifdef FEAT_FLOAT
19542 case VAR_FLOAT: n = 5; break;
19543#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019544 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19545 }
19546 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547}
19548
19549/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019550 * "undofile(name)" function
19551 */
19552 static void
19553f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019554 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019555 typval_T *rettv;
19556{
19557 rettv->v_type = VAR_STRING;
19558#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019559 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019560 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019561
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019562 if (*fname == NUL)
19563 {
19564 /* If there is no file name there will be no undo file. */
19565 rettv->vval.v_string = NULL;
19566 }
19567 else
19568 {
19569 char_u *ffname = FullName_save(fname, FALSE);
19570
19571 if (ffname != NULL)
19572 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19573 vim_free(ffname);
19574 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019575 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019576#else
19577 rettv->vval.v_string = NULL;
19578#endif
19579}
19580
19581/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019582 * "undotree()" function
19583 */
19584 static void
19585f_undotree(argvars, rettv)
19586 typval_T *argvars UNUSED;
19587 typval_T *rettv;
19588{
19589 if (rettv_dict_alloc(rettv) == OK)
19590 {
19591 dict_T *dict = rettv->vval.v_dict;
19592 list_T *list;
19593
Bram Moolenaar730cde92010-06-27 05:18:54 +020019594 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019595 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019596 dict_add_nr_str(dict, "save_last",
19597 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019598 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19599 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019600 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019601
19602 list = list_alloc();
19603 if (list != NULL)
19604 {
19605 u_eval_tree(curbuf->b_u_oldhead, list);
19606 dict_add_list(dict, "entries", list);
19607 }
19608 }
19609}
19610
19611/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019612 * "values(dict)" function
19613 */
19614 static void
19615f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 typval_T *argvars;
19617 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019618{
19619 dict_list(argvars, rettv, 1);
19620}
19621
19622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 * "virtcol(string)" function
19624 */
19625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019626f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019627 typval_T *argvars;
19628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629{
19630 colnr_T vcol = 0;
19631 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019632 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019634 fp = var2fpos(&argvars[0], FALSE, &fnum);
19635 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19636 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 {
19638 getvvcol(curwin, fp, NULL, NULL, &vcol);
19639 ++vcol;
19640 }
19641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019642 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643}
19644
19645/*
19646 * "visualmode()" function
19647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019649f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019650 typval_T *argvars UNUSED;
19651 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 char_u str[2];
19654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019655 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 str[0] = curbuf->b_visual_mode_eval;
19657 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019658 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659
19660 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019661 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663}
19664
19665/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019666 * "wildmenumode()" function
19667 */
19668 static void
19669f_wildmenumode(argvars, rettv)
19670 typval_T *argvars UNUSED;
19671 typval_T *rettv UNUSED;
19672{
19673#ifdef FEAT_WILDMENU
19674 if (wild_menu_showing)
19675 rettv->vval.v_number = 1;
19676#endif
19677}
19678
19679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 * "winbufnr(nr)" function
19681 */
19682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 typval_T *argvars;
19685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686{
19687 win_T *wp;
19688
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019689 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019693 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694}
19695
19696/*
19697 * "wincol()" function
19698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019700f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019701 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
19704 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019705 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706}
19707
19708/*
19709 * "winheight(nr)" function
19710 */
19711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019712f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019713 typval_T *argvars;
19714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715{
19716 win_T *wp;
19717
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019718 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723}
19724
19725/*
19726 * "winline()" function
19727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019730 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732{
19733 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735}
19736
19737/*
19738 * "winnr()" function
19739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019742 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744{
19745 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019746
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019748 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019750 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751}
19752
19753/*
19754 * "winrestcmd()" function
19755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760{
19761#ifdef FEAT_WINDOWS
19762 win_T *wp;
19763 int winnr = 1;
19764 garray_T ga;
19765 char_u buf[50];
19766
19767 ga_init2(&ga, (int)sizeof(char), 70);
19768 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19769 {
19770 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19771 ga_concat(&ga, buf);
19772# ifdef FEAT_VERTSPLIT
19773 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19774 ga_concat(&ga, buf);
19775# endif
19776 ++winnr;
19777 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019778 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785}
19786
19787/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019788 * "winrestview()" function
19789 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019790 static void
19791f_winrestview(argvars, rettv)
19792 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019793 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019794{
19795 dict_T *dict;
19796
19797 if (argvars[0].v_type != VAR_DICT
19798 || (dict = argvars[0].vval.v_dict) == NULL)
19799 EMSG(_(e_invarg));
19800 else
19801 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019802 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19803 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19804 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19805 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019806#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019807 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19808 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019809#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019810 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19811 {
19812 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19813 curwin->w_set_curswant = FALSE;
19814 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019815
Bram Moolenaar82c25852014-05-28 16:47:16 +020019816 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19817 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019818#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019819 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19820 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019821#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019822 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19823 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19824 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19825 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019826
19827 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019828 win_new_height(curwin, curwin->w_height);
19829# ifdef FEAT_VERTSPLIT
19830 win_new_width(curwin, W_WIDTH(curwin));
19831# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019832 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019833
Bram Moolenaarb851a962014-10-31 15:45:52 +010019834 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019835 curwin->w_topline = 1;
19836 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19837 curwin->w_topline = curbuf->b_ml.ml_line_count;
19838#ifdef FEAT_DIFF
19839 check_topfill(curwin, TRUE);
19840#endif
19841 }
19842}
19843
19844/*
19845 * "winsaveview()" function
19846 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019847 static void
19848f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019849 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019850 typval_T *rettv;
19851{
19852 dict_T *dict;
19853
Bram Moolenaara800b422010-06-27 01:15:55 +020019854 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019855 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019856 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019857
19858 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19859 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19860#ifdef FEAT_VIRTUALEDIT
19861 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19862#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019863 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019864 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19865
19866 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19867#ifdef FEAT_DIFF
19868 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19869#endif
19870 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19871 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19872}
19873
19874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 * "winwidth(nr)" function
19876 */
19877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019878f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019879 typval_T *argvars;
19880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881{
19882 win_T *wp;
19883
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019884 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019886 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887 else
19888#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019889 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019891 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892#endif
19893}
19894
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019896 * Write list of strings to file
19897 */
19898 static int
19899write_list(fd, list, binary)
19900 FILE *fd;
19901 list_T *list;
19902 int binary;
19903{
19904 listitem_T *li;
19905 int c;
19906 int ret = OK;
19907 char_u *s;
19908
19909 for (li = list->lv_first; li != NULL; li = li->li_next)
19910 {
19911 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19912 {
19913 if (*s == '\n')
19914 c = putc(NUL, fd);
19915 else
19916 c = putc(*s, fd);
19917 if (c == EOF)
19918 {
19919 ret = FAIL;
19920 break;
19921 }
19922 }
19923 if (!binary || li->li_next != NULL)
19924 if (putc('\n', fd) == EOF)
19925 {
19926 ret = FAIL;
19927 break;
19928 }
19929 if (ret == FAIL)
19930 {
19931 EMSG(_(e_write));
19932 break;
19933 }
19934 }
19935 return ret;
19936}
19937
19938/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019939 * "writefile()" function
19940 */
19941 static void
19942f_writefile(argvars, rettv)
19943 typval_T *argvars;
19944 typval_T *rettv;
19945{
19946 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019947 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019948 char_u *fname;
19949 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019950 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019951
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019952 if (check_restricted() || check_secure())
19953 return;
19954
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019955 if (argvars[0].v_type != VAR_LIST)
19956 {
19957 EMSG2(_(e_listarg), "writefile()");
19958 return;
19959 }
19960 if (argvars[0].vval.v_list == NULL)
19961 return;
19962
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019963 if (argvars[2].v_type != VAR_UNKNOWN)
19964 {
19965 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19966 binary = TRUE;
19967 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19968 append = TRUE;
19969 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019970
19971 /* Always open the file in binary mode, library functions have a mind of
19972 * their own about CR-LF conversion. */
19973 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019974 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19975 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019976 {
19977 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19978 ret = -1;
19979 }
19980 else
19981 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019982 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19983 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019984 fclose(fd);
19985 }
19986
19987 rettv->vval.v_number = ret;
19988}
19989
19990/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019991 * "xor(expr, expr)" function
19992 */
19993 static void
19994f_xor(argvars, rettv)
19995 typval_T *argvars;
19996 typval_T *rettv;
19997{
19998 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19999 ^ get_tv_number_chk(&argvars[1], NULL);
20000}
20001
20002
20003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020005 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 */
20007 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020008var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020009 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020010 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020011 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020013 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020015 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016
Bram Moolenaara5525202006-03-02 22:52:09 +000020017 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020018 if (varp->v_type == VAR_LIST)
20019 {
20020 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020021 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020022 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020023 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020024
20025 l = varp->vval.v_list;
20026 if (l == NULL)
20027 return NULL;
20028
20029 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020030 pos.lnum = list_find_nr(l, 0L, &error);
20031 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020032 return NULL; /* invalid line number */
20033
20034 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020035 pos.col = list_find_nr(l, 1L, &error);
20036 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020037 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020038 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020039
20040 /* We accept "$" for the column number: last column. */
20041 li = list_find(l, 1L);
20042 if (li != NULL && li->li_tv.v_type == VAR_STRING
20043 && li->li_tv.vval.v_string != NULL
20044 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20045 pos.col = len + 1;
20046
Bram Moolenaara5525202006-03-02 22:52:09 +000020047 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020048 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020049 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020050 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020051
Bram Moolenaara5525202006-03-02 22:52:09 +000020052#ifdef FEAT_VIRTUALEDIT
20053 /* Get the virtual offset. Defaults to zero. */
20054 pos.coladd = list_find_nr(l, 2L, &error);
20055 if (error)
20056 pos.coladd = 0;
20057#endif
20058
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020059 return &pos;
20060 }
20061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020062 name = get_tv_string_chk(varp);
20063 if (name == NULL)
20064 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020065 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020067 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20068 {
20069 if (VIsual_active)
20070 return &VIsual;
20071 return &curwin->w_cursor;
20072 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020073 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020075 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20077 return NULL;
20078 return pp;
20079 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020080
20081#ifdef FEAT_VIRTUALEDIT
20082 pos.coladd = 0;
20083#endif
20084
Bram Moolenaar477933c2007-07-17 14:32:23 +000020085 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020086 {
20087 pos.col = 0;
20088 if (name[1] == '0') /* "w0": first visible line */
20089 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020090 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020091 pos.lnum = curwin->w_topline;
20092 return &pos;
20093 }
20094 else if (name[1] == '$') /* "w$": last visible line */
20095 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020096 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020097 pos.lnum = curwin->w_botline - 1;
20098 return &pos;
20099 }
20100 }
20101 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020103 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 {
20105 pos.lnum = curbuf->b_ml.ml_line_count;
20106 pos.col = 0;
20107 }
20108 else
20109 {
20110 pos.lnum = curwin->w_cursor.lnum;
20111 pos.col = (colnr_T)STRLEN(ml_get_curline());
20112 }
20113 return &pos;
20114 }
20115 return NULL;
20116}
20117
20118/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020119 * Convert list in "arg" into a position and optional file number.
20120 * When "fnump" is NULL there is no file number, only 3 items.
20121 * Note that the column is passed on as-is, the caller may want to decrement
20122 * it to use 1 for the first column.
20123 * Return FAIL when conversion is not possible, doesn't check the position for
20124 * validity.
20125 */
20126 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020127list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020128 typval_T *arg;
20129 pos_T *posp;
20130 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020131 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020132{
20133 list_T *l = arg->vval.v_list;
20134 long i = 0;
20135 long n;
20136
Bram Moolenaar493c1782014-05-28 14:34:46 +020020137 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20138 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020139 if (arg->v_type != VAR_LIST
20140 || l == NULL
20141 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020142 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020143 return FAIL;
20144
20145 if (fnump != NULL)
20146 {
20147 n = list_find_nr(l, i++, NULL); /* fnum */
20148 if (n < 0)
20149 return FAIL;
20150 if (n == 0)
20151 n = curbuf->b_fnum; /* current buffer */
20152 *fnump = n;
20153 }
20154
20155 n = list_find_nr(l, i++, NULL); /* lnum */
20156 if (n < 0)
20157 return FAIL;
20158 posp->lnum = n;
20159
20160 n = list_find_nr(l, i++, NULL); /* col */
20161 if (n < 0)
20162 return FAIL;
20163 posp->col = n;
20164
20165#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020166 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020167 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020168 posp->coladd = 0;
20169 else
20170 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020171#endif
20172
Bram Moolenaar493c1782014-05-28 14:34:46 +020020173 if (curswantp != NULL)
20174 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20175
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020176 return OK;
20177}
20178
20179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 * Get the length of an environment variable name.
20181 * Advance "arg" to the first character after the name.
20182 * Return 0 for error.
20183 */
20184 static int
20185get_env_len(arg)
20186 char_u **arg;
20187{
20188 char_u *p;
20189 int len;
20190
20191 for (p = *arg; vim_isIDc(*p); ++p)
20192 ;
20193 if (p == *arg) /* no name found */
20194 return 0;
20195
20196 len = (int)(p - *arg);
20197 *arg = p;
20198 return len;
20199}
20200
20201/*
20202 * Get the length of the name of a function or internal variable.
20203 * "arg" is advanced to the first non-white character after the name.
20204 * Return 0 if something is wrong.
20205 */
20206 static int
20207get_id_len(arg)
20208 char_u **arg;
20209{
20210 char_u *p;
20211 int len;
20212
20213 /* Find the end of the name. */
20214 for (p = *arg; eval_isnamec(*p); ++p)
20215 ;
20216 if (p == *arg) /* no name found */
20217 return 0;
20218
20219 len = (int)(p - *arg);
20220 *arg = skipwhite(p);
20221
20222 return len;
20223}
20224
20225/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020226 * Get the length of the name of a variable or function.
20227 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020229 * Return -1 if curly braces expansion failed.
20230 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 * If the name contains 'magic' {}'s, expand them and return the
20232 * expanded name in an allocated string via 'alias' - caller must free.
20233 */
20234 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020235get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 char_u **arg;
20237 char_u **alias;
20238 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020239 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240{
20241 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 char_u *p;
20243 char_u *expr_start;
20244 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245
20246 *alias = NULL; /* default to no alias */
20247
20248 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20249 && (*arg)[2] == (int)KE_SNR)
20250 {
20251 /* hard coded <SNR>, already translated */
20252 *arg += 3;
20253 return get_id_len(arg) + 3;
20254 }
20255 len = eval_fname_script(*arg);
20256 if (len > 0)
20257 {
20258 /* literal "<SID>", "s:" or "<SNR>" */
20259 *arg += len;
20260 }
20261
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020265 p = find_name_end(*arg, &expr_start, &expr_end,
20266 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 if (expr_start != NULL)
20268 {
20269 char_u *temp_string;
20270
20271 if (!evaluate)
20272 {
20273 len += (int)(p - *arg);
20274 *arg = skipwhite(p);
20275 return len;
20276 }
20277
20278 /*
20279 * Include any <SID> etc in the expanded string:
20280 * Thus the -len here.
20281 */
20282 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20283 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020284 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 *alias = temp_string;
20286 *arg = skipwhite(p);
20287 return (int)STRLEN(temp_string);
20288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289
20290 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020291 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 EMSG2(_(e_invexpr2), *arg);
20293
20294 return len;
20295}
20296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020297/*
20298 * Find the end of a variable or function name, taking care of magic braces.
20299 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20300 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020301 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020302 * Return a pointer to just after the name. Equal to "arg" if there is no
20303 * valid name.
20304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020306find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 char_u *arg;
20308 char_u **expr_start;
20309 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020310 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020312 int mb_nest = 0;
20313 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 char_u *p;
20315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020316 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020318 *expr_start = NULL;
20319 *expr_end = NULL;
20320 }
20321
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020322 /* Quick check for valid starting character. */
20323 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20324 return arg;
20325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020326 for (p = arg; *p != NUL
20327 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020328 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020329 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020331 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020332 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020333 if (*p == '\'')
20334 {
20335 /* skip over 'string' to avoid counting [ and ] inside it. */
20336 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20337 ;
20338 if (*p == NUL)
20339 break;
20340 }
20341 else if (*p == '"')
20342 {
20343 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20344 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20345 if (*p == '\\' && p[1] != NUL)
20346 ++p;
20347 if (*p == NUL)
20348 break;
20349 }
20350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020351 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020353 if (*p == '[')
20354 ++br_nest;
20355 else if (*p == ']')
20356 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020361 if (*p == '{')
20362 {
20363 mb_nest++;
20364 if (expr_start != NULL && *expr_start == NULL)
20365 *expr_start = p;
20366 }
20367 else if (*p == '}')
20368 {
20369 mb_nest--;
20370 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20371 *expr_end = p;
20372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 }
20375
20376 return p;
20377}
20378
20379/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020380 * Expands out the 'magic' {}'s in a variable/function name.
20381 * Note that this can call itself recursively, to deal with
20382 * constructs like foo{bar}{baz}{bam}
20383 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20384 * "in_start" ^
20385 * "expr_start" ^
20386 * "expr_end" ^
20387 * "in_end" ^
20388 *
20389 * Returns a new allocated string, which the caller must free.
20390 * Returns NULL for failure.
20391 */
20392 static char_u *
20393make_expanded_name(in_start, expr_start, expr_end, in_end)
20394 char_u *in_start;
20395 char_u *expr_start;
20396 char_u *expr_end;
20397 char_u *in_end;
20398{
20399 char_u c1;
20400 char_u *retval = NULL;
20401 char_u *temp_result;
20402 char_u *nextcmd = NULL;
20403
20404 if (expr_end == NULL || in_end == NULL)
20405 return NULL;
20406 *expr_start = NUL;
20407 *expr_end = NUL;
20408 c1 = *in_end;
20409 *in_end = NUL;
20410
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020411 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020412 if (temp_result != NULL && nextcmd == NULL)
20413 {
20414 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20415 + (in_end - expr_end) + 1));
20416 if (retval != NULL)
20417 {
20418 STRCPY(retval, in_start);
20419 STRCAT(retval, temp_result);
20420 STRCAT(retval, expr_end + 1);
20421 }
20422 }
20423 vim_free(temp_result);
20424
20425 *in_end = c1; /* put char back for error messages */
20426 *expr_start = '{';
20427 *expr_end = '}';
20428
20429 if (retval != NULL)
20430 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020431 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020432 if (expr_start != NULL)
20433 {
20434 /* Further expansion! */
20435 temp_result = make_expanded_name(retval, expr_start,
20436 expr_end, temp_result);
20437 vim_free(retval);
20438 retval = temp_result;
20439 }
20440 }
20441
20442 return retval;
20443}
20444
20445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020447 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 */
20449 static int
20450eval_isnamec(c)
20451 int c;
20452{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020453 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20454}
20455
20456/*
20457 * Return TRUE if character "c" can be used as the first character in a
20458 * variable or function name (excluding '{' and '}').
20459 */
20460 static int
20461eval_isnamec1(c)
20462 int c;
20463{
20464 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465}
20466
20467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 * Set number v: variable to "val".
20469 */
20470 void
20471set_vim_var_nr(idx, val)
20472 int idx;
20473 long val;
20474{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020475 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476}
20477
20478/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020479 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480 */
20481 long
20482get_vim_var_nr(idx)
20483 int idx;
20484{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020485 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486}
20487
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020488/*
20489 * Get string v: variable value. Uses a static buffer, can only be used once.
20490 */
20491 char_u *
20492get_vim_var_str(idx)
20493 int idx;
20494{
20495 return get_tv_string(&vimvars[idx].vv_tv);
20496}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020497
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020499 * Get List v: variable value. Caller must take care of reference count when
20500 * needed.
20501 */
20502 list_T *
20503get_vim_var_list(idx)
20504 int idx;
20505{
20506 return vimvars[idx].vv_list;
20507}
20508
20509/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020510 * Set v:char to character "c".
20511 */
20512 void
20513set_vim_var_char(c)
20514 int c;
20515{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020516 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020517
20518#ifdef FEAT_MBYTE
20519 if (has_mbyte)
20520 buf[(*mb_char2bytes)(c, buf)] = NUL;
20521 else
20522#endif
20523 {
20524 buf[0] = c;
20525 buf[1] = NUL;
20526 }
20527 set_vim_var_string(VV_CHAR, buf, -1);
20528}
20529
20530/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020531 * Set v:count to "count" and v:count1 to "count1".
20532 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 */
20534 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020535set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 long count;
20537 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020538 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020540 if (set_prevcount)
20541 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020542 vimvars[VV_COUNT].vv_nr = count;
20543 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544}
20545
20546/*
20547 * Set string v: variable to a copy of "val".
20548 */
20549 void
20550set_vim_var_string(idx, val, len)
20551 int idx;
20552 char_u *val;
20553 int len; /* length of "val" to use or -1 (whole string) */
20554{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020555 /* Need to do this (at least) once, since we can't initialize a union.
20556 * Will always be invoked when "v:progname" is set. */
20557 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20558
Bram Moolenaare9a41262005-01-15 22:18:47 +000020559 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020561 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020563 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020565 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566}
20567
20568/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020569 * Set List v: variable to "val".
20570 */
20571 void
20572set_vim_var_list(idx, val)
20573 int idx;
20574 list_T *val;
20575{
20576 list_unref(vimvars[idx].vv_list);
20577 vimvars[idx].vv_list = val;
20578 if (val != NULL)
20579 ++val->lv_refcount;
20580}
20581
20582/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020583 * Set Dictionary v: variable to "val".
20584 */
20585 void
20586set_vim_var_dict(idx, val)
20587 int idx;
20588 dict_T *val;
20589{
20590 int todo;
20591 hashitem_T *hi;
20592
20593 dict_unref(vimvars[idx].vv_dict);
20594 vimvars[idx].vv_dict = val;
20595 if (val != NULL)
20596 {
20597 ++val->dv_refcount;
20598
20599 /* Set readonly */
20600 todo = (int)val->dv_hashtab.ht_used;
20601 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20602 {
20603 if (HASHITEM_EMPTY(hi))
20604 continue;
20605 --todo;
20606 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20607 }
20608 }
20609}
20610
20611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 * Set v:register if needed.
20613 */
20614 void
20615set_reg_var(c)
20616 int c;
20617{
20618 char_u regname;
20619
20620 if (c == 0 || c == ' ')
20621 regname = '"';
20622 else
20623 regname = c;
20624 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020625 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 set_vim_var_string(VV_REG, &regname, 1);
20627}
20628
20629/*
20630 * Get or set v:exception. If "oldval" == NULL, return the current value.
20631 * Otherwise, restore the value to "oldval" and return NULL.
20632 * Must always be called in pairs to save and restore v:exception! Does not
20633 * take care of memory allocations.
20634 */
20635 char_u *
20636v_exception(oldval)
20637 char_u *oldval;
20638{
20639 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020640 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641
Bram Moolenaare9a41262005-01-15 22:18:47 +000020642 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 return NULL;
20644}
20645
20646/*
20647 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20648 * Otherwise, restore the value to "oldval" and return NULL.
20649 * Must always be called in pairs to save and restore v:throwpoint! Does not
20650 * take care of memory allocations.
20651 */
20652 char_u *
20653v_throwpoint(oldval)
20654 char_u *oldval;
20655{
20656 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020657 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658
Bram Moolenaare9a41262005-01-15 22:18:47 +000020659 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 return NULL;
20661}
20662
20663#if defined(FEAT_AUTOCMD) || defined(PROTO)
20664/*
20665 * Set v:cmdarg.
20666 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20667 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20668 * Must always be called in pairs!
20669 */
20670 char_u *
20671set_cmdarg(eap, oldarg)
20672 exarg_T *eap;
20673 char_u *oldarg;
20674{
20675 char_u *oldval;
20676 char_u *newval;
20677 unsigned len;
20678
Bram Moolenaare9a41262005-01-15 22:18:47 +000020679 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020680 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020682 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020683 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020684 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 }
20686
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020687 if (eap->force_bin == FORCE_BIN)
20688 len = 6;
20689 else if (eap->force_bin == FORCE_NOBIN)
20690 len = 8;
20691 else
20692 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020693
20694 if (eap->read_edit)
20695 len += 7;
20696
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020697 if (eap->force_ff != 0)
20698 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20699# ifdef FEAT_MBYTE
20700 if (eap->force_enc != 0)
20701 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020702 if (eap->bad_char != 0)
20703 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020704# endif
20705
20706 newval = alloc(len + 1);
20707 if (newval == NULL)
20708 return NULL;
20709
20710 if (eap->force_bin == FORCE_BIN)
20711 sprintf((char *)newval, " ++bin");
20712 else if (eap->force_bin == FORCE_NOBIN)
20713 sprintf((char *)newval, " ++nobin");
20714 else
20715 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020716
20717 if (eap->read_edit)
20718 STRCAT(newval, " ++edit");
20719
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020720 if (eap->force_ff != 0)
20721 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20722 eap->cmd + eap->force_ff);
20723# ifdef FEAT_MBYTE
20724 if (eap->force_enc != 0)
20725 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20726 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020727 if (eap->bad_char == BAD_KEEP)
20728 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20729 else if (eap->bad_char == BAD_DROP)
20730 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20731 else if (eap->bad_char != 0)
20732 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020733# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020734 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020735 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736}
20737#endif
20738
20739/*
20740 * Get the value of internal variable "name".
20741 * Return OK or FAIL.
20742 */
20743 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020744get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 char_u *name;
20746 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020747 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020748 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020749 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020750 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751{
20752 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020753 typval_T *tv = NULL;
20754 typval_T atv;
20755 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757
20758 /* truncate the name, so that we can use strcmp() */
20759 cc = name[len];
20760 name[len] = NUL;
20761
20762 /*
20763 * Check for "b:changedtick".
20764 */
20765 if (STRCMP(name, "b:changedtick") == 0)
20766 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020767 atv.v_type = VAR_NUMBER;
20768 atv.vval.v_number = curbuf->b_changedtick;
20769 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 }
20771
20772 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 * Check for user-defined variables.
20774 */
20775 else
20776 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020777 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020779 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020780 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020781 if (dip != NULL)
20782 *dip = v;
20783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 }
20785
Bram Moolenaare9a41262005-01-15 22:18:47 +000020786 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020788 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 ret = FAIL;
20791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020792 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020793 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794
20795 name[len] = cc;
20796
20797 return ret;
20798}
20799
20800/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020801 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20802 * Also handle function call with Funcref variable: func(expr)
20803 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20804 */
20805 static int
20806handle_subscript(arg, rettv, evaluate, verbose)
20807 char_u **arg;
20808 typval_T *rettv;
20809 int evaluate; /* do more than finding the end */
20810 int verbose; /* give error messages */
20811{
20812 int ret = OK;
20813 dict_T *selfdict = NULL;
20814 char_u *s;
20815 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020816 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020817
20818 while (ret == OK
20819 && (**arg == '['
20820 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020821 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020822 && !vim_iswhite(*(*arg - 1)))
20823 {
20824 if (**arg == '(')
20825 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020826 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020827 if (evaluate)
20828 {
20829 functv = *rettv;
20830 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020831
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020832 /* Invoke the function. Recursive! */
20833 s = functv.vval.v_string;
20834 }
20835 else
20836 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020837 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020838 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20839 &len, evaluate, selfdict);
20840
20841 /* Clear the funcref afterwards, so that deleting it while
20842 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020843 if (evaluate)
20844 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020845
20846 /* Stop the expression evaluation when immediately aborting on
20847 * error, or when an interrupt occurred or an exception was thrown
20848 * but not caught. */
20849 if (aborting())
20850 {
20851 if (ret == OK)
20852 clear_tv(rettv);
20853 ret = FAIL;
20854 }
20855 dict_unref(selfdict);
20856 selfdict = NULL;
20857 }
20858 else /* **arg == '[' || **arg == '.' */
20859 {
20860 dict_unref(selfdict);
20861 if (rettv->v_type == VAR_DICT)
20862 {
20863 selfdict = rettv->vval.v_dict;
20864 if (selfdict != NULL)
20865 ++selfdict->dv_refcount;
20866 }
20867 else
20868 selfdict = NULL;
20869 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20870 {
20871 clear_tv(rettv);
20872 ret = FAIL;
20873 }
20874 }
20875 }
20876 dict_unref(selfdict);
20877 return ret;
20878}
20879
20880/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020881 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020882 * value).
20883 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020884 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020885alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020886{
Bram Moolenaar33570922005-01-25 22:26:29 +000020887 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020888}
20889
20890/*
20891 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 * The string "s" must have been allocated, it is consumed.
20893 * Return NULL for out of memory, the variable otherwise.
20894 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020895 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020896alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 char_u *s;
20898{
Bram Moolenaar33570922005-01-25 22:26:29 +000020899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020901 rettv = alloc_tv();
20902 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020904 rettv->v_type = VAR_STRING;
20905 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 }
20907 else
20908 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020909 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910}
20911
20912/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020913 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020915 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020916free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020917 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918{
20919 if (varp != NULL)
20920 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020921 switch (varp->v_type)
20922 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020923 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020924 func_unref(varp->vval.v_string);
20925 /*FALLTHROUGH*/
20926 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020927 vim_free(varp->vval.v_string);
20928 break;
20929 case VAR_LIST:
20930 list_unref(varp->vval.v_list);
20931 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020932 case VAR_DICT:
20933 dict_unref(varp->vval.v_dict);
20934 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020935 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020936#ifdef FEAT_FLOAT
20937 case VAR_FLOAT:
20938#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020939 case VAR_UNKNOWN:
20940 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020941 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020942 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020943 break;
20944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 vim_free(varp);
20946 }
20947}
20948
20949/*
20950 * Free the memory for a variable value and set the value to NULL or 0.
20951 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020952 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020953clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020954 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955{
20956 if (varp != NULL)
20957 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020958 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020960 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020961 func_unref(varp->vval.v_string);
20962 /*FALLTHROUGH*/
20963 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020964 vim_free(varp->vval.v_string);
20965 varp->vval.v_string = NULL;
20966 break;
20967 case VAR_LIST:
20968 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020969 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020970 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020971 case VAR_DICT:
20972 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020973 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020974 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020975 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020976 varp->vval.v_number = 0;
20977 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020978#ifdef FEAT_FLOAT
20979 case VAR_FLOAT:
20980 varp->vval.v_float = 0.0;
20981 break;
20982#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020983 case VAR_UNKNOWN:
20984 break;
20985 default:
20986 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020988 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 }
20990}
20991
20992/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020993 * Set the value of a variable to NULL without freeing items.
20994 */
20995 static void
20996init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020997 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020998{
20999 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021000 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021001}
21002
21003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 * Get the number value of a variable.
21005 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021006 * For incompatible types, return 0.
21007 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21008 * caller of incompatible types: it sets *denote to TRUE if "denote"
21009 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 */
21011 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021012get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021013 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021015 int error = FALSE;
21016
21017 return get_tv_number_chk(varp, &error); /* return 0L on error */
21018}
21019
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021020 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021021get_tv_number_chk(varp, denote)
21022 typval_T *varp;
21023 int *denote;
21024{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021025 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021027 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021029 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021030 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021031#ifdef FEAT_FLOAT
21032 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021033 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021034 break;
21035#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021036 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021037 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021038 break;
21039 case VAR_STRING:
21040 if (varp->vval.v_string != NULL)
21041 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020021042 TRUE, TRUE, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021043 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021044 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021045 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021046 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021047 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021048 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021049 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021050 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021051 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021052 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021054 if (denote == NULL) /* useful for values that must be unsigned */
21055 n = -1;
21056 else
21057 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021058 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059}
21060
21061/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021062 * Get the lnum from the first argument.
21063 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021064 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 */
21066 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021067get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021068 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069{
Bram Moolenaar33570922005-01-25 22:26:29 +000021070 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 linenr_T lnum;
21072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021073 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 if (lnum == 0) /* no valid number, try using line() */
21075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021076 rettv.v_type = VAR_NUMBER;
21077 f_line(argvars, &rettv);
21078 lnum = rettv.vval.v_number;
21079 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 }
21081 return lnum;
21082}
21083
21084/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021085 * Get the lnum from the first argument.
21086 * Also accepts "$", then "buf" is used.
21087 * Returns 0 on error.
21088 */
21089 static linenr_T
21090get_tv_lnum_buf(argvars, buf)
21091 typval_T *argvars;
21092 buf_T *buf;
21093{
21094 if (argvars[0].v_type == VAR_STRING
21095 && argvars[0].vval.v_string != NULL
21096 && argvars[0].vval.v_string[0] == '$'
21097 && buf != NULL)
21098 return buf->b_ml.ml_line_count;
21099 return get_tv_number_chk(&argvars[0], NULL);
21100}
21101
21102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 * Get the string value of a variable.
21104 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021105 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21106 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 * If the String variable has never been set, return an empty string.
21108 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021109 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21110 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 */
21112 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021113get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021114 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115{
21116 static char_u mybuf[NUMBUFLEN];
21117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021118 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119}
21120
21121 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021122get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021123 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021124 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021126 char_u *res = get_tv_string_buf_chk(varp, buf);
21127
21128 return res != NULL ? res : (char_u *)"";
21129}
21130
Bram Moolenaar7d647822014-04-05 21:28:56 +020021131/*
21132 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21133 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021134 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021135get_tv_string_chk(varp)
21136 typval_T *varp;
21137{
21138 static char_u mybuf[NUMBUFLEN];
21139
21140 return get_tv_string_buf_chk(varp, mybuf);
21141}
21142
21143 static char_u *
21144get_tv_string_buf_chk(varp, buf)
21145 typval_T *varp;
21146 char_u *buf;
21147{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021148 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021150 case VAR_NUMBER:
21151 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21152 return buf;
21153 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021154 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021155 break;
21156 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021157 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021158 break;
21159 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021160 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021162#ifdef FEAT_FLOAT
21163 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021164 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021165 break;
21166#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021167 case VAR_STRING:
21168 if (varp->vval.v_string != NULL)
21169 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021170 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021171 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021172 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021173 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021175 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176}
21177
21178/*
21179 * Find variable "name" in the list of variables.
21180 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021181 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021182 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021183 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021185 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021186find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021188 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021189 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021192 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193
Bram Moolenaara7043832005-01-21 11:56:39 +000021194 ht = find_var_ht(name, &varname);
21195 if (htp != NULL)
21196 *htp = ht;
21197 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021199 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200}
21201
21202/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021203 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021204 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021206 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021207find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021208 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021209 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021210 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021211 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021212{
Bram Moolenaar33570922005-01-25 22:26:29 +000021213 hashitem_T *hi;
21214
21215 if (*varname == NUL)
21216 {
21217 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021218 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021219 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021220 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021221 case 'g': return &globvars_var;
21222 case 'v': return &vimvars_var;
21223 case 'b': return &curbuf->b_bufvar;
21224 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021225#ifdef FEAT_WINDOWS
21226 case 't': return &curtab->tp_winvar;
21227#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021228 case 'l': return current_funccal == NULL
21229 ? NULL : &current_funccal->l_vars_var;
21230 case 'a': return current_funccal == NULL
21231 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021232 }
21233 return NULL;
21234 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021235
21236 hi = hash_find(ht, varname);
21237 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021238 {
21239 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021240 * worked find the variable again. Don't auto-load a script if it was
21241 * loaded already, otherwise it would be loaded every time when
21242 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021243 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021244 {
21245 /* Note: script_autoload() may make "hi" invalid. It must either
21246 * be obtained again or not used. */
21247 if (!script_autoload(varname, FALSE) || aborting())
21248 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021249 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021250 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021251 if (HASHITEM_EMPTY(hi))
21252 return NULL;
21253 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021254 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021255}
21256
21257/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021258 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021259 * Set "varname" to the start of name without ':'.
21260 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021261 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021262find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 char_u *name;
21264 char_u **varname;
21265{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021266 hashitem_T *hi;
21267
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 if (name[1] != ':')
21269 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021270 /* The name must not start with a colon or #. */
21271 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 return NULL;
21273 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021274
21275 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021276 hi = hash_find(&compat_hashtab, name);
21277 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021278 return &compat_hashtab;
21279
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 return &globvarht; /* global variable */
21282 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 }
21284 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021285 if (*name == 'g') /* global variable */
21286 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021287 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21288 */
21289 if (vim_strchr(name + 2, ':') != NULL
21290 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021291 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021293 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021295 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021296#ifdef FEAT_WINDOWS
21297 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021298 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021299#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021300 if (*name == 'v') /* v: variable */
21301 return &vimvarht;
21302 if (*name == 'a' && current_funccal != NULL) /* function argument */
21303 return &current_funccal->l_avars.dv_hashtab;
21304 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21305 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 if (*name == 's' /* script variable */
21307 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21308 return &SCRIPT_VARS(current_SID);
21309 return NULL;
21310}
21311
21312/*
21313 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021314 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 * Returns NULL when it doesn't exist.
21316 */
21317 char_u *
21318get_var_value(name)
21319 char_u *name;
21320{
Bram Moolenaar33570922005-01-25 22:26:29 +000021321 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021323 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 if (v == NULL)
21325 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021326 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327}
21328
21329/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021330 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 * sourcing this script and when executing functions defined in the script.
21332 */
21333 void
21334new_script_vars(id)
21335 scid_T id;
21336{
Bram Moolenaara7043832005-01-21 11:56:39 +000021337 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021338 hashtab_T *ht;
21339 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021340
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21342 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021343 /* Re-allocating ga_data means that an ht_array pointing to
21344 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021345 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021346 for (i = 1; i <= ga_scripts.ga_len; ++i)
21347 {
21348 ht = &SCRIPT_VARS(i);
21349 if (ht->ht_mask == HT_INIT_SIZE - 1)
21350 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021351 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021352 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021353 }
21354
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 while (ga_scripts.ga_len < id)
21356 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021357 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021358 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021359 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 }
21362 }
21363}
21364
21365/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021366 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21367 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 */
21369 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021370init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021371 dict_T *dict;
21372 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021373 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374{
Bram Moolenaar33570922005-01-25 22:26:29 +000021375 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021376 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021377 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021378 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021379 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021380 dict_var->di_tv.vval.v_dict = dict;
21381 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021382 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021383 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21384 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385}
21386
21387/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021388 * Unreference a dictionary initialized by init_var_dict().
21389 */
21390 void
21391unref_var_dict(dict)
21392 dict_T *dict;
21393{
21394 /* Now the dict needs to be freed if no one else is using it, go back to
21395 * normal reference counting. */
21396 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21397 dict_unref(dict);
21398}
21399
21400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021402 * Frees all allocated variables and the value they contain.
21403 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 */
21405 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021406vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021407 hashtab_T *ht;
21408{
21409 vars_clear_ext(ht, TRUE);
21410}
21411
21412/*
21413 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21414 */
21415 static void
21416vars_clear_ext(ht, free_val)
21417 hashtab_T *ht;
21418 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419{
Bram Moolenaara7043832005-01-21 11:56:39 +000021420 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021421 hashitem_T *hi;
21422 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423
Bram Moolenaar33570922005-01-25 22:26:29 +000021424 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021425 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021426 for (hi = ht->ht_array; todo > 0; ++hi)
21427 {
21428 if (!HASHITEM_EMPTY(hi))
21429 {
21430 --todo;
21431
Bram Moolenaar33570922005-01-25 22:26:29 +000021432 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021433 * ht_array might change then. hash_clear() takes care of it
21434 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021435 v = HI2DI(hi);
21436 if (free_val)
21437 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021438 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021439 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021440 }
21441 }
21442 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021443 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444}
21445
Bram Moolenaara7043832005-01-21 11:56:39 +000021446/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021447 * Delete a variable from hashtab "ht" at item "hi".
21448 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021451delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021452 hashtab_T *ht;
21453 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454{
Bram Moolenaar33570922005-01-25 22:26:29 +000021455 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021456
21457 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021458 clear_tv(&di->di_tv);
21459 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460}
21461
21462/*
21463 * List the value of one internal variable.
21464 */
21465 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021466list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021467 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021469 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021471 char_u *tofree;
21472 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021473 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021474
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021475 current_copyID += COPYID_INC;
21476 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021477 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021478 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021479 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480}
21481
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021483list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484 char_u *prefix;
21485 char_u *name;
21486 int type;
21487 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021488 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489{
Bram Moolenaar31859182007-08-14 20:41:13 +000021490 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21491 msg_start();
21492 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493 if (name != NULL) /* "a:" vars don't have a name stored */
21494 msg_puts(name);
21495 msg_putchar(' ');
21496 msg_advance(22);
21497 if (type == VAR_NUMBER)
21498 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021499 else if (type == VAR_FUNC)
21500 msg_putchar('*');
21501 else if (type == VAR_LIST)
21502 {
21503 msg_putchar('[');
21504 if (*string == '[')
21505 ++string;
21506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021507 else if (type == VAR_DICT)
21508 {
21509 msg_putchar('{');
21510 if (*string == '{')
21511 ++string;
21512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 else
21514 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021515
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021517
21518 if (type == VAR_FUNC)
21519 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021520 if (*first)
21521 {
21522 msg_clr_eos();
21523 *first = FALSE;
21524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525}
21526
21527/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021528 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 * If the variable already exists, the value is updated.
21530 * Otherwise the variable is created.
21531 */
21532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021533set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021536 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537{
Bram Moolenaar33570922005-01-25 22:26:29 +000021538 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021540 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021542 ht = find_var_ht(name, &varname);
21543 if (ht == NULL || *varname == NUL)
21544 {
21545 EMSG2(_(e_illvar), name);
21546 return;
21547 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021548 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021549
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021550 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21551 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021552
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021556 if (var_check_ro(v->di_flags, name, FALSE)
21557 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021558 return;
21559 if (v->di_tv.v_type != tv->v_type
21560 && !((v->di_tv.v_type == VAR_STRING
21561 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021562 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021563 || tv->v_type == VAR_NUMBER))
21564#ifdef FEAT_FLOAT
21565 && !((v->di_tv.v_type == VAR_NUMBER
21566 || v->di_tv.v_type == VAR_FLOAT)
21567 && (tv->v_type == VAR_NUMBER
21568 || tv->v_type == VAR_FLOAT))
21569#endif
21570 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021571 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021572 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021573 return;
21574 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021575
21576 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021577 * Handle setting internal v: variables separately where needed to
21578 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021579 */
21580 if (ht == &vimvarht)
21581 {
21582 if (v->di_tv.v_type == VAR_STRING)
21583 {
21584 vim_free(v->di_tv.vval.v_string);
21585 if (copy || tv->v_type != VAR_STRING)
21586 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21587 else
21588 {
21589 /* Take over the string to avoid an extra alloc/free. */
21590 v->di_tv.vval.v_string = tv->vval.v_string;
21591 tv->vval.v_string = NULL;
21592 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021593 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021594 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021595 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021596 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021597 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021598 if (STRCMP(varname, "searchforward") == 0)
21599 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021600#ifdef FEAT_SEARCH_EXTRA
21601 else if (STRCMP(varname, "hlsearch") == 0)
21602 {
21603 no_hlsearch = !v->di_tv.vval.v_number;
21604 redraw_all_later(SOME_VALID);
21605 }
21606#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021607 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021608 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021609 else if (v->di_tv.v_type != tv->v_type)
21610 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021611 }
21612
21613 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 }
21615 else /* add a new variable */
21616 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021617 /* Can't add "v:" variable. */
21618 if (ht == &vimvarht)
21619 {
21620 EMSG2(_(e_illvar), name);
21621 return;
21622 }
21623
Bram Moolenaar92124a32005-06-17 22:03:40 +000021624 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021625 if (!valid_varname(varname))
21626 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021627
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021628 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21629 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021630 if (v == NULL)
21631 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021632 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021633 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021635 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 return;
21637 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021638 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021640
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021641 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021642 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021643 else
21644 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021645 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021646 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021647 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649}
21650
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021651/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021652 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021653 * Also give an error message.
21654 */
21655 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021656var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021657 int flags;
21658 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021659 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021660{
21661 if (flags & DI_FLAGS_RO)
21662 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021663 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 return TRUE;
21665 }
21666 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21667 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021668 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021669 return TRUE;
21670 }
21671 return FALSE;
21672}
21673
21674/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021675 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21676 * Also give an error message.
21677 */
21678 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021679var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021680 int flags;
21681 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021682 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021683{
21684 if (flags & DI_FLAGS_FIX)
21685 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021686 EMSG2(_("E795: Cannot delete variable %s"),
21687 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021688 return TRUE;
21689 }
21690 return FALSE;
21691}
21692
21693/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021694 * Check if a funcref is assigned to a valid variable name.
21695 * Return TRUE and give an error if not.
21696 */
21697 static int
21698var_check_func_name(name, new_var)
21699 char_u *name; /* points to start of variable name */
21700 int new_var; /* TRUE when creating the variable */
21701{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021702 /* Allow for w: b: s: and t:. */
21703 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021704 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21705 ? name[2] : name[0]))
21706 {
21707 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21708 name);
21709 return TRUE;
21710 }
21711 /* Don't allow hiding a function. When "v" is not NULL we might be
21712 * assigning another function to the same var, the type is checked
21713 * below. */
21714 if (new_var && function_exists(name))
21715 {
21716 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21717 name);
21718 return TRUE;
21719 }
21720 return FALSE;
21721}
21722
21723/*
21724 * Check if a variable name is valid.
21725 * Return FALSE and give an error if not.
21726 */
21727 static int
21728valid_varname(varname)
21729 char_u *varname;
21730{
21731 char_u *p;
21732
21733 for (p = varname; *p != NUL; ++p)
21734 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21735 && *p != AUTOLOAD_CHAR)
21736 {
21737 EMSG2(_(e_illvar), varname);
21738 return FALSE;
21739 }
21740 return TRUE;
21741}
21742
21743/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021744 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021745 * Also give an error message, using "name" or _("name") when use_gettext is
21746 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021747 */
21748 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021749tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021750 int lock;
21751 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021752 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021753{
21754 if (lock & VAR_LOCKED)
21755 {
21756 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021757 name == NULL ? (char_u *)_("Unknown")
21758 : use_gettext ? (char_u *)_(name)
21759 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021760 return TRUE;
21761 }
21762 if (lock & VAR_FIXED)
21763 {
21764 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021765 name == NULL ? (char_u *)_("Unknown")
21766 : use_gettext ? (char_u *)_(name)
21767 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021768 return TRUE;
21769 }
21770 return FALSE;
21771}
21772
21773/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021775 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021776 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021777 * It is OK for "from" and "to" to point to the same item. This is used to
21778 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021779 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021780 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021781copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021782 typval_T *from;
21783 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021785 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021786 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021787 switch (from->v_type)
21788 {
21789 case VAR_NUMBER:
21790 to->vval.v_number = from->vval.v_number;
21791 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021792#ifdef FEAT_FLOAT
21793 case VAR_FLOAT:
21794 to->vval.v_float = from->vval.v_float;
21795 break;
21796#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021797 case VAR_STRING:
21798 case VAR_FUNC:
21799 if (from->vval.v_string == NULL)
21800 to->vval.v_string = NULL;
21801 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021802 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021803 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021804 if (from->v_type == VAR_FUNC)
21805 func_ref(to->vval.v_string);
21806 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021807 break;
21808 case VAR_LIST:
21809 if (from->vval.v_list == NULL)
21810 to->vval.v_list = NULL;
21811 else
21812 {
21813 to->vval.v_list = from->vval.v_list;
21814 ++to->vval.v_list->lv_refcount;
21815 }
21816 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021817 case VAR_DICT:
21818 if (from->vval.v_dict == NULL)
21819 to->vval.v_dict = NULL;
21820 else
21821 {
21822 to->vval.v_dict = from->vval.v_dict;
21823 ++to->vval.v_dict->dv_refcount;
21824 }
21825 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021826 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021827 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021828 break;
21829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830}
21831
21832/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021833 * Make a copy of an item.
21834 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021835 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21836 * reference to an already copied list/dict can be used.
21837 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021838 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021839 static int
21840item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021841 typval_T *from;
21842 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021843 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021844 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021845{
21846 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021847 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021848
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021850 {
21851 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021852 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021853 }
21854 ++recurse;
21855
21856 switch (from->v_type)
21857 {
21858 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021859#ifdef FEAT_FLOAT
21860 case VAR_FLOAT:
21861#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021862 case VAR_STRING:
21863 case VAR_FUNC:
21864 copy_tv(from, to);
21865 break;
21866 case VAR_LIST:
21867 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021868 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021869 if (from->vval.v_list == NULL)
21870 to->vval.v_list = NULL;
21871 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21872 {
21873 /* use the copy made earlier */
21874 to->vval.v_list = from->vval.v_list->lv_copylist;
21875 ++to->vval.v_list->lv_refcount;
21876 }
21877 else
21878 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21879 if (to->vval.v_list == NULL)
21880 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021881 break;
21882 case VAR_DICT:
21883 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021884 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021885 if (from->vval.v_dict == NULL)
21886 to->vval.v_dict = NULL;
21887 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21888 {
21889 /* use the copy made earlier */
21890 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21891 ++to->vval.v_dict->dv_refcount;
21892 }
21893 else
21894 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21895 if (to->vval.v_dict == NULL)
21896 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021897 break;
21898 default:
21899 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021900 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021901 }
21902 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021903 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021904}
21905
21906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 * ":echo expr1 ..." print each argument separated with a space, add a
21908 * newline at the end.
21909 * ":echon expr1 ..." print each argument plain.
21910 */
21911 void
21912ex_echo(eap)
21913 exarg_T *eap;
21914{
21915 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021916 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021917 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 char_u *p;
21919 int needclr = TRUE;
21920 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021921 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922
21923 if (eap->skip)
21924 ++emsg_skip;
21925 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021927 /* If eval1() causes an error message the text from the command may
21928 * still need to be cleared. E.g., "echo 22,44". */
21929 need_clr_eos = needclr;
21930
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021932 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 {
21934 /*
21935 * Report the invalid expression unless the expression evaluation
21936 * has been cancelled due to an aborting error, an interrupt, or an
21937 * exception.
21938 */
21939 if (!aborting())
21940 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021941 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 break;
21943 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021944 need_clr_eos = FALSE;
21945
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 if (!eap->skip)
21947 {
21948 if (atstart)
21949 {
21950 atstart = FALSE;
21951 /* Call msg_start() after eval1(), evaluating the expression
21952 * may cause a message to appear. */
21953 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021954 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021955 /* Mark the saved text as finishing the line, so that what
21956 * follows is displayed on a new line when scrolling back
21957 * at the more prompt. */
21958 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 }
21962 else if (eap->cmdidx == CMD_echo)
21963 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021964 current_copyID += COPYID_INC;
21965 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021966 if (p != NULL)
21967 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021969 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021971 if (*p != TAB && needclr)
21972 {
21973 /* remove any text still there from the command */
21974 msg_clr_eos();
21975 needclr = FALSE;
21976 }
21977 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 }
21979 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021980 {
21981#ifdef FEAT_MBYTE
21982 if (has_mbyte)
21983 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021984 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021985
21986 (void)msg_outtrans_len_attr(p, i, echo_attr);
21987 p += i - 1;
21988 }
21989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021991 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021994 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021996 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 arg = skipwhite(arg);
21998 }
21999 eap->nextcmd = check_nextcmd(arg);
22000
22001 if (eap->skip)
22002 --emsg_skip;
22003 else
22004 {
22005 /* remove text that may still be there from the command */
22006 if (needclr)
22007 msg_clr_eos();
22008 if (eap->cmdidx == CMD_echo)
22009 msg_end();
22010 }
22011}
22012
22013/*
22014 * ":echohl {name}".
22015 */
22016 void
22017ex_echohl(eap)
22018 exarg_T *eap;
22019{
22020 int id;
22021
22022 id = syn_name2id(eap->arg);
22023 if (id == 0)
22024 echo_attr = 0;
22025 else
22026 echo_attr = syn_id2attr(id);
22027}
22028
22029/*
22030 * ":execute expr1 ..." execute the result of an expression.
22031 * ":echomsg expr1 ..." Print a message
22032 * ":echoerr expr1 ..." Print an error
22033 * Each gets spaces around each argument and a newline at the end for
22034 * echo commands
22035 */
22036 void
22037ex_execute(eap)
22038 exarg_T *eap;
22039{
22040 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 int ret = OK;
22043 char_u *p;
22044 garray_T ga;
22045 int len;
22046 int save_did_emsg;
22047
22048 ga_init2(&ga, 1, 80);
22049
22050 if (eap->skip)
22051 ++emsg_skip;
22052 while (*arg != NUL && *arg != '|' && *arg != '\n')
22053 {
22054 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022055 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 {
22057 /*
22058 * Report the invalid expression unless the expression evaluation
22059 * has been cancelled due to an aborting error, an interrupt, or an
22060 * exception.
22061 */
22062 if (!aborting())
22063 EMSG2(_(e_invexpr2), p);
22064 ret = FAIL;
22065 break;
22066 }
22067
22068 if (!eap->skip)
22069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022070 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 len = (int)STRLEN(p);
22072 if (ga_grow(&ga, len + 2) == FAIL)
22073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022074 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 ret = FAIL;
22076 break;
22077 }
22078 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 ga.ga_len += len;
22082 }
22083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022084 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 arg = skipwhite(arg);
22086 }
22087
22088 if (ret != FAIL && ga.ga_data != NULL)
22089 {
22090 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022093 out_flush();
22094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 else if (eap->cmdidx == CMD_echoerr)
22096 {
22097 /* We don't want to abort following commands, restore did_emsg. */
22098 save_did_emsg = did_emsg;
22099 EMSG((char_u *)ga.ga_data);
22100 if (!force_abort)
22101 did_emsg = save_did_emsg;
22102 }
22103 else if (eap->cmdidx == CMD_execute)
22104 do_cmdline((char_u *)ga.ga_data,
22105 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22106 }
22107
22108 ga_clear(&ga);
22109
22110 if (eap->skip)
22111 --emsg_skip;
22112
22113 eap->nextcmd = check_nextcmd(arg);
22114}
22115
22116/*
22117 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22118 * "arg" points to the "&" or '+' when called, to "option" when returning.
22119 * Returns NULL when no option name found. Otherwise pointer to the char
22120 * after the option name.
22121 */
22122 static char_u *
22123find_option_end(arg, opt_flags)
22124 char_u **arg;
22125 int *opt_flags;
22126{
22127 char_u *p = *arg;
22128
22129 ++p;
22130 if (*p == 'g' && p[1] == ':')
22131 {
22132 *opt_flags = OPT_GLOBAL;
22133 p += 2;
22134 }
22135 else if (*p == 'l' && p[1] == ':')
22136 {
22137 *opt_flags = OPT_LOCAL;
22138 p += 2;
22139 }
22140 else
22141 *opt_flags = 0;
22142
22143 if (!ASCII_ISALPHA(*p))
22144 return NULL;
22145 *arg = p;
22146
22147 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22148 p += 4; /* termcap option */
22149 else
22150 while (ASCII_ISALPHA(*p))
22151 ++p;
22152 return p;
22153}
22154
22155/*
22156 * ":function"
22157 */
22158 void
22159ex_function(eap)
22160 exarg_T *eap;
22161{
22162 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022163 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 int j;
22165 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022167 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 char_u *name = NULL;
22169 char_u *p;
22170 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022171 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 garray_T newargs;
22173 garray_T newlines;
22174 int varargs = FALSE;
22175 int mustend = FALSE;
22176 int flags = 0;
22177 ufunc_T *fp;
22178 int indent;
22179 int nesting;
22180 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022181 dictitem_T *v;
22182 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022183 static int func_nr = 0; /* number for nameless function */
22184 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022185 hashtab_T *ht;
22186 int todo;
22187 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022188 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189
22190 /*
22191 * ":function" without argument: list functions.
22192 */
22193 if (ends_excmd(*eap->arg))
22194 {
22195 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022196 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022197 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022198 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022199 {
22200 if (!HASHITEM_EMPTY(hi))
22201 {
22202 --todo;
22203 fp = HI2UF(hi);
22204 if (!isdigit(*fp->uf_name))
22205 list_func_head(fp, FALSE);
22206 }
22207 }
22208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 eap->nextcmd = check_nextcmd(eap->arg);
22210 return;
22211 }
22212
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022213 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022214 * ":function /pat": list functions matching pattern.
22215 */
22216 if (*eap->arg == '/')
22217 {
22218 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22219 if (!eap->skip)
22220 {
22221 regmatch_T regmatch;
22222
22223 c = *p;
22224 *p = NUL;
22225 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22226 *p = c;
22227 if (regmatch.regprog != NULL)
22228 {
22229 regmatch.rm_ic = p_ic;
22230
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022231 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022232 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22233 {
22234 if (!HASHITEM_EMPTY(hi))
22235 {
22236 --todo;
22237 fp = HI2UF(hi);
22238 if (!isdigit(*fp->uf_name)
22239 && vim_regexec(&regmatch, fp->uf_name, 0))
22240 list_func_head(fp, FALSE);
22241 }
22242 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022243 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022244 }
22245 }
22246 if (*p == '/')
22247 ++p;
22248 eap->nextcmd = check_nextcmd(p);
22249 return;
22250 }
22251
22252 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022253 * Get the function name. There are these situations:
22254 * func normal function name
22255 * "name" == func, "fudi.fd_dict" == NULL
22256 * dict.func new dictionary entry
22257 * "name" == NULL, "fudi.fd_dict" set,
22258 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22259 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022260 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022261 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22262 * dict.func existing dict entry that's not a Funcref
22263 * "name" == NULL, "fudi.fd_dict" set,
22264 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022265 * s:func script-local function name
22266 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022269 name = trans_function_name(&p, eap->skip, 0, &fudi);
22270 paren = (vim_strchr(p, '(') != NULL);
22271 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 {
22273 /*
22274 * Return on an invalid expression in braces, unless the expression
22275 * evaluation has been cancelled due to an aborting error, an
22276 * interrupt, or an exception.
22277 */
22278 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022280 if (!eap->skip && fudi.fd_newkey != NULL)
22281 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022282 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 else
22286 eap->skip = TRUE;
22287 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022288
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 /* An error in a function call during evaluation of an expression in magic
22290 * braces should not cause the function not to be defined. */
22291 saved_did_emsg = did_emsg;
22292 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293
22294 /*
22295 * ":function func" with only function name: list function.
22296 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022297 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 {
22299 if (!ends_excmd(*skipwhite(p)))
22300 {
22301 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022302 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 }
22304 eap->nextcmd = check_nextcmd(p);
22305 if (eap->nextcmd != NULL)
22306 *p = NUL;
22307 if (!eap->skip && !got_int)
22308 {
22309 fp = find_func(name);
22310 if (fp != NULL)
22311 {
22312 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022313 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022315 if (FUNCLINE(fp, j) == NULL)
22316 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 msg_putchar('\n');
22318 msg_outnum((long)(j + 1));
22319 if (j < 9)
22320 msg_putchar(' ');
22321 if (j < 99)
22322 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022323 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 out_flush(); /* show a line at a time */
22325 ui_breakcheck();
22326 }
22327 if (!got_int)
22328 {
22329 msg_putchar('\n');
22330 msg_puts((char_u *)" endfunction");
22331 }
22332 }
22333 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022334 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022336 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 }
22338
22339 /*
22340 * ":function name(arg1, arg2)" Define function.
22341 */
22342 p = skipwhite(p);
22343 if (*p != '(')
22344 {
22345 if (!eap->skip)
22346 {
22347 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 }
22350 /* attempt to continue by skipping some text */
22351 if (vim_strchr(p, '(') != NULL)
22352 p = vim_strchr(p, '(');
22353 }
22354 p = skipwhite(p + 1);
22355
22356 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22357 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22358
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022359 if (!eap->skip)
22360 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022361 /* Check the name of the function. Unless it's a dictionary function
22362 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022363 if (name != NULL)
22364 arg = name;
22365 else
22366 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022367 if (arg != NULL && (fudi.fd_di == NULL
22368 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022369 {
22370 if (*arg == K_SPECIAL)
22371 j = 3;
22372 else
22373 j = 0;
22374 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22375 : eval_isnamec(arg[j])))
22376 ++j;
22377 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022378 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022379 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022380 /* Disallow using the g: dict. */
22381 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22382 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022383 }
22384
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 /*
22386 * Isolate the arguments: "arg1, arg2, ...)"
22387 */
22388 while (*p != ')')
22389 {
22390 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22391 {
22392 varargs = TRUE;
22393 p += 3;
22394 mustend = TRUE;
22395 }
22396 else
22397 {
22398 arg = p;
22399 while (ASCII_ISALNUM(*p) || *p == '_')
22400 ++p;
22401 if (arg == p || isdigit(*arg)
22402 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22403 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22404 {
22405 if (!eap->skip)
22406 EMSG2(_("E125: Illegal argument: %s"), arg);
22407 break;
22408 }
22409 if (ga_grow(&newargs, 1) == FAIL)
22410 goto erret;
22411 c = *p;
22412 *p = NUL;
22413 arg = vim_strsave(arg);
22414 if (arg == NULL)
22415 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022416
22417 /* Check for duplicate argument name. */
22418 for (i = 0; i < newargs.ga_len; ++i)
22419 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22420 {
22421 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022422 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022423 goto erret;
22424 }
22425
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22427 *p = c;
22428 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 if (*p == ',')
22430 ++p;
22431 else
22432 mustend = TRUE;
22433 }
22434 p = skipwhite(p);
22435 if (mustend && *p != ')')
22436 {
22437 if (!eap->skip)
22438 EMSG2(_(e_invarg2), eap->arg);
22439 break;
22440 }
22441 }
22442 ++p; /* skip the ')' */
22443
Bram Moolenaare9a41262005-01-15 22:18:47 +000022444 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 for (;;)
22446 {
22447 p = skipwhite(p);
22448 if (STRNCMP(p, "range", 5) == 0)
22449 {
22450 flags |= FC_RANGE;
22451 p += 5;
22452 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022453 else if (STRNCMP(p, "dict", 4) == 0)
22454 {
22455 flags |= FC_DICT;
22456 p += 4;
22457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 else if (STRNCMP(p, "abort", 5) == 0)
22459 {
22460 flags |= FC_ABORT;
22461 p += 5;
22462 }
22463 else
22464 break;
22465 }
22466
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022467 /* When there is a line break use what follows for the function body.
22468 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22469 if (*p == '\n')
22470 line_arg = p + 1;
22471 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 EMSG(_(e_trailing));
22473
22474 /*
22475 * Read the body of the function, until ":endfunction" is found.
22476 */
22477 if (KeyTyped)
22478 {
22479 /* Check if the function already exists, don't let the user type the
22480 * whole function before telling him it doesn't work! For a script we
22481 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022482 if (!eap->skip && !eap->forceit)
22483 {
22484 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22485 EMSG(_(e_funcdict));
22486 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022487 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022490 if (!eap->skip && did_emsg)
22491 goto erret;
22492
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 msg_putchar('\n'); /* don't overwrite the function name */
22494 cmdline_row = msg_row;
22495 }
22496
22497 indent = 2;
22498 nesting = 0;
22499 for (;;)
22500 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022501 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022502 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022503 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022504 saved_wait_return = FALSE;
22505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022507 sourcing_lnum_off = sourcing_lnum;
22508
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022509 if (line_arg != NULL)
22510 {
22511 /* Use eap->arg, split up in parts by line breaks. */
22512 theline = line_arg;
22513 p = vim_strchr(theline, '\n');
22514 if (p == NULL)
22515 line_arg += STRLEN(line_arg);
22516 else
22517 {
22518 *p = NUL;
22519 line_arg = p + 1;
22520 }
22521 }
22522 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 theline = getcmdline(':', 0L, indent);
22524 else
22525 theline = eap->getline(':', eap->cookie, indent);
22526 if (KeyTyped)
22527 lines_left = Rows - 1;
22528 if (theline == NULL)
22529 {
22530 EMSG(_("E126: Missing :endfunction"));
22531 goto erret;
22532 }
22533
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022534 /* Detect line continuation: sourcing_lnum increased more than one. */
22535 if (sourcing_lnum > sourcing_lnum_off + 1)
22536 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22537 else
22538 sourcing_lnum_off = 0;
22539
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 if (skip_until != NULL)
22541 {
22542 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22543 * don't check for ":endfunc". */
22544 if (STRCMP(theline, skip_until) == 0)
22545 {
22546 vim_free(skip_until);
22547 skip_until = NULL;
22548 }
22549 }
22550 else
22551 {
22552 /* skip ':' and blanks*/
22553 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22554 ;
22555
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022556 /* Check for "endfunction". */
22557 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022559 if (line_arg == NULL)
22560 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 break;
22562 }
22563
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022564 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 * at "end". */
22566 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22567 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022568 else if (STRNCMP(p, "if", 2) == 0
22569 || STRNCMP(p, "wh", 2) == 0
22570 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 || STRNCMP(p, "try", 3) == 0)
22572 indent += 2;
22573
22574 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022575 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022577 if (*p == '!')
22578 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022580 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22581 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022583 ++nesting;
22584 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 }
22586 }
22587
22588 /* Check for ":append" or ":insert". */
22589 p = skip_range(p, NULL);
22590 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22591 || (p[0] == 'i'
22592 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22593 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22594 skip_until = vim_strsave((char_u *)".");
22595
22596 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22597 arg = skipwhite(skiptowhite(p));
22598 if (arg[0] == '<' && arg[1] =='<'
22599 && ((p[0] == 'p' && p[1] == 'y'
22600 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22601 || (p[0] == 'p' && p[1] == 'e'
22602 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22603 || (p[0] == 't' && p[1] == 'c'
22604 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022605 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22606 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22608 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022609 || (p[0] == 'm' && p[1] == 'z'
22610 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 ))
22612 {
22613 /* ":python <<" continues until a dot, like ":append" */
22614 p = skipwhite(arg + 2);
22615 if (*p == NUL)
22616 skip_until = vim_strsave((char_u *)".");
22617 else
22618 skip_until = vim_strsave(p);
22619 }
22620 }
22621
22622 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022623 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022624 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022625 if (line_arg == NULL)
22626 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022628 }
22629
22630 /* Copy the line to newly allocated memory. get_one_sourceline()
22631 * allocates 250 bytes per line, this saves 80% on average. The cost
22632 * is an extra alloc/free. */
22633 p = vim_strsave(theline);
22634 if (p != NULL)
22635 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022636 if (line_arg == NULL)
22637 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022638 theline = p;
22639 }
22640
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022641 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22642
22643 /* Add NULL lines for continuation lines, so that the line count is
22644 * equal to the index in the growarray. */
22645 while (sourcing_lnum_off-- > 0)
22646 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022647
22648 /* Check for end of eap->arg. */
22649 if (line_arg != NULL && *line_arg == NUL)
22650 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 }
22652
22653 /* Don't define the function when skipping commands or when an error was
22654 * detected. */
22655 if (eap->skip || did_emsg)
22656 goto erret;
22657
22658 /*
22659 * If there are no errors, add the function
22660 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022661 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022662 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022663 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022664 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022665 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022666 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022667 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022668 goto erret;
22669 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022670
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022671 fp = find_func(name);
22672 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 if (!eap->forceit)
22675 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022676 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022677 goto erret;
22678 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022679 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022680 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022681 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022682 name);
22683 goto erret;
22684 }
22685 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022686 ga_clear_strings(&(fp->uf_args));
22687 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022688 vim_free(name);
22689 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 }
22692 else
22693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022694 char numbuf[20];
22695
22696 fp = NULL;
22697 if (fudi.fd_newkey == NULL && !eap->forceit)
22698 {
22699 EMSG(_(e_funcdict));
22700 goto erret;
22701 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022702 if (fudi.fd_di == NULL)
22703 {
22704 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022705 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022706 goto erret;
22707 }
22708 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022709 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022710 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022711
22712 /* Give the function a sequential number. Can only be used with a
22713 * Funcref! */
22714 vim_free(name);
22715 sprintf(numbuf, "%d", ++func_nr);
22716 name = vim_strsave((char_u *)numbuf);
22717 if (name == NULL)
22718 goto erret;
22719 }
22720
22721 if (fp == NULL)
22722 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022723 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022724 {
22725 int slen, plen;
22726 char_u *scriptname;
22727
22728 /* Check that the autoload name matches the script name. */
22729 j = FAIL;
22730 if (sourcing_name != NULL)
22731 {
22732 scriptname = autoload_name(name);
22733 if (scriptname != NULL)
22734 {
22735 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022736 plen = (int)STRLEN(p);
22737 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022738 if (slen > plen && fnamecmp(p,
22739 sourcing_name + slen - plen) == 0)
22740 j = OK;
22741 vim_free(scriptname);
22742 }
22743 }
22744 if (j == FAIL)
22745 {
22746 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22747 goto erret;
22748 }
22749 }
22750
22751 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 if (fp == NULL)
22753 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022754
22755 if (fudi.fd_dict != NULL)
22756 {
22757 if (fudi.fd_di == NULL)
22758 {
22759 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022760 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022761 if (fudi.fd_di == NULL)
22762 {
22763 vim_free(fp);
22764 goto erret;
22765 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022766 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22767 {
22768 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022769 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022770 goto erret;
22771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022772 }
22773 else
22774 /* overwrite existing dict entry */
22775 clear_tv(&fudi.fd_di->di_tv);
22776 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022777 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022778 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022779 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022780
22781 /* behave like "dict" was used */
22782 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022783 }
22784
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022786 STRCPY(fp->uf_name, name);
22787 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022789 fp->uf_args = newargs;
22790 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022791#ifdef FEAT_PROFILE
22792 fp->uf_tml_count = NULL;
22793 fp->uf_tml_total = NULL;
22794 fp->uf_tml_self = NULL;
22795 fp->uf_profiling = FALSE;
22796 if (prof_def_func())
22797 func_do_profile(fp);
22798#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022799 fp->uf_varargs = varargs;
22800 fp->uf_flags = flags;
22801 fp->uf_calls = 0;
22802 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022803 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804
22805erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 ga_clear_strings(&newargs);
22807 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022808ret_free:
22809 vim_free(skip_until);
22810 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022813 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814}
22815
22816/*
22817 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022818 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022820 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022821 * TFN_INT: internal function name OK
22822 * TFN_QUIET: be quiet
22823 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 * Advances "pp" to just after the function name (if no error).
22825 */
22826 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022827trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 char_u **pp;
22829 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022830 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022831 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022833 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 char_u *start;
22835 char_u *end;
22836 int lead;
22837 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022839 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022840
22841 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022842 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022844
22845 /* Check for hard coded <SNR>: already translated function ID (from a user
22846 * command). */
22847 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22848 && (*pp)[2] == (int)KE_SNR)
22849 {
22850 *pp += 3;
22851 len = get_id_len(pp) + 3;
22852 return vim_strnsave(start, len);
22853 }
22854
22855 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22856 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022858 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022860
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022861 /* Note that TFN_ flags use the same values as GLV_ flags. */
22862 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022863 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022864 if (end == start)
22865 {
22866 if (!skip)
22867 EMSG(_("E129: Function name required"));
22868 goto theend;
22869 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022870 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022871 {
22872 /*
22873 * Report an invalid expression in braces, unless the expression
22874 * evaluation has been cancelled due to an aborting error, an
22875 * interrupt, or an exception.
22876 */
22877 if (!aborting())
22878 {
22879 if (end != NULL)
22880 EMSG2(_(e_invarg2), start);
22881 }
22882 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022883 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022884 goto theend;
22885 }
22886
22887 if (lv.ll_tv != NULL)
22888 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022889 if (fdp != NULL)
22890 {
22891 fdp->fd_dict = lv.ll_dict;
22892 fdp->fd_newkey = lv.ll_newkey;
22893 lv.ll_newkey = NULL;
22894 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022895 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022896 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22897 {
22898 name = vim_strsave(lv.ll_tv->vval.v_string);
22899 *pp = end;
22900 }
22901 else
22902 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022903 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22904 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022905 EMSG(_(e_funcref));
22906 else
22907 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022908 name = NULL;
22909 }
22910 goto theend;
22911 }
22912
22913 if (lv.ll_name == NULL)
22914 {
22915 /* Error found, but continue after the function name. */
22916 *pp = end;
22917 goto theend;
22918 }
22919
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022920 /* Check if the name is a Funcref. If so, use the value. */
22921 if (lv.ll_exp_name != NULL)
22922 {
22923 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022924 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022925 if (name == lv.ll_exp_name)
22926 name = NULL;
22927 }
22928 else
22929 {
22930 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022931 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022932 if (name == *pp)
22933 name = NULL;
22934 }
22935 if (name != NULL)
22936 {
22937 name = vim_strsave(name);
22938 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022939 if (STRNCMP(name, "<SNR>", 5) == 0)
22940 {
22941 /* Change "<SNR>" to the byte sequence. */
22942 name[0] = K_SPECIAL;
22943 name[1] = KS_EXTRA;
22944 name[2] = (int)KE_SNR;
22945 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22946 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022947 goto theend;
22948 }
22949
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022950 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022951 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022952 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022953 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22954 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22955 {
22956 /* When there was "s:" already or the name expanded to get a
22957 * leading "s:" then remove it. */
22958 lv.ll_name += 2;
22959 len -= 2;
22960 lead = 2;
22961 }
22962 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022963 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022964 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022965 /* skip over "s:" and "g:" */
22966 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022967 lv.ll_name += 2;
22968 len = (int)(end - lv.ll_name);
22969 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022970
22971 /*
22972 * Copy the function name to allocated memory.
22973 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22974 * Accept <SNR>123_name() outside a script.
22975 */
22976 if (skip)
22977 lead = 0; /* do nothing */
22978 else if (lead > 0)
22979 {
22980 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022981 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22982 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022983 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022984 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022985 if (current_SID <= 0)
22986 {
22987 EMSG(_(e_usingsid));
22988 goto theend;
22989 }
22990 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22991 lead += (int)STRLEN(sid_buf);
22992 }
22993 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022994 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022995 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022996 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022997 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022998 goto theend;
22999 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023000 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023001 {
23002 char_u *cp = vim_strchr(lv.ll_name, ':');
23003
23004 if (cp != NULL && cp < end)
23005 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023006 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023007 goto theend;
23008 }
23009 }
23010
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023011 name = alloc((unsigned)(len + lead + 1));
23012 if (name != NULL)
23013 {
23014 if (lead > 0)
23015 {
23016 name[0] = K_SPECIAL;
23017 name[1] = KS_EXTRA;
23018 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023019 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023020 STRCPY(name + 3, sid_buf);
23021 }
23022 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023023 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023024 }
23025 *pp = end;
23026
23027theend:
23028 clear_lval(&lv);
23029 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030}
23031
23032/*
23033 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23034 * Return 2 if "p" starts with "s:".
23035 * Return 0 otherwise.
23036 */
23037 static int
23038eval_fname_script(p)
23039 char_u *p;
23040{
23041 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23042 || STRNICMP(p + 1, "SNR>", 4) == 0))
23043 return 5;
23044 if (p[0] == 's' && p[1] == ':')
23045 return 2;
23046 return 0;
23047}
23048
23049/*
23050 * Return TRUE if "p" starts with "<SID>" or "s:".
23051 * Only works if eval_fname_script() returned non-zero for "p"!
23052 */
23053 static int
23054eval_fname_sid(p)
23055 char_u *p;
23056{
23057 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23058}
23059
23060/*
23061 * List the head of the function: "name(arg1, arg2)".
23062 */
23063 static void
23064list_func_head(fp, indent)
23065 ufunc_T *fp;
23066 int indent;
23067{
23068 int j;
23069
23070 msg_start();
23071 if (indent)
23072 MSG_PUTS(" ");
23073 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023074 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 {
23076 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023077 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 }
23079 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023080 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023082 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 {
23084 if (j)
23085 MSG_PUTS(", ");
23086 msg_puts(FUNCARG(fp, j));
23087 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023088 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 {
23090 if (j)
23091 MSG_PUTS(", ");
23092 MSG_PUTS("...");
23093 }
23094 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023095 if (fp->uf_flags & FC_ABORT)
23096 MSG_PUTS(" abort");
23097 if (fp->uf_flags & FC_RANGE)
23098 MSG_PUTS(" range");
23099 if (fp->uf_flags & FC_DICT)
23100 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023101 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023102 if (p_verbose > 0)
23103 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104}
23105
23106/*
23107 * Find a function by name, return pointer to it in ufuncs.
23108 * Return NULL for unknown function.
23109 */
23110 static ufunc_T *
23111find_func(name)
23112 char_u *name;
23113{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023114 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023116 hi = hash_find(&func_hashtab, name);
23117 if (!HASHITEM_EMPTY(hi))
23118 return HI2UF(hi);
23119 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120}
23121
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023122#if defined(EXITFREE) || defined(PROTO)
23123 void
23124free_all_functions()
23125{
23126 hashitem_T *hi;
23127
23128 /* Need to start all over every time, because func_free() may change the
23129 * hash table. */
23130 while (func_hashtab.ht_used > 0)
23131 for (hi = func_hashtab.ht_array; ; ++hi)
23132 if (!HASHITEM_EMPTY(hi))
23133 {
23134 func_free(HI2UF(hi));
23135 break;
23136 }
23137}
23138#endif
23139
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023140 int
23141translated_function_exists(name)
23142 char_u *name;
23143{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023144 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023145 return find_internal_func(name) >= 0;
23146 return find_func(name) != NULL;
23147}
23148
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023149/*
23150 * Return TRUE if a function "name" exists.
23151 */
23152 static int
23153function_exists(name)
23154 char_u *name;
23155{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023156 char_u *nm = name;
23157 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023158 int n = FALSE;
23159
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023160 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23161 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023162 nm = skipwhite(nm);
23163
23164 /* Only accept "funcname", "funcname ", "funcname (..." and
23165 * "funcname(...", not "funcname!...". */
23166 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023167 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023168 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023169 return n;
23170}
23171
Bram Moolenaara1544c02013-05-30 12:35:52 +020023172 char_u *
23173get_expanded_name(name, check)
23174 char_u *name;
23175 int check;
23176{
23177 char_u *nm = name;
23178 char_u *p;
23179
23180 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23181
23182 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023183 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023184 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023185
Bram Moolenaara1544c02013-05-30 12:35:52 +020023186 vim_free(p);
23187 return NULL;
23188}
23189
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023190/*
23191 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023192 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23193 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023194 */
23195 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023196builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023197 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023198 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023199{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023200 char_u *p;
23201
23202 if (!ASCII_ISLOWER(name[0]))
23203 return FALSE;
23204 p = vim_strchr(name, AUTOLOAD_CHAR);
23205 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023206}
23207
Bram Moolenaar05159a02005-02-26 23:04:13 +000023208#if defined(FEAT_PROFILE) || defined(PROTO)
23209/*
23210 * Start profiling function "fp".
23211 */
23212 static void
23213func_do_profile(fp)
23214 ufunc_T *fp;
23215{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023216 int len = fp->uf_lines.ga_len;
23217
23218 if (len == 0)
23219 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023220 fp->uf_tm_count = 0;
23221 profile_zero(&fp->uf_tm_self);
23222 profile_zero(&fp->uf_tm_total);
23223 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023224 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023225 if (fp->uf_tml_total == NULL)
23226 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023227 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023228 if (fp->uf_tml_self == NULL)
23229 fp->uf_tml_self = (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 fp->uf_tml_idx = -1;
23232 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23233 || fp->uf_tml_self == NULL)
23234 return; /* out of memory */
23235
23236 fp->uf_profiling = TRUE;
23237}
23238
23239/*
23240 * Dump the profiling results for all functions in file "fd".
23241 */
23242 void
23243func_dump_profile(fd)
23244 FILE *fd;
23245{
23246 hashitem_T *hi;
23247 int todo;
23248 ufunc_T *fp;
23249 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023250 ufunc_T **sorttab;
23251 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023252
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023253 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023254 if (todo == 0)
23255 return; /* nothing to dump */
23256
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023257 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023258
Bram Moolenaar05159a02005-02-26 23:04:13 +000023259 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23260 {
23261 if (!HASHITEM_EMPTY(hi))
23262 {
23263 --todo;
23264 fp = HI2UF(hi);
23265 if (fp->uf_profiling)
23266 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023267 if (sorttab != NULL)
23268 sorttab[st_len++] = fp;
23269
Bram Moolenaar05159a02005-02-26 23:04:13 +000023270 if (fp->uf_name[0] == K_SPECIAL)
23271 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23272 else
23273 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23274 if (fp->uf_tm_count == 1)
23275 fprintf(fd, "Called 1 time\n");
23276 else
23277 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23278 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23279 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23280 fprintf(fd, "\n");
23281 fprintf(fd, "count total (s) self (s)\n");
23282
23283 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23284 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023285 if (FUNCLINE(fp, i) == NULL)
23286 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023287 prof_func_line(fd, fp->uf_tml_count[i],
23288 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023289 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23290 }
23291 fprintf(fd, "\n");
23292 }
23293 }
23294 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023295
23296 if (sorttab != NULL && st_len > 0)
23297 {
23298 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23299 prof_total_cmp);
23300 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23301 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23302 prof_self_cmp);
23303 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23304 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023305
23306 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023307}
Bram Moolenaar73830342005-02-28 22:48:19 +000023308
23309 static void
23310prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23311 FILE *fd;
23312 ufunc_T **sorttab;
23313 int st_len;
23314 char *title;
23315 int prefer_self; /* when equal print only self time */
23316{
23317 int i;
23318 ufunc_T *fp;
23319
23320 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23321 fprintf(fd, "count total (s) self (s) function\n");
23322 for (i = 0; i < 20 && i < st_len; ++i)
23323 {
23324 fp = sorttab[i];
23325 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23326 prefer_self);
23327 if (fp->uf_name[0] == K_SPECIAL)
23328 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23329 else
23330 fprintf(fd, " %s()\n", fp->uf_name);
23331 }
23332 fprintf(fd, "\n");
23333}
23334
23335/*
23336 * Print the count and times for one function or function line.
23337 */
23338 static void
23339prof_func_line(fd, count, total, self, prefer_self)
23340 FILE *fd;
23341 int count;
23342 proftime_T *total;
23343 proftime_T *self;
23344 int prefer_self; /* when equal print only self time */
23345{
23346 if (count > 0)
23347 {
23348 fprintf(fd, "%5d ", count);
23349 if (prefer_self && profile_equal(total, self))
23350 fprintf(fd, " ");
23351 else
23352 fprintf(fd, "%s ", profile_msg(total));
23353 if (!prefer_self && profile_equal(total, self))
23354 fprintf(fd, " ");
23355 else
23356 fprintf(fd, "%s ", profile_msg(self));
23357 }
23358 else
23359 fprintf(fd, " ");
23360}
23361
23362/*
23363 * Compare function for total time sorting.
23364 */
23365 static int
23366#ifdef __BORLANDC__
23367_RTLENTRYF
23368#endif
23369prof_total_cmp(s1, s2)
23370 const void *s1;
23371 const void *s2;
23372{
23373 ufunc_T *p1, *p2;
23374
23375 p1 = *(ufunc_T **)s1;
23376 p2 = *(ufunc_T **)s2;
23377 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23378}
23379
23380/*
23381 * Compare function for self time sorting.
23382 */
23383 static int
23384#ifdef __BORLANDC__
23385_RTLENTRYF
23386#endif
23387prof_self_cmp(s1, s2)
23388 const void *s1;
23389 const void *s2;
23390{
23391 ufunc_T *p1, *p2;
23392
23393 p1 = *(ufunc_T **)s1;
23394 p2 = *(ufunc_T **)s2;
23395 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23396}
23397
Bram Moolenaar05159a02005-02-26 23:04:13 +000023398#endif
23399
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023400/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023401 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023402 * Return TRUE if a package was loaded.
23403 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023404 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023405script_autoload(name, reload)
23406 char_u *name;
23407 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023408{
23409 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023410 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023411 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023412 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023413
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023414 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023415 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023416 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023417 return FALSE;
23418
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023419 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023420
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023421 /* Find the name in the list of previously loaded package names. Skip
23422 * "autoload/", it's always the same. */
23423 for (i = 0; i < ga_loaded.ga_len; ++i)
23424 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23425 break;
23426 if (!reload && i < ga_loaded.ga_len)
23427 ret = FALSE; /* was loaded already */
23428 else
23429 {
23430 /* Remember the name if it wasn't loaded already. */
23431 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23432 {
23433 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23434 tofree = NULL;
23435 }
23436
23437 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023438 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023439 ret = TRUE;
23440 }
23441
23442 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023443 return ret;
23444}
23445
23446/*
23447 * Return the autoload script name for a function or variable name.
23448 * Returns NULL when out of memory.
23449 */
23450 static char_u *
23451autoload_name(name)
23452 char_u *name;
23453{
23454 char_u *p;
23455 char_u *scriptname;
23456
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023457 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023458 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23459 if (scriptname == NULL)
23460 return FALSE;
23461 STRCPY(scriptname, "autoload/");
23462 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023463 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023464 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023465 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023466 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023467 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023468}
23469
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23471
23472/*
23473 * Function given to ExpandGeneric() to obtain the list of user defined
23474 * function names.
23475 */
23476 char_u *
23477get_user_func_name(xp, idx)
23478 expand_T *xp;
23479 int idx;
23480{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023481 static long_u done;
23482 static hashitem_T *hi;
23483 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484
23485 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023487 done = 0;
23488 hi = func_hashtab.ht_array;
23489 }
23490 if (done < func_hashtab.ht_used)
23491 {
23492 if (done++ > 0)
23493 ++hi;
23494 while (HASHITEM_EMPTY(hi))
23495 ++hi;
23496 fp = HI2UF(hi);
23497
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023498 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023499 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023500
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023501 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23502 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503
23504 cat_func_name(IObuff, fp);
23505 if (xp->xp_context != EXPAND_USER_FUNC)
23506 {
23507 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023508 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 STRCAT(IObuff, ")");
23510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 return IObuff;
23512 }
23513 return NULL;
23514}
23515
23516#endif /* FEAT_CMDL_COMPL */
23517
23518/*
23519 * Copy the function name of "fp" to buffer "buf".
23520 * "buf" must be able to hold the function name plus three bytes.
23521 * Takes care of script-local function names.
23522 */
23523 static void
23524cat_func_name(buf, fp)
23525 char_u *buf;
23526 ufunc_T *fp;
23527{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023528 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 {
23530 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023531 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 }
23533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023534 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535}
23536
23537/*
23538 * ":delfunction {name}"
23539 */
23540 void
23541ex_delfunction(eap)
23542 exarg_T *eap;
23543{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023544 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 char_u *p;
23546 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023547 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548
23549 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023550 name = trans_function_name(&p, eap->skip, 0, &fudi);
23551 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023553 {
23554 if (fudi.fd_dict != NULL && !eap->skip)
23555 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 if (!ends_excmd(*skipwhite(p)))
23559 {
23560 vim_free(name);
23561 EMSG(_(e_trailing));
23562 return;
23563 }
23564 eap->nextcmd = check_nextcmd(p);
23565 if (eap->nextcmd != NULL)
23566 *p = NUL;
23567
23568 if (!eap->skip)
23569 fp = find_func(name);
23570 vim_free(name);
23571
23572 if (!eap->skip)
23573 {
23574 if (fp == NULL)
23575 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023576 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 return;
23578 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023579 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 {
23581 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23582 return;
23583 }
23584
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023585 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023587 /* Delete the dict item that refers to the function, it will
23588 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023589 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023591 else
23592 func_free(fp);
23593 }
23594}
23595
23596/*
23597 * Free a function and remove it from the list of functions.
23598 */
23599 static void
23600func_free(fp)
23601 ufunc_T *fp;
23602{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023603 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023604
23605 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023606 ga_clear_strings(&(fp->uf_args));
23607 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023608#ifdef FEAT_PROFILE
23609 vim_free(fp->uf_tml_count);
23610 vim_free(fp->uf_tml_total);
23611 vim_free(fp->uf_tml_self);
23612#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023613
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023614 /* remove the function from the function hashtable */
23615 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23616 if (HASHITEM_EMPTY(hi))
23617 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023618 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023619 hash_remove(&func_hashtab, hi);
23620
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023621 vim_free(fp);
23622}
23623
23624/*
23625 * Unreference a Function: decrement the reference count and free it when it
23626 * becomes zero. Only for numbered functions.
23627 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023628 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023629func_unref(name)
23630 char_u *name;
23631{
23632 ufunc_T *fp;
23633
23634 if (name != NULL && isdigit(*name))
23635 {
23636 fp = find_func(name);
23637 if (fp == NULL)
23638 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023639 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023640 {
23641 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023642 * when "uf_calls" becomes zero. */
23643 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023644 func_free(fp);
23645 }
23646 }
23647}
23648
23649/*
23650 * Count a reference to a Function.
23651 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023652 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023653func_ref(name)
23654 char_u *name;
23655{
23656 ufunc_T *fp;
23657
23658 if (name != NULL && isdigit(*name))
23659 {
23660 fp = find_func(name);
23661 if (fp == NULL)
23662 EMSG2(_(e_intern2), "func_ref()");
23663 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023664 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 }
23666}
23667
23668/*
23669 * Call a user function.
23670 */
23671 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023672call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 ufunc_T *fp; /* pointer to function */
23674 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023675 typval_T *argvars; /* arguments */
23676 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 linenr_T firstline; /* first line of range */
23678 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023679 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680{
Bram Moolenaar33570922005-01-25 22:26:29 +000023681 char_u *save_sourcing_name;
23682 linenr_T save_sourcing_lnum;
23683 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023684 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023685 int save_did_emsg;
23686 static int depth = 0;
23687 dictitem_T *v;
23688 int fixvar_idx = 0; /* index in fixvar[] */
23689 int i;
23690 int ai;
23691 char_u numbuf[NUMBUFLEN];
23692 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023693#ifdef FEAT_PROFILE
23694 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023695 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697
23698 /* If depth of calling is getting too high, don't execute the function */
23699 if (depth >= p_mfd)
23700 {
23701 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023702 rettv->v_type = VAR_NUMBER;
23703 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 return;
23705 }
23706 ++depth;
23707
23708 line_breakcheck(); /* check for CTRL-C hit */
23709
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023710 fc = (funccall_T *)alloc(sizeof(funccall_T));
23711 fc->caller = current_funccal;
23712 current_funccal = fc;
23713 fc->func = fp;
23714 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023715 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023716 fc->linenr = 0;
23717 fc->returned = FALSE;
23718 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023720 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23721 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722
Bram Moolenaar33570922005-01-25 22:26:29 +000023723 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023724 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023725 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23726 * each argument variable and saves a lot of time.
23727 */
23728 /*
23729 * Init l: variables.
23730 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023731 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023732 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023733 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023734 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23735 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023736 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023737 name = v->di_key;
23738 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023739 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023740 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023741 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023742 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023743 v->di_tv.vval.v_dict = selfdict;
23744 ++selfdict->dv_refcount;
23745 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023746
Bram Moolenaar33570922005-01-25 22:26:29 +000023747 /*
23748 * Init a: variables.
23749 * Set a:0 to "argcount".
23750 * Set a:000 to a list with room for the "..." arguments.
23751 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023752 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023753 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023754 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023755 /* Use "name" to avoid a warning from some compiler that checks the
23756 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023757 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023758 name = v->di_key;
23759 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023760 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023761 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023762 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023763 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023764 v->di_tv.vval.v_list = &fc->l_varlist;
23765 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23766 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23767 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023768
23769 /*
23770 * Set a:firstline to "firstline" and a:lastline to "lastline".
23771 * Set a:name to named arguments.
23772 * Set a:N to the "..." arguments.
23773 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023774 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023775 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023776 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023777 (varnumber_T)lastline);
23778 for (i = 0; i < argcount; ++i)
23779 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023780 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023781 if (ai < 0)
23782 /* named argument a:name */
23783 name = FUNCARG(fp, i);
23784 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023785 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023786 /* "..." argument a:1, a:2, etc. */
23787 sprintf((char *)numbuf, "%d", ai + 1);
23788 name = numbuf;
23789 }
23790 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23791 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023792 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023793 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23794 }
23795 else
23796 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023797 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23798 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023799 if (v == NULL)
23800 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023801 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023802 }
23803 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023804 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023805
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023806 /* Note: the values are copied directly to avoid alloc/free.
23807 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023808 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023809 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023810
23811 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23812 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023813 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23814 fc->l_listitems[ai].li_tv = argvars[i];
23815 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023816 }
23817 }
23818
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 /* Don't redraw while executing the function. */
23820 ++RedrawingDisabled;
23821 save_sourcing_name = sourcing_name;
23822 save_sourcing_lnum = sourcing_lnum;
23823 sourcing_lnum = 1;
23824 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023825 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 if (sourcing_name != NULL)
23827 {
23828 if (save_sourcing_name != NULL
23829 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23830 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23831 else
23832 STRCPY(sourcing_name, "function ");
23833 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23834
23835 if (p_verbose >= 12)
23836 {
23837 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023838 verbose_enter_scroll();
23839
Bram Moolenaar555b2802005-05-19 21:08:39 +000023840 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 if (p_verbose >= 14)
23842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023844 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023845 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023846 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847
23848 msg_puts((char_u *)"(");
23849 for (i = 0; i < argcount; ++i)
23850 {
23851 if (i > 0)
23852 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023853 if (argvars[i].v_type == VAR_NUMBER)
23854 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 else
23856 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023857 /* Do not want errors such as E724 here. */
23858 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023859 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023860 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023861 if (s != NULL)
23862 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023863 if (vim_strsize(s) > MSG_BUF_CLEN)
23864 {
23865 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23866 s = buf;
23867 }
23868 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023869 vim_free(tofree);
23870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 }
23872 }
23873 msg_puts((char_u *)")");
23874 }
23875 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023876
23877 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 --no_wait_return;
23879 }
23880 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023881#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023882 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023883 {
23884 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23885 func_do_profile(fp);
23886 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023887 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023888 {
23889 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023890 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023891 profile_zero(&fp->uf_tm_children);
23892 }
23893 script_prof_save(&wait_start);
23894 }
23895#endif
23896
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023898 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 save_did_emsg = did_emsg;
23900 did_emsg = FALSE;
23901
23902 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023903 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23905
23906 --RedrawingDisabled;
23907
23908 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023909 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023911 clear_tv(rettv);
23912 rettv->v_type = VAR_NUMBER;
23913 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023914 }
23915
Bram Moolenaar05159a02005-02-26 23:04:13 +000023916#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023917 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023918 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023919 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023920 profile_end(&call_start);
23921 profile_sub_wait(&wait_start, &call_start);
23922 profile_add(&fp->uf_tm_total, &call_start);
23923 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023924 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023925 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023926 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23927 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023928 }
23929 }
23930#endif
23931
Bram Moolenaar071d4272004-06-13 20:20:40 +000023932 /* when being verbose, mention the return value */
23933 if (p_verbose >= 12)
23934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023936 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023939 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023940 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023941 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023942 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023943 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023945 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023946 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023947 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023948 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023949
Bram Moolenaar555b2802005-05-19 21:08:39 +000023950 /* The value may be very long. Skip the middle part, so that we
23951 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023952 * truncate it at the end. Don't want errors such as E724 here. */
23953 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023954 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023955 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023956 if (s != NULL)
23957 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023958 if (vim_strsize(s) > MSG_BUF_CLEN)
23959 {
23960 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23961 s = buf;
23962 }
23963 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023964 vim_free(tofree);
23965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 }
23967 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023968
23969 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 --no_wait_return;
23971 }
23972
23973 vim_free(sourcing_name);
23974 sourcing_name = save_sourcing_name;
23975 sourcing_lnum = save_sourcing_lnum;
23976 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023977#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023978 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023979 script_prof_restore(&wait_start);
23980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981
23982 if (p_verbose >= 12 && sourcing_name != NULL)
23983 {
23984 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023985 verbose_enter_scroll();
23986
Bram Moolenaar555b2802005-05-19 21:08:39 +000023987 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023989
23990 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 --no_wait_return;
23992 }
23993
23994 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023995 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023997
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023998 /* If the a:000 list and the l: and a: dicts are not referenced we can
23999 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024000 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24001 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24002 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24003 {
24004 free_funccal(fc, FALSE);
24005 }
24006 else
24007 {
24008 hashitem_T *hi;
24009 listitem_T *li;
24010 int todo;
24011
24012 /* "fc" is still in use. This can happen when returning "a:000" or
24013 * assigning "l:" to a global variable.
24014 * Link "fc" in the list for garbage collection later. */
24015 fc->caller = previous_funccal;
24016 previous_funccal = fc;
24017
24018 /* Make a copy of the a: variables, since we didn't do that above. */
24019 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24020 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24021 {
24022 if (!HASHITEM_EMPTY(hi))
24023 {
24024 --todo;
24025 v = HI2DI(hi);
24026 copy_tv(&v->di_tv, &v->di_tv);
24027 }
24028 }
24029
24030 /* Make a copy of the a:000 items, since we didn't do that above. */
24031 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24032 copy_tv(&li->li_tv, &li->li_tv);
24033 }
24034}
24035
24036/*
24037 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024038 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024039 */
24040 static int
24041can_free_funccal(fc, copyID)
24042 funccall_T *fc;
24043 int copyID;
24044{
24045 return (fc->l_varlist.lv_copyID != copyID
24046 && fc->l_vars.dv_copyID != copyID
24047 && fc->l_avars.dv_copyID != copyID);
24048}
24049
24050/*
24051 * Free "fc" and what it contains.
24052 */
24053 static void
24054free_funccal(fc, free_val)
24055 funccall_T *fc;
24056 int free_val; /* a: vars were allocated */
24057{
24058 listitem_T *li;
24059
24060 /* The a: variables typevals may not have been allocated, only free the
24061 * allocated variables. */
24062 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24063
24064 /* free all l: variables */
24065 vars_clear(&fc->l_vars.dv_hashtab);
24066
24067 /* Free the a:000 variables if they were allocated. */
24068 if (free_val)
24069 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24070 clear_tv(&li->li_tv);
24071
24072 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073}
24074
24075/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024076 * Add a number variable "name" to dict "dp" with value "nr".
24077 */
24078 static void
24079add_nr_var(dp, v, name, nr)
24080 dict_T *dp;
24081 dictitem_T *v;
24082 char *name;
24083 varnumber_T nr;
24084{
24085 STRCPY(v->di_key, name);
24086 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24087 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24088 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024089 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024090 v->di_tv.vval.v_number = nr;
24091}
24092
24093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 * ":return [expr]"
24095 */
24096 void
24097ex_return(eap)
24098 exarg_T *eap;
24099{
24100 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024101 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102 int returning = FALSE;
24103
24104 if (current_funccal == NULL)
24105 {
24106 EMSG(_("E133: :return not inside a function"));
24107 return;
24108 }
24109
24110 if (eap->skip)
24111 ++emsg_skip;
24112
24113 eap->nextcmd = NULL;
24114 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024115 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 {
24117 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024118 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024120 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 }
24122 /* It's safer to return also on error. */
24123 else if (!eap->skip)
24124 {
24125 /*
24126 * Return unless the expression evaluation has been cancelled due to an
24127 * aborting error, an interrupt, or an exception.
24128 */
24129 if (!aborting())
24130 returning = do_return(eap, FALSE, TRUE, NULL);
24131 }
24132
24133 /* When skipping or the return gets pending, advance to the next command
24134 * in this line (!returning). Otherwise, ignore the rest of the line.
24135 * Following lines will be ignored by get_func_line(). */
24136 if (returning)
24137 eap->nextcmd = NULL;
24138 else if (eap->nextcmd == NULL) /* no argument */
24139 eap->nextcmd = check_nextcmd(arg);
24140
24141 if (eap->skip)
24142 --emsg_skip;
24143}
24144
24145/*
24146 * Return from a function. Possibly makes the return pending. Also called
24147 * for a pending return at the ":endtry" or after returning from an extra
24148 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024149 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024150 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 * FALSE when the return gets pending.
24152 */
24153 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024154do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155 exarg_T *eap;
24156 int reanimate;
24157 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024158 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159{
24160 int idx;
24161 struct condstack *cstack = eap->cstack;
24162
24163 if (reanimate)
24164 /* Undo the return. */
24165 current_funccal->returned = FALSE;
24166
24167 /*
24168 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24169 * not in its finally clause (which then is to be executed next) is found.
24170 * In this case, make the ":return" pending for execution at the ":endtry".
24171 * Otherwise, return normally.
24172 */
24173 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24174 if (idx >= 0)
24175 {
24176 cstack->cs_pending[idx] = CSTP_RETURN;
24177
24178 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024179 /* A pending return again gets pending. "rettv" points to an
24180 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024182 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 else
24184 {
24185 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024186 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024188 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024190 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 {
24192 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024193 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024194 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 else
24196 EMSG(_(e_outofmem));
24197 }
24198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024199 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200
24201 if (reanimate)
24202 {
24203 /* The pending return value could be overwritten by a ":return"
24204 * without argument in a finally clause; reset the default
24205 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024206 current_funccal->rettv->v_type = VAR_NUMBER;
24207 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 }
24209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024210 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 }
24212 else
24213 {
24214 current_funccal->returned = TRUE;
24215
24216 /* If the return is carried out now, store the return value. For
24217 * a return immediately after reanimation, the value is already
24218 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024219 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024221 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024222 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024224 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 }
24226 }
24227
24228 return idx < 0;
24229}
24230
24231/*
24232 * Free the variable with a pending return value.
24233 */
24234 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024235discard_pending_return(rettv)
24236 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237{
Bram Moolenaar33570922005-01-25 22:26:29 +000024238 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239}
24240
24241/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024242 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 * is an allocated string. Used by report_pending() for verbose messages.
24244 */
24245 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024246get_return_cmd(rettv)
24247 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024249 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024250 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024251 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024253 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024254 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024255 if (s == NULL)
24256 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024257
24258 STRCPY(IObuff, ":return ");
24259 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24260 if (STRLEN(s) + 8 >= IOSIZE)
24261 STRCPY(IObuff + IOSIZE - 4, "...");
24262 vim_free(tofree);
24263 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264}
24265
24266/*
24267 * Get next function line.
24268 * Called by do_cmdline() to get the next line.
24269 * Returns allocated string, or NULL for end of function.
24270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 char_u *
24272get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024273 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024275 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276{
Bram Moolenaar33570922005-01-25 22:26:29 +000024277 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024278 ufunc_T *fp = fcp->func;
24279 char_u *retval;
24280 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281
24282 /* If breakpoints have been added/deleted need to check for it. */
24283 if (fcp->dbg_tick != debug_tick)
24284 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024285 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 sourcing_lnum);
24287 fcp->dbg_tick = debug_tick;
24288 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024289#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024290 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024291 func_line_end(cookie);
24292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293
Bram Moolenaar05159a02005-02-26 23:04:13 +000024294 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024295 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24296 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 retval = NULL;
24298 else
24299 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024300 /* Skip NULL lines (continuation lines). */
24301 while (fcp->linenr < gap->ga_len
24302 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24303 ++fcp->linenr;
24304 if (fcp->linenr >= gap->ga_len)
24305 retval = NULL;
24306 else
24307 {
24308 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24309 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024310#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024311 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024312 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024313#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 }
24316
24317 /* Did we encounter a breakpoint? */
24318 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24319 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024320 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024322 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323 sourcing_lnum);
24324 fcp->dbg_tick = debug_tick;
24325 }
24326
24327 return retval;
24328}
24329
Bram Moolenaar05159a02005-02-26 23:04:13 +000024330#if defined(FEAT_PROFILE) || defined(PROTO)
24331/*
24332 * Called when starting to read a function line.
24333 * "sourcing_lnum" must be correct!
24334 * When skipping lines it may not actually be executed, but we won't find out
24335 * until later and we need to store the time now.
24336 */
24337 void
24338func_line_start(cookie)
24339 void *cookie;
24340{
24341 funccall_T *fcp = (funccall_T *)cookie;
24342 ufunc_T *fp = fcp->func;
24343
24344 if (fp->uf_profiling && sourcing_lnum >= 1
24345 && sourcing_lnum <= fp->uf_lines.ga_len)
24346 {
24347 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024348 /* Skip continuation lines. */
24349 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24350 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024351 fp->uf_tml_execed = FALSE;
24352 profile_start(&fp->uf_tml_start);
24353 profile_zero(&fp->uf_tml_children);
24354 profile_get_wait(&fp->uf_tml_wait);
24355 }
24356}
24357
24358/*
24359 * Called when actually executing a function line.
24360 */
24361 void
24362func_line_exec(cookie)
24363 void *cookie;
24364{
24365 funccall_T *fcp = (funccall_T *)cookie;
24366 ufunc_T *fp = fcp->func;
24367
24368 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24369 fp->uf_tml_execed = TRUE;
24370}
24371
24372/*
24373 * Called when done with a function line.
24374 */
24375 void
24376func_line_end(cookie)
24377 void *cookie;
24378{
24379 funccall_T *fcp = (funccall_T *)cookie;
24380 ufunc_T *fp = fcp->func;
24381
24382 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24383 {
24384 if (fp->uf_tml_execed)
24385 {
24386 ++fp->uf_tml_count[fp->uf_tml_idx];
24387 profile_end(&fp->uf_tml_start);
24388 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024389 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024390 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24391 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024392 }
24393 fp->uf_tml_idx = -1;
24394 }
24395}
24396#endif
24397
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398/*
24399 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024400 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 */
24402 int
24403func_has_ended(cookie)
24404 void *cookie;
24405{
Bram Moolenaar33570922005-01-25 22:26:29 +000024406 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407
24408 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24409 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024410 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411 || fcp->returned);
24412}
24413
24414/*
24415 * return TRUE if cookie indicates a function which "abort"s on errors.
24416 */
24417 int
24418func_has_abort(cookie)
24419 void *cookie;
24420{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024421 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024422}
24423
24424#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24425typedef enum
24426{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024427 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24428 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24429 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024430} var_flavour_T;
24431
24432static var_flavour_T var_flavour __ARGS((char_u *varname));
24433
24434 static var_flavour_T
24435var_flavour(varname)
24436 char_u *varname;
24437{
24438 char_u *p = varname;
24439
24440 if (ASCII_ISUPPER(*p))
24441 {
24442 while (*(++p))
24443 if (ASCII_ISLOWER(*p))
24444 return VAR_FLAVOUR_SESSION;
24445 return VAR_FLAVOUR_VIMINFO;
24446 }
24447 else
24448 return VAR_FLAVOUR_DEFAULT;
24449}
24450#endif
24451
24452#if defined(FEAT_VIMINFO) || defined(PROTO)
24453/*
24454 * Restore global vars that start with a capital from the viminfo file
24455 */
24456 int
24457read_viminfo_varlist(virp, writing)
24458 vir_T *virp;
24459 int writing;
24460{
24461 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024462 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024463 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024464
24465 if (!writing && (find_viminfo_parameter('!') != NULL))
24466 {
24467 tab = vim_strchr(virp->vir_line + 1, '\t');
24468 if (tab != NULL)
24469 {
24470 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024471 switch (*tab)
24472 {
24473 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024474#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024475 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024476#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024477 case 'D': type = VAR_DICT; break;
24478 case 'L': type = VAR_LIST; break;
24479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480
24481 tab = vim_strchr(tab, '\t');
24482 if (tab != NULL)
24483 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024484 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024485 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024486 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024488#ifdef FEAT_FLOAT
24489 else if (type == VAR_FLOAT)
24490 (void)string2float(tab + 1, &tv.vval.v_float);
24491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024493 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024494 if (type == VAR_DICT || type == VAR_LIST)
24495 {
24496 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24497
24498 if (etv == NULL)
24499 /* Failed to parse back the dict or list, use it as a
24500 * string. */
24501 tv.v_type = VAR_STRING;
24502 else
24503 {
24504 vim_free(tv.vval.v_string);
24505 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024506 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024507 }
24508 }
24509
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024510 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024511
24512 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024513 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024514 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24515 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 }
24517 }
24518 }
24519
24520 return viminfo_readline(virp);
24521}
24522
24523/*
24524 * Write global vars that start with a capital to the viminfo file
24525 */
24526 void
24527write_viminfo_varlist(fp)
24528 FILE *fp;
24529{
Bram Moolenaar33570922005-01-25 22:26:29 +000024530 hashitem_T *hi;
24531 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024532 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024533 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024534 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024535 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024536 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537
24538 if (find_viminfo_parameter('!') == NULL)
24539 return;
24540
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024541 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024542
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024543 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024544 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024546 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024548 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024549 this_var = HI2DI(hi);
24550 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024551 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024552 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024553 {
24554 case VAR_STRING: s = "STR"; break;
24555 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024556#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024557 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024558#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024559 case VAR_DICT: s = "DIC"; break;
24560 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024561 default: continue;
24562 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024563 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024564 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024565 if (p != NULL)
24566 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024567 vim_free(tofree);
24568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 }
24570 }
24571}
24572#endif
24573
24574#if defined(FEAT_SESSION) || defined(PROTO)
24575 int
24576store_session_globals(fd)
24577 FILE *fd;
24578{
Bram Moolenaar33570922005-01-25 22:26:29 +000024579 hashitem_T *hi;
24580 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024581 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582 char_u *p, *t;
24583
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024584 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024585 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024587 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024589 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024590 this_var = HI2DI(hi);
24591 if ((this_var->di_tv.v_type == VAR_NUMBER
24592 || this_var->di_tv.v_type == VAR_STRING)
24593 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024594 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024595 /* Escape special characters with a backslash. Turn a LF and
24596 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024597 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024598 (char_u *)"\\\"\n\r");
24599 if (p == NULL) /* out of memory */
24600 break;
24601 for (t = p; *t != NUL; ++t)
24602 if (*t == '\n')
24603 *t = 'n';
24604 else if (*t == '\r')
24605 *t = 'r';
24606 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024607 this_var->di_key,
24608 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24609 : ' ',
24610 p,
24611 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24612 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024613 || put_eol(fd) == FAIL)
24614 {
24615 vim_free(p);
24616 return FAIL;
24617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618 vim_free(p);
24619 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024620#ifdef FEAT_FLOAT
24621 else if (this_var->di_tv.v_type == VAR_FLOAT
24622 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24623 {
24624 float_T f = this_var->di_tv.vval.v_float;
24625 int sign = ' ';
24626
24627 if (f < 0)
24628 {
24629 f = -f;
24630 sign = '-';
24631 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024632 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024633 this_var->di_key, sign, f) < 0)
24634 || put_eol(fd) == FAIL)
24635 return FAIL;
24636 }
24637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 }
24639 }
24640 return OK;
24641}
24642#endif
24643
Bram Moolenaar661b1822005-07-28 22:36:45 +000024644/*
24645 * Display script name where an item was last set.
24646 * Should only be invoked when 'verbose' is non-zero.
24647 */
24648 void
24649last_set_msg(scriptID)
24650 scid_T scriptID;
24651{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024652 char_u *p;
24653
Bram Moolenaar661b1822005-07-28 22:36:45 +000024654 if (scriptID != 0)
24655 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024656 p = home_replace_save(NULL, get_scriptname(scriptID));
24657 if (p != NULL)
24658 {
24659 verbose_enter();
24660 MSG_PUTS(_("\n\tLast set from "));
24661 MSG_PUTS(p);
24662 vim_free(p);
24663 verbose_leave();
24664 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024665 }
24666}
24667
Bram Moolenaard812df62008-11-09 12:46:09 +000024668/*
24669 * List v:oldfiles in a nice way.
24670 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024671 void
24672ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024673 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024674{
24675 list_T *l = vimvars[VV_OLDFILES].vv_list;
24676 listitem_T *li;
24677 int nr = 0;
24678
24679 if (l == NULL)
24680 msg((char_u *)_("No old files"));
24681 else
24682 {
24683 msg_start();
24684 msg_scroll = TRUE;
24685 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24686 {
24687 msg_outnum((long)++nr);
24688 MSG_PUTS(": ");
24689 msg_outtrans(get_tv_string(&li->li_tv));
24690 msg_putchar('\n');
24691 out_flush(); /* output one line at a time */
24692 ui_breakcheck();
24693 }
24694 /* Assume "got_int" was set to truncate the listing. */
24695 got_int = FALSE;
24696
24697#ifdef FEAT_BROWSE_CMD
24698 if (cmdmod.browse)
24699 {
24700 quit_more = FALSE;
24701 nr = prompt_for_number(FALSE);
24702 msg_starthere();
24703 if (nr > 0)
24704 {
24705 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24706 (long)nr);
24707
24708 if (p != NULL)
24709 {
24710 p = expand_env_save(p);
24711 eap->arg = p;
24712 eap->cmdidx = CMD_edit;
24713 cmdmod.browse = FALSE;
24714 do_exedit(eap, NULL);
24715 vim_free(p);
24716 }
24717 }
24718 }
24719#endif
24720 }
24721}
24722
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723#endif /* FEAT_EVAL */
24724
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024726#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727
24728#ifdef WIN3264
24729/*
24730 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24731 */
24732static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24733static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24734static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24735
24736/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024737 * Get the short path (8.3) for the filename in "fnamep".
24738 * Only works for a valid file name.
24739 * When the path gets longer "fnamep" is changed and the allocated buffer
24740 * is put in "bufp".
24741 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24742 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743 */
24744 static int
24745get_short_pathname(fnamep, bufp, fnamelen)
24746 char_u **fnamep;
24747 char_u **bufp;
24748 int *fnamelen;
24749{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024750 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 char_u *newbuf;
24752
24753 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754 l = GetShortPathName(*fnamep, *fnamep, len);
24755 if (l > len - 1)
24756 {
24757 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024758 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759 newbuf = vim_strnsave(*fnamep, l);
24760 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024761 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762
24763 vim_free(*bufp);
24764 *fnamep = *bufp = newbuf;
24765
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024766 /* Really should always succeed, as the buffer is big enough. */
24767 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 }
24769
24770 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024771 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772}
24773
24774/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024775 * Get the short path (8.3) for the filename in "fname". The converted
24776 * path is returned in "bufp".
24777 *
24778 * Some of the directories specified in "fname" may not exist. This function
24779 * will shorten the existing directories at the beginning of the path and then
24780 * append the remaining non-existing path.
24781 *
24782 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024783 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024784 * bufp - Pointer to an allocated buffer for the filename.
24785 * fnamelen - Length of the filename pointed to by fname
24786 *
24787 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788 */
24789 static int
24790shortpath_for_invalid_fname(fname, bufp, fnamelen)
24791 char_u **fname;
24792 char_u **bufp;
24793 int *fnamelen;
24794{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024795 char_u *short_fname, *save_fname, *pbuf_unused;
24796 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024797 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024798 int old_len, len;
24799 int new_len, sfx_len;
24800 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801
24802 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024803 old_len = *fnamelen;
24804 save_fname = vim_strnsave(*fname, old_len);
24805 pbuf_unused = NULL;
24806 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024808 endp = save_fname + old_len - 1; /* Find the end of the copy */
24809 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024811 /*
24812 * Try shortening the supplied path till it succeeds by removing one
24813 * directory at a time from the tail of the path.
24814 */
24815 len = 0;
24816 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024818 /* go back one path-separator */
24819 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24820 --endp;
24821 if (endp <= save_fname)
24822 break; /* processed the complete path */
24823
24824 /*
24825 * Replace the path separator with a NUL and try to shorten the
24826 * resulting path.
24827 */
24828 ch = *endp;
24829 *endp = 0;
24830 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024831 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024832 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24833 {
24834 retval = FAIL;
24835 goto theend;
24836 }
24837 *endp = ch; /* preserve the string */
24838
24839 if (len > 0)
24840 break; /* successfully shortened the path */
24841
24842 /* failed to shorten the path. Skip the path separator */
24843 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024844 }
24845
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024846 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024848 /*
24849 * Succeeded in shortening the path. Now concatenate the shortened
24850 * path with the remaining path at the tail.
24851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024853 /* Compute the length of the new path. */
24854 sfx_len = (int)(save_endp - endp) + 1;
24855 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024857 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024859 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024861 /* There is not enough space in the currently allocated string,
24862 * copy it to a buffer big enough. */
24863 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024865 {
24866 retval = FAIL;
24867 goto theend;
24868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 }
24870 else
24871 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024872 /* Transfer short_fname to the main buffer (it's big enough),
24873 * unless get_short_pathname() did its work in-place. */
24874 *fname = *bufp = save_fname;
24875 if (short_fname != save_fname)
24876 vim_strncpy(save_fname, short_fname, len);
24877 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024879
24880 /* concat the not-shortened part of the path */
24881 vim_strncpy(*fname + len, endp, sfx_len);
24882 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024883 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024884
24885theend:
24886 vim_free(pbuf_unused);
24887 vim_free(save_fname);
24888
24889 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024890}
24891
24892/*
24893 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024894 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 */
24896 static int
24897shortpath_for_partial(fnamep, bufp, fnamelen)
24898 char_u **fnamep;
24899 char_u **bufp;
24900 int *fnamelen;
24901{
24902 int sepcount, len, tflen;
24903 char_u *p;
24904 char_u *pbuf, *tfname;
24905 int hasTilde;
24906
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024907 /* Count up the path separators from the RHS.. so we know which part
24908 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024910 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 if (vim_ispathsep(*p))
24912 ++sepcount;
24913
24914 /* Need full path first (use expand_env() to remove a "~/") */
24915 hasTilde = (**fnamep == '~');
24916 if (hasTilde)
24917 pbuf = tfname = expand_env_save(*fnamep);
24918 else
24919 pbuf = tfname = FullName_save(*fnamep, FALSE);
24920
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024921 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024923 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24924 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925
24926 if (len == 0)
24927 {
24928 /* Don't have a valid filename, so shorten the rest of the
24929 * path if we can. This CAN give us invalid 8.3 filenames, but
24930 * there's not a lot of point in guessing what it might be.
24931 */
24932 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024933 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24934 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935 }
24936
24937 /* Count the paths backward to find the beginning of the desired string. */
24938 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024939 {
24940#ifdef FEAT_MBYTE
24941 if (has_mbyte)
24942 p -= mb_head_off(tfname, p);
24943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944 if (vim_ispathsep(*p))
24945 {
24946 if (sepcount == 0 || (hasTilde && sepcount == 1))
24947 break;
24948 else
24949 sepcount --;
24950 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 if (hasTilde)
24953 {
24954 --p;
24955 if (p >= tfname)
24956 *p = '~';
24957 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024958 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959 }
24960 else
24961 ++p;
24962
24963 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24964 vim_free(*bufp);
24965 *fnamelen = (int)STRLEN(p);
24966 *bufp = pbuf;
24967 *fnamep = p;
24968
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024969 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970}
24971#endif /* WIN3264 */
24972
24973/*
24974 * Adjust a filename, according to a string of modifiers.
24975 * *fnamep must be NUL terminated when called. When returning, the length is
24976 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024977 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978 * When there is an error, *fnamep is set to NULL.
24979 */
24980 int
24981modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24982 char_u *src; /* string with modifiers */
24983 int *usedlen; /* characters after src that are used */
24984 char_u **fnamep; /* file name so far */
24985 char_u **bufp; /* buffer for allocated file name or NULL */
24986 int *fnamelen; /* length of fnamep */
24987{
24988 int valid = 0;
24989 char_u *tail;
24990 char_u *s, *p, *pbuf;
24991 char_u dirname[MAXPATHL];
24992 int c;
24993 int has_fullname = 0;
24994#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024995 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996 int has_shortname = 0;
24997#endif
24998
24999repeat:
25000 /* ":p" - full path/file_name */
25001 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25002 {
25003 has_fullname = 1;
25004
25005 valid |= VALID_PATH;
25006 *usedlen += 2;
25007
25008 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25009 if ((*fnamep)[0] == '~'
25010#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25011 && ((*fnamep)[1] == '/'
25012# ifdef BACKSLASH_IN_FILENAME
25013 || (*fnamep)[1] == '\\'
25014# endif
25015 || (*fnamep)[1] == NUL)
25016
25017#endif
25018 )
25019 {
25020 *fnamep = expand_env_save(*fnamep);
25021 vim_free(*bufp); /* free any allocated file name */
25022 *bufp = *fnamep;
25023 if (*fnamep == NULL)
25024 return -1;
25025 }
25026
25027 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025028 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029 {
25030 if (vim_ispathsep(*p)
25031 && p[1] == '.'
25032 && (p[2] == NUL
25033 || vim_ispathsep(p[2])
25034 || (p[2] == '.'
25035 && (p[3] == NUL || vim_ispathsep(p[3])))))
25036 break;
25037 }
25038
25039 /* FullName_save() is slow, don't use it when not needed. */
25040 if (*p != NUL || !vim_isAbsName(*fnamep))
25041 {
25042 *fnamep = FullName_save(*fnamep, *p != NUL);
25043 vim_free(*bufp); /* free any allocated file name */
25044 *bufp = *fnamep;
25045 if (*fnamep == NULL)
25046 return -1;
25047 }
25048
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025049#ifdef WIN3264
25050# if _WIN32_WINNT >= 0x0500
25051 if (vim_strchr(*fnamep, '~') != NULL)
25052 {
25053 /* Expand 8.3 filename to full path. Needed to make sure the same
25054 * file does not have two different names.
25055 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25056 p = alloc(_MAX_PATH + 1);
25057 if (p != NULL)
25058 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025059 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025060 {
25061 vim_free(*bufp);
25062 *bufp = *fnamep = p;
25063 }
25064 else
25065 vim_free(p);
25066 }
25067 }
25068# endif
25069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 /* Append a path separator to a directory. */
25071 if (mch_isdir(*fnamep))
25072 {
25073 /* Make room for one or two extra characters. */
25074 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25075 vim_free(*bufp); /* free any allocated file name */
25076 *bufp = *fnamep;
25077 if (*fnamep == NULL)
25078 return -1;
25079 add_pathsep(*fnamep);
25080 }
25081 }
25082
25083 /* ":." - path relative to the current directory */
25084 /* ":~" - path relative to the home directory */
25085 /* ":8" - shortname path - postponed till after */
25086 while (src[*usedlen] == ':'
25087 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25088 {
25089 *usedlen += 2;
25090 if (c == '8')
25091 {
25092#ifdef WIN3264
25093 has_shortname = 1; /* Postpone this. */
25094#endif
25095 continue;
25096 }
25097 pbuf = NULL;
25098 /* Need full path first (use expand_env() to remove a "~/") */
25099 if (!has_fullname)
25100 {
25101 if (c == '.' && **fnamep == '~')
25102 p = pbuf = expand_env_save(*fnamep);
25103 else
25104 p = pbuf = FullName_save(*fnamep, FALSE);
25105 }
25106 else
25107 p = *fnamep;
25108
25109 has_fullname = 0;
25110
25111 if (p != NULL)
25112 {
25113 if (c == '.')
25114 {
25115 mch_dirname(dirname, MAXPATHL);
25116 s = shorten_fname(p, dirname);
25117 if (s != NULL)
25118 {
25119 *fnamep = s;
25120 if (pbuf != NULL)
25121 {
25122 vim_free(*bufp); /* free any allocated file name */
25123 *bufp = pbuf;
25124 pbuf = NULL;
25125 }
25126 }
25127 }
25128 else
25129 {
25130 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25131 /* Only replace it when it starts with '~' */
25132 if (*dirname == '~')
25133 {
25134 s = vim_strsave(dirname);
25135 if (s != NULL)
25136 {
25137 *fnamep = s;
25138 vim_free(*bufp);
25139 *bufp = s;
25140 }
25141 }
25142 }
25143 vim_free(pbuf);
25144 }
25145 }
25146
25147 tail = gettail(*fnamep);
25148 *fnamelen = (int)STRLEN(*fnamep);
25149
25150 /* ":h" - head, remove "/file_name", can be repeated */
25151 /* Don't remove the first "/" or "c:\" */
25152 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25153 {
25154 valid |= VALID_HEAD;
25155 *usedlen += 2;
25156 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025157 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025158 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159 *fnamelen = (int)(tail - *fnamep);
25160#ifdef VMS
25161 if (*fnamelen > 0)
25162 *fnamelen += 1; /* the path separator is part of the path */
25163#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025164 if (*fnamelen == 0)
25165 {
25166 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25167 p = vim_strsave((char_u *)".");
25168 if (p == NULL)
25169 return -1;
25170 vim_free(*bufp);
25171 *bufp = *fnamep = tail = p;
25172 *fnamelen = 1;
25173 }
25174 else
25175 {
25176 while (tail > s && !after_pathsep(s, tail))
25177 mb_ptr_back(*fnamep, tail);
25178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179 }
25180
25181 /* ":8" - shortname */
25182 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25183 {
25184 *usedlen += 2;
25185#ifdef WIN3264
25186 has_shortname = 1;
25187#endif
25188 }
25189
25190#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025191 /*
25192 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193 */
25194 if (has_shortname)
25195 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025196 /* Copy the string if it is shortened by :h and when it wasn't copied
25197 * yet, because we are going to change it in place. Avoids changing
25198 * the buffer name for "%:8". */
25199 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 {
25201 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025202 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203 return -1;
25204 vim_free(*bufp);
25205 *bufp = *fnamep = p;
25206 }
25207
25208 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025209 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210 if (!has_fullname && !vim_isAbsName(*fnamep))
25211 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025212 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213 return -1;
25214 }
25215 else
25216 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025217 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025218
Bram Moolenaardc935552011-08-17 15:23:23 +020025219 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025221 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222 return -1;
25223
25224 if (l == 0)
25225 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025226 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025228 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229 return -1;
25230 }
25231 *fnamelen = l;
25232 }
25233 }
25234#endif /* WIN3264 */
25235
25236 /* ":t" - tail, just the basename */
25237 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25238 {
25239 *usedlen += 2;
25240 *fnamelen -= (int)(tail - *fnamep);
25241 *fnamep = tail;
25242 }
25243
25244 /* ":e" - extension, can be repeated */
25245 /* ":r" - root, without extension, can be repeated */
25246 while (src[*usedlen] == ':'
25247 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25248 {
25249 /* find a '.' in the tail:
25250 * - for second :e: before the current fname
25251 * - otherwise: The last '.'
25252 */
25253 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25254 s = *fnamep - 2;
25255 else
25256 s = *fnamep + *fnamelen - 1;
25257 for ( ; s > tail; --s)
25258 if (s[0] == '.')
25259 break;
25260 if (src[*usedlen + 1] == 'e') /* :e */
25261 {
25262 if (s > tail)
25263 {
25264 *fnamelen += (int)(*fnamep - (s + 1));
25265 *fnamep = s + 1;
25266#ifdef VMS
25267 /* cut version from the extension */
25268 s = *fnamep + *fnamelen - 1;
25269 for ( ; s > *fnamep; --s)
25270 if (s[0] == ';')
25271 break;
25272 if (s > *fnamep)
25273 *fnamelen = s - *fnamep;
25274#endif
25275 }
25276 else if (*fnamep <= tail)
25277 *fnamelen = 0;
25278 }
25279 else /* :r */
25280 {
25281 if (s > tail) /* remove one extension */
25282 *fnamelen = (int)(s - *fnamep);
25283 }
25284 *usedlen += 2;
25285 }
25286
25287 /* ":s?pat?foo?" - substitute */
25288 /* ":gs?pat?foo?" - global substitute */
25289 if (src[*usedlen] == ':'
25290 && (src[*usedlen + 1] == 's'
25291 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25292 {
25293 char_u *str;
25294 char_u *pat;
25295 char_u *sub;
25296 int sep;
25297 char_u *flags;
25298 int didit = FALSE;
25299
25300 flags = (char_u *)"";
25301 s = src + *usedlen + 2;
25302 if (src[*usedlen + 1] == 'g')
25303 {
25304 flags = (char_u *)"g";
25305 ++s;
25306 }
25307
25308 sep = *s++;
25309 if (sep)
25310 {
25311 /* find end of pattern */
25312 p = vim_strchr(s, sep);
25313 if (p != NULL)
25314 {
25315 pat = vim_strnsave(s, (int)(p - s));
25316 if (pat != NULL)
25317 {
25318 s = p + 1;
25319 /* find end of substitution */
25320 p = vim_strchr(s, sep);
25321 if (p != NULL)
25322 {
25323 sub = vim_strnsave(s, (int)(p - s));
25324 str = vim_strnsave(*fnamep, *fnamelen);
25325 if (sub != NULL && str != NULL)
25326 {
25327 *usedlen = (int)(p + 1 - src);
25328 s = do_string_sub(str, pat, sub, flags);
25329 if (s != NULL)
25330 {
25331 *fnamep = s;
25332 *fnamelen = (int)STRLEN(s);
25333 vim_free(*bufp);
25334 *bufp = s;
25335 didit = TRUE;
25336 }
25337 }
25338 vim_free(sub);
25339 vim_free(str);
25340 }
25341 vim_free(pat);
25342 }
25343 }
25344 /* after using ":s", repeat all the modifiers */
25345 if (didit)
25346 goto repeat;
25347 }
25348 }
25349
Bram Moolenaar26df0922014-02-23 23:39:13 +010025350 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25351 {
25352 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25353 if (p == NULL)
25354 return -1;
25355 vim_free(*bufp);
25356 *bufp = *fnamep = p;
25357 *fnamelen = (int)STRLEN(p);
25358 *usedlen += 2;
25359 }
25360
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 return valid;
25362}
25363
25364/*
25365 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25366 * "flags" can be "g" to do a global substitute.
25367 * Returns an allocated string, NULL for error.
25368 */
25369 char_u *
25370do_string_sub(str, pat, sub, flags)
25371 char_u *str;
25372 char_u *pat;
25373 char_u *sub;
25374 char_u *flags;
25375{
25376 int sublen;
25377 regmatch_T regmatch;
25378 int i;
25379 int do_all;
25380 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025381 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 garray_T ga;
25383 char_u *ret;
25384 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025385 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025386
25387 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25388 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025389 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025390
25391 ga_init2(&ga, 1, 200);
25392
25393 do_all = (flags[0] == 'g');
25394
25395 regmatch.rm_ic = p_ic;
25396 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25397 if (regmatch.regprog != NULL)
25398 {
25399 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025400 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25402 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025403 /* Skip empty match except for first match. */
25404 if (regmatch.startp[0] == regmatch.endp[0])
25405 {
25406 if (zero_width == regmatch.startp[0])
25407 {
25408 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025409 i = MB_PTR2LEN(tail);
25410 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25411 (size_t)i);
25412 ga.ga_len += i;
25413 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025414 continue;
25415 }
25416 zero_width = regmatch.startp[0];
25417 }
25418
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 /*
25420 * Get some space for a temporary buffer to do the substitution
25421 * into. It will contain:
25422 * - The text up to where the match is.
25423 * - The substituted text.
25424 * - The text after the match.
25425 */
25426 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025427 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25429 {
25430 ga_clear(&ga);
25431 break;
25432 }
25433
25434 /* copy the text up to where the match is */
25435 i = (int)(regmatch.startp[0] - tail);
25436 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25437 /* add the substituted text */
25438 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25439 + ga.ga_len + i, TRUE, TRUE, FALSE);
25440 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025441 tail = regmatch.endp[0];
25442 if (*tail == NUL)
25443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444 if (!do_all)
25445 break;
25446 }
25447
25448 if (ga.ga_data != NULL)
25449 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25450
Bram Moolenaar473de612013-06-08 18:19:48 +020025451 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452 }
25453
25454 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25455 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025456 if (p_cpo == empty_option)
25457 p_cpo = save_cpo;
25458 else
25459 /* Darn, evaluating {sub} expression changed the value. */
25460 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461
25462 return ret;
25463}
25464
25465#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */