blob: 743c7436c6f3dba79ec35f94e9b47240f0ad4ea4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000372};
373
374/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_type vv_di.di_tv.v_type
376#define vv_nr vv_di.di_tv.vval.v_number
377#define vv_float vv_di.di_tv.vval.v_float
378#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000379#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200380#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000382
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200383static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000384#define vimvarht vimvardict.dv_hashtab
385
Bram Moolenaara40058a2005-07-11 22:42:07 +0000386static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
387static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100404static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000434static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
438static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100441static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000443static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200444static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
452static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static int string2float __ARGS((char_u *text, float_T *value));
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200461static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100470static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200474static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100476static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200480static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100493static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100495static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
519static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200529static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531#ifdef FEAT_FLOAT
532static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000536static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#ifdef FEAT_FLOAT
543static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200545static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000547static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000556static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000558static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200562static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200566static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000574static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000575static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200576static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000577static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000578static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200581static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000582static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100588static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000591static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000605static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100610static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200625static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200628#ifdef FEAT_LUA
629static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000635static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200636static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000638static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000640static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000644#ifdef vim_mkdir
645static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100648#ifdef FEAT_MZSCHEME
649static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100653static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000654static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
656static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
657#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000659static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000660static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200661#ifdef FEAT_PYTHON3
662static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
663#endif
664#ifdef FEAT_PYTHON
665static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000668static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000669static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000681#ifdef FEAT_FLOAT
682static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
683#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200684static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100686static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000689static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000691static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200696static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000699static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000700static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000701static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000702static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200704static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000705static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100707#ifdef FEAT_CRYPT
708static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
709#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000710static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200711static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000713#ifdef FEAT_FLOAT
714static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200715static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000718static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000719static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#ifdef FEAT_FLOAT
723static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
725#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000726static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728#ifdef HAVE_STRFTIME
729static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
730#endif
731static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200737static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200738static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000744static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200745static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200747static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000748static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000749static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000750static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000751static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000752static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000754static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200755#ifdef FEAT_FLOAT
756static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
758#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
763static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
764#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200766static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200767static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100768static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
770static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100772static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
775static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000779static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000782static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100783static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784
Bram Moolenaar493c1782014-05-28 14:34:46 +0200785static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000786static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static int get_env_len __ARGS((char_u **arg));
788static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000789static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000790static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
791#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
792#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
793 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000794static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000796static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200797static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000798static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static typval_T *alloc_tv __ARGS((void));
800static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static void init_tv __ARGS((typval_T *varp));
802static long get_tv_number __ARGS((typval_T *varp));
803static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000804static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static char_u *get_tv_string __ARGS((typval_T *varp));
806static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000807static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100808static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
809static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
811static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
812static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000813static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
814static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200816static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
817static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200818static int var_check_func_name __ARGS((char_u *name, int new_var));
819static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200820static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000821static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
823static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
824static int eval_fname_script __ARGS((char_u *p));
825static int eval_fname_sid __ARGS((char_u *p));
826static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static ufunc_T *find_func __ARGS((char_u *name));
828static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200829static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830#ifdef FEAT_PROFILE
831static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000832static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
833static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
834static int
835# ifdef __BORLANDC__
836 _RTLENTRYF
837# endif
838 prof_total_cmp __ARGS((const void *s1, const void *s2));
839static int
840# ifdef __BORLANDC__
841 _RTLENTRYF
842# endif
843 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200845static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000847static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000848static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000850static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
851static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000852static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000853static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
854static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000855static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000856static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000857static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200858static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200859static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200861
862#ifdef EBCDIC
863static int compare_func_name __ARGS((const void *s1, const void *s2));
864static void sortFunctions __ARGS(());
865#endif
866
Bram Moolenaar33570922005-01-25 22:26:29 +0000867/*
868 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000869 */
870 void
871eval_init()
872{
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 int i;
874 struct vimvar *p;
875
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200876 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
877 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200878 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000880 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881
882 for (i = 0; i < VV_LEN; ++i)
883 {
884 p = &vimvars[i];
885 STRCPY(p->vv_di.di_key, p->vv_name);
886 if (p->vv_flags & VV_RO)
887 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
888 else if (p->vv_flags & VV_RO_SBX)
889 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
890 else
891 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000892
893 /* add to v: scope dict, unless the value is not always available */
894 if (p->vv_type != VAR_UNKNOWN)
895 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000896 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000897 /* add to compat scope dict */
898 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000899 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000900 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100901 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200902 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100903 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200904 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200905
906#ifdef EBCDIC
907 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100908 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909 */
910 sortFunctions();
911#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000912}
913
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914#if defined(EXITFREE) || defined(PROTO)
915 void
916eval_clear()
917{
918 int i;
919 struct vimvar *p;
920
921 for (i = 0; i < VV_LEN; ++i)
922 {
923 p = &vimvars[i];
924 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000925 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000926 vim_free(p->vv_str);
927 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000928 }
929 else if (p->vv_di.di_tv.v_type == VAR_LIST)
930 {
931 list_unref(p->vv_list);
932 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000934 }
935 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000936 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000937 hash_clear(&compat_hashtab);
938
Bram Moolenaard9fba312005-06-26 22:34:35 +0000939 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100940# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200941 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100942# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000943
944 /* global variables */
945 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000946
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000947 /* autoloaded script names */
948 ga_clear_strings(&ga_loaded);
949
Bram Moolenaarcca74132013-09-25 21:00:28 +0200950 /* Script-local variables. First clear all the variables and in a second
951 * loop free the scriptvar_T, because a variable in one script might hold
952 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200953 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200954 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200955 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200956 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200957 ga_clear(&ga_scripts);
958
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000959 /* unreferenced lists and dicts */
960 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000961
962 /* functions */
963 free_all_functions();
964 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000965}
966#endif
967
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 * Return the name of the executed function.
970 */
971 char_u *
972func_name(cookie)
973 void *cookie;
974{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000975 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the address holding the next breakpoint line for a funccall cookie.
980 */
981 linenr_T *
982func_breakpoint(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/*
989 * Return the address holding the debug tick for a funccall cookie.
990 */
991 int *
992func_dbg_tick(cookie)
993 void *cookie;
994{
Bram Moolenaar33570922005-01-25 22:26:29 +0000995 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996}
997
998/*
999 * Return the nesting level for a funccall cookie.
1000 */
1001 int
1002func_level(cookie)
1003 void *cookie;
1004{
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006}
1007
1008/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001009funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001011/* pointer to list of previously used funccal, still around because some
1012 * item in it is still being used. */
1013funccall_T *previous_funccal = NULL;
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015/*
1016 * Return TRUE when a function was ended by a ":return" command.
1017 */
1018 int
1019current_func_returned()
1020{
1021 return current_funccal->returned;
1022}
1023
1024
1025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * Set an internal variable to a string value. Creates the variable if it does
1027 * not already exist.
1028 */
1029 void
1030set_internal_string_var(name, value)
1031 char_u *name;
1032 char_u *value;
1033{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001034 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037 val = vim_strsave(value);
1038 if (val != NULL)
1039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001040 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001041 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001043 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001044 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 }
1046 }
1047}
1048
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001050static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051static char_u *redir_endp = NULL;
1052static char_u *redir_varname = NULL;
1053
1054/*
1055 * Start recording command output to a variable
1056 * Returns OK if successfully completed the setup. FAIL otherwise.
1057 */
1058 int
1059var_redir_start(name, append)
1060 char_u *name;
1061 int append; /* append to an existing variable */
1062{
1063 int save_emsg;
1064 int err;
1065 typval_T tv;
1066
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001068 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 {
1070 EMSG(_(e_invarg));
1071 return FAIL;
1072 }
1073
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 redir_varname = vim_strsave(name);
1076 if (redir_varname == NULL)
1077 return FAIL;
1078
1079 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1080 if (redir_lval == NULL)
1081 {
1082 var_redir_stop();
1083 return FAIL;
1084 }
1085
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001086 /* The output is stored in growarray "redir_ga" until redirection ends. */
1087 ga_init2(&redir_ga, (int)sizeof(char), 500);
1088
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001090 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001091 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1093 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001094 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 if (redir_endp != NULL && *redir_endp != NUL)
1096 /* Trailing characters are present after the variable name */
1097 EMSG(_(e_trailing));
1098 else
1099 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
1104
1105 /* check if we can write to the variable: set it to or append an empty
1106 * string */
1107 save_emsg = did_emsg;
1108 did_emsg = FALSE;
1109 tv.v_type = VAR_STRING;
1110 tv.vval.v_string = (char_u *)"";
1111 if (append)
1112 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1113 else
1114 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001115 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001117 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 if (err)
1119 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001120 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 var_redir_stop();
1122 return FAIL;
1123 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124
1125 return OK;
1126}
1127
1128/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001129 * Append "value[value_len]" to the variable set by var_redir_start().
1130 * The actual appending is postponed until redirection ends, because the value
1131 * appended may in fact be the string we write to, changing it may cause freed
1132 * memory to be used:
1133 * :redir => foo
1134 * :let foo
1135 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 */
1137 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143
1144 if (redir_lval == NULL)
1145 return;
1146
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001147 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001148 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001150 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001152 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153 {
1154 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001155 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156 }
1157 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159}
1160
1161/*
1162 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001163 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 */
1165 void
1166var_redir_stop()
1167{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001168 typval_T tv;
1169
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 if (redir_lval != NULL)
1171 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 /* If there was no error: assign the text to the variable. */
1173 if (redir_endp != NULL)
1174 {
1175 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1176 tv.v_type = VAR_STRING;
1177 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001178 /* Call get_lval() again, if it's inside a Dict or List it may
1179 * have changed. */
1180 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001181 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001182 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1183 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1184 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001185 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001186
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001187 /* free the collected output */
1188 vim_free(redir_ga.ga_data);
1189 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 vim_free(redir_lval);
1192 redir_lval = NULL;
1193 }
1194 vim_free(redir_varname);
1195 redir_varname = NULL;
1196}
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198# if defined(FEAT_MBYTE) || defined(PROTO)
1199 int
1200eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1201 char_u *enc_from;
1202 char_u *enc_to;
1203 char_u *fname_from;
1204 char_u *fname_to;
1205{
1206 int err = FALSE;
1207
1208 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1209 set_vim_var_string(VV_CC_TO, enc_to, -1);
1210 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1211 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1212 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1213 err = TRUE;
1214 set_vim_var_string(VV_CC_FROM, NULL, -1);
1215 set_vim_var_string(VV_CC_TO, NULL, -1);
1216 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1217 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1218
1219 if (err)
1220 return FAIL;
1221 return OK;
1222}
1223# endif
1224
1225# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1226 int
1227eval_printexpr(fname, args)
1228 char_u *fname;
1229 char_u *args;
1230{
1231 int err = FALSE;
1232
1233 set_vim_var_string(VV_FNAME_IN, fname, -1);
1234 set_vim_var_string(VV_CMDARG, args, -1);
1235 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1236 err = TRUE;
1237 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1238 set_vim_var_string(VV_CMDARG, NULL, -1);
1239
1240 if (err)
1241 {
1242 mch_remove(fname);
1243 return FAIL;
1244 }
1245 return OK;
1246}
1247# endif
1248
1249# if defined(FEAT_DIFF) || defined(PROTO)
1250 void
1251eval_diff(origfile, newfile, outfile)
1252 char_u *origfile;
1253 char_u *newfile;
1254 char_u *outfile;
1255{
1256 int err = FALSE;
1257
1258 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1259 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1260 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1261 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1262 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1263 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1264 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1265}
1266
1267 void
1268eval_patch(origfile, difffile, outfile)
1269 char_u *origfile;
1270 char_u *difffile;
1271 char_u *outfile;
1272{
1273 int err;
1274
1275 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1276 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1277 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1278 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1279 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1280 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1281 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1282}
1283# endif
1284
1285/*
1286 * Top level evaluation function, returning a boolean.
1287 * Sets "error" to TRUE if there was an error.
1288 * Return TRUE or FALSE.
1289 */
1290 int
1291eval_to_bool(arg, error, nextcmd, skip)
1292 char_u *arg;
1293 int *error;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 int retval = FALSE;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 else
1305 {
1306 *error = FALSE;
1307 if (!skip)
1308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001309 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 }
1313 if (skip)
1314 --emsg_skip;
1315
1316 return retval;
1317}
1318
1319/*
1320 * Top level evaluation function, returning a string. If "skip" is TRUE,
1321 * only parsing to "nextcmd" is done, without reporting errors. Return
1322 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1323 */
1324 char_u *
1325eval_to_string_skip(arg, nextcmd, skip)
1326 char_u *arg;
1327 char_u **nextcmd;
1328 int skip; /* only parse, don't execute */
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 char_u *retval;
1332
1333 if (skip)
1334 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 retval = NULL;
1337 else
1338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 retval = vim_strsave(get_tv_string(&tv));
1340 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342 if (skip)
1343 --emsg_skip;
1344
1345 return retval;
1346}
1347
1348/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001349 * Skip over an expression at "*pp".
1350 * Return FAIL for an error, OK otherwise.
1351 */
1352 int
1353skip_expr(pp)
1354 char_u **pp;
1355{
Bram Moolenaar33570922005-01-25 22:26:29 +00001356 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001357
1358 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001360}
1361
1362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364 * When "convert" is TRUE convert a List into a sequence of lines and convert
1365 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 * Return pointer to allocated memory, or NULL for failure.
1367 */
1368 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001369eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *arg;
1371 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001376 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001377#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001378 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 retval = NULL;
1383 else
1384 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001385 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001386 {
1387 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001388 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001389 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001390 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001391 if (tv.vval.v_list->lv_len > 0)
1392 ga_append(&ga, NL);
1393 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 ga_append(&ga, NUL);
1395 retval = (char_u *)ga.ga_data;
1396 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001397#ifdef FEAT_FLOAT
1398 else if (convert && tv.v_type == VAR_FLOAT)
1399 {
1400 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1401 retval = vim_strsave(numbuf);
1402 }
1403#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001404 else
1405 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408
1409 return retval;
1410}
1411
1412/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 * Call eval_to_string() without using current local variables and using
1414 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 */
1416 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 char_u *arg;
1419 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001420 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 char_u *retval;
1423 void *save_funccalp;
1424
1425 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001426 if (use_sandbox)
1427 ++sandbox;
1428 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001430 if (use_sandbox)
1431 --sandbox;
1432 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 restore_funccal(save_funccalp);
1434 return retval;
1435}
1436
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437/*
1438 * Top level evaluation function, returning a number.
1439 * Evaluates "expr" silently.
1440 * Returns -1 for an error.
1441 */
1442 int
1443eval_to_number(expr)
1444 char_u *expr;
1445{
Bram Moolenaar33570922005-01-25 22:26:29 +00001446 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001448 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 ++emsg_off;
1451
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 retval = -1;
1454 else
1455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001456 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001457 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
1459 --emsg_off;
1460
1461 return retval;
1462}
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464/*
1465 * Prepare v: variable "idx" to be used.
1466 * Save the current typeval in "save_tv".
1467 * When not used yet add the variable to the v: hashtable.
1468 */
1469 static void
1470prepare_vimvar(idx, save_tv)
1471 int idx;
1472 typval_T *save_tv;
1473{
1474 *save_tv = vimvars[idx].vv_tv;
1475 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1476 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1477}
1478
1479/*
1480 * Restore v: variable "idx" to typeval "save_tv".
1481 * When no longer defined, remove the variable from the v: hashtable.
1482 */
1483 static void
1484restore_vimvar(idx, save_tv)
1485 int idx;
1486 typval_T *save_tv;
1487{
1488 hashitem_T *hi;
1489
Bram Moolenaara40058a2005-07-11 22:42:07 +00001490 vimvars[idx].vv_tv = *save_tv;
1491 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1492 {
1493 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1494 if (HASHITEM_EMPTY(hi))
1495 EMSG2(_(e_intern2), "restore_vimvar()");
1496 else
1497 hash_remove(&vimvarht, hi);
1498 }
1499}
1500
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001501#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001502/*
1503 * Evaluate an expression to a list with suggestions.
1504 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001505 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001506 */
1507 list_T *
1508eval_spell_expr(badword, expr)
1509 char_u *badword;
1510 char_u *expr;
1511{
1512 typval_T save_val;
1513 typval_T rettv;
1514 list_T *list = NULL;
1515 char_u *p = skipwhite(expr);
1516
1517 /* Set "v:val" to the bad word. */
1518 prepare_vimvar(VV_VAL, &save_val);
1519 vimvars[VV_VAL].vv_type = VAR_STRING;
1520 vimvars[VV_VAL].vv_str = badword;
1521 if (p_verbose == 0)
1522 ++emsg_off;
1523
1524 if (eval1(&p, &rettv, TRUE) == OK)
1525 {
1526 if (rettv.v_type != VAR_LIST)
1527 clear_tv(&rettv);
1528 else
1529 list = rettv.vval.v_list;
1530 }
1531
1532 if (p_verbose == 0)
1533 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534 restore_vimvar(VV_VAL, &save_val);
1535
1536 return list;
1537}
1538
1539/*
1540 * "list" is supposed to contain two items: a word and a number. Return the
1541 * word in "pp" and the number as the return value.
1542 * Return -1 if anything isn't right.
1543 * Used to get the good word and score from the eval_spell_expr() result.
1544 */
1545 int
1546get_spellword(list, pp)
1547 list_T *list;
1548 char_u **pp;
1549{
1550 listitem_T *li;
1551
1552 li = list->lv_first;
1553 if (li == NULL)
1554 return -1;
1555 *pp = get_tv_string(&li->li_tv);
1556
1557 li = li->li_next;
1558 if (li == NULL)
1559 return -1;
1560 return get_tv_number(&li->li_tv);
1561}
1562#endif
1563
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001564/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001565 * Top level evaluation function.
1566 * Returns an allocated typval_T with the result.
1567 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 */
1569 typval_T *
1570eval_expr(arg, nextcmd)
1571 char_u *arg;
1572 char_u **nextcmd;
1573{
1574 typval_T *tv;
1575
1576 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001577 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001578 {
1579 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001580 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001581 }
1582
1583 return tv;
1584}
1585
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589 * Uses argv[argc] for the function arguments. Only Number and String
1590 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001593 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001594call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 char_u *func;
1596 int argc;
1597 char_u **argv;
1598 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001599 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 long n;
1604 int len;
1605 int i;
1606 int doesrange;
1607 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001610 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614 for (i = 0; i < argc; i++)
1615 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001616 /* Pass a NULL or empty argument as an empty string */
1617 if (argv[i] == NULL || *argv[i] == NUL)
1618 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001619 argvars[i].v_type = VAR_STRING;
1620 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001621 continue;
1622 }
1623
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001624 if (str_arg_only)
1625 len = 0;
1626 else
1627 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001628 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (len != 0 && len == (int)STRLEN(argv[i]))
1630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001631 argvars[i].v_type = VAR_NUMBER;
1632 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 else
1635 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001636 argvars[i].v_type = VAR_STRING;
1637 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
1639 }
1640
1641 if (safe)
1642 {
1643 save_funccalp = save_funccal();
1644 ++sandbox;
1645 }
1646
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1648 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001650 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (safe)
1652 {
1653 --sandbox;
1654 restore_funccal(save_funccalp);
1655 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 vim_free(argvars);
1657
1658 if (ret == FAIL)
1659 clear_tv(rettv);
1660
1661 return ret;
1662}
1663
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001664/*
1665 * Call vimL function "func" and return the result as a number.
1666 * Returns -1 when calling the function fails.
1667 * Uses argv[argc] for the function arguments.
1668 */
1669 long
1670call_func_retnr(func, argc, argv, safe)
1671 char_u *func;
1672 int argc;
1673 char_u **argv;
1674 int safe; /* use the sandbox */
1675{
1676 typval_T rettv;
1677 long retval;
1678
1679 /* All arguments are passed as strings, no conversion to number. */
1680 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1681 return -1;
1682
1683 retval = get_tv_number_chk(&rettv, NULL);
1684 clear_tv(&rettv);
1685 return retval;
1686}
1687
1688#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1689 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1690
Bram Moolenaar4f688582007-07-24 12:34:30 +00001691# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001693 * Call vimL function "func" and return the result as a string.
1694 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 * Uses argv[argc] for the function arguments.
1696 */
1697 void *
1698call_func_retstr(func, argc, argv, safe)
1699 char_u *func;
1700 int argc;
1701 char_u **argv;
1702 int safe; /* use the sandbox */
1703{
1704 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001705 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001707 /* All arguments are passed as strings, no conversion to number. */
1708 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709 return NULL;
1710
1711 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 return retval;
1714}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001715# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001717/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001718 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001720 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 */
1722 void *
1723call_func_retlist(func, argc, argv, safe)
1724 char_u *func;
1725 int argc;
1726 char_u **argv;
1727 int safe; /* use the sandbox */
1728{
1729 typval_T rettv;
1730
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001731 /* All arguments are passed as strings, no conversion to number. */
1732 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 return NULL;
1734
1735 if (rettv.v_type != VAR_LIST)
1736 {
1737 clear_tv(&rettv);
1738 return NULL;
1739 }
1740
1741 return rettv.vval.v_list;
1742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
1744
1745/*
1746 * Save the current function call pointer, and set it to NULL.
1747 * Used when executing autocommands and for ":source".
1748 */
1749 void *
1750save_funccal()
1751{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001752 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 current_funccal = NULL;
1755 return (void *)fc;
1756}
1757
1758 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001759restore_funccal(vfc)
1760 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762 funccall_T *fc = (funccall_T *)vfc;
1763
1764 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765}
1766
Bram Moolenaar05159a02005-02-26 23:04:13 +00001767#if defined(FEAT_PROFILE) || defined(PROTO)
1768/*
1769 * Prepare profiling for entering a child or something else that is not
1770 * counted for the script/function itself.
1771 * Should always be called in pair with prof_child_exit().
1772 */
1773 void
1774prof_child_enter(tm)
1775 proftime_T *tm; /* place to store waittime */
1776{
1777 funccall_T *fc = current_funccal;
1778
1779 if (fc != NULL && fc->func->uf_profiling)
1780 profile_start(&fc->prof_child);
1781 script_prof_save(tm);
1782}
1783
1784/*
1785 * Take care of time spent in a child.
1786 * Should always be called after prof_child_enter().
1787 */
1788 void
1789prof_child_exit(tm)
1790 proftime_T *tm; /* where waittime was stored */
1791{
1792 funccall_T *fc = current_funccal;
1793
1794 if (fc != NULL && fc->func->uf_profiling)
1795 {
1796 profile_end(&fc->prof_child);
1797 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1798 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1799 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1800 }
1801 script_prof_restore(tm);
1802}
1803#endif
1804
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806#ifdef FEAT_FOLDING
1807/*
1808 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1809 * it in "*cp". Doesn't give error messages.
1810 */
1811 int
1812eval_foldexpr(arg, cp)
1813 char_u *arg;
1814 int *cp;
1815{
Bram Moolenaar33570922005-01-25 22:26:29 +00001816 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 int retval;
1818 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001819 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1820 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001823 if (use_sandbox)
1824 ++sandbox;
1825 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 retval = 0;
1829 else
1830 {
1831 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 if (tv.v_type == VAR_NUMBER)
1833 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001834 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 retval = 0;
1836 else
1837 {
1838 /* If the result is a string, check if there is a non-digit before
1839 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (!VIM_ISDIGIT(*s) && *s != '-')
1842 *cp = *s++;
1843 retval = atol((char *)s);
1844 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001848 if (use_sandbox)
1849 --sandbox;
1850 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852 return retval;
1853}
1854#endif
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 * ":let" list all variable values
1858 * ":let var1 var2" list variable values
1859 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001860 * ":let var += expr" assignment command.
1861 * ":let var -= expr" assignment command.
1862 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 */
1865 void
1866ex_let(eap)
1867 exarg_T *eap;
1868{
1869 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 int var_count = 0;
1874 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001876 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001877 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 argend = skip_var_list(arg, &var_count, &semicolon);
1880 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001882 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1883 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001884 expr = skipwhite(argend);
1885 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1886 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001888 /*
1889 * ":let" without "=": list variables
1890 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 if (*arg == '[')
1892 EMSG(_(e_invarg));
1893 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001895 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001899 list_glob_vars(&first);
1900 list_buf_vars(&first);
1901 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001902#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001904#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 list_script_vars(&first);
1906 list_func_vars(&first);
1907 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 eap->nextcmd = check_nextcmd(arg);
1910 }
1911 else
1912 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op[0] = '=';
1914 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001917 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1918 op[0] = *expr; /* +=, -= or .= */
1919 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001921 else
1922 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (eap->skip)
1925 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (eap->skip)
1928 {
1929 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 --emsg_skip;
1932 }
1933 else if (i != FAIL)
1934 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 }
1940}
1941
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942/*
1943 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1944 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 * When "nextchars" is not NULL it points to a string with characters that
1946 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1947 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 * Returns OK or FAIL;
1949 */
1950 static int
1951ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1952 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 int copy; /* copy values from "tv", don't move */
1955 int semicolon; /* from skip_var_list() */
1956 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958{
1959 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001962 listitem_T *item;
1963 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964
1965 if (*arg != '[')
1966 {
1967 /*
1968 * ":let var = expr" or ":for var in list"
1969 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 return FAIL;
1972 return OK;
1973 }
1974
1975 /*
1976 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1977 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001978 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 {
1980 EMSG(_(e_listreq));
1981 return FAIL;
1982 }
1983
1984 i = list_len(l);
1985 if (semicolon == 0 && var_count < i)
1986 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001987 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 return FAIL;
1989 }
1990 if (var_count - semicolon > i)
1991 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001992 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 return FAIL;
1994 }
1995
1996 item = l->lv_first;
1997 while (*arg != ']')
1998 {
1999 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 item = item->li_next;
2002 if (arg == NULL)
2003 return FAIL;
2004
2005 arg = skipwhite(arg);
2006 if (*arg == ';')
2007 {
2008 /* Put the rest of the list (may be empty) in the var after ';'.
2009 * Create a new list for this. */
2010 l = list_alloc();
2011 if (l == NULL)
2012 return FAIL;
2013 while (item != NULL)
2014 {
2015 list_append_tv(l, &item->li_tv);
2016 item = item->li_next;
2017 }
2018
2019 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002020 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 ltv.vval.v_list = l;
2022 l->lv_refcount = 1;
2023
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2025 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 clear_tv(&ltv);
2027 if (arg == NULL)
2028 return FAIL;
2029 break;
2030 }
2031 else if (*arg != ',' && *arg != ']')
2032 {
2033 EMSG2(_(e_intern2), "ex_let_vars()");
2034 return FAIL;
2035 }
2036 }
2037
2038 return OK;
2039}
2040
2041/*
2042 * Skip over assignable variable "var" or list of variables "[var, var]".
2043 * Used for ":let varvar = expr" and ":for varvar in expr".
2044 * For "[var, var]" increment "*var_count" for each variable.
2045 * for "[var, var; var]" set "semicolon".
2046 * Return NULL for an error.
2047 */
2048 static char_u *
2049skip_var_list(arg, var_count, semicolon)
2050 char_u *arg;
2051 int *var_count;
2052 int *semicolon;
2053{
2054 char_u *p, *s;
2055
2056 if (*arg == '[')
2057 {
2058 /* "[var, var]": find the matching ']'. */
2059 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002060 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 {
2062 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2063 s = skip_var_one(p);
2064 if (s == p)
2065 {
2066 EMSG2(_(e_invarg2), p);
2067 return NULL;
2068 }
2069 ++*var_count;
2070
2071 p = skipwhite(s);
2072 if (*p == ']')
2073 break;
2074 else if (*p == ';')
2075 {
2076 if (*semicolon == 1)
2077 {
2078 EMSG(_("Double ; in list of variables"));
2079 return NULL;
2080 }
2081 *semicolon = 1;
2082 }
2083 else if (*p != ',')
2084 {
2085 EMSG2(_(e_invarg2), p);
2086 return NULL;
2087 }
2088 }
2089 return p + 1;
2090 }
2091 else
2092 return skip_var_one(arg);
2093}
2094
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002095/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002096 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002097 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099 static char_u *
2100skip_var_one(arg)
2101 char_u *arg;
2102{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002103 if (*arg == '@' && arg[1] != NUL)
2104 return arg + 2;
2105 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2106 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002107}
2108
Bram Moolenaara7043832005-01-21 11:56:39 +00002109/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 * List variables for hashtab "ht" with prefix "prefix".
2111 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002115 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002116 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 hashitem_T *hi;
2121 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 int todo;
2123
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002124 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2126 {
2127 if (!HASHITEM_EMPTY(hi))
2128 {
2129 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 di = HI2DI(hi);
2131 if (empty || di->di_tv.v_type != VAR_STRING
2132 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134 }
2135 }
2136}
2137
2138/*
2139 * List global variables.
2140 */
2141 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142list_glob_vars(first)
2143 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002146}
2147
2148/*
2149 * List buffer variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_buf_vars(first)
2153 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002154{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155 char_u numbuf[NUMBUFLEN];
2156
Bram Moolenaar429fa852013-04-15 12:27:36 +02002157 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002159
2160 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2162 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002163}
2164
2165/*
2166 * List window variables.
2167 */
2168 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169list_win_vars(first)
2170 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002171{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002172 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002174}
2175
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176#ifdef FEAT_WINDOWS
2177/*
2178 * List tab page variables.
2179 */
2180 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181list_tab_vars(first)
2182 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002183{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002184 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186}
2187#endif
2188
Bram Moolenaara7043832005-01-21 11:56:39 +00002189/*
2190 * List Vim variables.
2191 */
2192 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193list_vim_vars(first)
2194 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197}
2198
2199/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200 * List script-local variables, if there is a script.
2201 */
2202 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203list_script_vars(first)
2204 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002205{
2206 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2208 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002209}
2210
2211/*
2212 * List function variables, if there is a function.
2213 */
2214 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215list_func_vars(first)
2216 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002217{
2218 if (current_funccal != NULL)
2219 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002221}
2222
2223/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 * List variables in "arg".
2225 */
2226 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002227list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 exarg_T *eap;
2229 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231{
2232 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 char_u *name_start;
2236 char_u *arg_subsc;
2237 char_u *tofree;
2238 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239
2240 while (!ends_excmd(*arg) && !got_int)
2241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002244 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2246 {
2247 emsg_severe = TRUE;
2248 EMSG(_(e_trailing));
2249 break;
2250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* get_name_len() takes care of expanding curly braces */
2255 name_start = name = arg;
2256 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2257 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 /* This is mainly to keep test 49 working: when expanding
2260 * curly braces fails overrule the exception error message. */
2261 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 emsg_severe = TRUE;
2264 EMSG2(_(e_invarg2), arg);
2265 break;
2266 }
2267 error = TRUE;
2268 }
2269 else
2270 {
2271 if (tofree != NULL)
2272 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002273 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 else
2276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 /* handle d.key, l[idx], f(expr) */
2278 arg_subsc = arg;
2279 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002280 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002287 case 'g': list_glob_vars(first); break;
2288 case 'b': list_buf_vars(first); break;
2289 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002290#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002291 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002292#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002293 case 'v': list_vim_vars(first); break;
2294 case 's': list_script_vars(first); break;
2295 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 default:
2297 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
2300 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 {
2302 char_u numbuf[NUMBUFLEN];
2303 char_u *tf;
2304 int c;
2305 char_u *s;
2306
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002307 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 c = *arg;
2309 *arg = NUL;
2310 list_one_var_a((char_u *)"",
2311 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002312 tv.v_type,
2313 s == NULL ? (char_u *)"" : s,
2314 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 *arg = c;
2316 vim_free(tf);
2317 }
2318 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
2321 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322
2323 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325
2326 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
2328
2329 return arg;
2330}
2331
2332/*
2333 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2334 * Returns a pointer to the char just after the var name.
2335 * Returns NULL if there is an error.
2336 */
2337 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002340 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002342 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344{
2345 int c1;
2346 char_u *name;
2347 char_u *p;
2348 char_u *arg_end = NULL;
2349 int len;
2350 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002351 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352
2353 /*
2354 * ":let $VAR = expr": Set environment variable.
2355 */
2356 if (*arg == '$')
2357 {
2358 /* Find the end of the name. */
2359 ++arg;
2360 name = arg;
2361 len = get_env_len(&arg);
2362 if (len == 0)
2363 EMSG2(_(e_invarg2), name - 1);
2364 else
2365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 if (op != NULL && (*op == '+' || *op == '-'))
2367 EMSG2(_(e_letwrong), op);
2368 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002369 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002371 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 {
2373 c1 = name[len];
2374 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002375 p = get_tv_string_chk(tv);
2376 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 {
2378 int mustfree = FALSE;
2379 char_u *s = vim_getenv(name, &mustfree);
2380
2381 if (s != NULL)
2382 {
2383 p = tofree = concat_str(s, p);
2384 if (mustfree)
2385 vim_free(s);
2386 }
2387 }
2388 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 if (STRICMP(name, "HOME") == 0)
2392 init_homedir();
2393 else if (didset_vim && STRICMP(name, "VIM") == 0)
2394 didset_vim = FALSE;
2395 else if (didset_vimruntime
2396 && STRICMP(name, "VIMRUNTIME") == 0)
2397 didset_vimruntime = FALSE;
2398 arg_end = arg;
2399 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 }
2403 }
2404 }
2405
2406 /*
2407 * ":let &option = expr": Set option value.
2408 * ":let &l:option = expr": Set local option value.
2409 * ":let &g:option = expr": Set global option value.
2410 */
2411 else if (*arg == '&')
2412 {
2413 /* Find the end of the name. */
2414 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002415 if (p == NULL || (endchars != NULL
2416 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 EMSG(_(e_letunexp));
2418 else
2419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 long n;
2421 int opt_type;
2422 long numval;
2423 char_u *stringval = NULL;
2424 char_u *s;
2425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 c1 = *p;
2427 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428
2429 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002430 s = get_tv_string_chk(tv); /* != NULL if number or string */
2431 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 {
2433 opt_type = get_option_value(arg, &numval,
2434 &stringval, opt_flags);
2435 if ((opt_type == 1 && *op == '.')
2436 || (opt_type == 0 && *op != '.'))
2437 EMSG2(_(e_letwrong), op);
2438 else
2439 {
2440 if (opt_type == 1) /* number */
2441 {
2442 if (*op == '+')
2443 n = numval + n;
2444 else
2445 n = numval - n;
2446 }
2447 else if (opt_type == 0 && stringval != NULL) /* string */
2448 {
2449 s = concat_str(stringval, s);
2450 vim_free(stringval);
2451 stringval = s;
2452 }
2453 }
2454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 if (s != NULL)
2456 {
2457 set_option_value(arg, n, s, opt_flags);
2458 arg_end = p;
2459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
2463 }
2464
2465 /*
2466 * ":let @r = expr": Set register contents.
2467 */
2468 else if (*arg == '@')
2469 {
2470 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 if (op != NULL && (*op == '+' || *op == '-'))
2472 EMSG2(_(e_letwrong), op);
2473 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002474 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002478 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 char_u *s;
2480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 p = get_tv_string_chk(tv);
2482 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002484 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (s != NULL)
2486 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 vim_free(s);
2489 }
2490 }
2491 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 arg_end = arg + 1;
2495 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498 }
2499
2500 /*
2501 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002504 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002506 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002508 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2512 EMSG(_(e_letunexp));
2513 else
2514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 arg_end = p;
2517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 }
2521
2522 else
2523 EMSG2(_(e_invarg2), arg);
2524
2525 return arg_end;
2526}
2527
2528/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2530 */
2531 static int
2532check_changedtick(arg)
2533 char_u *arg;
2534{
2535 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2536 {
2537 EMSG2(_(e_readonlyvar), arg);
2538 return TRUE;
2539 }
2540 return FALSE;
2541}
2542
2543/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Get an lval: variable, Dict item or List item that can be assigned a value
2545 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2546 * "name.key", "name.key[expr]" etc.
2547 * Indexing only works if "name" is an existing List or Dictionary.
2548 * "name" points to the start of the name.
2549 * If "rettv" is not NULL it points to the value to be assigned.
2550 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2551 * wrong; must end in space or cmd separator.
2552 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 * flags:
2554 * GLV_QUIET: do not give error messages
2555 * GLV_NO_AUTOLOAD: do not use script autoloading
2556 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002558 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 */
2561 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 typval_T *rettv;
2565 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 int unlet;
2567 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002569 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 char_u *expr_start, *expr_end;
2573 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 dictitem_T *v;
2575 typval_T var1;
2576 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002578 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002582 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586
2587 if (skip)
2588 {
2589 /* When skipping just find the end of the name. */
2590 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002591 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 }
2593
2594 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002595 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (expr_start != NULL)
2597 {
2598 /* Don't expand the name when we already know there is an error. */
2599 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2600 && *p != '[' && *p != '.')
2601 {
2602 EMSG(_(e_trailing));
2603 return NULL;
2604 }
2605
2606 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2607 if (lp->ll_exp_name == NULL)
2608 {
2609 /* Report an invalid expression in braces, unless the
2610 * expression evaluation has been cancelled due to an
2611 * aborting error, an interrupt, or an exception. */
2612 if (!aborting() && !quiet)
2613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002614 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 EMSG2(_(e_invarg2), name);
2616 return NULL;
2617 }
2618 }
2619 lp->ll_name = lp->ll_exp_name;
2620 }
2621 else
2622 lp->ll_name = name;
2623
2624 /* Without [idx] or .key we are done. */
2625 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2626 return p;
2627
2628 cc = *p;
2629 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002630 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (v == NULL && !quiet)
2632 EMSG2(_(e_undefvar), lp->ll_name);
2633 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002634 if (v == NULL)
2635 return NULL;
2636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 /*
2638 * Loop until no more [idx] or .key is following.
2639 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002640 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2644 && !(lp->ll_tv->v_type == VAR_DICT
2645 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (!quiet)
2648 EMSG(_("E689: Can only index a List or Dictionary"));
2649 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E708: [:] must come last"));
2655 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 len = -1;
2659 if (*p == '.')
2660 {
2661 key = p + 1;
2662 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2663 ;
2664 if (len == 0)
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_(e_emptykey));
2668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = key + len;
2671 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 else
2673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (*p == ':')
2677 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 else
2679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 empty1 = FALSE;
2681 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002683 if (get_tv_string_chk(&var1) == NULL)
2684 {
2685 /* not a number or string */
2686 clear_tv(&var1);
2687 return NULL;
2688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690
2691 /* Optionally get the second index [ :expr]. */
2692 if (*p == ':')
2693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002697 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 if (!empty1)
2699 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (rettv != NULL && (rettv->v_type != VAR_LIST
2703 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
2706 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 if (!empty1)
2708 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 }
2711 p = skipwhite(p + 1);
2712 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 else
2715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (!empty1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002723 if (get_tv_string_chk(&var2) == NULL)
2724 {
2725 /* not a number or string */
2726 if (!empty1)
2727 clear_tv(&var1);
2728 clear_tv(&var2);
2729 return NULL;
2730 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002738 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (!quiet)
2740 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 if (!empty1)
2742 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747
2748 /* Skip to past ']'. */
2749 ++p;
2750 }
2751
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
2754 if (len == -1)
2755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (*key == NUL)
2759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (!quiet)
2761 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 }
2765 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002766 lp->ll_list = NULL;
2767 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002768 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002770 /* When assigning to a scope dictionary check that a function and
2771 * variable name is valid (only variable name unless it is l: or
2772 * g: dictionary). Disallow overwriting a builtin function. */
2773 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 int prevval;
2776 int wrong;
2777
2778 if (len != -1)
2779 {
2780 prevval = key[len];
2781 key[len] = NUL;
2782 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002783 else
2784 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002785 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2786 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 || !valid_varname(key);
2789 if (len != -1)
2790 key[len] = prevval;
2791 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002792 return NULL;
2793 }
2794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002797 /* Can't add "v:" variable. */
2798 if (lp->ll_dict == &vimvardict)
2799 {
2800 EMSG2(_(e_illvar), name);
2801 return NULL;
2802 }
2803
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002804 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002808 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 if (len == -1)
2810 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 }
2813 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 if (len == -1)
2818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 p = NULL;
2821 break;
2822 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002823 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002824 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002825 return NULL;
2826
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 if (len == -1)
2828 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 }
2831 else
2832 {
2833 /*
2834 * Get the number and item for the only or first index of the List.
2835 */
2836 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 else
2839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002840 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 clear_tv(&var1);
2842 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_list = lp->ll_tv->vval.v_list;
2845 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2846 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002848 if (lp->ll_n1 < 0)
2849 {
2850 lp->ll_n1 = 0;
2851 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2852 }
2853 }
2854 if (lp->ll_li == NULL)
2855 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002858 if (!quiet)
2859 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 }
2862
2863 /*
2864 * May need to find the item or absolute index for the second
2865 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 * When no index given: "lp->ll_empty2" is TRUE.
2867 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002871 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002877 {
2878 if (!quiet)
2879 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002881 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 }
2884
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2886 if (lp->ll_n1 < 0)
2887 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2888 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 {
2890 if (!quiet)
2891 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 }
2899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 return p;
2901}
2902
2903/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
2907clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909{
2910 vim_free(lp->ll_exp_name);
2911 vim_free(lp->ll_newkey);
2912}
2913
2914/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 */
2919 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926{
2927 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002928 listitem_T *ri;
2929 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930
2931 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 cc = *endp;
2936 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 if (op != NULL && *op != '=')
2938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002941 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002942 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002943 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002944 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002946 if ((di == NULL
2947 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2948 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2949 FALSE)))
2950 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 set_var(lp->ll_name, &tv, FALSE);
2952 clear_tv(&tv);
2953 }
2954 }
2955 else
2956 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 else if (tv_check_lock(lp->ll_newkey == NULL
2961 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002962 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 else if (lp->ll_range)
2965 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002966 listitem_T *ll_li = lp->ll_li;
2967 int ll_n1 = lp->ll_n1;
2968
2969 /*
2970 * Check whether any of the list items is locked
2971 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002972 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002973 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002974 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002975 return;
2976 ri = ri->li_next;
2977 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2978 break;
2979 ll_li = ll_li->li_next;
2980 ++ll_n1;
2981 }
2982
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 /*
2984 * Assign the List values to the list items.
2985 */
2986 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988 if (op != NULL && *op != '=')
2989 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2990 else
2991 {
2992 clear_tv(&lp->ll_li->li_tv);
2993 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 ri = ri->li_next;
2996 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2997 break;
2998 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003001 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 ri = NULL;
3004 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 lp->ll_li = lp->ll_li->li_next;
3008 ++lp->ll_n1;
3009 }
3010 if (ri != NULL)
3011 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 else if (lp->ll_empty2
3013 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 : lp->ll_n1 != lp->ll_n2)
3015 EMSG(_("E711: List value has not enough items"));
3016 }
3017 else
3018 {
3019 /*
3020 * Assign to a List or Dictionary item.
3021 */
3022 if (lp->ll_newkey != NULL)
3023 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 if (op != NULL && *op != '=')
3025 {
3026 EMSG2(_(e_letwrong), op);
3027 return;
3028 }
3029
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003031 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 if (di == NULL)
3033 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3035 {
3036 vim_free(di);
3037 return;
3038 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003040 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003041 else if (op != NULL && *op != '=')
3042 {
3043 tv_op(lp->ll_tv, rettv, op);
3044 return;
3045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003048
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 /*
3050 * Assign the value to the variable or list item.
3051 */
3052 if (copy)
3053 copy_tv(rettv, lp->ll_tv);
3054 else
3055 {
3056 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003057 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 }
3060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061}
3062
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003063/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3065 * Returns OK or FAIL.
3066 */
3067 static int
3068tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003069 typval_T *tv1;
3070 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 char_u *op;
3072{
3073 long n;
3074 char_u numbuf[NUMBUFLEN];
3075 char_u *s;
3076
3077 /* Can't do anything with a Funcref or a Dict on the right. */
3078 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3079 {
3080 switch (tv1->v_type)
3081 {
3082 case VAR_DICT:
3083 case VAR_FUNC:
3084 break;
3085
3086 case VAR_LIST:
3087 if (*op != '+' || tv2->v_type != VAR_LIST)
3088 break;
3089 /* List += List */
3090 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3091 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3092 return OK;
3093
3094 case VAR_NUMBER:
3095 case VAR_STRING:
3096 if (tv2->v_type == VAR_LIST)
3097 break;
3098 if (*op == '+' || *op == '-')
3099 {
3100 /* nr += nr or nr -= nr*/
3101 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003102#ifdef FEAT_FLOAT
3103 if (tv2->v_type == VAR_FLOAT)
3104 {
3105 float_T f = n;
3106
3107 if (*op == '+')
3108 f += tv2->vval.v_float;
3109 else
3110 f -= tv2->vval.v_float;
3111 clear_tv(tv1);
3112 tv1->v_type = VAR_FLOAT;
3113 tv1->vval.v_float = f;
3114 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#endif
3117 {
3118 if (*op == '+')
3119 n += get_tv_number(tv2);
3120 else
3121 n -= get_tv_number(tv2);
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_NUMBER;
3124 tv1->vval.v_number = n;
3125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 }
3127 else
3128 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129 if (tv2->v_type == VAR_FLOAT)
3130 break;
3131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 /* str .= str */
3133 s = get_tv_string(tv1);
3134 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3135 clear_tv(tv1);
3136 tv1->v_type = VAR_STRING;
3137 tv1->vval.v_string = s;
3138 }
3139 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140
3141#ifdef FEAT_FLOAT
3142 case VAR_FLOAT:
3143 {
3144 float_T f;
3145
3146 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3147 && tv2->v_type != VAR_NUMBER
3148 && tv2->v_type != VAR_STRING))
3149 break;
3150 if (tv2->v_type == VAR_FLOAT)
3151 f = tv2->vval.v_float;
3152 else
3153 f = get_tv_number(tv2);
3154 if (*op == '+')
3155 tv1->vval.v_float += f;
3156 else
3157 tv1->vval.v_float -= f;
3158 }
3159 return OK;
3160#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003161 }
3162 }
3163
3164 EMSG2(_(e_letwrong), op);
3165 return FAIL;
3166}
3167
3168/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 * Add a watcher to a list.
3170 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003171 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 list_T *l;
3174 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175{
3176 lw->lw_next = l->lv_watch;
3177 l->lv_watch = lw;
3178}
3179
3180/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003181 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 * No warning when it isn't found...
3183 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003184 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003186 list_T *l;
3187 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188{
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190
3191 lwp = &l->lv_watch;
3192 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3193 {
3194 if (lw == lwrem)
3195 {
3196 *lwp = lw->lw_next;
3197 break;
3198 }
3199 lwp = &lw->lw_next;
3200 }
3201}
3202
3203/*
3204 * Just before removing an item from a list: advance watchers to the next
3205 * item.
3206 */
3207 static void
3208list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003209 list_T *l;
3210 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211{
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213
3214 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3215 if (lw->lw_item == item)
3216 lw->lw_item = item->li_next;
3217}
3218
3219/*
3220 * Evaluate the expression used in a ":for var in expr" command.
3221 * "arg" points to "var".
3222 * Set "*errp" to TRUE for an error, FALSE otherwise;
3223 * Return a pointer that holds the info. Null when there is an error.
3224 */
3225 void *
3226eval_for_line(arg, errp, nextcmdp, skip)
3227 char_u *arg;
3228 int *errp;
3229 char_u **nextcmdp;
3230 int skip;
3231{
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 typval_T tv;
3235 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236
3237 *errp = TRUE; /* default: there is an error */
3238
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240 if (fi == NULL)
3241 return NULL;
3242
3243 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3244 if (expr == NULL)
3245 return fi;
3246
3247 expr = skipwhite(expr);
3248 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3249 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003250 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 return fi;
3252 }
3253
3254 if (skip)
3255 ++emsg_skip;
3256 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3257 {
3258 *errp = FALSE;
3259 if (!skip)
3260 {
3261 l = tv.vval.v_list;
3262 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003263 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003265 clear_tv(&tv);
3266 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 else
3268 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003269 /* No need to increment the refcount, it's already set for the
3270 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 fi->fi_list = l;
3272 list_add_watch(l, &fi->fi_lw);
3273 fi->fi_lw.lw_item = l->lv_first;
3274 }
3275 }
3276 }
3277 if (skip)
3278 --emsg_skip;
3279
3280 return fi;
3281}
3282
3283/*
3284 * Use the first item in a ":for" list. Advance to the next.
3285 * Assign the values to the variable (list). "arg" points to the first one.
3286 * Return TRUE when a valid item was found, FALSE when at end of list or
3287 * something wrong.
3288 */
3289 int
3290next_for_item(fi_void, arg)
3291 void *fi_void;
3292 char_u *arg;
3293{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003294 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297
3298 item = fi->fi_lw.lw_item;
3299 if (item == NULL)
3300 result = FALSE;
3301 else
3302 {
3303 fi->fi_lw.lw_item = item->li_next;
3304 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3305 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3306 }
3307 return result;
3308}
3309
3310/*
3311 * Free the structure used to store info used by ":for".
3312 */
3313 void
3314free_for_info(fi_void)
3315 void *fi_void;
3316{
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003319 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 list_unref(fi->fi_list);
3323 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 vim_free(fi);
3325}
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3328
3329 void
3330set_context_for_expression(xp, arg, cmdidx)
3331 expand_T *xp;
3332 char_u *arg;
3333 cmdidx_T cmdidx;
3334{
3335 int got_eq = FALSE;
3336 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 if (cmdidx == CMD_let)
3340 {
3341 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003342 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 {
3344 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003348 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 if (vim_iswhite(*p))
3350 break;
3351 }
3352 return;
3353 }
3354 }
3355 else
3356 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3357 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 while ((xp->xp_pattern = vim_strpbrk(arg,
3359 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3360 {
3361 c = *xp->xp_pattern;
3362 if (c == '&')
3363 {
3364 c = xp->xp_pattern[1];
3365 if (c == '&')
3366 {
3367 ++xp->xp_pattern;
3368 xp->xp_context = cmdidx != CMD_let || got_eq
3369 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3370 }
3371 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3375 xp->xp_pattern += 2;
3376
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 else if (c == '$')
3380 {
3381 /* environment variable */
3382 xp->xp_context = EXPAND_ENV_VARS;
3383 }
3384 else if (c == '=')
3385 {
3386 got_eq = TRUE;
3387 xp->xp_context = EXPAND_EXPRESSION;
3388 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003389 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 && xp->xp_context == EXPAND_FUNCTIONS
3391 && vim_strchr(xp->xp_pattern, '(') == NULL)
3392 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003393 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395 }
3396 else if (cmdidx != CMD_let || got_eq)
3397 {
3398 if (c == '"') /* string */
3399 {
3400 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3401 if (c == '\\' && xp->xp_pattern[1] != NUL)
3402 ++xp->xp_pattern;
3403 xp->xp_context = EXPAND_NOTHING;
3404 }
3405 else if (c == '\'') /* literal string */
3406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3409 /* skip */ ;
3410 xp->xp_context = EXPAND_NOTHING;
3411 }
3412 else if (c == '|')
3413 {
3414 if (xp->xp_pattern[1] == '|')
3415 {
3416 ++xp->xp_pattern;
3417 xp->xp_context = EXPAND_EXPRESSION;
3418 }
3419 else
3420 xp->xp_context = EXPAND_COMMANDS;
3421 }
3422 else
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003426 /* Doesn't look like something valid, expand as an expression
3427 * anyway. */
3428 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 arg = xp->xp_pattern;
3430 if (*arg != NUL)
3431 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3432 /* skip */ ;
3433 }
3434 xp->xp_pattern = arg;
3435}
3436
3437#endif /* FEAT_CMDL_COMPL */
3438
3439/*
3440 * ":1,25call func(arg1, arg2)" function call.
3441 */
3442 void
3443ex_call(eap)
3444 exarg_T *eap;
3445{
3446 char_u *arg = eap->arg;
3447 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003449 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 linenr_T lnum;
3453 int doesrange;
3454 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003457 if (eap->skip)
3458 {
3459 /* trans_function_name() doesn't work well when skipping, use eval0()
3460 * instead to skip to any following command, e.g. for:
3461 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003462 ++emsg_skip;
3463 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3464 clear_tv(&rettv);
3465 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003466 return;
3467 }
3468
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003469 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003470 if (fudi.fd_newkey != NULL)
3471 {
3472 /* Still need to give an error message for missing key. */
3473 EMSG2(_(e_dictkey), fudi.fd_newkey);
3474 vim_free(fudi.fd_newkey);
3475 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003476 if (tofree == NULL)
3477 return;
3478
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 /* Increase refcount on dictionary, it could get deleted when evaluating
3480 * the arguments. */
3481 if (fudi.fd_dict != NULL)
3482 ++fudi.fd_dict->dv_refcount;
3483
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003484 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003485 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003486 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaar532c7802005-01-27 14:44:31 +00003488 /* Skip white space to allow ":call func ()". Not good, but required for
3489 * backward compatibility. */
3490 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 if (*startarg != '(')
3494 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003495 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 goto end;
3497 }
3498
3499 /*
3500 * When skipping, evaluate the function once, to find the end of the
3501 * arguments.
3502 * When the function takes a range, this is discovered after the first
3503 * call, and the loop is broken.
3504 */
3505 if (eap->skip)
3506 {
3507 ++emsg_skip;
3508 lnum = eap->line2; /* do it once, also with an invalid range */
3509 }
3510 else
3511 lnum = eap->line1;
3512 for ( ; lnum <= eap->line2; ++lnum)
3513 {
3514 if (!eap->skip && eap->addr_count > 0)
3515 {
3516 curwin->w_cursor.lnum = lnum;
3517 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003518#ifdef FEAT_VIRTUALEDIT
3519 curwin->w_cursor.coladd = 0;
3520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003523 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003524 eap->line1, eap->line2, &doesrange,
3525 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 failed = TRUE;
3528 break;
3529 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003530
3531 /* Handle a function returning a Funcref, Dictionary or List. */
3532 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3533 {
3534 failed = TRUE;
3535 break;
3536 }
3537
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (doesrange || eap->skip)
3540 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003543 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (aborting())
3547 break;
3548 }
3549 if (eap->skip)
3550 --emsg_skip;
3551
3552 if (!failed)
3553 {
3554 /* Check for trailing illegal characters and a following command. */
3555 if (!ends_excmd(*arg))
3556 {
3557 emsg_severe = TRUE;
3558 EMSG(_(e_trailing));
3559 }
3560 else
3561 eap->nextcmd = check_nextcmd(arg);
3562 }
3563
3564end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003565 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003566 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567}
3568
3569/*
3570 * ":unlet[!] var1 ... " command.
3571 */
3572 void
3573ex_unlet(eap)
3574 exarg_T *eap;
3575{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 ex_unletlock(eap, eap->arg, 0);
3577}
3578
3579/*
3580 * ":lockvar" and ":unlockvar" commands
3581 */
3582 void
3583ex_lockvar(eap)
3584 exarg_T *eap;
3585{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 int deep = 2;
3588
3589 if (eap->forceit)
3590 deep = -1;
3591 else if (vim_isdigit(*arg))
3592 {
3593 deep = getdigits(&arg);
3594 arg = skipwhite(arg);
3595 }
3596
3597 ex_unletlock(eap, arg, deep);
3598}
3599
3600/*
3601 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3602 */
3603 static void
3604ex_unletlock(eap, argstart, deep)
3605 exarg_T *eap;
3606 char_u *argstart;
3607 int deep;
3608{
3609 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 do
3615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003617 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003618 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (lv.ll_name == NULL)
3620 error = TRUE; /* error but continue parsing */
3621 if (name_end == NULL || (!vim_iswhite(*name_end)
3622 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 if (name_end != NULL)
3625 {
3626 emsg_severe = TRUE;
3627 EMSG(_(e_trailing));
3628 }
3629 if (!(eap->skip || error))
3630 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 break;
3632 }
3633
3634 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635 {
3636 if (eap->cmdidx == CMD_unlet)
3637 {
3638 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3639 error = TRUE;
3640 }
3641 else
3642 {
3643 if (do_lock_var(&lv, name_end, deep,
3644 eap->cmdidx == CMD_lockvar) == FAIL)
3645 error = TRUE;
3646 }
3647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 if (!eap->skip)
3650 clear_lval(&lv);
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 arg = skipwhite(name_end);
3653 } while (!ends_excmd(*arg));
3654
3655 eap->nextcmd = check_nextcmd(arg);
3656}
3657
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003660 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 int forceit;
3663{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 int ret = OK;
3665 int cc;
3666
3667 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 cc = *name_end;
3670 *name_end = NUL;
3671
3672 /* Normal name or expanded name. */
3673 if (check_changedtick(lp->ll_name))
3674 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003679 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003680 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 else if (lp->ll_range)
3685 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003687 listitem_T *ll_li = lp->ll_li;
3688 int ll_n1 = lp->ll_n1;
3689
3690 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3691 {
3692 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003694 return FAIL;
3695 ll_li = li;
3696 ++ll_n1;
3697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698
3699 /* Delete a range of List items. */
3700 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3701 {
3702 li = lp->ll_li->li_next;
3703 listitem_remove(lp->ll_list, lp->ll_li);
3704 lp->ll_li = li;
3705 ++lp->ll_n1;
3706 }
3707 }
3708 else
3709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 /* unlet a List item. */
3712 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003715 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 }
3717
3718 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719}
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721/*
3722 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
3725 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003728 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 hashtab_T *ht;
3731 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003732 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 ht = find_var_ht(name, &varname);
3737 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003739 if (ht == &globvarht)
3740 d = &globvardict;
3741 else if (current_funccal != NULL
3742 && ht == &current_funccal->l_vars.dv_hashtab)
3743 d = &current_funccal->l_vars;
3744 else
3745 {
3746 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3747 d = di->di_tv.vval.v_dict;
3748 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 hi = hash_find(ht, varname);
3750 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003751 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003752 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003753 if (var_check_fixed(di->di_flags, name, FALSE)
3754 || var_check_ro(di->di_flags, name, FALSE)
3755 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756 return FAIL;
3757 delete_var(ht, hi);
3758 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003761 if (forceit)
3762 return OK;
3763 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 return FAIL;
3765}
3766
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003767/*
3768 * Lock or unlock variable indicated by "lp".
3769 * "deep" is the levels to go (-1 for unlimited);
3770 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3771 */
3772 static int
3773do_lock_var(lp, name_end, deep, lock)
3774 lval_T *lp;
3775 char_u *name_end;
3776 int deep;
3777 int lock;
3778{
3779 int ret = OK;
3780 int cc;
3781 dictitem_T *di;
3782
3783 if (deep == 0) /* nothing to do */
3784 return OK;
3785
3786 if (lp->ll_tv == NULL)
3787 {
3788 cc = *name_end;
3789 *name_end = NUL;
3790
3791 /* Normal name or expanded name. */
3792 if (check_changedtick(lp->ll_name))
3793 ret = FAIL;
3794 else
3795 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003796 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797 if (di == NULL)
3798 ret = FAIL;
3799 else
3800 {
3801 if (lock)
3802 di->di_flags |= DI_FLAGS_LOCK;
3803 else
3804 di->di_flags &= ~DI_FLAGS_LOCK;
3805 item_lock(&di->di_tv, deep, lock);
3806 }
3807 }
3808 *name_end = cc;
3809 }
3810 else if (lp->ll_range)
3811 {
3812 listitem_T *li = lp->ll_li;
3813
3814 /* (un)lock a range of List items. */
3815 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3816 {
3817 item_lock(&li->li_tv, deep, lock);
3818 li = li->li_next;
3819 ++lp->ll_n1;
3820 }
3821 }
3822 else if (lp->ll_list != NULL)
3823 /* (un)lock a List item. */
3824 item_lock(&lp->ll_li->li_tv, deep, lock);
3825 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003826 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003827 item_lock(&lp->ll_di->di_tv, deep, lock);
3828
3829 return ret;
3830}
3831
3832/*
3833 * Lock or unlock an item. "deep" is nr of levels to go.
3834 */
3835 static void
3836item_lock(tv, deep, lock)
3837 typval_T *tv;
3838 int deep;
3839 int lock;
3840{
3841 static int recurse = 0;
3842 list_T *l;
3843 listitem_T *li;
3844 dict_T *d;
3845 hashitem_T *hi;
3846 int todo;
3847
3848 if (recurse >= DICT_MAXNEST)
3849 {
3850 EMSG(_("E743: variable nested too deep for (un)lock"));
3851 return;
3852 }
3853 if (deep == 0)
3854 return;
3855 ++recurse;
3856
3857 /* lock/unlock the item itself */
3858 if (lock)
3859 tv->v_lock |= VAR_LOCKED;
3860 else
3861 tv->v_lock &= ~VAR_LOCKED;
3862
3863 switch (tv->v_type)
3864 {
3865 case VAR_LIST:
3866 if ((l = tv->vval.v_list) != NULL)
3867 {
3868 if (lock)
3869 l->lv_lock |= VAR_LOCKED;
3870 else
3871 l->lv_lock &= ~VAR_LOCKED;
3872 if (deep < 0 || deep > 1)
3873 /* recursive: lock/unlock the items the List contains */
3874 for (li = l->lv_first; li != NULL; li = li->li_next)
3875 item_lock(&li->li_tv, deep - 1, lock);
3876 }
3877 break;
3878 case VAR_DICT:
3879 if ((d = tv->vval.v_dict) != NULL)
3880 {
3881 if (lock)
3882 d->dv_lock |= VAR_LOCKED;
3883 else
3884 d->dv_lock &= ~VAR_LOCKED;
3885 if (deep < 0 || deep > 1)
3886 {
3887 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003888 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003889 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3890 {
3891 if (!HASHITEM_EMPTY(hi))
3892 {
3893 --todo;
3894 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3895 }
3896 }
3897 }
3898 }
3899 }
3900 --recurse;
3901}
3902
Bram Moolenaara40058a2005-07-11 22:42:07 +00003903/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003904 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3905 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003906 */
3907 static int
3908tv_islocked(tv)
3909 typval_T *tv;
3910{
3911 return (tv->v_lock & VAR_LOCKED)
3912 || (tv->v_type == VAR_LIST
3913 && tv->vval.v_list != NULL
3914 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3915 || (tv->v_type == VAR_DICT
3916 && tv->vval.v_dict != NULL
3917 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3918}
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3921/*
3922 * Delete all "menutrans_" variables.
3923 */
3924 void
3925del_menutrans_vars()
3926{
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003931 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 {
3934 if (!HASHITEM_EMPTY(hi))
3935 {
3936 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3938 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 }
3940 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942}
3943#endif
3944
3945#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3946
3947/*
3948 * Local string buffer for the next two functions to store a variable name
3949 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3950 * get_user_var_name().
3951 */
3952
3953static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3954
3955static char_u *varnamebuf = NULL;
3956static int varnamebuflen = 0;
3957
3958/*
3959 * Function to concatenate a prefix and a variable name.
3960 */
3961 static char_u *
3962cat_prefix_varname(prefix, name)
3963 int prefix;
3964 char_u *name;
3965{
3966 int len;
3967
3968 len = (int)STRLEN(name) + 3;
3969 if (len > varnamebuflen)
3970 {
3971 vim_free(varnamebuf);
3972 len += 10; /* some additional space */
3973 varnamebuf = alloc(len);
3974 if (varnamebuf == NULL)
3975 {
3976 varnamebuflen = 0;
3977 return NULL;
3978 }
3979 varnamebuflen = len;
3980 }
3981 *varnamebuf = prefix;
3982 varnamebuf[1] = ':';
3983 STRCPY(varnamebuf + 2, name);
3984 return varnamebuf;
3985}
3986
3987/*
3988 * Function given to ExpandGeneric() to obtain the list of user defined
3989 * (global/buffer/window/built-in) variable names.
3990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 char_u *
3992get_user_var_name(xp, idx)
3993 expand_T *xp;
3994 int idx;
3995{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003996 static long_u gdone;
3997 static long_u bdone;
3998 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003999#ifdef FEAT_WINDOWS
4000 static long_u tdone;
4001#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004002 static int vidx;
4003 static hashitem_T *hi;
4004 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
4006 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004008 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009#ifdef FEAT_WINDOWS
4010 tdone = 0;
4011#endif
4012 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004013
4014 /* Global variables */
4015 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004017 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004019 else
4020 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 while (HASHITEM_EMPTY(hi))
4022 ++hi;
4023 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4024 return cat_prefix_varname('g', hi->hi_key);
4025 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004027
4028 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004029 ht = &curbuf->b_vars->dv_hashtab;
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 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004034 else
4035 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 while (HASHITEM_EMPTY(hi))
4037 ++hi;
4038 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return (char_u *)"b:changedtick";
4044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045
4046 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004047 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004050 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004052 else
4053 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 while (HASHITEM_EMPTY(hi))
4055 ++hi;
4056 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004058
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059#ifdef FEAT_WINDOWS
4060 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004061 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004062 if (tdone < ht->ht_used)
4063 {
4064 if (tdone++ == 0)
4065 hi = ht->ht_array;
4066 else
4067 ++hi;
4068 while (HASHITEM_EMPTY(hi))
4069 ++hi;
4070 return cat_prefix_varname('t', hi->hi_key);
4071 }
4072#endif
4073
Bram Moolenaar33570922005-01-25 22:26:29 +00004074 /* v: variables */
4075 if (vidx < VV_LEN)
4076 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 vim_free(varnamebuf);
4079 varnamebuf = NULL;
4080 varnamebuflen = 0;
4081 return NULL;
4082}
4083
4084#endif /* FEAT_CMDL_COMPL */
4085
4086/*
4087 * types for expressions.
4088 */
4089typedef enum
4090{
4091 TYPE_UNKNOWN = 0
4092 , TYPE_EQUAL /* == */
4093 , TYPE_NEQUAL /* != */
4094 , TYPE_GREATER /* > */
4095 , TYPE_GEQUAL /* >= */
4096 , TYPE_SMALLER /* < */
4097 , TYPE_SEQUAL /* <= */
4098 , TYPE_MATCH /* =~ */
4099 , TYPE_NOMATCH /* !~ */
4100} exptype_T;
4101
4102/*
4103 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4106 */
4107
4108/*
4109 * Handle zero level expression.
4110 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004112 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * Return OK or FAIL.
4114 */
4115 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 char_u **nextcmd;
4120 int evaluate;
4121{
4122 int ret;
4123 char_u *p;
4124
4125 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 if (ret == FAIL || !ends_excmd(*p))
4128 {
4129 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /*
4132 * Report the invalid expression unless the expression evaluation has
4133 * been cancelled due to an aborting error, an interrupt, or an
4134 * exception.
4135 */
4136 if (!aborting())
4137 EMSG2(_(e_invexpr2), arg);
4138 ret = FAIL;
4139 }
4140 if (nextcmd != NULL)
4141 *nextcmd = check_nextcmd(p);
4142
4143 return ret;
4144}
4145
4146/*
4147 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004148 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 *
4150 * "arg" must point to the first non-white of the expression.
4151 * "arg" is advanced to the next non-white after the recognized expression.
4152 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004153 * Note: "rettv.v_lock" is not set.
4154 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Return OK or FAIL.
4156 */
4157 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int evaluate;
4162{
4163 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /*
4167 * Get the first variable.
4168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return FAIL;
4171
4172 if ((*arg)[0] == '?')
4173 {
4174 result = FALSE;
4175 if (evaluate)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 int error = FALSE;
4178
4179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 /*
4194 * Check for the ":".
4195 */
4196 if ((*arg)[0] != ':')
4197 {
4198 EMSG(_("E109: Missing ':' after '?'"));
4199 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202 }
4203
4204 /*
4205 * Get the third variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
4208 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4209 {
4210 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle first level expression:
4223 * expr2 || expr2 || expr2 logical OR
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int evaluate;
4235{
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 long result;
4238 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 /*
4242 * Get the first variable.
4243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return FAIL;
4246
4247 /*
4248 * Repeat until there is no following "||".
4249 */
4250 first = TRUE;
4251 result = FALSE;
4252 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4253 {
4254 if (evaluate && first)
4255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (error)
4260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 first = FALSE;
4262 }
4263
4264 /*
4265 * Get the second variable.
4266 */
4267 *arg = skipwhite(*arg + 2);
4268 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4269 return FAIL;
4270
4271 /*
4272 * Compute the result.
4273 */
4274 if (evaluate && !result)
4275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 if (evaluate)
4283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 rettv->v_type = VAR_NUMBER;
4285 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 }
4288
4289 return OK;
4290}
4291
4292/*
4293 * Handle second level expression:
4294 * expr3 && expr3 && expr3 logical AND
4295 *
4296 * "arg" must point to the first non-white of the expression.
4297 * "arg" is advanced to the next non-white after the recognized expression.
4298 *
4299 * Return OK or FAIL.
4300 */
4301 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 int evaluate;
4306{
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 long result;
4309 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 /*
4313 * Get the first variable.
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return FAIL;
4317
4318 /*
4319 * Repeat until there is no following "&&".
4320 */
4321 first = TRUE;
4322 result = TRUE;
4323 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4324 {
4325 if (evaluate && first)
4326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 if (error)
4331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 first = FALSE;
4333 }
4334
4335 /*
4336 * Get the second variable.
4337 */
4338 *arg = skipwhite(*arg + 2);
4339 if (eval4(arg, &var2, evaluate && result) == FAIL)
4340 return FAIL;
4341
4342 /*
4343 * Compute the result.
4344 */
4345 if (evaluate && result)
4346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (error)
4351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 if (evaluate)
4354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 rettv->v_type = VAR_NUMBER;
4356 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358 }
4359
4360 return OK;
4361}
4362
4363/*
4364 * Handle third level expression:
4365 * var1 == var2
4366 * var1 =~ var2
4367 * var1 != var2
4368 * var1 !~ var2
4369 * var1 > var2
4370 * var1 >= var2
4371 * var1 < var2
4372 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 * var1 is var2
4374 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 *
4376 * "arg" must point to the first non-white of the expression.
4377 * "arg" is advanced to the next non-white after the recognized expression.
4378 *
4379 * Return OK or FAIL.
4380 */
4381 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int evaluate;
4386{
Bram Moolenaar33570922005-01-25 22:26:29 +00004387 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 char_u *p;
4389 int i;
4390 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004391 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 int len = 2;
4393 long n1, n2;
4394 char_u *s1, *s2;
4395 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4396 regmatch_T regmatch;
4397 int ic;
4398 char_u *save_cpo;
4399
4400 /*
4401 * Get the first variable.
4402 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return FAIL;
4405
4406 p = *arg;
4407 switch (p[0])
4408 {
4409 case '=': if (p[1] == '=')
4410 type = TYPE_EQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_MATCH;
4413 break;
4414 case '!': if (p[1] == '=')
4415 type = TYPE_NEQUAL;
4416 else if (p[1] == '~')
4417 type = TYPE_NOMATCH;
4418 break;
4419 case '>': if (p[1] != '=')
4420 {
4421 type = TYPE_GREATER;
4422 len = 1;
4423 }
4424 else
4425 type = TYPE_GEQUAL;
4426 break;
4427 case '<': if (p[1] != '=')
4428 {
4429 type = TYPE_SMALLER;
4430 len = 1;
4431 }
4432 else
4433 type = TYPE_SEQUAL;
4434 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 case 'i': if (p[1] == 's')
4436 {
4437 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4438 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004439 i = p[len];
4440 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 {
4442 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4443 type_is = TRUE;
4444 }
4445 }
4446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448
4449 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004450 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 */
4452 if (type != TYPE_UNKNOWN)
4453 {
4454 /* extra question mark appended: ignore case */
4455 if (p[len] == '?')
4456 {
4457 ic = TRUE;
4458 ++len;
4459 }
4460 /* extra '#' appended: match case */
4461 else if (p[len] == '#')
4462 {
4463 ic = FALSE;
4464 ++len;
4465 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004466 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 else
4468 ic = p_ic;
4469
4470 /*
4471 * Get the second variable.
4472 */
4473 *arg = skipwhite(p + len);
4474 if (eval5(arg, &var2, evaluate) == FAIL)
4475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 return FAIL;
4478 }
4479
4480 if (evaluate)
4481 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004482 if (type_is && rettv->v_type != var2.v_type)
4483 {
4484 /* For "is" a different type always means FALSE, for "notis"
4485 * it means TRUE. */
4486 n1 = (type == TYPE_NEQUAL);
4487 }
4488 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4489 {
4490 if (type_is)
4491 {
4492 n1 = (rettv->v_type == var2.v_type
4493 && rettv->vval.v_list == var2.vval.v_list);
4494 if (type == TYPE_NEQUAL)
4495 n1 = !n1;
4496 }
4497 else if (rettv->v_type != var2.v_type
4498 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4499 {
4500 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004501 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004503 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004504 clear_tv(rettv);
4505 clear_tv(&var2);
4506 return FAIL;
4507 }
4508 else
4509 {
4510 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004511 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4512 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 if (type == TYPE_NEQUAL)
4514 n1 = !n1;
4515 }
4516 }
4517
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004518 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4519 {
4520 if (type_is)
4521 {
4522 n1 = (rettv->v_type == var2.v_type
4523 && rettv->vval.v_dict == var2.vval.v_dict);
4524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 else if (rettv->v_type != var2.v_type
4528 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4529 {
4530 if (rettv->v_type != var2.v_type)
4531 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4532 else
4533 EMSG(_("E736: Invalid operation for Dictionary"));
4534 clear_tv(rettv);
4535 clear_tv(&var2);
4536 return FAIL;
4537 }
4538 else
4539 {
4540 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004541 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4542 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004543 if (type == TYPE_NEQUAL)
4544 n1 = !n1;
4545 }
4546 }
4547
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4549 {
4550 if (rettv->v_type != var2.v_type
4551 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4552 {
4553 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004554 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004556 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 clear_tv(rettv);
4558 clear_tv(&var2);
4559 return FAIL;
4560 }
4561 else
4562 {
4563 /* Compare two Funcrefs for being equal or unequal. */
4564 if (rettv->vval.v_string == NULL
4565 || var2.vval.v_string == NULL)
4566 n1 = FALSE;
4567 else
4568 n1 = STRCMP(rettv->vval.v_string,
4569 var2.vval.v_string) == 0;
4570 if (type == TYPE_NEQUAL)
4571 n1 = !n1;
4572 }
4573 }
4574
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004575#ifdef FEAT_FLOAT
4576 /*
4577 * If one of the two variables is a float, compare as a float.
4578 * When using "=~" or "!~", always compare as string.
4579 */
4580 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4581 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4582 {
4583 float_T f1, f2;
4584
4585 if (rettv->v_type == VAR_FLOAT)
4586 f1 = rettv->vval.v_float;
4587 else
4588 f1 = get_tv_number(rettv);
4589 if (var2.v_type == VAR_FLOAT)
4590 f2 = var2.vval.v_float;
4591 else
4592 f2 = get_tv_number(&var2);
4593 n1 = FALSE;
4594 switch (type)
4595 {
4596 case TYPE_EQUAL: n1 = (f1 == f2); break;
4597 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4598 case TYPE_GREATER: n1 = (f1 > f2); break;
4599 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4600 case TYPE_SMALLER: n1 = (f1 < f2); break;
4601 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4602 case TYPE_UNKNOWN:
4603 case TYPE_MATCH:
4604 case TYPE_NOMATCH: break; /* avoid gcc warning */
4605 }
4606 }
4607#endif
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 /*
4610 * If one of the two variables is a number, compare as a number.
4611 * When using "=~" or "!~", always compare as string.
4612 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004613 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 n1 = get_tv_number(rettv);
4617 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 switch (type)
4619 {
4620 case TYPE_EQUAL: n1 = (n1 == n2); break;
4621 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4622 case TYPE_GREATER: n1 = (n1 > n2); break;
4623 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4624 case TYPE_SMALLER: n1 = (n1 < n2); break;
4625 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4626 case TYPE_UNKNOWN:
4627 case TYPE_MATCH:
4628 case TYPE_NOMATCH: break; /* avoid gcc warning */
4629 }
4630 }
4631 else
4632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 s1 = get_tv_string_buf(rettv, buf1);
4634 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4636 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4637 else
4638 i = 0;
4639 n1 = FALSE;
4640 switch (type)
4641 {
4642 case TYPE_EQUAL: n1 = (i == 0); break;
4643 case TYPE_NEQUAL: n1 = (i != 0); break;
4644 case TYPE_GREATER: n1 = (i > 0); break;
4645 case TYPE_GEQUAL: n1 = (i >= 0); break;
4646 case TYPE_SMALLER: n1 = (i < 0); break;
4647 case TYPE_SEQUAL: n1 = (i <= 0); break;
4648
4649 case TYPE_MATCH:
4650 case TYPE_NOMATCH:
4651 /* avoid 'l' flag in 'cpoptions' */
4652 save_cpo = p_cpo;
4653 p_cpo = (char_u *)"";
4654 regmatch.regprog = vim_regcomp(s2,
4655 RE_MAGIC + RE_STRING);
4656 regmatch.rm_ic = ic;
4657 if (regmatch.regprog != NULL)
4658 {
4659 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004660 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (type == TYPE_NOMATCH)
4662 n1 = !n1;
4663 }
4664 p_cpo = save_cpo;
4665 break;
4666
4667 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4668 }
4669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004670 clear_tv(rettv);
4671 clear_tv(&var2);
4672 rettv->v_type = VAR_NUMBER;
4673 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 }
4676
4677 return OK;
4678}
4679
4680/*
4681 * Handle fourth level expression:
4682 * + number addition
4683 * - number subtraction
4684 * . string concatenation
4685 *
4686 * "arg" must point to the first non-white of the expression.
4687 * "arg" is advanced to the next non-white after the recognized expression.
4688 *
4689 * Return OK or FAIL.
4690 */
4691 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 int evaluate;
4696{
Bram Moolenaar33570922005-01-25 22:26:29 +00004697 typval_T var2;
4698 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 int op;
4700 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701#ifdef FEAT_FLOAT
4702 float_T f1 = 0, f2 = 0;
4703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 char_u *s1, *s2;
4705 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4706 char_u *p;
4707
4708 /*
4709 * Get the first variable.
4710 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004711 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 return FAIL;
4713
4714 /*
4715 * Repeat computing, until no '+', '-' or '.' is following.
4716 */
4717 for (;;)
4718 {
4719 op = **arg;
4720 if (op != '+' && op != '-' && op != '.')
4721 break;
4722
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004723 if ((op != '+' || rettv->v_type != VAR_LIST)
4724#ifdef FEAT_FLOAT
4725 && (op == '.' || rettv->v_type != VAR_FLOAT)
4726#endif
4727 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004728 {
4729 /* For "list + ...", an illegal use of the first operand as
4730 * a number cannot be determined before evaluating the 2nd
4731 * operand: if this is also a list, all is ok.
4732 * For "something . ...", "something - ..." or "non-list + ...",
4733 * we know that the first operand needs to be a string or number
4734 * without evaluating the 2nd operand. So check before to avoid
4735 * side effects after an error. */
4736 if (evaluate && get_tv_string_chk(rettv) == NULL)
4737 {
4738 clear_tv(rettv);
4739 return FAIL;
4740 }
4741 }
4742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 /*
4744 * Get the second variable.
4745 */
4746 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004747 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 return FAIL;
4751 }
4752
4753 if (evaluate)
4754 {
4755 /*
4756 * Compute the result.
4757 */
4758 if (op == '.')
4759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004760 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4761 s2 = get_tv_string_buf_chk(&var2, buf2);
4762 if (s2 == NULL) /* type error ? */
4763 {
4764 clear_tv(rettv);
4765 clear_tv(&var2);
4766 return FAIL;
4767 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004768 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(rettv);
4770 rettv->v_type = VAR_STRING;
4771 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004773 else if (op == '+' && rettv->v_type == VAR_LIST
4774 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004775 {
4776 /* concatenate Lists */
4777 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4778 &var3) == FAIL)
4779 {
4780 clear_tv(rettv);
4781 clear_tv(&var2);
4782 return FAIL;
4783 }
4784 clear_tv(rettv);
4785 *rettv = var3;
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 else
4788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 int error = FALSE;
4790
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791#ifdef FEAT_FLOAT
4792 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 f1 = rettv->vval.v_float;
4795 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004796 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797 else
4798#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004799 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800 n1 = get_tv_number_chk(rettv, &error);
4801 if (error)
4802 {
4803 /* This can only happen for "list + non-list". For
4804 * "non-list + ..." or "something - ...", we returned
4805 * before evaluating the 2nd operand. */
4806 clear_tv(rettv);
4807 return FAIL;
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 f1 = n1;
4812#endif
4813 }
4814#ifdef FEAT_FLOAT
4815 if (var2.v_type == VAR_FLOAT)
4816 {
4817 f2 = var2.vval.v_float;
4818 n2 = 0;
4819 }
4820 else
4821#endif
4822 {
4823 n2 = get_tv_number_chk(&var2, &error);
4824 if (error)
4825 {
4826 clear_tv(rettv);
4827 clear_tv(&var2);
4828 return FAIL;
4829 }
4830#ifdef FEAT_FLOAT
4831 if (rettv->v_type == VAR_FLOAT)
4832 f2 = n2;
4833#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836
4837#ifdef FEAT_FLOAT
4838 /* If there is a float on either side the result is a float. */
4839 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4840 {
4841 if (op == '+')
4842 f1 = f1 + f2;
4843 else
4844 f1 = f1 - f2;
4845 rettv->v_type = VAR_FLOAT;
4846 rettv->vval.v_float = f1;
4847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004849#endif
4850 {
4851 if (op == '+')
4852 n1 = n1 + n2;
4853 else
4854 n1 = n1 - n2;
4855 rettv->v_type = VAR_NUMBER;
4856 rettv->vval.v_number = n1;
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861 }
4862 return OK;
4863}
4864
4865/*
4866 * Handle fifth level expression:
4867 * * number multiplication
4868 * / number division
4869 * % number modulo
4870 *
4871 * "arg" must point to the first non-white of the expression.
4872 * "arg" is advanced to the next non-white after the recognized expression.
4873 *
4874 * Return OK or FAIL.
4875 */
4876 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004877eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004881 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
Bram Moolenaar33570922005-01-25 22:26:29 +00004883 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 int op;
4885 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004886#ifdef FEAT_FLOAT
4887 int use_float = FALSE;
4888 float_T f1 = 0, f2;
4889#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004890 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891
4892 /*
4893 * Get the first variable.
4894 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004895 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 return FAIL;
4897
4898 /*
4899 * Repeat computing, until no '*', '/' or '%' is following.
4900 */
4901 for (;;)
4902 {
4903 op = **arg;
4904 if (op != '*' && op != '/' && op != '%')
4905 break;
4906
4907 if (evaluate)
4908 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909#ifdef FEAT_FLOAT
4910 if (rettv->v_type == VAR_FLOAT)
4911 {
4912 f1 = rettv->vval.v_float;
4913 use_float = TRUE;
4914 n1 = 0;
4915 }
4916 else
4917#endif
4918 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004919 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004920 if (error)
4921 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 else
4924 n1 = 0;
4925
4926 /*
4927 * Get the second variable.
4928 */
4929 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004930 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 return FAIL;
4932
4933 if (evaluate)
4934 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004935#ifdef FEAT_FLOAT
4936 if (var2.v_type == VAR_FLOAT)
4937 {
4938 if (!use_float)
4939 {
4940 f1 = n1;
4941 use_float = TRUE;
4942 }
4943 f2 = var2.vval.v_float;
4944 n2 = 0;
4945 }
4946 else
4947#endif
4948 {
4949 n2 = get_tv_number_chk(&var2, &error);
4950 clear_tv(&var2);
4951 if (error)
4952 return FAIL;
4953#ifdef FEAT_FLOAT
4954 if (use_float)
4955 f2 = n2;
4956#endif
4957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958
4959 /*
4960 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004963#ifdef FEAT_FLOAT
4964 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 if (op == '*')
4967 f1 = f1 * f2;
4968 else if (op == '/')
4969 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970# ifdef VMS
4971 /* VMS crashes on divide by zero, work around it */
4972 if (f2 == 0.0)
4973 {
4974 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004975 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004976 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004977 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004978 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004979 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004980 }
4981 else
4982 f1 = f1 / f2;
4983# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 /* We rely on the floating point library to handle divide
4985 * by zero to result in "inf" and not a crash. */
4986 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004991 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 return FAIL;
4993 }
4994 rettv->v_type = VAR_FLOAT;
4995 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005000 if (op == '*')
5001 n1 = n1 * n2;
5002 else if (op == '/')
5003 {
5004 if (n2 == 0) /* give an error message? */
5005 {
5006 if (n1 == 0)
5007 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5008 else if (n1 < 0)
5009 n1 = -0x7fffffffL;
5010 else
5011 n1 = 0x7fffffffL;
5012 }
5013 else
5014 n1 = n1 / n2;
5015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 {
5018 if (n2 == 0) /* give an error message? */
5019 n1 = 0;
5020 else
5021 n1 = n1 % n2;
5022 }
5023 rettv->v_type = VAR_NUMBER;
5024 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 }
5028
5029 return OK;
5030}
5031
5032/*
5033 * Handle sixth level expression:
5034 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005035 * "string" string constant
5036 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 * &option-name option value
5038 * @r register contents
5039 * identifier variable value
5040 * function() function call
5041 * $VAR environment variable
5042 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005043 * [expr, expr] List
5044 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 *
5046 * Also handle:
5047 * ! in front logical NOT
5048 * - in front unary minus
5049 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 * trailing [] subscript in String or List
5051 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 *
5053 * "arg" must point to the first non-white of the expression.
5054 * "arg" is advanced to the next non-white after the recognized expression.
5055 *
5056 * Return OK or FAIL.
5057 */
5058 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005059eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005063 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 long n;
5066 int len;
5067 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 char_u *start_leader, *end_leader;
5069 int ret = OK;
5070 char_u *alias;
5071
5072 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
5078 /*
5079 * Skip '!' and '-' characters. They are handled later.
5080 */
5081 start_leader = *arg;
5082 while (**arg == '!' || **arg == '-' || **arg == '+')
5083 *arg = skipwhite(*arg + 1);
5084 end_leader = *arg;
5085
5086 switch (**arg)
5087 {
5088 /*
5089 * Number constant.
5090 */
5091 case '0':
5092 case '1':
5093 case '2':
5094 case '3':
5095 case '4':
5096 case '5':
5097 case '6':
5098 case '7':
5099 case '8':
5100 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 {
5102#ifdef FEAT_FLOAT
5103 char_u *p = skipdigits(*arg + 1);
5104 int get_float = FALSE;
5105
5106 /* We accept a float when the format matches
5107 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005108 * strict to avoid backwards compatibility problems.
5109 * Don't look for a float after the "." operator, so that
5110 * ":let vers = 1.2.3" doesn't fail. */
5111 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005113 get_float = TRUE;
5114 p = skipdigits(p + 2);
5115 if (*p == 'e' || *p == 'E')
5116 {
5117 ++p;
5118 if (*p == '-' || *p == '+')
5119 ++p;
5120 if (!vim_isdigit(*p))
5121 get_float = FALSE;
5122 else
5123 p = skipdigits(p + 1);
5124 }
5125 if (ASCII_ISALPHA(*p) || *p == '.')
5126 get_float = FALSE;
5127 }
5128 if (get_float)
5129 {
5130 float_T f;
5131
5132 *arg += string2float(*arg, &f);
5133 if (evaluate)
5134 {
5135 rettv->v_type = VAR_FLOAT;
5136 rettv->vval.v_float = f;
5137 }
5138 }
5139 else
5140#endif
5141 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005142 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 *arg += len;
5144 if (evaluate)
5145 {
5146 rettv->v_type = VAR_NUMBER;
5147 rettv->vval.v_number = n;
5148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
5153 /*
5154 * String constant: "string".
5155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158
5159 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163 break;
5164
5165 /*
5166 * List: [expr, expr]
5167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 break;
5170
5171 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 * Dictionary: {key: val, key: val}
5173 */
5174 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5175 break;
5176
5177 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005178 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005180 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /*
5184 * Environment variable: $VAR.
5185 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 break;
5188
5189 /*
5190 * Register contents: @r.
5191 */
5192 case '@': ++*arg;
5193 if (evaluate)
5194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005196 rettv->vval.v_string = get_reg_contents(**arg,
5197 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199 if (**arg != NUL)
5200 ++*arg;
5201 break;
5202
5203 /*
5204 * nested expression: (expression).
5205 */
5206 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (**arg == ')')
5209 ++*arg;
5210 else if (ret == OK)
5211 {
5212 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 ret = FAIL;
5215 }
5216 break;
5217
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 break;
5220 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221
5222 if (ret == NOTDONE)
5223 {
5224 /*
5225 * Must be a variable or function name.
5226 * Can also be a curly-braces kind of name: {expr}.
5227 */
5228 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 if (alias != NULL)
5231 s = alias;
5232
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 ret = FAIL;
5235 else
5236 {
5237 if (**arg == '(') /* recursive! */
5238 {
5239 /* If "s" is the name of a variable of type VAR_FUNC
5240 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005241 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242
5243 /* Invoke the function. */
5244 ret = get_func_tv(s, len, rettv, arg,
5245 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005246 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005247
5248 /* If evaluate is FALSE rettv->v_type was not set in
5249 * get_func_tv, but it's needed in handle_subscript() to parse
5250 * what follows. So set it here. */
5251 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5252 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005253 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005254 rettv->v_type = VAR_FUNC;
5255 }
5256
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 /* Stop the expression evaluation when immediately
5258 * aborting on error, or when an interrupt occurred or
5259 * an exception was thrown but not caught. */
5260 if (aborting())
5261 {
5262 if (ret == OK)
5263 clear_tv(rettv);
5264 ret = FAIL;
5265 }
5266 }
5267 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005268 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005269 else
5270 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005272 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 }
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 *arg = skipwhite(*arg);
5276
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5278 * expr(expr). */
5279 if (ret == OK)
5280 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282 /*
5283 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5284 */
5285 if (ret == OK && evaluate && end_leader > start_leader)
5286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005288 int val = 0;
5289#ifdef FEAT_FLOAT
5290 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005292 if (rettv->v_type == VAR_FLOAT)
5293 f = rettv->vval.v_float;
5294 else
5295#endif
5296 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 clear_tv(rettv);
5300 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 else
5303 {
5304 while (end_leader > start_leader)
5305 {
5306 --end_leader;
5307 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005308 {
5309#ifdef FEAT_FLOAT
5310 if (rettv->v_type == VAR_FLOAT)
5311 f = !f;
5312 else
5313#endif
5314 val = !val;
5315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005317 {
5318#ifdef FEAT_FLOAT
5319 if (rettv->v_type == VAR_FLOAT)
5320 f = -f;
5321 else
5322#endif
5323 val = -val;
5324 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005326#ifdef FEAT_FLOAT
5327 if (rettv->v_type == VAR_FLOAT)
5328 {
5329 clear_tv(rettv);
5330 rettv->vval.v_float = f;
5331 }
5332 else
5333#endif
5334 {
5335 clear_tv(rettv);
5336 rettv->v_type = VAR_NUMBER;
5337 rettv->vval.v_number = val;
5338 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341
5342 return ret;
5343}
5344
5345/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005346 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5347 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5349 */
5350 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005351eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356{
5357 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 long len = -1;
5361 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005365 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005367 if (verbose)
5368 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 return FAIL;
5370 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005371#ifdef FEAT_FLOAT
5372 else if (rettv->v_type == VAR_FLOAT)
5373 {
5374 if (verbose)
5375 EMSG(_(e_float_as_string));
5376 return FAIL;
5377 }
5378#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005380 init_tv(&var1);
5381 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384 /*
5385 * dict.name
5386 */
5387 key = *arg + 1;
5388 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5389 ;
5390 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 }
5394 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 /*
5397 * something[idx]
5398 *
5399 * Get the (first) variable from inside the [].
5400 */
5401 *arg = skipwhite(*arg + 1);
5402 if (**arg == ':')
5403 empty1 = TRUE;
5404 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5405 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005406 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5407 {
5408 /* not a number or string */
5409 clear_tv(&var1);
5410 return FAIL;
5411 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412
5413 /*
5414 * Get the second variable from inside the [:].
5415 */
5416 if (**arg == ':')
5417 {
5418 range = TRUE;
5419 *arg = skipwhite(*arg + 1);
5420 if (**arg == ']')
5421 empty2 = TRUE;
5422 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005424 if (!empty1)
5425 clear_tv(&var1);
5426 return FAIL;
5427 }
5428 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5429 {
5430 /* not a number or string */
5431 if (!empty1)
5432 clear_tv(&var1);
5433 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 return FAIL;
5435 }
5436 }
5437
5438 /* Check for the ']'. */
5439 if (**arg != ']')
5440 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005441 if (verbose)
5442 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 clear_tv(&var1);
5444 if (range)
5445 clear_tv(&var2);
5446 return FAIL;
5447 }
5448 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 }
5450
5451 if (evaluate)
5452 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 n1 = 0;
5454 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 n1 = get_tv_number(&var1);
5457 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 if (range)
5460 {
5461 if (empty2)
5462 n2 = -1;
5463 else
5464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 n2 = get_tv_number(&var2);
5466 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468 }
5469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 {
5472 case VAR_NUMBER:
5473 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 len = (long)STRLEN(s);
5476 if (range)
5477 {
5478 /* The resulting variable is a substring. If the indexes
5479 * are out of range the result is empty. */
5480 if (n1 < 0)
5481 {
5482 n1 = len + n1;
5483 if (n1 < 0)
5484 n1 = 0;
5485 }
5486 if (n2 < 0)
5487 n2 = len + n2;
5488 else if (n2 >= len)
5489 n2 = len;
5490 if (n1 >= len || n2 < 0 || n1 > n2)
5491 s = NULL;
5492 else
5493 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5494 }
5495 else
5496 {
5497 /* The resulting variable is a string of a single
5498 * character. If the index is too big or negative the
5499 * result is empty. */
5500 if (n1 >= len || n1 < 0)
5501 s = NULL;
5502 else
5503 s = vim_strnsave(s + n1, 1);
5504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 clear_tv(rettv);
5506 rettv->v_type = VAR_STRING;
5507 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 break;
5509
5510 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005511 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 if (n1 < 0)
5513 n1 = len + n1;
5514 if (!empty1 && (n1 < 0 || n1 >= len))
5515 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005516 /* For a range we allow invalid values and return an empty
5517 * list. A list index out of range is an error. */
5518 if (!range)
5519 {
5520 if (verbose)
5521 EMSGN(_(e_listidx), n1);
5522 return FAIL;
5523 }
5524 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 }
5526 if (range)
5527 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 list_T *l;
5529 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530
5531 if (n2 < 0)
5532 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005533 else if (n2 >= len)
5534 n2 = len - 1;
5535 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005536 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 l = list_alloc();
5538 if (l == NULL)
5539 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 n1 <= n2; ++n1)
5542 {
5543 if (list_append_tv(l, &item->li_tv) == FAIL)
5544 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005545 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 return FAIL;
5547 }
5548 item = item->li_next;
5549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 clear_tv(rettv);
5551 rettv->v_type = VAR_LIST;
5552 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005553 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 }
5555 else
5556 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005557 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 clear_tv(rettv);
5559 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 }
5561 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562
5563 case VAR_DICT:
5564 if (range)
5565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005566 if (verbose)
5567 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005568 if (len == -1)
5569 clear_tv(&var1);
5570 return FAIL;
5571 }
5572 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005573 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574
5575 if (len == -1)
5576 {
5577 key = get_tv_string(&var1);
5578 if (*key == NUL)
5579 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005580 if (verbose)
5581 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582 clear_tv(&var1);
5583 return FAIL;
5584 }
5585 }
5586
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005587 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005589 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005590 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 if (len == -1)
5592 clear_tv(&var1);
5593 if (item == NULL)
5594 return FAIL;
5595
5596 copy_tv(&item->di_tv, &var1);
5597 clear_tv(rettv);
5598 *rettv = var1;
5599 }
5600 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
5602 }
5603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 return OK;
5605}
5606
5607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * Get an option value.
5609 * "arg" points to the '&' or '+' before the option name.
5610 * "arg" is advanced to character after the option name.
5611 * Return OK or FAIL.
5612 */
5613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005616 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 int evaluate;
5618{
5619 char_u *option_end;
5620 long numval;
5621 char_u *stringval;
5622 int opt_type;
5623 int c;
5624 int working = (**arg == '+'); /* has("+option") */
5625 int ret = OK;
5626 int opt_flags;
5627
5628 /*
5629 * Isolate the option name and find its value.
5630 */
5631 option_end = find_option_end(arg, &opt_flags);
5632 if (option_end == NULL)
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 EMSG2(_("E112: Option name missing: %s"), *arg);
5636 return FAIL;
5637 }
5638
5639 if (!evaluate)
5640 {
5641 *arg = option_end;
5642 return OK;
5643 }
5644
5645 c = *option_end;
5646 *option_end = NUL;
5647 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 if (opt_type == -3) /* invalid name */
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 EMSG2(_("E113: Unknown option: %s"), *arg);
5654 ret = FAIL;
5655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
5658 if (opt_type == -2) /* hidden string option */
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->v_type = VAR_STRING;
5661 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else if (opt_type == -1) /* hidden number option */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv->v_type = VAR_NUMBER;
5666 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668 else if (opt_type == 1) /* number option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_NUMBER;
5671 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 else /* string option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_STRING;
5676 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 }
5679 else if (working && (opt_type == -2 || opt_type == -1))
5680 ret = FAIL;
5681
5682 *option_end = c; /* put back for error messages */
5683 *arg = option_end;
5684
5685 return ret;
5686}
5687
5688/*
5689 * Allocate a variable for a string constant.
5690 * Return OK or FAIL.
5691 */
5692 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 int evaluate;
5697{
5698 char_u *p;
5699 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 int extra = 0;
5701
5702 /*
5703 * Find the end of the string, skipping backslashed characters.
5704 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005705 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 if (*p == '\\' && p[1] != NUL)
5708 {
5709 ++p;
5710 /* A "\<x>" form occupies at least 4 characters, and produces up
5711 * to 6 characters: reserve space for 2 extra */
5712 if (*p == '<')
5713 extra += 2;
5714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716
5717 if (*p != '"')
5718 {
5719 EMSG2(_("E114: Missing quote: %s"), *arg);
5720 return FAIL;
5721 }
5722
5723 /* If only parsing, set *arg and return here */
5724 if (!evaluate)
5725 {
5726 *arg = p + 1;
5727 return OK;
5728 }
5729
5730 /*
5731 * Copy the string into allocated memory, handling backslashed
5732 * characters.
5733 */
5734 name = alloc((unsigned)(p - *arg + extra));
5735 if (name == NULL)
5736 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 rettv->v_type = VAR_STRING;
5738 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
5742 if (*p == '\\')
5743 {
5744 switch (*++p)
5745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 case 'b': *name++ = BS; ++p; break;
5747 case 'e': *name++ = ESC; ++p; break;
5748 case 'f': *name++ = FF; ++p; break;
5749 case 'n': *name++ = NL; ++p; break;
5750 case 'r': *name++ = CAR; ++p; break;
5751 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
5753 case 'X': /* hex: "\x1", "\x12" */
5754 case 'x':
5755 case 'u': /* Unicode: "\u0023" */
5756 case 'U':
5757 if (vim_isxdigit(p[1]))
5758 {
5759 int n, nr;
5760 int c = toupper(*p);
5761
5762 if (c == 'X')
5763 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005764 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005766 else
5767 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 nr = 0;
5769 while (--n >= 0 && vim_isxdigit(p[1]))
5770 {
5771 ++p;
5772 nr = (nr << 4) + hex2nr(*p);
5773 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775#ifdef FEAT_MBYTE
5776 /* For "\u" store the number according to
5777 * 'encoding'. */
5778 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 else
5781#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 break;
5785
5786 /* octal: "\1", "\12", "\123" */
5787 case '0':
5788 case '1':
5789 case '2':
5790 case '3':
5791 case '4':
5792 case '5':
5793 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 case '7': *name = *p++ - '0';
5795 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 *name = (*name << 3) + *p++ - '0';
5798 if (*p >= '0' && *p <= '7')
5799 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 break;
5803
5804 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (extra != 0)
5807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 break;
5810 }
5811 /* FALLTHROUGH */
5812
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815 }
5816 }
5817 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 *arg = p + 1;
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 return OK;
5825}
5826
5827/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 * Return OK or FAIL.
5830 */
5831 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005832get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 int evaluate;
5836{
5837 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 int reduce = 0;
5840
5841 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 */
5844 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 break;
5850 ++reduce;
5851 ++p;
5852 }
5853 }
5854
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 return FAIL;
5859 }
5860
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 if (!evaluate)
5863 {
5864 *arg = p + 1;
5865 return OK;
5866 }
5867
5868 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 */
5871 str = alloc((unsigned)((p - *arg) - reduce));
5872 if (str == NULL)
5873 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 rettv->v_type = VAR_STRING;
5875 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 break;
5883 ++p;
5884 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 *arg = p + 1;
5889
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 return OK;
5891}
5892
5893/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 * Allocate a variable for a List and fill it from "*arg".
5895 * Return OK or FAIL.
5896 */
5897 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005898get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 int evaluate;
5902{
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 list_T *l = NULL;
5904 typval_T tv;
5905 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906
5907 if (evaluate)
5908 {
5909 l = list_alloc();
5910 if (l == NULL)
5911 return FAIL;
5912 }
5913
5914 *arg = skipwhite(*arg + 1);
5915 while (**arg != ']' && **arg != NUL)
5916 {
5917 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5918 goto failret;
5919 if (evaluate)
5920 {
5921 item = listitem_alloc();
5922 if (item != NULL)
5923 {
5924 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005925 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 list_append(l, item);
5927 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005928 else
5929 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 }
5931
5932 if (**arg == ']')
5933 break;
5934 if (**arg != ',')
5935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 goto failret;
5938 }
5939 *arg = skipwhite(*arg + 1);
5940 }
5941
5942 if (**arg != ']')
5943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945failret:
5946 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005947 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 return FAIL;
5949 }
5950
5951 *arg = skipwhite(*arg + 1);
5952 if (evaluate)
5953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005954 rettv->v_type = VAR_LIST;
5955 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 ++l->lv_refcount;
5957 }
5958
5959 return OK;
5960}
5961
5962/*
5963 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005964 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005966 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967list_alloc()
5968{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005969 list_T *l;
5970
5971 l = (list_T *)alloc_clear(sizeof(list_T));
5972 if (l != NULL)
5973 {
5974 /* Prepend the list to the list of lists for garbage collection. */
5975 if (first_list != NULL)
5976 first_list->lv_used_prev = l;
5977 l->lv_used_prev = NULL;
5978 l->lv_used_next = first_list;
5979 first_list = l;
5980 }
5981 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982}
5983
5984/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005985 * Allocate an empty list for a return value.
5986 * Returns OK or FAIL.
5987 */
5988 static int
5989rettv_list_alloc(rettv)
5990 typval_T *rettv;
5991{
5992 list_T *l = list_alloc();
5993
5994 if (l == NULL)
5995 return FAIL;
5996
5997 rettv->vval.v_list = l;
5998 rettv->v_type = VAR_LIST;
5999 ++l->lv_refcount;
6000 return OK;
6001}
6002
6003/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004 * Unreference a list: decrement the reference count and free it when it
6005 * becomes zero.
6006 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006007 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006011 if (l != NULL && --l->lv_refcount <= 0)
6012 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013}
6014
6015/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006016 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 * Ignores the reference count.
6018 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006019 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006020list_free(l, recurse)
6021 list_T *l;
6022 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 /* Remove the list from the list of lists for garbage collection. */
6027 if (l->lv_used_prev == NULL)
6028 first_list = l->lv_used_next;
6029 else
6030 l->lv_used_prev->lv_used_next = l->lv_used_next;
6031 if (l->lv_used_next != NULL)
6032 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6033
Bram Moolenaard9fba312005-06-26 22:34:35 +00006034 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036 /* Remove the item before deleting it. */
6037 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006038 if (recurse || (item->li_tv.v_type != VAR_LIST
6039 && item->li_tv.v_type != VAR_DICT))
6040 clear_tv(&item->li_tv);
6041 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 }
6043 vim_free(l);
6044}
6045
6046/*
6047 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006048 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006050 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051listitem_alloc()
6052{
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054}
6055
6056/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006057 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006059 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006063 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 vim_free(item);
6065}
6066
6067/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006068 * Remove a list item from a List and free it. Also clears the value.
6069 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006070 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 list_T *l;
6073 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006074{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006075 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006076 listitem_free(item);
6077}
6078
6079/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 * Get the number of items in a list.
6081 */
6082 static long
6083list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 if (l == NULL)
6087 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006088 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 * Return TRUE when two lists have exactly the same values.
6093 */
6094 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 list_T *l1;
6097 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100{
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006103 if (l1 == NULL || l2 == NULL)
6104 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 if (l1 == l2)
6106 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006107 if (list_len(l1) != list_len(l2))
6108 return FALSE;
6109
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 for (item1 = l1->lv_first, item2 = l2->lv_first;
6111 item1 != NULL && item2 != NULL;
6112 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 return FALSE;
6115 return item1 == NULL && item2 == NULL;
6116}
6117
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006118#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6119 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006120/*
6121 * Return the dictitem that an entry in a hashtable points to.
6122 */
6123 dictitem_T *
6124dict_lookup(hi)
6125 hashitem_T *hi;
6126{
6127 return HI2DI(hi);
6128}
6129#endif
6130
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132 * Return TRUE when two dictionaries have exactly the same key/values.
6133 */
6134 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006135dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 dict_T *d1;
6137 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006138 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140{
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 hashitem_T *hi;
6142 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006145 if (d1 == NULL || d2 == NULL)
6146 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006147 if (d1 == d2)
6148 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 if (dict_len(d1) != dict_len(d2))
6150 return FALSE;
6151
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006152 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 if (!HASHITEM_EMPTY(hi))
6156 {
6157 item2 = dict_find(d2, hi->hi_key, -1);
6158 if (item2 == NULL)
6159 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161 return FALSE;
6162 --todo;
6163 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164 }
6165 return TRUE;
6166}
6167
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168static int tv_equal_recurse_limit;
6169
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006170/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006172 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006173 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174 */
6175 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 typval_T *tv1;
6178 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179 int ic; /* ignore case */
6180 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181{
6182 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006183 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006185 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006187 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006190 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 * recursiveness to a limit. We guess they are equal then.
6192 * A fixed limit has the problem of still taking an awful long time.
6193 * Reduce the limit every time running into it. That should work fine for
6194 * deeply linked structures that are not recursively linked and catch
6195 * recursiveness quickly. */
6196 if (!recursive)
6197 tv_equal_recurse_limit = 1000;
6198 if (recursive_cnt >= tv_equal_recurse_limit)
6199 {
6200 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 ++recursive_cnt;
6208 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6209 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211
6212 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 ++recursive_cnt;
6214 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6215 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006216 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217
6218 case VAR_FUNC:
6219 return (tv1->vval.v_string != NULL
6220 && tv2->vval.v_string != NULL
6221 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6222
6223 case VAR_NUMBER:
6224 return tv1->vval.v_number == tv2->vval.v_number;
6225
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006226#ifdef FEAT_FLOAT
6227 case VAR_FLOAT:
6228 return tv1->vval.v_float == tv2->vval.v_float;
6229#endif
6230
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231 case VAR_STRING:
6232 s1 = get_tv_string_buf(tv1, buf1);
6233 s2 = get_tv_string_buf(tv2, buf2);
6234 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006235 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006236
6237 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006238 return TRUE;
6239}
6240
6241/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 * Locate item with index "n" in list "l" and return it.
6243 * A negative index is counted from the end; -1 is the last item.
6244 * Returns NULL when "n" is out of range.
6245 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006246 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006249 long n;
6250{
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 long idx;
6253
6254 if (l == NULL)
6255 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006256
6257 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006259 n = l->lv_len + n;
6260
6261 /* Check for index out of range. */
6262 if (n < 0 || n >= l->lv_len)
6263 return NULL;
6264
6265 /* When there is a cached index may start search from there. */
6266 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006268 if (n < l->lv_idx / 2)
6269 {
6270 /* closest to the start of the list */
6271 item = l->lv_first;
6272 idx = 0;
6273 }
6274 else if (n > (l->lv_idx + l->lv_len) / 2)
6275 {
6276 /* closest to the end of the list */
6277 item = l->lv_last;
6278 idx = l->lv_len - 1;
6279 }
6280 else
6281 {
6282 /* closest to the cached index */
6283 item = l->lv_idx_item;
6284 idx = l->lv_idx;
6285 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 }
6287 else
6288 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006289 if (n < l->lv_len / 2)
6290 {
6291 /* closest to the start of the list */
6292 item = l->lv_first;
6293 idx = 0;
6294 }
6295 else
6296 {
6297 /* closest to the end of the list */
6298 item = l->lv_last;
6299 idx = l->lv_len - 1;
6300 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006302
6303 while (n > idx)
6304 {
6305 /* search forward */
6306 item = item->li_next;
6307 ++idx;
6308 }
6309 while (n < idx)
6310 {
6311 /* search backward */
6312 item = item->li_prev;
6313 --idx;
6314 }
6315
6316 /* cache the used index */
6317 l->lv_idx = idx;
6318 l->lv_idx_item = item;
6319
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 return item;
6321}
6322
6323/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006324 * Get list item "l[idx]" as a number.
6325 */
6326 static long
6327list_find_nr(l, idx, errorp)
6328 list_T *l;
6329 long idx;
6330 int *errorp; /* set to TRUE when something wrong */
6331{
6332 listitem_T *li;
6333
6334 li = list_find(l, idx);
6335 if (li == NULL)
6336 {
6337 if (errorp != NULL)
6338 *errorp = TRUE;
6339 return -1L;
6340 }
6341 return get_tv_number_chk(&li->li_tv, errorp);
6342}
6343
6344/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006345 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6346 */
6347 char_u *
6348list_find_str(l, idx)
6349 list_T *l;
6350 long idx;
6351{
6352 listitem_T *li;
6353
6354 li = list_find(l, idx - 1);
6355 if (li == NULL)
6356 {
6357 EMSGN(_(e_listidx), idx);
6358 return NULL;
6359 }
6360 return get_tv_string(&li->li_tv);
6361}
6362
6363/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006364 * Locate "item" list "l" and return its index.
6365 * Returns -1 when "item" is not in the list.
6366 */
6367 static long
6368list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 list_T *l;
6370 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006371{
6372 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374
6375 if (l == NULL)
6376 return -1;
6377 idx = 0;
6378 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6379 ++idx;
6380 if (li == NULL)
6381 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006382 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006383}
6384
6385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386 * Append item "item" to the end of list "l".
6387 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006389list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 list_T *l;
6391 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392{
6393 if (l->lv_last == NULL)
6394 {
6395 /* empty list */
6396 l->lv_first = item;
6397 l->lv_last = item;
6398 item->li_prev = NULL;
6399 }
6400 else
6401 {
6402 l->lv_last->li_next = item;
6403 item->li_prev = l->lv_last;
6404 l->lv_last = item;
6405 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006406 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 item->li_next = NULL;
6408}
6409
6410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006414 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 list_T *l;
6417 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 copy_tv(tv, &li->li_tv);
6424 list_append(l, li);
6425 return OK;
6426}
6427
6428/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006429 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 * Return FAIL when out of memory.
6431 */
6432 int
6433list_append_dict(list, dict)
6434 list_T *list;
6435 dict_T *dict;
6436{
6437 listitem_T *li = listitem_alloc();
6438
6439 if (li == NULL)
6440 return FAIL;
6441 li->li_tv.v_type = VAR_DICT;
6442 li->li_tv.v_lock = 0;
6443 li->li_tv.vval.v_dict = dict;
6444 list_append(list, li);
6445 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 return OK;
6447}
6448
6449/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006451 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452 * Returns FAIL when out of memory.
6453 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006454 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006455list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006456 list_T *l;
6457 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006458 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006459{
6460 listitem_T *li = listitem_alloc();
6461
6462 if (li == NULL)
6463 return FAIL;
6464 list_append(l, li);
6465 li->li_tv.v_type = VAR_STRING;
6466 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006467 if (str == NULL)
6468 li->li_tv.vval.v_string = NULL;
6469 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006470 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006471 return FAIL;
6472 return OK;
6473}
6474
6475/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006476 * Append "n" to list "l".
6477 * Returns FAIL when out of memory.
6478 */
6479 static int
6480list_append_number(l, n)
6481 list_T *l;
6482 varnumber_T n;
6483{
6484 listitem_T *li;
6485
6486 li = listitem_alloc();
6487 if (li == NULL)
6488 return FAIL;
6489 li->li_tv.v_type = VAR_NUMBER;
6490 li->li_tv.v_lock = 0;
6491 li->li_tv.vval.v_number = n;
6492 list_append(l, li);
6493 return OK;
6494}
6495
6496/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 * If "item" is NULL append at the end.
6499 * Return FAIL when out of memory.
6500 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006501 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *l;
6504 typval_T *tv;
6505 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506{
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508
6509 if (ni == NULL)
6510 return FAIL;
6511 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006512 list_insert(l, ni, item);
6513 return OK;
6514}
6515
6516 void
6517list_insert(l, ni, item)
6518 list_T *l;
6519 listitem_T *ni;
6520 listitem_T *item;
6521{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 if (item == NULL)
6523 /* Append new item at end of list. */
6524 list_append(l, ni);
6525 else
6526 {
6527 /* Insert new item before existing item. */
6528 ni->li_prev = item->li_prev;
6529 ni->li_next = item;
6530 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 ++l->lv_idx;
6534 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006538 l->lv_idx_item = NULL;
6539 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006541 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543}
6544
6545/*
6546 * Extend "l1" with "l2".
6547 * If "bef" is NULL append at the end, otherwise insert before this item.
6548 * Returns FAIL when out of memory.
6549 */
6550 static int
6551list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 list_T *l1;
6553 list_T *l2;
6554 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006557 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006559 /* We also quit the loop when we have inserted the original item count of
6560 * the list, avoid a hang when we extend a list with itself. */
6561 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6563 return FAIL;
6564 return OK;
6565}
6566
6567/*
6568 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6569 * Return FAIL when out of memory.
6570 */
6571 static int
6572list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 list_T *l1;
6574 list_T *l2;
6575 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576{
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006579 if (l1 == NULL || l2 == NULL)
6580 return FAIL;
6581
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 if (l == NULL)
6585 return FAIL;
6586 tv->v_type = VAR_LIST;
6587 tv->vval.v_list = l;
6588
6589 /* append all items from the second list */
6590 return list_extend(l, l2, NULL);
6591}
6592
6593/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006594 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 * Returns NULL when out of memory.
6598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604{
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 list_T *copy;
6606 listitem_T *item;
6607 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608
6609 if (orig == NULL)
6610 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611
6612 copy = list_alloc();
6613 if (copy != NULL)
6614 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 if (copyID != 0)
6616 {
6617 /* Do this before adding the items, because one of the items may
6618 * refer back to this list. */
6619 orig->lv_copyID = copyID;
6620 orig->lv_copylist = copy;
6621 }
6622 for (item = orig->lv_first; item != NULL && !got_int;
6623 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 {
6625 ni = listitem_alloc();
6626 if (ni == NULL)
6627 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006628 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006629 {
6630 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6631 {
6632 vim_free(ni);
6633 break;
6634 }
6635 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006637 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 list_append(copy, ni);
6639 }
6640 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006641 if (item != NULL)
6642 {
6643 list_unref(copy);
6644 copy = NULL;
6645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 }
6647
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 return copy;
6649}
6650
6651/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006653 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006654 * This used to be called list_remove, but that conflicts with a Sun header
6655 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006657 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006658vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 list_T *l;
6660 listitem_T *item;
6661 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006662{
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665 /* notify watchers */
6666 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006668 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 list_fix_watch(l, ip);
6670 if (ip == item2)
6671 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673
6674 if (item2->li_next == NULL)
6675 l->lv_last = item->li_prev;
6676 else
6677 item2->li_next->li_prev = item->li_prev;
6678 if (item->li_prev == NULL)
6679 l->lv_first = item2->li_next;
6680 else
6681 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006682 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683}
6684
6685/*
6686 * Return an allocated string with the string representation of a list.
6687 * May return NULL.
6688 */
6689 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006691 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006692 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693{
6694 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695
6696 if (tv->vval.v_list == NULL)
6697 return NULL;
6698 ga_init2(&ga, (int)sizeof(char), 80);
6699 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006700 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006701 {
6702 vim_free(ga.ga_data);
6703 return NULL;
6704 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006705 ga_append(&ga, ']');
6706 ga_append(&ga, NUL);
6707 return (char_u *)ga.ga_data;
6708}
6709
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710typedef struct join_S {
6711 char_u *s;
6712 char_u *tofree;
6713} join_T;
6714
6715 static int
6716list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6717 garray_T *gap; /* to store the result in */
6718 list_T *l;
6719 char_u *sep;
6720 int echo_style;
6721 int copyID;
6722 garray_T *join_gap; /* to keep each list item string */
6723{
6724 int i;
6725 join_T *p;
6726 int len;
6727 int sumlen = 0;
6728 int first = TRUE;
6729 char_u *tofree;
6730 char_u numbuf[NUMBUFLEN];
6731 listitem_T *item;
6732 char_u *s;
6733
6734 /* Stringify each item in the list. */
6735 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6736 {
6737 if (echo_style)
6738 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6739 else
6740 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6741 if (s == NULL)
6742 return FAIL;
6743
6744 len = (int)STRLEN(s);
6745 sumlen += len;
6746
Bram Moolenaarcde88542015-08-11 19:14:00 +02006747 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006748 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6749 if (tofree != NULL || s != numbuf)
6750 {
6751 p->s = s;
6752 p->tofree = tofree;
6753 }
6754 else
6755 {
6756 p->s = vim_strnsave(s, len);
6757 p->tofree = p->s;
6758 }
6759
6760 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006761 if (did_echo_string_emsg) /* recursion error, bail out */
6762 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006763 }
6764
6765 /* Allocate result buffer with its total size, avoid re-allocation and
6766 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6767 if (join_gap->ga_len >= 2)
6768 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6769 if (ga_grow(gap, sumlen + 2) == FAIL)
6770 return FAIL;
6771
6772 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6773 {
6774 if (first)
6775 first = FALSE;
6776 else
6777 ga_concat(gap, sep);
6778 p = ((join_T *)join_gap->ga_data) + i;
6779
6780 if (p->s != NULL)
6781 ga_concat(gap, p->s);
6782 line_breakcheck();
6783 }
6784
6785 return OK;
6786}
6787
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006790 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006791 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006793 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006794list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006796 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006798 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006799 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006801 garray_T join_ga;
6802 int retval;
6803 join_T *p;
6804 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805
Bram Moolenaard39a7512015-04-16 22:51:22 +02006806 if (l->lv_len < 1)
6807 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006808 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6809 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6810
6811 /* Dispose each item in join_ga. */
6812 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006813 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814 p = (join_T *)join_ga.ga_data;
6815 for (i = 0; i < join_ga.ga_len; ++i)
6816 {
6817 vim_free(p->tofree);
6818 ++p;
6819 }
6820 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006821 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006822
6823 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824}
6825
6826/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 * Garbage collection for lists and dictionaries.
6828 *
6829 * We use reference counts to be able to free most items right away when they
6830 * are no longer used. But for composite items it's possible that it becomes
6831 * unused while the reference count is > 0: When there is a recursive
6832 * reference. Example:
6833 * :let l = [1, 2, 3]
6834 * :let d = {9: l}
6835 * :let l[1] = d
6836 *
6837 * Since this is quite unusual we handle this with garbage collection: every
6838 * once in a while find out which lists and dicts are not referenced from any
6839 * variable.
6840 *
6841 * Here is a good reference text about garbage collection (refers to Python
6842 * but it applies to all reference-counting mechanisms):
6843 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845
6846/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 * Do garbage collection for lists and dicts.
6848 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 int
6851garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006852{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006854 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 buf_T *buf;
6856 win_T *wp;
6857 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006858 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006859 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006861#ifdef FEAT_WINDOWS
6862 tabpage_T *tp;
6863#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006865 /* Only do this once. */
6866 want_garbage_collect = FALSE;
6867 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006868 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006869
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 /* We advance by two because we add one for items referenced through
6871 * previous_funccal. */
6872 current_copyID += COPYID_INC;
6873 copyID = current_copyID;
6874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 /*
6876 * 1. Go through all accessible variables and mark all lists and dicts
6877 * with copyID.
6878 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879
6880 /* Don't free variables in the previous_funccal list unless they are only
6881 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006882 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6884 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6886 NULL);
6887 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6888 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 }
6890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 /* script-local variables */
6892 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894
6895 /* buffer-local variables */
6896 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899
6900 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6903 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006904#ifdef FEAT_AUTOCMD
6905 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6907 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006908#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006910#ifdef FEAT_WINDOWS
6911 /* tabpage-local variables */
6912 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006915#endif
6916
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919
6920 /* function-local variables */
6921 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6922 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6924 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 }
6926
Bram Moolenaard812df62008-11-09 12:46:09 +00006927 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006929
Bram Moolenaar1dced572012-04-05 16:54:08 +02006930#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006932#endif
6933
Bram Moolenaardb913952012-06-29 12:54:53 +02006934#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006936#endif
6937
6938#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006940#endif
6941
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 /*
6945 * 2. Free lists and dictionaries that are not referenced.
6946 */
6947 did_free = free_unref_items(copyID);
6948
6949 /*
6950 * 3. Check if any funccal can be freed now.
6951 */
6952 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006953 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 if (can_free_funccal(*pfc, copyID))
6955 {
6956 fc = *pfc;
6957 *pfc = fc->caller;
6958 free_funccal(fc, TRUE);
6959 did_free = TRUE;
6960 did_free_funccal = TRUE;
6961 }
6962 else
6963 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 if (did_free_funccal)
6966 /* When a funccal was freed some more items might be garbage
6967 * collected, so run again. */
6968 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006970 else if (p_verbose > 0)
6971 {
6972 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6973 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974
6975 return did_free;
6976}
6977
6978/*
6979 * Free lists and dictionaries that are no longer referenced.
6980 */
6981 static int
6982free_unref_items(copyID)
6983 int copyID;
6984{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 dict_T *dd, *dd_next;
6986 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 int did_free = FALSE;
6988
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 */
6992 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006993 {
6994 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006995 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006997 /* Free the Dictionary and ordinary items it contains, but don't
6998 * recurse into Lists and Dictionaries, they will be in the list
6999 * of dicts or list of lists. */
7000 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007003 dd = dd_next;
7004 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005
7006 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007007 * Go through the list of lists and free items without the copyID.
7008 * But don't free a list that has a watcher (used in a for loop), these
7009 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 */
7011 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 {
7013 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007014 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7015 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007017 /* Free the List and ordinary items it contains, but don't recurse
7018 * into Lists and Dictionaries, they will be in the list of dicts
7019 * or list of lists. */
7020 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 ll = ll_next;
7024 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 return did_free;
7026}
7027
7028/*
7029 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 * "list_stack" is used to add lists to be marked. Can be NULL.
7031 *
7032 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 int
7035set_ref_in_ht(ht, copyID, list_stack)
7036 hashtab_T *ht;
7037 int copyID;
7038 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039{
7040 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007043 hashtab_T *cur_ht;
7044 ht_stack_T *ht_stack = NULL;
7045 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 cur_ht = ht;
7048 for (;;)
7049 {
7050 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 /* Mark each item in the hashtab. If the item contains a hashtab
7053 * it is added to ht_stack, if it contains a list it is added to
7054 * list_stack. */
7055 todo = (int)cur_ht->ht_used;
7056 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7057 if (!HASHITEM_EMPTY(hi))
7058 {
7059 --todo;
7060 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7061 &ht_stack, list_stack);
7062 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064
7065 if (ht_stack == NULL)
7066 break;
7067
7068 /* take an item from the stack */
7069 cur_ht = ht_stack->ht;
7070 tempitem = ht_stack;
7071 ht_stack = ht_stack->prev;
7072 free(tempitem);
7073 }
7074
7075 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076}
7077
7078/*
7079 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7081 *
7082 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 int
7085set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 list_T *l;
7087 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007090 listitem_T *li;
7091 int abort = FALSE;
7092 list_T *cur_l;
7093 list_stack_T *list_stack = NULL;
7094 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 cur_l = l;
7097 for (;;)
7098 {
7099 if (!abort)
7100 /* Mark each item in the list. If the item contains a hashtab
7101 * it is added to ht_stack, if it contains a list it is added to
7102 * list_stack. */
7103 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7104 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7105 ht_stack, &list_stack);
7106 if (list_stack == NULL)
7107 break;
7108
7109 /* take an item from the stack */
7110 cur_l = list_stack->list;
7111 tempitem = list_stack;
7112 list_stack = list_stack->prev;
7113 free(tempitem);
7114 }
7115
7116 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117}
7118
7119/*
7120 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007121 * "list_stack" is used to add lists to be marked. Can be NULL.
7122 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7123 *
7124 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007125 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007126 int
7127set_ref_in_item(tv, copyID, ht_stack, list_stack)
7128 typval_T *tv;
7129 int copyID;
7130 ht_stack_T **ht_stack;
7131 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007132{
7133 dict_T *dd;
7134 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007136
7137 switch (tv->v_type)
7138 {
7139 case VAR_DICT:
7140 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007141 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007142 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007143 /* Didn't see this dict yet. */
7144 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007145 if (ht_stack == NULL)
7146 {
7147 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7148 }
7149 else
7150 {
7151 ht_stack_T *newitem = (ht_stack_T*)malloc(
7152 sizeof(ht_stack_T));
7153 if (newitem == NULL)
7154 abort = TRUE;
7155 else
7156 {
7157 newitem->ht = &dd->dv_hashtab;
7158 newitem->prev = *ht_stack;
7159 *ht_stack = newitem;
7160 }
7161 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007163 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164
7165 case VAR_LIST:
7166 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007167 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007169 /* Didn't see this list yet. */
7170 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007171 if (list_stack == NULL)
7172 {
7173 abort = set_ref_in_list(ll, copyID, ht_stack);
7174 }
7175 else
7176 {
7177 list_stack_T *newitem = (list_stack_T*)malloc(
7178 sizeof(list_stack_T));
7179 if (newitem == NULL)
7180 abort = TRUE;
7181 else
7182 {
7183 newitem->list = ll;
7184 newitem->prev = *list_stack;
7185 *list_stack = newitem;
7186 }
7187 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007188 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007189 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007190 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007191 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192}
7193
7194/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195 * Allocate an empty header for a dictionary.
7196 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007197 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198dict_alloc()
7199{
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007201
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007203 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007204 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007205 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206 if (first_dict != NULL)
7207 first_dict->dv_used_prev = d;
7208 d->dv_used_next = first_dict;
7209 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007210 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007213 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007214 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007216 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007217 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007218 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219}
7220
7221/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007222 * Allocate an empty dict for a return value.
7223 * Returns OK or FAIL.
7224 */
7225 static int
7226rettv_dict_alloc(rettv)
7227 typval_T *rettv;
7228{
7229 dict_T *d = dict_alloc();
7230
7231 if (d == NULL)
7232 return FAIL;
7233
7234 rettv->vval.v_dict = d;
7235 rettv->v_type = VAR_DICT;
7236 ++d->dv_refcount;
7237 return OK;
7238}
7239
7240
7241/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 * Unreference a Dictionary: decrement the reference count and free it when it
7243 * becomes zero.
7244 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007245 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007249 if (d != NULL && --d->dv_refcount <= 0)
7250 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251}
7252
7253/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007254 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 * Ignores the reference count.
7256 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007257 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007258dict_free(d, recurse)
7259 dict_T *d;
7260 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007263 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007264 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007266 /* Remove the dict from the list of dicts for garbage collection. */
7267 if (d->dv_used_prev == NULL)
7268 first_dict = d->dv_used_next;
7269 else
7270 d->dv_used_prev->dv_used_next = d->dv_used_next;
7271 if (d->dv_used_next != NULL)
7272 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7273
7274 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007275 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007276 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 if (!HASHITEM_EMPTY(hi))
7280 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007281 /* Remove the item before deleting it, just in case there is
7282 * something recursive causing trouble. */
7283 di = HI2DI(hi);
7284 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007285 if (recurse || (di->di_tv.v_type != VAR_LIST
7286 && di->di_tv.v_type != VAR_DICT))
7287 clear_tv(&di->di_tv);
7288 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 --todo;
7290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 vim_free(d);
7294}
7295
7296/*
7297 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 * The "key" is copied to the new item.
7299 * Note that the value of the item "di_tv" still needs to be initialized!
7300 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007302 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303dictitem_alloc(key)
7304 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305{
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007308 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007312 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007313 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315}
7316
7317/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 * Make a copy of a Dictionary item.
7319 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323{
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007326 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7327 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328 if (di != NULL)
7329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007331 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 copy_tv(&org->di_tv, &di->di_tv);
7333 }
7334 return di;
7335}
7336
7337/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 * Remove item "item" from Dictionary "dict" and free it.
7339 */
7340 static void
7341dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 dict_T *dict;
7343 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344{
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007346
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 if (HASHITEM_EMPTY(hi))
7349 EMSG2(_(e_intern2), "dictitem_remove()");
7350 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 dictitem_free(item);
7353}
7354
7355/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356 * Free a dict item. Also clears the value.
7357 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007358 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007363 if (item->di_flags & DI_FLAGS_ALLOC)
7364 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365}
7366
7367/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7369 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007370 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 * Returns NULL when out of memory.
7372 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378{
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 dict_T *copy;
7380 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007381 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383
7384 if (orig == NULL)
7385 return NULL;
7386
7387 copy = dict_alloc();
7388 if (copy != NULL)
7389 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007390 if (copyID != 0)
7391 {
7392 orig->dv_copyID = copyID;
7393 orig->dv_copydict = copy;
7394 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007395 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007397 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 --todo;
7401
7402 di = dictitem_alloc(hi->hi_key);
7403 if (di == NULL)
7404 break;
7405 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007406 {
7407 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7408 copyID) == FAIL)
7409 {
7410 vim_free(di);
7411 break;
7412 }
7413 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 else
7415 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7416 if (dict_add(copy, di) == FAIL)
7417 {
7418 dictitem_free(di);
7419 break;
7420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007423
Bram Moolenaare9a41262005-01-15 22:18:47 +00007424 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 if (todo > 0)
7426 {
7427 dict_unref(copy);
7428 copy = NULL;
7429 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430 }
7431
7432 return copy;
7433}
7434
7435/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007437 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007439 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 dict_T *d;
7442 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443{
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445}
7446
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007448 * Add a number or string entry to dictionary "d".
7449 * When "str" is NULL use number "nr", otherwise use "str".
7450 * Returns FAIL when out of memory and when key already exists.
7451 */
7452 int
7453dict_add_nr_str(d, key, nr, str)
7454 dict_T *d;
7455 char *key;
7456 long nr;
7457 char_u *str;
7458{
7459 dictitem_T *item;
7460
7461 item = dictitem_alloc((char_u *)key);
7462 if (item == NULL)
7463 return FAIL;
7464 item->di_tv.v_lock = 0;
7465 if (str == NULL)
7466 {
7467 item->di_tv.v_type = VAR_NUMBER;
7468 item->di_tv.vval.v_number = nr;
7469 }
7470 else
7471 {
7472 item->di_tv.v_type = VAR_STRING;
7473 item->di_tv.vval.v_string = vim_strsave(str);
7474 }
7475 if (dict_add(d, item) == FAIL)
7476 {
7477 dictitem_free(item);
7478 return FAIL;
7479 }
7480 return OK;
7481}
7482
7483/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007484 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007485 * Returns FAIL when out of memory and when key already exists.
7486 */
7487 int
7488dict_add_list(d, key, list)
7489 dict_T *d;
7490 char *key;
7491 list_T *list;
7492{
7493 dictitem_T *item;
7494
7495 item = dictitem_alloc((char_u *)key);
7496 if (item == NULL)
7497 return FAIL;
7498 item->di_tv.v_lock = 0;
7499 item->di_tv.v_type = VAR_LIST;
7500 item->di_tv.vval.v_list = list;
7501 if (dict_add(d, item) == FAIL)
7502 {
7503 dictitem_free(item);
7504 return FAIL;
7505 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007506 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007507 return OK;
7508}
7509
7510/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511 * Get the number of items in a Dictionary.
7512 */
7513 static long
7514dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007516{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007517 if (d == NULL)
7518 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007519 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007520}
7521
7522/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 * Find item "key[len]" in Dictionary "d".
7524 * If "len" is negative use strlen(key).
7525 * Returns NULL when not found.
7526 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007527 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 char_u *key;
7531 int len;
7532{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533#define AKEYLEN 200
7534 char_u buf[AKEYLEN];
7535 char_u *akey;
7536 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 if (len < 0)
7540 akey = key;
7541 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007542 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 tofree = akey = vim_strnsave(key, len);
7544 if (akey == NULL)
7545 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007546 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 else
7548 {
7549 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007550 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 akey = buf;
7552 }
7553
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 vim_free(tofree);
7556 if (HASHITEM_EMPTY(hi))
7557 return NULL;
7558 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559}
7560
7561/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562 * Get a string item from a dictionary.
7563 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007564 * Returns NULL if the entry doesn't exist or out of memory.
7565 */
7566 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007567get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007568 dict_T *d;
7569 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007570 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007571{
7572 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007573 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007574
7575 di = dict_find(d, key, -1);
7576 if (di == NULL)
7577 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007578 s = get_tv_string(&di->di_tv);
7579 if (save && s != NULL)
7580 s = vim_strsave(s);
7581 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582}
7583
7584/*
7585 * Get a number item from a dictionary.
7586 * Returns 0 if the entry doesn't exist or out of memory.
7587 */
7588 long
7589get_dict_number(d, key)
7590 dict_T *d;
7591 char_u *key;
7592{
7593 dictitem_T *di;
7594
7595 di = dict_find(d, key, -1);
7596 if (di == NULL)
7597 return 0;
7598 return get_tv_number(&di->di_tv);
7599}
7600
7601/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 * Return an allocated string with the string representation of a Dictionary.
7603 * May return NULL.
7604 */
7605 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007607 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007608 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609{
7610 garray_T ga;
7611 int first = TRUE;
7612 char_u *tofree;
7613 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 return NULL;
7621 ga_init2(&ga, (int)sizeof(char), 80);
7622 ga_append(&ga, '{');
7623
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007624 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 --todo;
7630
7631 if (first)
7632 first = FALSE;
7633 else
7634 ga_concat(&ga, (char_u *)", ");
7635
7636 tofree = string_quote(hi->hi_key, FALSE);
7637 if (tofree != NULL)
7638 {
7639 ga_concat(&ga, tofree);
7640 vim_free(tofree);
7641 }
7642 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007643 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007644 if (s != NULL)
7645 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007647 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007648 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007649 line_breakcheck();
7650
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007653 if (todo > 0)
7654 {
7655 vim_free(ga.ga_data);
7656 return NULL;
7657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658
7659 ga_append(&ga, '}');
7660 ga_append(&ga, NUL);
7661 return (char_u *)ga.ga_data;
7662}
7663
7664/*
7665 * Allocate a variable for a Dictionary and fill it from "*arg".
7666 * Return OK or FAIL. Returns NOTDONE for {expr}.
7667 */
7668 static int
7669get_dict_tv(arg, rettv, evaluate)
7670 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 int evaluate;
7673{
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 dict_T *d = NULL;
7675 typval_T tvkey;
7676 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007677 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007680 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681
7682 /*
7683 * First check if it's not a curly-braces thing: {expr}.
7684 * Must do this without evaluating, otherwise a function may be called
7685 * twice. Unfortunately this means we need to call eval1() twice for the
7686 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 if (*start != '}')
7690 {
7691 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7692 return FAIL;
7693 if (*start == '}')
7694 return NOTDONE;
7695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696
7697 if (evaluate)
7698 {
7699 d = dict_alloc();
7700 if (d == NULL)
7701 return FAIL;
7702 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007703 tvkey.v_type = VAR_UNKNOWN;
7704 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705
7706 *arg = skipwhite(*arg + 1);
7707 while (**arg != '}' && **arg != NUL)
7708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007709 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 goto failret;
7711 if (**arg != ':')
7712 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007713 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 goto failret;
7716 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007717 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007719 key = get_tv_string_buf_chk(&tvkey, buf);
7720 if (key == NULL || *key == NUL)
7721 {
7722 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7723 if (key != NULL)
7724 EMSG(_(e_emptykey));
7725 clear_tv(&tvkey);
7726 goto failret;
7727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729
7730 *arg = skipwhite(*arg + 1);
7731 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7732 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007733 if (evaluate)
7734 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 goto failret;
7736 }
7737 if (evaluate)
7738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 if (item != NULL)
7741 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007742 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 clear_tv(&tv);
7745 goto failret;
7746 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 item = dictitem_alloc(key);
7748 clear_tv(&tvkey);
7749 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007752 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 if (dict_add(d, item) == FAIL)
7754 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 }
7756 }
7757
7758 if (**arg == '}')
7759 break;
7760 if (**arg != ',')
7761 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007762 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 goto failret;
7764 }
7765 *arg = skipwhite(*arg + 1);
7766 }
7767
7768 if (**arg != '}')
7769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007770 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771failret:
7772 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007773 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774 return FAIL;
7775 }
7776
7777 *arg = skipwhite(*arg + 1);
7778 if (evaluate)
7779 {
7780 rettv->v_type = VAR_DICT;
7781 rettv->vval.v_dict = d;
7782 ++d->dv_refcount;
7783 }
7784
7785 return OK;
7786}
7787
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007789 * Return a string with the string representation of a variable.
7790 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007792 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007793 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007794 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 */
7796 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007797echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007800 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007801 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007802{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007803 static int recurse = 0;
7804 char_u *r = NULL;
7805
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007808 if (!did_echo_string_emsg)
7809 {
7810 /* Only give this message once for a recursive call to avoid
7811 * flooding the user with errors. And stop iterating over lists
7812 * and dicts. */
7813 did_echo_string_emsg = TRUE;
7814 EMSG(_("E724: variable nested too deep for displaying"));
7815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007817 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 }
7819 ++recurse;
7820
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821 switch (tv->v_type)
7822 {
7823 case VAR_FUNC:
7824 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 r = tv->vval.v_string;
7826 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007827
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007829 if (tv->vval.v_list == NULL)
7830 {
7831 *tofree = NULL;
7832 r = NULL;
7833 }
7834 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7835 {
7836 *tofree = NULL;
7837 r = (char_u *)"[...]";
7838 }
7839 else
7840 {
7841 tv->vval.v_list->lv_copyID = copyID;
7842 *tofree = list2string(tv, copyID);
7843 r = *tofree;
7844 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846
Bram Moolenaar8c711452005-01-14 21:53:12 +00007847 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848 if (tv->vval.v_dict == NULL)
7849 {
7850 *tofree = NULL;
7851 r = NULL;
7852 }
7853 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7854 {
7855 *tofree = NULL;
7856 r = (char_u *)"{...}";
7857 }
7858 else
7859 {
7860 tv->vval.v_dict->dv_copyID = copyID;
7861 *tofree = dict2string(tv, copyID);
7862 r = *tofree;
7863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007865
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 case VAR_STRING:
7867 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 *tofree = NULL;
7869 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007871
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
7873 case VAR_FLOAT:
7874 *tofree = NULL;
7875 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7876 r = numbuf;
7877 break;
7878#endif
7879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007881 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007882 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007884
Bram Moolenaar8502c702014-06-17 12:51:16 +02007885 if (--recurse == 0)
7886 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007887 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888}
7889
7890/*
7891 * Return a string with the string representation of a variable.
7892 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7893 * "numbuf" is used for a number.
7894 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007895 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 */
7897 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007898tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900 char_u **tofree;
7901 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007902 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007903{
7904 switch (tv->v_type)
7905 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 case VAR_FUNC:
7907 *tofree = string_quote(tv->vval.v_string, TRUE);
7908 return *tofree;
7909 case VAR_STRING:
7910 *tofree = string_quote(tv->vval.v_string, FALSE);
7911 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007912#ifdef FEAT_FLOAT
7913 case VAR_FLOAT:
7914 *tofree = NULL;
7915 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7916 return numbuf;
7917#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007918 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007920 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007921 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007922 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007923 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007924 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007925 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926}
7927
7928/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 * Return string "str" in ' quotes, doubling ' characters.
7930 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007932 */
7933 static char_u *
7934string_quote(str, function)
7935 char_u *str;
7936 int function;
7937{
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 char_u *p, *r, *s;
7940
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 len = (function ? 13 : 3);
7942 if (str != NULL)
7943 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007944 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 for (p = str; *p != NUL; mb_ptr_adv(p))
7946 if (*p == '\'')
7947 ++len;
7948 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007949 s = r = alloc(len);
7950 if (r != NULL)
7951 {
7952 if (function)
7953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007955 r += 10;
7956 }
7957 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 if (str != NULL)
7960 for (p = str; *p != NUL; )
7961 {
7962 if (*p == '\'')
7963 *r++ = '\'';
7964 MB_COPY_CHAR(p, r);
7965 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007967 if (function)
7968 *r++ = ')';
7969 *r++ = NUL;
7970 }
7971 return s;
7972}
7973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007974#ifdef FEAT_FLOAT
7975/*
7976 * Convert the string "text" to a floating point number.
7977 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7978 * this always uses a decimal point.
7979 * Returns the length of the text that was consumed.
7980 */
7981 static int
7982string2float(text, value)
7983 char_u *text;
7984 float_T *value; /* result stored here */
7985{
7986 char *s = (char *)text;
7987 float_T f;
7988
7989 f = strtod(s, &s);
7990 *value = f;
7991 return (int)((char_u *)s - text);
7992}
7993#endif
7994
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 * Get the value of an environment variable.
7997 * "arg" is pointing to the '$'. It is advanced to after the name.
7998 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007999 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 */
8001 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008002get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 int evaluate;
8006{
8007 char_u *string = NULL;
8008 int len;
8009 int cc;
8010 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008011 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
8013 ++*arg;
8014 name = *arg;
8015 len = get_env_len(arg);
8016 if (evaluate)
8017 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008018 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008019 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008020
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008021 cc = name[len];
8022 name[len] = NUL;
8023 /* first try vim_getenv(), fast for normal environment vars */
8024 string = vim_getenv(name, &mustfree);
8025 if (string != NULL && *string != NUL)
8026 {
8027 if (!mustfree)
8028 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008030 else
8031 {
8032 if (mustfree)
8033 vim_free(string);
8034
8035 /* next try expanding things like $VIM and ${HOME} */
8036 string = expand_env_save(name - 1);
8037 if (string != NULL && *string == '$')
8038 {
8039 vim_free(string);
8040 string = NULL;
8041 }
8042 }
8043 name[len] = cc;
8044
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008045 rettv->v_type = VAR_STRING;
8046 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
8048
8049 return OK;
8050}
8051
8052/*
8053 * Array with names and number of arguments of all internal functions
8054 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8055 */
8056static struct fst
8057{
8058 char *f_name; /* function name */
8059 char f_min_argc; /* minimal number of arguments */
8060 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008062 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063} functions[] =
8064{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008065#ifdef FEAT_FLOAT
8066 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008067 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008069 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008070 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"append", 2, 2, f_append},
8072 {"argc", 0, 0, f_argc},
8073 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008074 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008075 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008076#ifdef FEAT_FLOAT
8077 {"asin", 1, 1, f_asin}, /* WJMc */
8078#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008079 {"assert_equal", 2, 3, f_assert_equal},
8080 {"assert_false", 1, 2, f_assert_false},
8081 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#ifdef FEAT_FLOAT
8083 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008084 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008087 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"bufexists", 1, 1, f_bufexists},
8089 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8090 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8091 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8092 {"buflisted", 1, 1, f_buflisted},
8093 {"bufloaded", 1, 1, f_bufloaded},
8094 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008095 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"bufwinnr", 1, 1, f_bufwinnr},
8097 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008098 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008099 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#ifdef FEAT_FLOAT
8102 {"ceil", 1, 1, f_ceil},
8103#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008104 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008105 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008107 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008109#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008110 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008111 {"complete_add", 1, 1, f_complete_add},
8112 {"complete_check", 0, 0, f_complete_check},
8113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008115 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008118 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008120 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008122 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008123 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"delete", 1, 1, f_delete},
8125 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008126 {"diff_filler", 1, 1, f_diff_filler},
8127 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008128 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008130 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"eventhandler", 0, 0, f_eventhandler},
8132 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008133 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008135#ifdef FEAT_FLOAT
8136 {"exp", 1, 1, f_exp},
8137#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008138 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008139 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008140 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8142 {"filereadable", 1, 1, f_filereadable},
8143 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008144 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008145 {"finddir", 1, 3, f_finddir},
8146 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008147#ifdef FEAT_FLOAT
8148 {"float2nr", 1, 1, f_float2nr},
8149 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008151#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008152 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"fnamemodify", 2, 2, f_fnamemodify},
8154 {"foldclosed", 1, 1, f_foldclosed},
8155 {"foldclosedend", 1, 1, f_foldclosedend},
8156 {"foldlevel", 1, 1, f_foldlevel},
8157 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008158 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008160 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008161 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008162 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008163 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008164 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"getchar", 0, 1, f_getchar},
8166 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008167 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"getcmdline", 0, 0, f_getcmdline},
8169 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008170 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008171 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008172 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008174 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008175 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"getfsize", 1, 1, f_getfsize},
8177 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008178 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008179 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008180 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008181 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008182 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008183 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008184 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008185 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008187 {"gettabvar", 2, 3, f_gettabvar},
8188 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"getwinposx", 0, 0, f_getwinposx},
8190 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008191 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008192 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008193 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008194 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008197 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008198 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8200 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8201 {"histadd", 2, 2, f_histadd},
8202 {"histdel", 1, 2, f_histdel},
8203 {"histget", 1, 2, f_histget},
8204 {"histnr", 1, 1, f_histnr},
8205 {"hlID", 1, 1, f_hlID},
8206 {"hlexists", 1, 1, f_hlexists},
8207 {"hostname", 0, 0, f_hostname},
8208 {"iconv", 3, 3, f_iconv},
8209 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008211 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008213 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"inputrestore", 0, 0, f_inputrestore},
8215 {"inputsave", 0, 0, f_inputsave},
8216 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008217 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008218 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008220 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008221 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008222 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008223 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008225 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"libcall", 3, 3, f_libcall},
8227 {"libcallnr", 3, 3, f_libcallnr},
8228 {"line", 1, 1, f_line},
8229 {"line2byte", 1, 1, f_line2byte},
8230 {"lispindent", 1, 1, f_lispindent},
8231 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008232#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008233 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234 {"log10", 1, 1, f_log10},
8235#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008236#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008237 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008238#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008240 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008241 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008242 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008243 {"matchadd", 2, 5, f_matchadd},
8244 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008245 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008246 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008247 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008248 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008249 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008250 {"max", 1, 1, f_max},
8251 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008252#ifdef vim_mkdir
8253 {"mkdir", 1, 3, f_mkdir},
8254#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008256#ifdef FEAT_MZSCHEME
8257 {"mzeval", 1, 1, f_mzeval},
8258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008260 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008261 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008262 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008263#ifdef FEAT_FLOAT
8264 {"pow", 2, 2, f_pow},
8265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008267 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008268 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008269#ifdef FEAT_PYTHON3
8270 {"py3eval", 1, 1, f_py3eval},
8271#endif
8272#ifdef FEAT_PYTHON
8273 {"pyeval", 1, 1, f_pyeval},
8274#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008275 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008276 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008277 {"reltime", 0, 2, f_reltime},
8278 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"remote_expr", 2, 3, f_remote_expr},
8280 {"remote_foreground", 1, 1, f_remote_foreground},
8281 {"remote_peek", 1, 2, f_remote_peek},
8282 {"remote_read", 1, 1, f_remote_read},
8283 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008284 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008286 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008288 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008289#ifdef FEAT_FLOAT
8290 {"round", 1, 1, f_round},
8291#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008292 {"screenattr", 2, 2, f_screenattr},
8293 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008294 {"screencol", 0, 0, f_screencol},
8295 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008296 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008297 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008298 {"searchpair", 3, 7, f_searchpair},
8299 {"searchpairpos", 3, 7, f_searchpairpos},
8300 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"server2client", 2, 2, f_server2client},
8302 {"serverlist", 0, 0, f_serverlist},
8303 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008304 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"setcmdpos", 1, 1, f_setcmdpos},
8306 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008307 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008308 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008309 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008310 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008312 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008313 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008315#ifdef FEAT_CRYPT
8316 {"sha256", 1, 1, f_sha256},
8317#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008318 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008319 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008321#ifdef FEAT_FLOAT
8322 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008323 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008324#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008325 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008326 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008327 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008328 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008329 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008330#ifdef FEAT_FLOAT
8331 {"sqrt", 1, 1, f_sqrt},
8332 {"str2float", 1, 1, f_str2float},
8333#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008334 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008335 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008336 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337#ifdef HAVE_STRFTIME
8338 {"strftime", 1, 2, f_strftime},
8339#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008341 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {"strlen", 1, 1, f_strlen},
8343 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008344 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008346 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008347 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"substitute", 4, 4, f_substitute},
8349 {"synID", 3, 3, f_synID},
8350 {"synIDattr", 2, 3, f_synIDattr},
8351 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008352 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008353 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008354 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008355 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008356 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008357 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008358 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008359 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008360 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008361#ifdef FEAT_FLOAT
8362 {"tan", 1, 1, f_tan},
8363 {"tanh", 1, 1, f_tanh},
8364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008366 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {"tolower", 1, 1, f_tolower},
8368 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008369 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#ifdef FEAT_FLOAT
8371 {"trunc", 1, 1, f_trunc},
8372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008374 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008375 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008376 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008377 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"virtcol", 1, 1, f_virtcol},
8379 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008380 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {"winbufnr", 1, 1, f_winbufnr},
8382 {"wincol", 0, 0, f_wincol},
8383 {"winheight", 1, 1, f_winheight},
8384 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008385 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008387 {"winrestview", 1, 1, f_winrestview},
8388 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008390 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008391 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392};
8393
8394#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8395
8396/*
8397 * Function given to ExpandGeneric() to obtain the list of internal
8398 * or user defined function names.
8399 */
8400 char_u *
8401get_function_name(xp, idx)
8402 expand_T *xp;
8403 int idx;
8404{
8405 static int intidx = -1;
8406 char_u *name;
8407
8408 if (idx == 0)
8409 intidx = -1;
8410 if (intidx < 0)
8411 {
8412 name = get_user_func_name(xp, idx);
8413 if (name != NULL)
8414 return name;
8415 }
8416 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8417 {
8418 STRCPY(IObuff, functions[intidx].f_name);
8419 STRCAT(IObuff, "(");
8420 if (functions[intidx].f_max_argc == 0)
8421 STRCAT(IObuff, ")");
8422 return IObuff;
8423 }
8424
8425 return NULL;
8426}
8427
8428/*
8429 * Function given to ExpandGeneric() to obtain the list of internal or
8430 * user defined variable or function names.
8431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 char_u *
8433get_expr_name(xp, idx)
8434 expand_T *xp;
8435 int idx;
8436{
8437 static int intidx = -1;
8438 char_u *name;
8439
8440 if (idx == 0)
8441 intidx = -1;
8442 if (intidx < 0)
8443 {
8444 name = get_function_name(xp, idx);
8445 if (name != NULL)
8446 return name;
8447 }
8448 return get_user_var_name(xp, ++intidx);
8449}
8450
8451#endif /* FEAT_CMDL_COMPL */
8452
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008453#if defined(EBCDIC) || defined(PROTO)
8454/*
8455 * Compare struct fst by function name.
8456 */
8457 static int
8458compare_func_name(s1, s2)
8459 const void *s1;
8460 const void *s2;
8461{
8462 struct fst *p1 = (struct fst *)s1;
8463 struct fst *p2 = (struct fst *)s2;
8464
8465 return STRCMP(p1->f_name, p2->f_name);
8466}
8467
8468/*
8469 * Sort the function table by function name.
8470 * The sorting of the table above is ASCII dependant.
8471 * On machines using EBCDIC we have to sort it.
8472 */
8473 static void
8474sortFunctions()
8475{
8476 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8477
8478 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8479}
8480#endif
8481
8482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483/*
8484 * Find internal function in table above.
8485 * Return index, or -1 if not found
8486 */
8487 static int
8488find_internal_func(name)
8489 char_u *name; /* name of the function */
8490{
8491 int first = 0;
8492 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8493 int cmp;
8494 int x;
8495
8496 /*
8497 * Find the function name in the table. Binary search.
8498 */
8499 while (first <= last)
8500 {
8501 x = first + ((unsigned)(last - first) >> 1);
8502 cmp = STRCMP(name, functions[x].f_name);
8503 if (cmp < 0)
8504 last = x - 1;
8505 else if (cmp > 0)
8506 first = x + 1;
8507 else
8508 return x;
8509 }
8510 return -1;
8511}
8512
8513/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8515 * name it contains, otherwise return "name".
8516 */
8517 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008518deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519 char_u *name;
8520 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008521 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522{
Bram Moolenaar33570922005-01-25 22:26:29 +00008523 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 int cc;
8525
8526 cc = name[*lenp];
8527 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008528 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 {
8534 *lenp = 0;
8535 return (char_u *)""; /* just in case */
8536 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008537 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008539 }
8540
8541 return name;
8542}
8543
8544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 * Allocate a variable for the result of a function.
8546 * Return OK or FAIL.
8547 */
8548 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008549get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8550 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 char_u *name; /* name of the function */
8552 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 char_u **arg; /* argument, pointing to the '(' */
8555 linenr_T firstline; /* first line of range */
8556 linenr_T lastline; /* last line of range */
8557 int *doesrange; /* return: function handled range */
8558 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008559 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 char_u *argp;
8562 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008563 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 int argcount = 0; /* number of arguments found */
8565
8566 /*
8567 * Get the arguments.
8568 */
8569 argp = *arg;
8570 while (argcount < MAX_FUNC_ARGS)
8571 {
8572 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8573 if (*argp == ')' || *argp == ',' || *argp == NUL)
8574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8576 {
8577 ret = FAIL;
8578 break;
8579 }
8580 ++argcount;
8581 if (*argp != ',')
8582 break;
8583 }
8584 if (*argp == ')')
8585 ++argp;
8586 else
8587 ret = FAIL;
8588
8589 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008591 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 {
8594 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008595 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008597 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
8603 *arg = skipwhite(argp);
8604 return ret;
8605}
8606
8607
8608/*
8609 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008610 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008611 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 */
8613 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008614call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008615 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008616 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008620 typval_T *argvars; /* vars for arguments, must have "argcount"
8621 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 linenr_T firstline; /* first line of range */
8623 linenr_T lastline; /* last line of range */
8624 int *doesrange; /* return: function handled range */
8625 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#define ERROR_UNKNOWN 0
8630#define ERROR_TOOMANY 1
8631#define ERROR_TOOFEW 2
8632#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633#define ERROR_DICT 4
8634#define ERROR_NONE 5
8635#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 int error = ERROR_NONE;
8637 int i;
8638 int llen;
8639 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#define FLEN_FIXED 40
8641 char_u fname_buf[FLEN_FIXED + 1];
8642 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008643 char_u *name;
8644
8645 /* Make a copy of the name, if it comes from a funcref variable it could
8646 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008647 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008648 if (name == NULL)
8649 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651 /*
8652 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8653 * Change <SNR>123_name() to K_SNR 123_name().
8654 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 llen = eval_fname_script(name);
8657 if (llen > 0)
8658 {
8659 fname_buf[0] = K_SPECIAL;
8660 fname_buf[1] = KS_EXTRA;
8661 fname_buf[2] = (int)KE_SNR;
8662 i = 3;
8663 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8664 {
8665 if (current_SID <= 0)
8666 error = ERROR_SCRIPT;
8667 else
8668 {
8669 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8670 i = (int)STRLEN(fname_buf);
8671 }
8672 }
8673 if (i + STRLEN(name + llen) < FLEN_FIXED)
8674 {
8675 STRCPY(fname_buf + i, name + llen);
8676 fname = fname_buf;
8677 }
8678 else
8679 {
8680 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8681 if (fname == NULL)
8682 error = ERROR_OTHER;
8683 else
8684 {
8685 mch_memmove(fname, fname_buf, (size_t)i);
8686 STRCPY(fname + i, name + llen);
8687 }
8688 }
8689 }
8690 else
8691 fname = name;
8692
8693 *doesrange = FALSE;
8694
8695
8696 /* execute the function if no errors detected and executing */
8697 if (evaluate && error == ERROR_NONE)
8698 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008699 char_u *rfname = fname;
8700
8701 /* Ignore "g:" before a function name. */
8702 if (fname[0] == 'g' && fname[1] == ':')
8703 rfname = fname + 2;
8704
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008705 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8706 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 error = ERROR_UNKNOWN;
8708
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008709 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 {
8711 /*
8712 * User defined function.
8713 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008714 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008717 /* Trigger FuncUndefined event, may load the function. */
8718 if (fp == NULL
8719 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008720 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008721 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008723 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008724 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 }
8726#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008727 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008728 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008729 {
8730 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008731 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008732 }
8733
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 if (fp != NULL)
8735 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008736 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008738 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008740 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008742 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008743 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 else
8745 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008746 int did_save_redo = FALSE;
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 /*
8749 * Call the user function.
8750 * Save and restore search patterns, script variables and
8751 * redo buffer.
8752 */
8753 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008754#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008755 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008756#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008757 {
8758 saveRedobuff();
8759 did_save_redo = TRUE;
8760 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008761 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008763 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008764 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8765 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8766 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008767 /* Function was unreferenced while being used, free it
8768 * now. */
8769 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008770 if (did_save_redo)
8771 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 restore_search_patterns();
8773 error = ERROR_NONE;
8774 }
8775 }
8776 }
8777 else
8778 {
8779 /*
8780 * Find the function name in the table, call its implementation.
8781 */
8782 i = find_internal_func(fname);
8783 if (i >= 0)
8784 {
8785 if (argcount < functions[i].f_min_argc)
8786 error = ERROR_TOOFEW;
8787 else if (argcount > functions[i].f_max_argc)
8788 error = ERROR_TOOMANY;
8789 else
8790 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008791 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 error = ERROR_NONE;
8794 }
8795 }
8796 }
8797 /*
8798 * The function call (or "FuncUndefined" autocommand sequence) might
8799 * have been aborted by an error, an interrupt, or an explicitly thrown
8800 * exception that has not been caught so far. This situation can be
8801 * tested for by calling aborting(). For an error in an internal
8802 * function or for the "E132" error in call_user_func(), however, the
8803 * throw point at which the "force_abort" flag (temporarily reset by
8804 * emsg()) is normally updated has not been reached yet. We need to
8805 * update that flag first to make aborting() reliable.
8806 */
8807 update_force_abort();
8808 }
8809 if (error == ERROR_NONE)
8810 ret = OK;
8811
8812 /*
8813 * Report an error unless the argument evaluation or function call has been
8814 * cancelled due to an aborting error, an interrupt, or an exception.
8815 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008816 if (!aborting())
8817 {
8818 switch (error)
8819 {
8820 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008821 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008822 break;
8823 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008824 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008825 break;
8826 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008827 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008828 name);
8829 break;
8830 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008831 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008832 name);
8833 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008834 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008835 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008836 name);
8837 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008838 }
8839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 if (fname != name && fname != fname_buf)
8842 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008843 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
8845 return ret;
8846}
8847
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008848/*
8849 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008850 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008851 */
8852 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008853emsg_funcname(ermsg, name)
8854 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008855 char_u *name;
8856{
8857 char_u *p;
8858
8859 if (*name == K_SPECIAL)
8860 p = concat_str((char_u *)"<SNR>", name + 3);
8861 else
8862 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008863 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008864 if (p != name)
8865 vim_free(p);
8866}
8867
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008868/*
8869 * Return TRUE for a non-zero Number and a non-empty String.
8870 */
8871 static int
8872non_zero_arg(argvars)
8873 typval_T *argvars;
8874{
8875 return ((argvars[0].v_type == VAR_NUMBER
8876 && argvars[0].vval.v_number != 0)
8877 || (argvars[0].v_type == VAR_STRING
8878 && argvars[0].vval.v_string != NULL
8879 && *argvars[0].vval.v_string != NUL));
8880}
8881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882/*********************************************
8883 * Implementation of the built-in functions
8884 */
8885
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008887static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8888
8889/*
8890 * Get the float value of "argvars[0]" into "f".
8891 * Returns FAIL when the argument is not a Number or Float.
8892 */
8893 static int
8894get_float_arg(argvars, f)
8895 typval_T *argvars;
8896 float_T *f;
8897{
8898 if (argvars[0].v_type == VAR_FLOAT)
8899 {
8900 *f = argvars[0].vval.v_float;
8901 return OK;
8902 }
8903 if (argvars[0].v_type == VAR_NUMBER)
8904 {
8905 *f = (float_T)argvars[0].vval.v_number;
8906 return OK;
8907 }
8908 EMSG(_("E808: Number or Float required"));
8909 return FAIL;
8910}
8911
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008912/*
8913 * "abs(expr)" function
8914 */
8915 static void
8916f_abs(argvars, rettv)
8917 typval_T *argvars;
8918 typval_T *rettv;
8919{
8920 if (argvars[0].v_type == VAR_FLOAT)
8921 {
8922 rettv->v_type = VAR_FLOAT;
8923 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8924 }
8925 else
8926 {
8927 varnumber_T n;
8928 int error = FALSE;
8929
8930 n = get_tv_number_chk(&argvars[0], &error);
8931 if (error)
8932 rettv->vval.v_number = -1;
8933 else if (n > 0)
8934 rettv->vval.v_number = n;
8935 else
8936 rettv->vval.v_number = -n;
8937 }
8938}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008939
8940/*
8941 * "acos()" function
8942 */
8943 static void
8944f_acos(argvars, rettv)
8945 typval_T *argvars;
8946 typval_T *rettv;
8947{
8948 float_T f;
8949
8950 rettv->v_type = VAR_FLOAT;
8951 if (get_float_arg(argvars, &f) == OK)
8952 rettv->vval.v_float = acos(f);
8953 else
8954 rettv->vval.v_float = 0.0;
8955}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008956#endif
8957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 */
8961 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008962f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *argvars;
8964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008969 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008971 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008972 && !tv_check_lock(l->lv_lock,
8973 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008974 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 }
8977 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008978 EMSG(_(e_listreq));
8979}
8980
8981/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008982 * "and(expr, expr)" function
8983 */
8984 static void
8985f_and(argvars, rettv)
8986 typval_T *argvars;
8987 typval_T *rettv;
8988{
8989 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8990 & get_tv_number_chk(&argvars[1], NULL);
8991}
8992
8993/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008994 * "append(lnum, string/list)" function
8995 */
8996 static void
8997f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *argvars;
8999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009000{
9001 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 list_T *l = NULL;
9004 listitem_T *li = NULL;
9005 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009006 long added = 0;
9007
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009008 /* When coming here from Insert mode, sync undo, so that this can be
9009 * undone separately from what was previously inserted. */
9010 if (u_sync_once == 2)
9011 {
9012 u_sync_once = 1; /* notify that u_sync() was called */
9013 u_sync(TRUE);
9014 }
9015
Bram Moolenaar0d660222005-01-07 21:51:51 +00009016 lnum = get_tv_lnum(argvars);
9017 if (lnum >= 0
9018 && lnum <= curbuf->b_ml.ml_line_count
9019 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009021 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009023 l = argvars[1].vval.v_list;
9024 if (l == NULL)
9025 return;
9026 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009027 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009028 for (;;)
9029 {
9030 if (l == NULL)
9031 tv = &argvars[1]; /* append a string */
9032 else if (li == NULL)
9033 break; /* end of list */
9034 else
9035 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009036 line = get_tv_string_chk(tv);
9037 if (line == NULL) /* type error */
9038 {
9039 rettv->vval.v_number = 1; /* Failed */
9040 break;
9041 }
9042 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009043 ++added;
9044 if (l == NULL)
9045 break;
9046 li = li->li_next;
9047 }
9048
9049 appended_lines_mark(lnum, added);
9050 if (curwin->w_cursor.lnum > lnum)
9051 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009053 else
9054 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055}
9056
9057/*
9058 * "argc()" function
9059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009062 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066}
9067
9068/*
9069 * "argidx()" function
9070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009073 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077}
9078
9079/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009080 * "arglistid()" function
9081 */
9082 static void
9083f_arglistid(argvars, rettv)
9084 typval_T *argvars UNUSED;
9085 typval_T *rettv;
9086{
9087 win_T *wp;
9088 tabpage_T *tp = NULL;
9089 long n;
9090
9091 rettv->vval.v_number = -1;
9092 if (argvars[0].v_type != VAR_UNKNOWN)
9093 {
9094 if (argvars[1].v_type != VAR_UNKNOWN)
9095 {
9096 n = get_tv_number(&argvars[1]);
9097 if (n >= 0)
9098 tp = find_tabpage(n);
9099 }
9100 else
9101 tp = curtab;
9102
9103 if (tp != NULL)
9104 {
9105 wp = find_win_by_nr(&argvars[0], tp);
9106 if (wp != NULL)
9107 rettv->vval.v_number = wp->w_alist->id;
9108 }
9109 }
9110 else
9111 rettv->vval.v_number = curwin->w_alist->id;
9112}
9113
9114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 * "argv(nr)" function
9116 */
9117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009119 typval_T *argvars;
9120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122 int idx;
9123
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009124 if (argvars[0].v_type != VAR_UNKNOWN)
9125 {
9126 idx = get_tv_number_chk(&argvars[0], NULL);
9127 if (idx >= 0 && idx < ARGCOUNT)
9128 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9129 else
9130 rettv->vval.v_string = NULL;
9131 rettv->v_type = VAR_STRING;
9132 }
9133 else if (rettv_list_alloc(rettv) == OK)
9134 for (idx = 0; idx < ARGCOUNT; ++idx)
9135 list_append_string(rettv->vval.v_list,
9136 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137}
9138
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009139static void prepare_assert_error __ARGS((garray_T*gap));
9140static void fill_assert_error __ARGS((garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv));
9141static void assert_error __ARGS((garray_T *gap));
9142static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9143
9144/*
9145 * Prepare "gap" for an assert error and add the sourcing position.
9146 */
9147 static void
9148prepare_assert_error(gap)
9149 garray_T *gap;
9150{
9151 char buf[NUMBUFLEN];
9152
9153 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009154 if (sourcing_name != NULL)
9155 {
9156 ga_concat(gap, sourcing_name);
9157 if (sourcing_lnum > 0)
9158 ga_concat(gap, (char_u *)" ");
9159 }
9160 if (sourcing_lnum > 0)
9161 {
9162 sprintf(buf, "line %ld", (long)sourcing_lnum);
9163 ga_concat(gap, (char_u *)buf);
9164 }
9165 if (sourcing_name != NULL || sourcing_lnum > 0)
9166 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009167}
9168
9169/*
9170 * Fill "gap" with information about an assert error.
9171 */
9172 static void
9173fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9174 garray_T *gap;
9175 typval_T *opt_msg_tv;
9176 char_u *exp_str;
9177 typval_T *exp_tv;
9178 typval_T *got_tv;
9179{
9180 char_u numbuf[NUMBUFLEN];
9181 char_u *tofree;
9182
9183 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9184 {
9185 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9186 vim_free(tofree);
9187 }
9188 else
9189 {
9190 ga_concat(gap, (char_u *)"Expected ");
9191 if (exp_str == NULL)
9192 {
9193 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9194 vim_free(tofree);
9195 }
9196 else
9197 ga_concat(gap, exp_str);
9198 ga_concat(gap, (char_u *)" but got ");
9199 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9200 vim_free(tofree);
9201 }
9202}
Bram Moolenaar43345542015-11-29 17:35:35 +01009203
9204/*
9205 * Add an assert error to v:errors.
9206 */
9207 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009208assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009209 garray_T *gap;
9210{
9211 struct vimvar *vp = &vimvars[VV_ERRORS];
9212
9213 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9214 /* Make sure v:errors is a list. */
9215 set_vim_var_list(VV_ERRORS, list_alloc());
9216 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9217}
9218
Bram Moolenaar43345542015-11-29 17:35:35 +01009219/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009220 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009221 */
9222 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009223f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009224 typval_T *argvars;
9225 typval_T *rettv UNUSED;
9226{
9227 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009228
9229 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9230 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009231 prepare_assert_error(&ga);
9232 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9233 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009234 ga_clear(&ga);
9235 }
9236}
9237
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009238/*
9239 * Common for assert_true() and assert_false().
9240 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009241 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009242assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009243 typval_T *argvars;
9244 int isTrue;
9245{
9246 int error = FALSE;
9247 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009248
9249 if (argvars[0].v_type != VAR_NUMBER
9250 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9251 || error)
9252 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009253 prepare_assert_error(&ga);
9254 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009255 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256 NULL, &argvars[0]);
9257 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009258 ga_clear(&ga);
9259 }
9260}
9261
9262/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009263 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009264 */
9265 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009266f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009267 typval_T *argvars;
9268 typval_T *rettv UNUSED;
9269{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009270 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009271}
9272
9273/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009274 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009275 */
9276 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009277f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009278 typval_T *argvars;
9279 typval_T *rettv UNUSED;
9280{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009281 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009282}
9283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009284#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009285/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009286 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009287 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009288 static void
9289f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009290 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009291 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009292{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009293 float_T f;
9294
9295 rettv->v_type = VAR_FLOAT;
9296 if (get_float_arg(argvars, &f) == OK)
9297 rettv->vval.v_float = asin(f);
9298 else
9299 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009300}
9301
9302/*
9303 * "atan()" function
9304 */
9305 static void
9306f_atan(argvars, rettv)
9307 typval_T *argvars;
9308 typval_T *rettv;
9309{
9310 float_T f;
9311
9312 rettv->v_type = VAR_FLOAT;
9313 if (get_float_arg(argvars, &f) == OK)
9314 rettv->vval.v_float = atan(f);
9315 else
9316 rettv->vval.v_float = 0.0;
9317}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009318
9319/*
9320 * "atan2()" function
9321 */
9322 static void
9323f_atan2(argvars, rettv)
9324 typval_T *argvars;
9325 typval_T *rettv;
9326{
9327 float_T fx, fy;
9328
9329 rettv->v_type = VAR_FLOAT;
9330 if (get_float_arg(argvars, &fx) == OK
9331 && get_float_arg(&argvars[1], &fy) == OK)
9332 rettv->vval.v_float = atan2(fx, fy);
9333 else
9334 rettv->vval.v_float = 0.0;
9335}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009336#endif
9337
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338/*
9339 * "browse(save, title, initdir, default)" function
9340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009343 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
9346#ifdef FEAT_BROWSE
9347 int save;
9348 char_u *title;
9349 char_u *initdir;
9350 char_u *defname;
9351 char_u buf[NUMBUFLEN];
9352 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009353 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 save = get_tv_number_chk(&argvars[0], &error);
9356 title = get_tv_string_chk(&argvars[1]);
9357 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9358 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 if (error || title == NULL || initdir == NULL || defname == NULL)
9361 rettv->vval.v_string = NULL;
9362 else
9363 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009364 do_browse(save ? BROWSE_SAVE : 0,
9365 title, defname, NULL, initdir, NULL, curbuf);
9366#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009367 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009368#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009369 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009370}
9371
9372/*
9373 * "browsedir(title, initdir)" function
9374 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009377 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009378 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009379{
9380#ifdef FEAT_BROWSE
9381 char_u *title;
9382 char_u *initdir;
9383 char_u buf[NUMBUFLEN];
9384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009385 title = get_tv_string_chk(&argvars[0]);
9386 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 if (title == NULL || initdir == NULL)
9389 rettv->vval.v_string = NULL;
9390 else
9391 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009392 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397}
9398
Bram Moolenaar33570922005-01-25 22:26:29 +00009399static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009400
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401/*
9402 * Find a buffer by number or exact name.
9403 */
9404 static buf_T *
9405find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009406 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009410 if (avar->v_type == VAR_NUMBER)
9411 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009412 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009414 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009415 if (buf == NULL)
9416 {
9417 /* No full path name match, try a match with a URL or a "nofile"
9418 * buffer, these don't use the full path. */
9419 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9420 if (buf->b_fname != NULL
9421 && (path_with_url(buf->b_fname)
9422#ifdef FEAT_QUICKFIX
9423 || bt_nofile(buf)
9424#endif
9425 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009426 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009427 break;
9428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 }
9430 return buf;
9431}
9432
9433/*
9434 * "bufexists(expr)" function
9435 */
9436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009438 typval_T *argvars;
9439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442}
9443
9444/*
9445 * "buflisted(expr)" function
9446 */
9447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *argvars;
9450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 buf_T *buf;
9453
9454 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456}
9457
9458/*
9459 * "bufloaded(expr)" function
9460 */
9461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009462f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009463 typval_T *argvars;
9464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466 buf_T *buf;
9467
9468 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470}
9471
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009472static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009473
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474/*
9475 * Get buffer by number or pattern.
9476 */
9477 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009478get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009479 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009480 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 int save_magic;
9484 char_u *save_cpo;
9485 buf_T *buf;
9486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 if (tv->v_type == VAR_NUMBER)
9488 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009489 if (tv->v_type != VAR_STRING)
9490 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 if (name == NULL || *name == NUL)
9492 return curbuf;
9493 if (name[0] == '$' && name[1] == NUL)
9494 return lastbuf;
9495
9496 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9497 save_magic = p_magic;
9498 p_magic = TRUE;
9499 save_cpo = p_cpo;
9500 p_cpo = (char_u *)"";
9501
9502 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009503 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504
9505 p_magic = save_magic;
9506 p_cpo = save_cpo;
9507
9508 /* If not found, try expanding the name, like done for bufexists(). */
9509 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
9512 return buf;
9513}
9514
9515/*
9516 * "bufname(expr)" function
9517 */
9518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009520 typval_T *argvars;
9521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
9523 buf_T *buf;
9524
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009527 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 --emsg_off;
9534}
9535
9536/*
9537 * "bufnr(expr)" function
9538 */
9539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009541 typval_T *argvars;
9542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009545 int error = FALSE;
9546 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009548 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009550 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009551 --emsg_off;
9552
9553 /* If the buffer isn't found and the second argument is not zero create a
9554 * new buffer. */
9555 if (buf == NULL
9556 && argvars[1].v_type != VAR_UNKNOWN
9557 && get_tv_number_chk(&argvars[1], &error) != 0
9558 && !error
9559 && (name = get_tv_string_chk(&argvars[0])) != NULL
9560 && !error)
9561 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9562
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567}
9568
9569/*
9570 * "bufwinnr(nr)" function
9571 */
9572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009574 typval_T *argvars;
9575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
9577#ifdef FEAT_WINDOWS
9578 win_T *wp;
9579 int winnr = 0;
9580#endif
9581 buf_T *buf;
9582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009585 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586#ifdef FEAT_WINDOWS
9587 for (wp = firstwin; wp; wp = wp->w_next)
9588 {
9589 ++winnr;
9590 if (wp->w_buffer == buf)
9591 break;
9592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596#endif
9597 --emsg_off;
9598}
9599
9600/*
9601 * "byte2line(byte)" function
9602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
9608#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610#else
9611 long boff = 0;
9612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009613 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 (linenr_T)0, &boff);
9619#endif
9620}
9621
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009622 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009623byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009624 typval_T *argvars;
9625 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009626 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009627{
9628#ifdef FEAT_MBYTE
9629 char_u *t;
9630#endif
9631 char_u *str;
9632 long idx;
9633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 str = get_tv_string_chk(&argvars[0]);
9635 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009637 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009638 return;
9639
9640#ifdef FEAT_MBYTE
9641 t = str;
9642 for ( ; idx > 0; idx--)
9643 {
9644 if (*t == NUL) /* EOL reached */
9645 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009646 if (enc_utf8 && comp)
9647 t += utf_ptr2len(t);
9648 else
9649 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009650 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009651 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009652#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009653 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009655#endif
9656}
9657
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009658/*
9659 * "byteidx()" function
9660 */
9661 static void
9662f_byteidx(argvars, rettv)
9663 typval_T *argvars;
9664 typval_T *rettv;
9665{
9666 byteidx(argvars, rettv, FALSE);
9667}
9668
9669/*
9670 * "byteidxcomp()" function
9671 */
9672 static void
9673f_byteidxcomp(argvars, rettv)
9674 typval_T *argvars;
9675 typval_T *rettv;
9676{
9677 byteidx(argvars, rettv, TRUE);
9678}
9679
Bram Moolenaardb913952012-06-29 12:54:53 +02009680 int
9681func_call(name, args, selfdict, rettv)
9682 char_u *name;
9683 typval_T *args;
9684 dict_T *selfdict;
9685 typval_T *rettv;
9686{
9687 listitem_T *item;
9688 typval_T argv[MAX_FUNC_ARGS + 1];
9689 int argc = 0;
9690 int dummy;
9691 int r = 0;
9692
9693 for (item = args->vval.v_list->lv_first; item != NULL;
9694 item = item->li_next)
9695 {
9696 if (argc == MAX_FUNC_ARGS)
9697 {
9698 EMSG(_("E699: Too many arguments"));
9699 break;
9700 }
9701 /* Make a copy of each argument. This is needed to be able to set
9702 * v_lock to VAR_FIXED in the copy without changing the original list.
9703 */
9704 copy_tv(&item->li_tv, &argv[argc++]);
9705 }
9706
9707 if (item == NULL)
9708 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9709 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9710 &dummy, TRUE, selfdict);
9711
9712 /* Free the arguments. */
9713 while (argc > 0)
9714 clear_tv(&argv[--argc]);
9715
9716 return r;
9717}
9718
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009719/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009720 * "call(func, arglist)" function
9721 */
9722 static void
9723f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009724 typval_T *argvars;
9725 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009726{
9727 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009729
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009730 if (argvars[1].v_type != VAR_LIST)
9731 {
9732 EMSG(_(e_listreq));
9733 return;
9734 }
9735 if (argvars[1].vval.v_list == NULL)
9736 return;
9737
9738 if (argvars[0].v_type == VAR_FUNC)
9739 func = argvars[0].vval.v_string;
9740 else
9741 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 if (*func == NUL)
9743 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009744
Bram Moolenaare9a41262005-01-15 22:18:47 +00009745 if (argvars[2].v_type != VAR_UNKNOWN)
9746 {
9747 if (argvars[2].v_type != VAR_DICT)
9748 {
9749 EMSG(_(e_dictreq));
9750 return;
9751 }
9752 selfdict = argvars[2].vval.v_dict;
9753 }
9754
Bram Moolenaardb913952012-06-29 12:54:53 +02009755 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009756}
9757
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009758#ifdef FEAT_FLOAT
9759/*
9760 * "ceil({float})" function
9761 */
9762 static void
9763f_ceil(argvars, rettv)
9764 typval_T *argvars;
9765 typval_T *rettv;
9766{
9767 float_T f;
9768
9769 rettv->v_type = VAR_FLOAT;
9770 if (get_float_arg(argvars, &f) == OK)
9771 rettv->vval.v_float = ceil(f);
9772 else
9773 rettv->vval.v_float = 0.0;
9774}
9775#endif
9776
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009777/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009778 * "changenr()" function
9779 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009780 static void
9781f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009782 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009783 typval_T *rettv;
9784{
9785 rettv->vval.v_number = curbuf->b_u_seq_cur;
9786}
9787
9788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 * "char2nr(string)" function
9790 */
9791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 typval_T *argvars;
9794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
9796#ifdef FEAT_MBYTE
9797 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009798 {
9799 int utf8 = 0;
9800
9801 if (argvars[1].v_type != VAR_UNKNOWN)
9802 utf8 = get_tv_number_chk(&argvars[1], NULL);
9803
9804 if (utf8)
9805 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9806 else
9807 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 else
9810#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812}
9813
9814/*
9815 * "cindent(lnum)" function
9816 */
9817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009819 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822#ifdef FEAT_CINDENT
9823 pos_T pos;
9824 linenr_T lnum;
9825
9826 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9829 {
9830 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 curwin->w_cursor = pos;
9833 }
9834 else
9835#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837}
9838
9839/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009840 * "clearmatches()" function
9841 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009842 static void
9843f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009844 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009845 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009846{
9847#ifdef FEAT_SEARCH_EXTRA
9848 clear_matches(curwin);
9849#endif
9850}
9851
9852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 * "col(string)" function
9854 */
9855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009857 typval_T *argvars;
9858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859{
9860 colnr_T col = 0;
9861 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009862 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009864 fp = var2fpos(&argvars[0], FALSE, &fnum);
9865 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 {
9867 if (fp->col == MAXCOL)
9868 {
9869 /* '> can be MAXCOL, get the length of the line then */
9870 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009871 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 else
9873 col = MAXCOL;
9874 }
9875 else
9876 {
9877 col = fp->col + 1;
9878#ifdef FEAT_VIRTUALEDIT
9879 /* col(".") when the cursor is on the NUL at the end of the line
9880 * because of "coladd" can be seen as an extra column. */
9881 if (virtual_active() && fp == &curwin->w_cursor)
9882 {
9883 char_u *p = ml_get_cursor();
9884
9885 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9886 curwin->w_virtcol - curwin->w_cursor.coladd))
9887 {
9888# ifdef FEAT_MBYTE
9889 int l;
9890
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009891 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 col += l;
9893# else
9894 if (*p != NUL && p[1] == NUL)
9895 ++col;
9896# endif
9897 }
9898 }
9899#endif
9900 }
9901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903}
9904
Bram Moolenaar572cb562005-08-05 21:35:02 +00009905#if defined(FEAT_INS_EXPAND)
9906/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009907 * "complete()" function
9908 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009909 static void
9910f_complete(argvars, rettv)
9911 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009912 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009913{
9914 int startcol;
9915
9916 if ((State & INSERT) == 0)
9917 {
9918 EMSG(_("E785: complete() can only be used in Insert mode"));
9919 return;
9920 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009921
9922 /* Check for undo allowed here, because if something was already inserted
9923 * the line was already saved for undo and this check isn't done. */
9924 if (!undo_allowed())
9925 return;
9926
Bram Moolenaarade00832006-03-10 21:46:58 +00009927 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9928 {
9929 EMSG(_(e_invarg));
9930 return;
9931 }
9932
9933 startcol = get_tv_number_chk(&argvars[0], NULL);
9934 if (startcol <= 0)
9935 return;
9936
9937 set_completion(startcol - 1, argvars[1].vval.v_list);
9938}
9939
9940/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009941 * "complete_add()" function
9942 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009943 static void
9944f_complete_add(argvars, rettv)
9945 typval_T *argvars;
9946 typval_T *rettv;
9947{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009948 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009949}
9950
9951/*
9952 * "complete_check()" function
9953 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009954 static void
9955f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009956 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009957 typval_T *rettv;
9958{
9959 int saved = RedrawingDisabled;
9960
9961 RedrawingDisabled = 0;
9962 ins_compl_check_keys(0);
9963 rettv->vval.v_number = compl_interrupted;
9964 RedrawingDisabled = saved;
9965}
9966#endif
9967
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968/*
9969 * "confirm(message, buttons[, default [, type]])" function
9970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009973 typval_T *argvars UNUSED;
9974 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975{
9976#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9977 char_u *message;
9978 char_u *buttons = NULL;
9979 char_u buf[NUMBUFLEN];
9980 char_u buf2[NUMBUFLEN];
9981 int def = 1;
9982 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009983 char_u *typestr;
9984 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009986 message = get_tv_string_chk(&argvars[0]);
9987 if (message == NULL)
9988 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009989 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009991 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9992 if (buttons == NULL)
9993 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009994 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009996 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009997 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009999 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10000 if (typestr == NULL)
10001 error = TRUE;
10002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010004 switch (TOUPPER_ASC(*typestr))
10005 {
10006 case 'E': type = VIM_ERROR; break;
10007 case 'Q': type = VIM_QUESTION; break;
10008 case 'I': type = VIM_INFO; break;
10009 case 'W': type = VIM_WARNING; break;
10010 case 'G': type = VIM_GENERIC; break;
10011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 }
10013 }
10014 }
10015 }
10016
10017 if (buttons == NULL || *buttons == NUL)
10018 buttons = (char_u *)_("&Ok");
10019
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010020 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010021 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010022 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023#endif
10024}
10025
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010026/*
10027 * "copy()" function
10028 */
10029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010031 typval_T *argvars;
10032 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010033{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010034 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010035}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010037#ifdef FEAT_FLOAT
10038/*
10039 * "cos()" function
10040 */
10041 static void
10042f_cos(argvars, rettv)
10043 typval_T *argvars;
10044 typval_T *rettv;
10045{
10046 float_T f;
10047
10048 rettv->v_type = VAR_FLOAT;
10049 if (get_float_arg(argvars, &f) == OK)
10050 rettv->vval.v_float = cos(f);
10051 else
10052 rettv->vval.v_float = 0.0;
10053}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010054
10055/*
10056 * "cosh()" function
10057 */
10058 static void
10059f_cosh(argvars, rettv)
10060 typval_T *argvars;
10061 typval_T *rettv;
10062{
10063 float_T f;
10064
10065 rettv->v_type = VAR_FLOAT;
10066 if (get_float_arg(argvars, &f) == OK)
10067 rettv->vval.v_float = cosh(f);
10068 else
10069 rettv->vval.v_float = 0.0;
10070}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010071#endif
10072
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010074 * "count()" function
10075 */
10076 static void
10077f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010078 typval_T *argvars;
10079 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010080{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010081 long n = 0;
10082 int ic = FALSE;
10083
Bram Moolenaare9a41262005-01-15 22:18:47 +000010084 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010085 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010086 listitem_T *li;
10087 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010088 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010089
Bram Moolenaare9a41262005-01-15 22:18:47 +000010090 if ((l = argvars[0].vval.v_list) != NULL)
10091 {
10092 li = l->lv_first;
10093 if (argvars[2].v_type != VAR_UNKNOWN)
10094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010095 int error = FALSE;
10096
10097 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010098 if (argvars[3].v_type != VAR_UNKNOWN)
10099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 idx = get_tv_number_chk(&argvars[3], &error);
10101 if (!error)
10102 {
10103 li = list_find(l, idx);
10104 if (li == NULL)
10105 EMSGN(_(e_listidx), idx);
10106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010108 if (error)
10109 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010110 }
10111
10112 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010113 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010114 ++n;
10115 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010116 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010117 else if (argvars[0].v_type == VAR_DICT)
10118 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010119 int todo;
10120 dict_T *d;
10121 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010122
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010123 if ((d = argvars[0].vval.v_dict) != NULL)
10124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125 int error = FALSE;
10126
Bram Moolenaare9a41262005-01-15 22:18:47 +000010127 if (argvars[2].v_type != VAR_UNKNOWN)
10128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010130 if (argvars[3].v_type != VAR_UNKNOWN)
10131 EMSG(_(e_invarg));
10132 }
10133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010134 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010135 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010136 {
10137 if (!HASHITEM_EMPTY(hi))
10138 {
10139 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010140 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010141 ++n;
10142 }
10143 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010144 }
10145 }
10146 else
10147 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010148 rettv->vval.v_number = n;
10149}
10150
10151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10153 *
10154 * Checks the existence of a cscope connection.
10155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010158 typval_T *argvars UNUSED;
10159 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160{
10161#ifdef FEAT_CSCOPE
10162 int num = 0;
10163 char_u *dbpath = NULL;
10164 char_u *prepend = NULL;
10165 char_u buf[NUMBUFLEN];
10166
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010167 if (argvars[0].v_type != VAR_UNKNOWN
10168 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 num = (int)get_tv_number(&argvars[0]);
10171 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010172 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 }
10175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010176 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177#endif
10178}
10179
10180/*
10181 * "cursor(lnum, col)" function
10182 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010183 * Moves the cursor to the specified line and column.
10184 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 typval_T *argvars;
10189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010192#ifdef FEAT_VIRTUALEDIT
10193 long coladd = 0;
10194#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010195 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010197 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010198 if (argvars[1].v_type == VAR_UNKNOWN)
10199 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010200 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010201 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010202
Bram Moolenaar493c1782014-05-28 14:34:46 +020010203 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010204 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010205 line = pos.lnum;
10206 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010207#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010208 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010209#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010210 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010211 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010212 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010213 set_curswant = FALSE;
10214 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010215 }
10216 else
10217 {
10218 line = get_tv_lnum(argvars);
10219 col = get_tv_number_chk(&argvars[1], NULL);
10220#ifdef FEAT_VIRTUALEDIT
10221 if (argvars[2].v_type != VAR_UNKNOWN)
10222 coladd = get_tv_number_chk(&argvars[2], NULL);
10223#endif
10224 }
10225 if (line < 0 || col < 0
10226#ifdef FEAT_VIRTUALEDIT
10227 || coladd < 0
10228#endif
10229 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 if (line > 0)
10232 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 if (col > 0)
10234 curwin->w_cursor.col = col - 1;
10235#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010236 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237#endif
10238
10239 /* Make sure the cursor is in a valid position. */
10240 check_cursor();
10241#ifdef FEAT_MBYTE
10242 /* Correct cursor for multi-byte character. */
10243 if (has_mbyte)
10244 mb_adjust_cursor();
10245#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010246
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010247 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010248 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249}
10250
10251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010252 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 */
10254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010256 typval_T *argvars;
10257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010259 int noref = 0;
10260
10261 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010263 if (noref < 0 || noref > 1)
10264 EMSG(_(e_invarg));
10265 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010266 {
10267 current_copyID += COPYID_INC;
10268 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270}
10271
10272/*
10273 * "delete()" function
10274 */
10275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010276f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010277 typval_T *argvars;
10278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279{
10280 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010281 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010283 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284}
10285
10286/*
10287 * "did_filetype()" function
10288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010290f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010291 typval_T *argvars UNUSED;
10292 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293{
10294#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296#endif
10297}
10298
10299/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010300 * "diff_filler()" function
10301 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010304 typval_T *argvars UNUSED;
10305 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010306{
10307#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010309#endif
10310}
10311
10312/*
10313 * "diff_hlID()" function
10314 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010317 typval_T *argvars UNUSED;
10318 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010319{
10320#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010321 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010322 static linenr_T prev_lnum = 0;
10323 static int changedtick = 0;
10324 static int fnum = 0;
10325 static int change_start = 0;
10326 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010327 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010328 int filler_lines;
10329 int col;
10330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010331 if (lnum < 0) /* ignore type error in {lnum} arg */
10332 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010333 if (lnum != prev_lnum
10334 || changedtick != curbuf->b_changedtick
10335 || fnum != curbuf->b_fnum)
10336 {
10337 /* New line, buffer, change: need to get the values. */
10338 filler_lines = diff_check(curwin, lnum);
10339 if (filler_lines < 0)
10340 {
10341 if (filler_lines == -1)
10342 {
10343 change_start = MAXCOL;
10344 change_end = -1;
10345 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10346 hlID = HLF_ADD; /* added line */
10347 else
10348 hlID = HLF_CHD; /* changed line */
10349 }
10350 else
10351 hlID = HLF_ADD; /* added line */
10352 }
10353 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010354 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010355 prev_lnum = lnum;
10356 changedtick = curbuf->b_changedtick;
10357 fnum = curbuf->b_fnum;
10358 }
10359
10360 if (hlID == HLF_CHD || hlID == HLF_TXD)
10361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010362 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010363 if (col >= change_start && col <= change_end)
10364 hlID = HLF_TXD; /* changed text */
10365 else
10366 hlID = HLF_CHD; /* changed line */
10367 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010368 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010369#endif
10370}
10371
10372/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010373 * "empty({expr})" function
10374 */
10375 static void
10376f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010379{
10380 int n;
10381
10382 switch (argvars[0].v_type)
10383 {
10384 case VAR_STRING:
10385 case VAR_FUNC:
10386 n = argvars[0].vval.v_string == NULL
10387 || *argvars[0].vval.v_string == NUL;
10388 break;
10389 case VAR_NUMBER:
10390 n = argvars[0].vval.v_number == 0;
10391 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010392#ifdef FEAT_FLOAT
10393 case VAR_FLOAT:
10394 n = argvars[0].vval.v_float == 0.0;
10395 break;
10396#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010397 case VAR_LIST:
10398 n = argvars[0].vval.v_list == NULL
10399 || argvars[0].vval.v_list->lv_first == NULL;
10400 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010401 case VAR_DICT:
10402 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010403 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010404 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010405 default:
10406 EMSG2(_(e_intern2), "f_empty()");
10407 n = 0;
10408 }
10409
10410 rettv->vval.v_number = n;
10411}
10412
10413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 * "escape({string}, {chars})" function
10415 */
10416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 typval_T *argvars;
10419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420{
10421 char_u buf[NUMBUFLEN];
10422
Bram Moolenaar758711c2005-02-02 23:11:38 +000010423 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10424 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426}
10427
10428/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010429 * "eval()" function
10430 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010431 static void
10432f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010433 typval_T *argvars;
10434 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010435{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010436 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 s = get_tv_string_chk(&argvars[0]);
10439 if (s != NULL)
10440 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010441
Bram Moolenaar615b9972015-01-14 17:15:05 +010010442 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10444 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010445 if (p != NULL && !aborting())
10446 EMSG2(_(e_invexpr2), p);
10447 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010449 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010451 else if (*s != NUL)
10452 EMSG(_(e_trailing));
10453}
10454
10455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 * "eventhandler()" function
10457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010460 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464}
10465
10466/*
10467 * "executable()" function
10468 */
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010474 char_u *name = get_tv_string(&argvars[0]);
10475
10476 /* Check in $PATH and also check directly if there is a directory name. */
10477 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10478 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010479}
10480
10481/*
10482 * "exepath()" function
10483 */
10484 static void
10485f_exepath(argvars, rettv)
10486 typval_T *argvars;
10487 typval_T *rettv;
10488{
10489 char_u *p = NULL;
10490
Bram Moolenaarb5971142015-03-21 17:32:19 +010010491 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010492 rettv->v_type = VAR_STRING;
10493 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494}
10495
10496/*
10497 * "exists()" function
10498 */
10499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010501 typval_T *argvars;
10502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 char_u *p;
10505 char_u *name;
10506 int n = FALSE;
10507 int len = 0;
10508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 if (*p == '$') /* environment variable */
10511 {
10512 /* first try "normal" environment variables (fast) */
10513 if (mch_getenv(p + 1) != NULL)
10514 n = TRUE;
10515 else
10516 {
10517 /* try expanding things like $VIM and ${HOME} */
10518 p = expand_env_save(p);
10519 if (p != NULL && *p != '$')
10520 n = TRUE;
10521 vim_free(p);
10522 }
10523 }
10524 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010527 if (*skipwhite(p) != NUL)
10528 n = FALSE; /* trailing garbage */
10529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 else if (*p == '*') /* internal or user defined function */
10531 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010532 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 }
10534 else if (*p == ':')
10535 {
10536 n = cmd_exists(p + 1);
10537 }
10538 else if (*p == '#')
10539 {
10540#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010541 if (p[1] == '#')
10542 n = autocmd_supported(p + 2);
10543 else
10544 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545#endif
10546 }
10547 else /* internal variable */
10548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010549 char_u *tofree;
10550 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010552 /* get_name_len() takes care of expanding curly braces */
10553 name = p;
10554 len = get_name_len(&p, &tofree, TRUE, FALSE);
10555 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010557 if (tofree != NULL)
10558 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010559 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010560 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010562 /* handle d.key, l[idx], f(expr) */
10563 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10564 if (n)
10565 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 }
10567 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010568 if (*p != NUL)
10569 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 }
10573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575}
10576
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010577#ifdef FEAT_FLOAT
10578/*
10579 * "exp()" function
10580 */
10581 static void
10582f_exp(argvars, rettv)
10583 typval_T *argvars;
10584 typval_T *rettv;
10585{
10586 float_T f;
10587
10588 rettv->v_type = VAR_FLOAT;
10589 if (get_float_arg(argvars, &f) == OK)
10590 rettv->vval.v_float = exp(f);
10591 else
10592 rettv->vval.v_float = 0.0;
10593}
10594#endif
10595
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596/*
10597 * "expand()" function
10598 */
10599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *argvars;
10602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 char_u *s;
10605 int len;
10606 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010607 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010610 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010612 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010613 if (argvars[1].v_type != VAR_UNKNOWN
10614 && argvars[2].v_type != VAR_UNKNOWN
10615 && get_tv_number_chk(&argvars[2], &error)
10616 && !error)
10617 {
10618 rettv->v_type = VAR_LIST;
10619 rettv->vval.v_list = NULL;
10620 }
10621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 if (*s == '%' || *s == '#' || *s == '<')
10624 {
10625 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010626 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010628 if (rettv->v_type == VAR_LIST)
10629 {
10630 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10631 list_append_string(rettv->vval.v_list, result, -1);
10632 else
10633 vim_free(result);
10634 }
10635 else
10636 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 }
10638 else
10639 {
10640 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010641 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 if (argvars[1].v_type != VAR_UNKNOWN
10643 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010644 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 if (!error)
10646 {
10647 ExpandInit(&xpc);
10648 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010649 if (p_wic)
10650 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010651 if (rettv->v_type == VAR_STRING)
10652 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10653 options, WILD_ALL);
10654 else if (rettv_list_alloc(rettv) != FAIL)
10655 {
10656 int i;
10657
10658 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10659 for (i = 0; i < xpc.xp_numfiles; i++)
10660 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10661 ExpandCleanup(&xpc);
10662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010663 }
10664 else
10665 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 }
10667}
10668
10669/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010670 * Go over all entries in "d2" and add them to "d1".
10671 * When "action" is "error" then a duplicate key is an error.
10672 * When "action" is "force" then a duplicate key is overwritten.
10673 * Otherwise duplicate keys are ignored ("action" is "keep").
10674 */
10675 void
10676dict_extend(d1, d2, action)
10677 dict_T *d1;
10678 dict_T *d2;
10679 char_u *action;
10680{
10681 dictitem_T *di1;
10682 hashitem_T *hi2;
10683 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010684 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010685
10686 todo = (int)d2->dv_hashtab.ht_used;
10687 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10688 {
10689 if (!HASHITEM_EMPTY(hi2))
10690 {
10691 --todo;
10692 di1 = dict_find(d1, hi2->hi_key, -1);
10693 if (d1->dv_scope != 0)
10694 {
10695 /* Disallow replacing a builtin function in l: and g:.
10696 * Check the key to be valid when adding to any
10697 * scope. */
10698 if (d1->dv_scope == VAR_DEF_SCOPE
10699 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10700 && var_check_func_name(hi2->hi_key,
10701 di1 == NULL))
10702 break;
10703 if (!valid_varname(hi2->hi_key))
10704 break;
10705 }
10706 if (di1 == NULL)
10707 {
10708 di1 = dictitem_copy(HI2DI(hi2));
10709 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10710 dictitem_free(di1);
10711 }
10712 else if (*action == 'e')
10713 {
10714 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10715 break;
10716 }
10717 else if (*action == 'f' && HI2DI(hi2) != di1)
10718 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010719 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10720 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010721 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010722 clear_tv(&di1->di_tv);
10723 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10724 }
10725 }
10726 }
10727}
10728
10729/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010730 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010731 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010732 */
10733 static void
10734f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010735 typval_T *argvars;
10736 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010737{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010738 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010739
Bram Moolenaare9a41262005-01-15 22:18:47 +000010740 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010741 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 list_T *l1, *l2;
10743 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010744 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010745 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010746
Bram Moolenaare9a41262005-01-15 22:18:47 +000010747 l1 = argvars[0].vval.v_list;
10748 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010749 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010750 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010751 {
10752 if (argvars[2].v_type != VAR_UNKNOWN)
10753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 before = get_tv_number_chk(&argvars[2], &error);
10755 if (error)
10756 return; /* type error; errmsg already given */
10757
Bram Moolenaar758711c2005-02-02 23:11:38 +000010758 if (before == l1->lv_len)
10759 item = NULL;
10760 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010761 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010762 item = list_find(l1, before);
10763 if (item == NULL)
10764 {
10765 EMSGN(_(e_listidx), before);
10766 return;
10767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010768 }
10769 }
10770 else
10771 item = NULL;
10772 list_extend(l1, l2, item);
10773
Bram Moolenaare9a41262005-01-15 22:18:47 +000010774 copy_tv(&argvars[0], rettv);
10775 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010777 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10778 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010779 dict_T *d1, *d2;
10780 char_u *action;
10781 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782
10783 d1 = argvars[0].vval.v_dict;
10784 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010785 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010786 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 {
10788 /* Check the third argument. */
10789 if (argvars[2].v_type != VAR_UNKNOWN)
10790 {
10791 static char *(av[]) = {"keep", "force", "error"};
10792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010793 action = get_tv_string_chk(&argvars[2]);
10794 if (action == NULL)
10795 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010796 for (i = 0; i < 3; ++i)
10797 if (STRCMP(action, av[i]) == 0)
10798 break;
10799 if (i == 3)
10800 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010801 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010802 return;
10803 }
10804 }
10805 else
10806 action = (char_u *)"force";
10807
Bram Moolenaara9922d62013-05-30 13:01:18 +020010808 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809
Bram Moolenaare9a41262005-01-15 22:18:47 +000010810 copy_tv(&argvars[0], rettv);
10811 }
10812 }
10813 else
10814 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010815}
10816
10817/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010818 * "feedkeys()" function
10819 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010820 static void
10821f_feedkeys(argvars, rettv)
10822 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010823 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010824{
10825 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010826 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010827 char_u *keys, *flags;
10828 char_u nbuf[NUMBUFLEN];
10829 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010830 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010831
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010832 /* This is not allowed in the sandbox. If the commands would still be
10833 * executed in the sandbox it would be OK, but it probably happens later,
10834 * when "sandbox" is no longer set. */
10835 if (check_secure())
10836 return;
10837
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010838 keys = get_tv_string(&argvars[0]);
10839 if (*keys != NUL)
10840 {
10841 if (argvars[1].v_type != VAR_UNKNOWN)
10842 {
10843 flags = get_tv_string_buf(&argvars[1], nbuf);
10844 for ( ; *flags != NUL; ++flags)
10845 {
10846 switch (*flags)
10847 {
10848 case 'n': remap = FALSE; break;
10849 case 'm': remap = TRUE; break;
10850 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010851 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010852 }
10853 }
10854 }
10855
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010856 /* Need to escape K_SPECIAL and CSI before putting the string in the
10857 * typeahead buffer. */
10858 keys_esc = vim_strsave_escape_csi(keys);
10859 if (keys_esc != NULL)
10860 {
10861 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010862 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010863 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010864 if (vgetc_busy)
10865 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010866 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010867 }
10868}
10869
10870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 * "filereadable()" function
10872 */
10873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010875 typval_T *argvars;
10876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010878 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 char_u *p;
10880 int n;
10881
Bram Moolenaarc236c162008-07-13 17:41:49 +000010882#ifndef O_NONBLOCK
10883# define O_NONBLOCK 0
10884#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010886 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10887 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 {
10889 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010890 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 }
10892 else
10893 n = FALSE;
10894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896}
10897
10898/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010899 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 * rights to write into.
10901 */
10902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010904 typval_T *argvars;
10905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010907 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908}
10909
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010910static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010911
10912 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010913findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010914 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010915 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010916 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010917{
10918#ifdef FEAT_SEARCHPATH
10919 char_u *fname;
10920 char_u *fresult = NULL;
10921 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10922 char_u *p;
10923 char_u pathbuf[NUMBUFLEN];
10924 int count = 1;
10925 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010926 int error = FALSE;
10927#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010928
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010929 rettv->vval.v_string = NULL;
10930 rettv->v_type = VAR_STRING;
10931
10932#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010934
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010935 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010937 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10938 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010939 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 else
10941 {
10942 if (*p != NUL)
10943 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010946 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010948 }
10949
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010950 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10951 error = TRUE;
10952
10953 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 do
10956 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010957 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010958 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 fresult = find_file_in_path_option(first ? fname : NULL,
10960 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010961 0, first, path,
10962 find_what,
10963 curbuf->b_ffname,
10964 find_what == FINDFILE_DIR
10965 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010966 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010967
10968 if (fresult != NULL && rettv->v_type == VAR_LIST)
10969 list_append_string(rettv->vval.v_list, fresult, -1);
10970
10971 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010973
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010974 if (rettv->v_type == VAR_STRING)
10975 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010976#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010977}
10978
Bram Moolenaar33570922005-01-25 22:26:29 +000010979static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10980static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010981
10982/*
10983 * Implementation of map() and filter().
10984 */
10985 static void
10986filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 typval_T *argvars;
10988 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010989 int map;
10990{
10991 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010993 listitem_T *li, *nli;
10994 list_T *l = NULL;
10995 dictitem_T *di;
10996 hashtab_T *ht;
10997 hashitem_T *hi;
10998 dict_T *d = NULL;
10999 typval_T save_val;
11000 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011002 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011003 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011004 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011005 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011006 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011007 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011008
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011010 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011011 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011012 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011013 return;
11014 }
11015 else if (argvars[0].v_type == VAR_DICT)
11016 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011017 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011018 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019 return;
11020 }
11021 else
11022 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011023 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011024 return;
11025 }
11026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011027 expr = get_tv_string_buf_chk(&argvars[1], buf);
11028 /* On type errors, the preceding call has already displayed an error
11029 * message. Avoid a misleading error message for an empty string that
11030 * was not passed as argument. */
11031 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011033 prepare_vimvar(VV_VAL, &save_val);
11034 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011035
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011036 /* We reset "did_emsg" to be able to detect whether an error
11037 * occurred during evaluation of the expression. */
11038 save_did_emsg = did_emsg;
11039 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011040
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011041 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 vimvars[VV_KEY].vv_type = VAR_STRING;
11045
11046 ht = &d->dv_hashtab;
11047 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011048 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011049 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 if (!HASHITEM_EMPTY(hi))
11052 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011053 int r;
11054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 --todo;
11056 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011057 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011058 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11059 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 break;
11061 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011062 r = filter_map_one(&di->di_tv, expr, map, &rem);
11063 clear_tv(&vimvars[VV_KEY].vv_tv);
11064 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 break;
11066 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011067 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011068 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11069 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011070 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 }
11074 }
11075 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 }
11077 else
11078 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011079 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 for (li = l->lv_first; li != NULL; li = nli)
11082 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011083 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011084 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011086 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011087 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011088 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011089 break;
11090 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011091 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011092 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011093 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011094 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011095
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011096 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011098
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011099 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011100 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101
11102 copy_tv(&argvars[0], rettv);
11103}
11104
11105 static int
11106filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011107 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011108 char_u *expr;
11109 int map;
11110 int *remp;
11111{
Bram Moolenaar33570922005-01-25 22:26:29 +000011112 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011114 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011115
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 s = expr;
11118 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011119 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011120 if (*s != NUL) /* check for trailing chars after expr */
11121 {
11122 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011123 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011124 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 }
11126 if (map)
11127 {
11128 /* map(): replace the list item value */
11129 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011130 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011131 *tv = rettv;
11132 }
11133 else
11134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135 int error = FALSE;
11136
Bram Moolenaare9a41262005-01-15 22:18:47 +000011137 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011138 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011139 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 /* On type error, nothing has been removed; return FAIL to stop the
11141 * loop. The error message was given by get_tv_number_chk(). */
11142 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011143 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011144 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011145 retval = OK;
11146theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011148 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011149}
11150
11151/*
11152 * "filter()" function
11153 */
11154 static void
11155f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011158{
11159 filter_map(argvars, rettv, FALSE);
11160}
11161
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011162/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011163 * "finddir({fname}[, {path}[, {count}]])" function
11164 */
11165 static void
11166f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 typval_T *argvars;
11168 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011169{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011170 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011171}
11172
11173/*
11174 * "findfile({fname}[, {path}[, {count}]])" function
11175 */
11176 static void
11177f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011178 typval_T *argvars;
11179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011181 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182}
11183
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011184#ifdef FEAT_FLOAT
11185/*
11186 * "float2nr({float})" function
11187 */
11188 static void
11189f_float2nr(argvars, rettv)
11190 typval_T *argvars;
11191 typval_T *rettv;
11192{
11193 float_T f;
11194
11195 if (get_float_arg(argvars, &f) == OK)
11196 {
11197 if (f < -0x7fffffff)
11198 rettv->vval.v_number = -0x7fffffff;
11199 else if (f > 0x7fffffff)
11200 rettv->vval.v_number = 0x7fffffff;
11201 else
11202 rettv->vval.v_number = (varnumber_T)f;
11203 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011204}
11205
11206/*
11207 * "floor({float})" function
11208 */
11209 static void
11210f_floor(argvars, rettv)
11211 typval_T *argvars;
11212 typval_T *rettv;
11213{
11214 float_T f;
11215
11216 rettv->v_type = VAR_FLOAT;
11217 if (get_float_arg(argvars, &f) == OK)
11218 rettv->vval.v_float = floor(f);
11219 else
11220 rettv->vval.v_float = 0.0;
11221}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011222
11223/*
11224 * "fmod()" function
11225 */
11226 static void
11227f_fmod(argvars, rettv)
11228 typval_T *argvars;
11229 typval_T *rettv;
11230{
11231 float_T fx, fy;
11232
11233 rettv->v_type = VAR_FLOAT;
11234 if (get_float_arg(argvars, &fx) == OK
11235 && get_float_arg(&argvars[1], &fy) == OK)
11236 rettv->vval.v_float = fmod(fx, fy);
11237 else
11238 rettv->vval.v_float = 0.0;
11239}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011240#endif
11241
Bram Moolenaar0d660222005-01-07 21:51:51 +000011242/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011243 * "fnameescape({string})" function
11244 */
11245 static void
11246f_fnameescape(argvars, rettv)
11247 typval_T *argvars;
11248 typval_T *rettv;
11249{
11250 rettv->vval.v_string = vim_strsave_fnameescape(
11251 get_tv_string(&argvars[0]), FALSE);
11252 rettv->v_type = VAR_STRING;
11253}
11254
11255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 * "fnamemodify({fname}, {mods})" function
11257 */
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
11263 char_u *fname;
11264 char_u *mods;
11265 int usedlen = 0;
11266 int len;
11267 char_u *fbuf = NULL;
11268 char_u buf[NUMBUFLEN];
11269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 fname = get_tv_string_chk(&argvars[0]);
11271 mods = get_tv_string_buf_chk(&argvars[1], buf);
11272 if (fname == NULL || mods == NULL)
11273 fname = NULL;
11274 else
11275 {
11276 len = (int)STRLEN(fname);
11277 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 vim_free(fbuf);
11286}
11287
Bram Moolenaar33570922005-01-25 22:26:29 +000011288static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289
11290/*
11291 * "foldclosed()" function
11292 */
11293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011295 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011297 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298{
11299#ifdef FEAT_FOLDING
11300 linenr_T lnum;
11301 linenr_T first, last;
11302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11305 {
11306 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11307 {
11308 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 return;
11313 }
11314 }
11315#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317}
11318
11319/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011320 * "foldclosed()" function
11321 */
11322 static void
11323f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *argvars;
11325 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011326{
11327 foldclosed_both(argvars, rettv, FALSE);
11328}
11329
11330/*
11331 * "foldclosedend()" function
11332 */
11333 static void
11334f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011335 typval_T *argvars;
11336 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011337{
11338 foldclosed_both(argvars, rettv, TRUE);
11339}
11340
11341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 * "foldlevel()" function
11343 */
11344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011346 typval_T *argvars UNUSED;
11347 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348{
11349#ifdef FEAT_FOLDING
11350 linenr_T lnum;
11351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356}
11357
11358/*
11359 * "foldtext()" function
11360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011363 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
11366#ifdef FEAT_FOLDING
11367 linenr_T lnum;
11368 char_u *s;
11369 char_u *r;
11370 int len;
11371 char *txt;
11372#endif
11373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374 rettv->v_type = VAR_STRING;
11375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011377 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11378 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11379 <= curbuf->b_ml.ml_line_count
11380 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 {
11382 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011383 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11384 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 {
11386 if (!linewhite(lnum))
11387 break;
11388 ++lnum;
11389 }
11390
11391 /* Find interesting text in this line. */
11392 s = skipwhite(ml_get(lnum));
11393 /* skip C comment-start */
11394 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011397 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011398 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011399 {
11400 s = skipwhite(ml_get(lnum + 1));
11401 if (*s == '*')
11402 s = skipwhite(s + 1);
11403 }
11404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 txt = _("+-%s%3ld lines: ");
11406 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011407 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 + 20 /* for %3ld */
11409 + STRLEN(s))); /* concatenated */
11410 if (r != NULL)
11411 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011412 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11413 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11414 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 len = (int)STRLEN(r);
11416 STRCAT(r, s);
11417 /* remove 'foldmarker' and 'commentstring' */
11418 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 }
11421 }
11422#endif
11423}
11424
11425/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011426 * "foldtextresult(lnum)" function
11427 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011430 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011432{
11433#ifdef FEAT_FOLDING
11434 linenr_T lnum;
11435 char_u *text;
11436 char_u buf[51];
11437 foldinfo_T foldinfo;
11438 int fold_count;
11439#endif
11440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->v_type = VAR_STRING;
11442 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011443#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011445 /* treat illegal types and illegal string values for {lnum} the same */
11446 if (lnum < 0)
11447 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011448 fold_count = foldedCount(curwin, lnum, &foldinfo);
11449 if (fold_count > 0)
11450 {
11451 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11452 &foldinfo, buf);
11453 if (text == buf)
11454 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011456 }
11457#endif
11458}
11459
11460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 * "foreground()" function
11462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011465 typval_T *argvars UNUSED;
11466 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468#ifdef FEAT_GUI
11469 if (gui.in_use)
11470 gui_mch_set_foreground();
11471#else
11472# ifdef WIN32
11473 win32_set_foreground();
11474# endif
11475#endif
11476}
11477
11478/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011479 * "function()" function
11480 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011483 typval_T *argvars;
11484 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011485{
11486 char_u *s;
11487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011489 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011490 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011491 /* Don't check an autoload name for existence here. */
11492 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011493 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011494 else
11495 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011496 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011497 {
11498 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011499 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011500
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011501 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11502 * also be called from another script. Using trans_function_name()
11503 * would also work, but some plugins depend on the name being
11504 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011505 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011506 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011507 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011508 if (rettv->vval.v_string != NULL)
11509 {
11510 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011511 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011512 }
11513 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011514 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011515 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011517 }
11518}
11519
11520/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011521 * "garbagecollect()" function
11522 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011523 static void
11524f_garbagecollect(argvars, rettv)
11525 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011526 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011527{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011528 /* This is postponed until we are back at the toplevel, because we may be
11529 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11530 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011531
11532 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11533 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011534}
11535
11536/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011537 * "get()" function
11538 */
11539 static void
11540f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011541 typval_T *argvars;
11542 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011543{
Bram Moolenaar33570922005-01-25 22:26:29 +000011544 listitem_T *li;
11545 list_T *l;
11546 dictitem_T *di;
11547 dict_T *d;
11548 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549
Bram Moolenaare9a41262005-01-15 22:18:47 +000011550 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011551 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011552 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 int error = FALSE;
11555
11556 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11557 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011558 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011559 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011560 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011561 else if (argvars[0].v_type == VAR_DICT)
11562 {
11563 if ((d = argvars[0].vval.v_dict) != NULL)
11564 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011565 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011566 if (di != NULL)
11567 tv = &di->di_tv;
11568 }
11569 }
11570 else
11571 EMSG2(_(e_listdictarg), "get()");
11572
11573 if (tv == NULL)
11574 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011575 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011576 copy_tv(&argvars[2], rettv);
11577 }
11578 else
11579 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011580}
11581
Bram Moolenaar342337a2005-07-21 21:11:17 +000011582static 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 +000011583
11584/*
11585 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011586 * Return a range (from start to end) of lines in rettv from the specified
11587 * buffer.
11588 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011589 */
11590 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011591get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011592 buf_T *buf;
11593 linenr_T start;
11594 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011595 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011596 typval_T *rettv;
11597{
11598 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011599
Bram Moolenaar959a1432013-12-14 12:17:38 +010011600 rettv->v_type = VAR_STRING;
11601 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011602 if (retlist && rettv_list_alloc(rettv) == FAIL)
11603 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011604
11605 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11606 return;
11607
11608 if (!retlist)
11609 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011610 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11611 p = ml_get_buf(buf, start, FALSE);
11612 else
11613 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011614 rettv->vval.v_string = vim_strsave(p);
11615 }
11616 else
11617 {
11618 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011619 return;
11620
11621 if (start < 1)
11622 start = 1;
11623 if (end > buf->b_ml.ml_line_count)
11624 end = buf->b_ml.ml_line_count;
11625 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011626 if (list_append_string(rettv->vval.v_list,
11627 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011628 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011629 }
11630}
11631
11632/*
11633 * "getbufline()" function
11634 */
11635 static void
11636f_getbufline(argvars, rettv)
11637 typval_T *argvars;
11638 typval_T *rettv;
11639{
11640 linenr_T lnum;
11641 linenr_T end;
11642 buf_T *buf;
11643
11644 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11645 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011646 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011647 --emsg_off;
11648
Bram Moolenaar661b1822005-07-28 22:36:45 +000011649 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011650 if (argvars[2].v_type == VAR_UNKNOWN)
11651 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011652 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011653 end = get_tv_lnum_buf(&argvars[2], buf);
11654
Bram Moolenaar342337a2005-07-21 21:11:17 +000011655 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011656}
11657
Bram Moolenaar0d660222005-01-07 21:51:51 +000011658/*
11659 * "getbufvar()" function
11660 */
11661 static void
11662f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011663 typval_T *argvars;
11664 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011665{
11666 buf_T *buf;
11667 buf_T *save_curbuf;
11668 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011669 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011670 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11673 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011674 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011675 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011676
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011677 rettv->v_type = VAR_STRING;
11678 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011679
11680 if (buf != NULL && varname != NULL)
11681 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011682 /* set curbuf to be our buf, temporarily */
11683 save_curbuf = curbuf;
11684 curbuf = buf;
11685
Bram Moolenaar0d660222005-01-07 21:51:51 +000011686 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011687 {
11688 if (get_option_tv(&varname, rettv, TRUE) == OK)
11689 done = TRUE;
11690 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011691 else if (STRCMP(varname, "changedtick") == 0)
11692 {
11693 rettv->v_type = VAR_NUMBER;
11694 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011695 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011696 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011697 else
11698 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011699 /* Look up the variable. */
11700 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11701 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11702 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011703 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011704 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011705 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011706 done = TRUE;
11707 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011708 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011709
11710 /* restore previous notion of curbuf */
11711 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011712 }
11713
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011714 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11715 /* use the default value */
11716 copy_tv(&argvars[2], rettv);
11717
Bram Moolenaar0d660222005-01-07 21:51:51 +000011718 --emsg_off;
11719}
11720
11721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 * "getchar()" function
11723 */
11724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011726 typval_T *argvars;
11727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728{
11729 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011732 /* Position the cursor. Needed after a message that ends in a space. */
11733 windgoto(msg_row, msg_col);
11734
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 ++no_mapping;
11736 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011737 for (;;)
11738 {
11739 if (argvars[0].v_type == VAR_UNKNOWN)
11740 /* getchar(): blocking wait. */
11741 n = safe_vgetc();
11742 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11743 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011744 n = vpeekc_any();
11745 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011746 /* illegal argument or getchar(0) and no char avail: return zero */
11747 n = 0;
11748 else
11749 /* getchar(0) and char avail: return char */
11750 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011751
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011752 if (n == K_IGNORE)
11753 continue;
11754 break;
11755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 --no_mapping;
11757 --allow_keys;
11758
Bram Moolenaar219b8702006-11-01 14:32:36 +000011759 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11760 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11761 vimvars[VV_MOUSE_COL].vv_nr = 0;
11762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 if (IS_SPECIAL(n) || mod_mask != 0)
11765 {
11766 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11767 int i = 0;
11768
11769 /* Turn a special key into three bytes, plus modifier. */
11770 if (mod_mask != 0)
11771 {
11772 temp[i++] = K_SPECIAL;
11773 temp[i++] = KS_MODIFIER;
11774 temp[i++] = mod_mask;
11775 }
11776 if (IS_SPECIAL(n))
11777 {
11778 temp[i++] = K_SPECIAL;
11779 temp[i++] = K_SECOND(n);
11780 temp[i++] = K_THIRD(n);
11781 }
11782#ifdef FEAT_MBYTE
11783 else if (has_mbyte)
11784 i += (*mb_char2bytes)(n, temp + i);
11785#endif
11786 else
11787 temp[i++] = n;
11788 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011789 rettv->v_type = VAR_STRING;
11790 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011791
11792#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011793 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011794 {
11795 int row = mouse_row;
11796 int col = mouse_col;
11797 win_T *win;
11798 linenr_T lnum;
11799# ifdef FEAT_WINDOWS
11800 win_T *wp;
11801# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011802 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011803
11804 if (row >= 0 && col >= 0)
11805 {
11806 /* Find the window at the mouse coordinates and compute the
11807 * text position. */
11808 win = mouse_find_win(&row, &col);
11809 (void)mouse_comp_pos(win, &row, &col, &lnum);
11810# ifdef FEAT_WINDOWS
11811 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011812 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011813# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011814 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011815 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11816 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11817 }
11818 }
11819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 }
11821}
11822
11823/*
11824 * "getcharmod()" function
11825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011828 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832}
11833
11834/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011835 * "getcharsearch()" function
11836 */
11837 static void
11838f_getcharsearch(argvars, rettv)
11839 typval_T *argvars UNUSED;
11840 typval_T *rettv;
11841{
11842 if (rettv_dict_alloc(rettv) != FAIL)
11843 {
11844 dict_T *dict = rettv->vval.v_dict;
11845
11846 dict_add_nr_str(dict, "char", 0L, last_csearch());
11847 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11848 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11849 }
11850}
11851
11852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 * "getcmdline()" function
11854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011857 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 rettv->v_type = VAR_STRING;
11861 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862}
11863
11864/*
11865 * "getcmdpos()" function
11866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011869 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873}
11874
11875/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011876 * "getcmdtype()" function
11877 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011878 static void
11879f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011880 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011881 typval_T *rettv;
11882{
11883 rettv->v_type = VAR_STRING;
11884 rettv->vval.v_string = alloc(2);
11885 if (rettv->vval.v_string != NULL)
11886 {
11887 rettv->vval.v_string[0] = get_cmdline_type();
11888 rettv->vval.v_string[1] = NUL;
11889 }
11890}
11891
11892/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011893 * "getcmdwintype()" function
11894 */
11895 static void
11896f_getcmdwintype(argvars, rettv)
11897 typval_T *argvars UNUSED;
11898 typval_T *rettv;
11899{
11900 rettv->v_type = VAR_STRING;
11901 rettv->vval.v_string = NULL;
11902#ifdef FEAT_CMDWIN
11903 rettv->vval.v_string = alloc(2);
11904 if (rettv->vval.v_string != NULL)
11905 {
11906 rettv->vval.v_string[0] = cmdwin_type;
11907 rettv->vval.v_string[1] = NUL;
11908 }
11909#endif
11910}
11911
11912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 * "getcwd()" function
11914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011920 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011922 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011923 rettv->vval.v_string = NULL;
11924 cwd = alloc(MAXPATHL);
11925 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011927 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11928 {
11929 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011931 if (rettv->vval.v_string != NULL)
11932 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011934 }
11935 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 }
11937}
11938
11939/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011940 * "getfontname()" function
11941 */
11942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011944 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011945 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947 rettv->v_type = VAR_STRING;
11948 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011949#ifdef FEAT_GUI
11950 if (gui.in_use)
11951 {
11952 GuiFont font;
11953 char_u *name = NULL;
11954
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011955 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011956 {
11957 /* Get the "Normal" font. Either the name saved by
11958 * hl_set_font_name() or from the font ID. */
11959 font = gui.norm_font;
11960 name = hl_get_font_name();
11961 }
11962 else
11963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011965 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11966 return;
11967 font = gui_mch_get_font(name, FALSE);
11968 if (font == NOFONT)
11969 return; /* Invalid font name, return empty string. */
11970 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011971 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011972 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011973 gui_mch_free_font(font);
11974 }
11975#endif
11976}
11977
11978/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011979 * "getfperm({fname})" function
11980 */
11981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011983 typval_T *argvars;
11984 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011985{
11986 char_u *fname;
11987 struct stat st;
11988 char_u *perm = NULL;
11989 char_u flags[] = "rwx";
11990 int i;
11991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011995 if (mch_stat((char *)fname, &st) >= 0)
11996 {
11997 perm = vim_strsave((char_u *)"---------");
11998 if (perm != NULL)
11999 {
12000 for (i = 0; i < 9; i++)
12001 {
12002 if (st.st_mode & (1 << (8 - i)))
12003 perm[i] = flags[i % 3];
12004 }
12005 }
12006 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012008}
12009
12010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 * "getfsize({fname})" function
12012 */
12013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012015 typval_T *argvars;
12016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017{
12018 char_u *fname;
12019 struct stat st;
12020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024
12025 if (mch_stat((char *)fname, &st) >= 0)
12026 {
12027 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012032
12033 /* non-perfect check for overflow */
12034 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12035 rettv->vval.v_number = -2;
12036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 }
12038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040}
12041
12042/*
12043 * "getftime({fname})" function
12044 */
12045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012047 typval_T *argvars;
12048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049{
12050 char_u *fname;
12051 struct stat st;
12052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054
12055 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059}
12060
12061/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012062 * "getftype({fname})" function
12063 */
12064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012066 typval_T *argvars;
12067 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012068{
12069 char_u *fname;
12070 struct stat st;
12071 char_u *type = NULL;
12072 char *t;
12073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012074 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012077 if (mch_lstat((char *)fname, &st) >= 0)
12078 {
12079#ifdef S_ISREG
12080 if (S_ISREG(st.st_mode))
12081 t = "file";
12082 else if (S_ISDIR(st.st_mode))
12083 t = "dir";
12084# ifdef S_ISLNK
12085 else if (S_ISLNK(st.st_mode))
12086 t = "link";
12087# endif
12088# ifdef S_ISBLK
12089 else if (S_ISBLK(st.st_mode))
12090 t = "bdev";
12091# endif
12092# ifdef S_ISCHR
12093 else if (S_ISCHR(st.st_mode))
12094 t = "cdev";
12095# endif
12096# ifdef S_ISFIFO
12097 else if (S_ISFIFO(st.st_mode))
12098 t = "fifo";
12099# endif
12100# ifdef S_ISSOCK
12101 else if (S_ISSOCK(st.st_mode))
12102 t = "fifo";
12103# endif
12104 else
12105 t = "other";
12106#else
12107# ifdef S_IFMT
12108 switch (st.st_mode & S_IFMT)
12109 {
12110 case S_IFREG: t = "file"; break;
12111 case S_IFDIR: t = "dir"; break;
12112# ifdef S_IFLNK
12113 case S_IFLNK: t = "link"; break;
12114# endif
12115# ifdef S_IFBLK
12116 case S_IFBLK: t = "bdev"; break;
12117# endif
12118# ifdef S_IFCHR
12119 case S_IFCHR: t = "cdev"; break;
12120# endif
12121# ifdef S_IFIFO
12122 case S_IFIFO: t = "fifo"; break;
12123# endif
12124# ifdef S_IFSOCK
12125 case S_IFSOCK: t = "socket"; break;
12126# endif
12127 default: t = "other";
12128 }
12129# else
12130 if (mch_isdir(fname))
12131 t = "dir";
12132 else
12133 t = "file";
12134# endif
12135#endif
12136 type = vim_strsave((char_u *)t);
12137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012139}
12140
12141/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012142 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143 */
12144 static void
12145f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012146 typval_T *argvars;
12147 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148{
12149 linenr_T lnum;
12150 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012151 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012152
12153 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012154 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012155 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012156 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012157 retlist = FALSE;
12158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012160 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012162 retlist = TRUE;
12163 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012164
Bram Moolenaar342337a2005-07-21 21:11:17 +000012165 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012166}
12167
12168/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012169 * "getmatches()" function
12170 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012171 static void
12172f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012173 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012174 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012175{
12176#ifdef FEAT_SEARCH_EXTRA
12177 dict_T *dict;
12178 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012179 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012180
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012181 if (rettv_list_alloc(rettv) == OK)
12182 {
12183 while (cur != NULL)
12184 {
12185 dict = dict_alloc();
12186 if (dict == NULL)
12187 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012188 if (cur->match.regprog == NULL)
12189 {
12190 /* match added with matchaddpos() */
12191 for (i = 0; i < MAXPOSMATCH; ++i)
12192 {
12193 llpos_T *llpos;
12194 char buf[6];
12195 list_T *l;
12196
12197 llpos = &cur->pos.pos[i];
12198 if (llpos->lnum == 0)
12199 break;
12200 l = list_alloc();
12201 if (l == NULL)
12202 break;
12203 list_append_number(l, (varnumber_T)llpos->lnum);
12204 if (llpos->col > 0)
12205 {
12206 list_append_number(l, (varnumber_T)llpos->col);
12207 list_append_number(l, (varnumber_T)llpos->len);
12208 }
12209 sprintf(buf, "pos%d", i + 1);
12210 dict_add_list(dict, buf, l);
12211 }
12212 }
12213 else
12214 {
12215 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12216 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012217 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012218 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12219 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012220# ifdef FEAT_CONCEAL
12221 if (cur->conceal_char)
12222 {
12223 char_u buf[MB_MAXBYTES + 1];
12224
12225 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12226 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12227 }
12228# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012229 list_append_dict(rettv->vval.v_list, dict);
12230 cur = cur->next;
12231 }
12232 }
12233#endif
12234}
12235
12236/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012237 * "getpid()" function
12238 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012239 static void
12240f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012241 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012242 typval_T *rettv;
12243{
12244 rettv->vval.v_number = mch_get_pid();
12245}
12246
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012247static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12248
12249/*
12250 * "getcurpos()" function
12251 */
12252 static void
12253f_getcurpos(argvars, rettv)
12254 typval_T *argvars;
12255 typval_T *rettv;
12256{
12257 getpos_both(argvars, rettv, TRUE);
12258}
12259
Bram Moolenaar18081e32008-02-20 19:11:07 +000012260/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012261 * "getpos(string)" function
12262 */
12263 static void
12264f_getpos(argvars, rettv)
12265 typval_T *argvars;
12266 typval_T *rettv;
12267{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012268 getpos_both(argvars, rettv, FALSE);
12269}
12270
12271 static void
12272getpos_both(argvars, rettv, getcurpos)
12273 typval_T *argvars;
12274 typval_T *rettv;
12275 int getcurpos;
12276{
Bram Moolenaara5525202006-03-02 22:52:09 +000012277 pos_T *fp;
12278 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012279 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012280
12281 if (rettv_list_alloc(rettv) == OK)
12282 {
12283 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012284 if (getcurpos)
12285 fp = &curwin->w_cursor;
12286 else
12287 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012288 if (fnum != -1)
12289 list_append_number(l, (varnumber_T)fnum);
12290 else
12291 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012292 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12293 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012294 list_append_number(l, (fp != NULL)
12295 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012296 : (varnumber_T)0);
12297 list_append_number(l,
12298#ifdef FEAT_VIRTUALEDIT
12299 (fp != NULL) ? (varnumber_T)fp->coladd :
12300#endif
12301 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012302 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012303 list_append_number(l, curwin->w_curswant == MAXCOL ?
12304 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012305 }
12306 else
12307 rettv->vval.v_number = FALSE;
12308}
12309
12310/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012311 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012312 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012313 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012314f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012315 typval_T *argvars UNUSED;
12316 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012317{
12318#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012319 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012320#endif
12321
Bram Moolenaar2641f772005-03-25 21:58:17 +000012322#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012323 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012324 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012325 wp = NULL;
12326 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12327 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012328 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012329 if (wp == NULL)
12330 return;
12331 }
12332
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012333 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012334 }
12335#endif
12336}
12337
12338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339 * "getreg()" function
12340 */
12341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012343 typval_T *argvars;
12344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345{
12346 char_u *strregname;
12347 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012348 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012349 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012350 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012352 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012354 strregname = get_tv_string_chk(&argvars[0]);
12355 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012356 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012358 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012359 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12360 return_list = get_tv_number_chk(&argvars[2], &error);
12361 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012364 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012365
12366 if (error)
12367 return;
12368
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 regname = (strregname == NULL ? '"' : *strregname);
12370 if (regname == 0)
12371 regname = '"';
12372
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012373 if (return_list)
12374 {
12375 rettv->v_type = VAR_LIST;
12376 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12377 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012378 if (rettv->vval.v_list != NULL)
12379 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012380 }
12381 else
12382 {
12383 rettv->v_type = VAR_STRING;
12384 rettv->vval.v_string = get_reg_contents(regname,
12385 arg2 ? GREG_EXPR_SRC : 0);
12386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387}
12388
12389/*
12390 * "getregtype()" function
12391 */
12392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012394 typval_T *argvars;
12395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396{
12397 char_u *strregname;
12398 int regname;
12399 char_u buf[NUMBUFLEN + 2];
12400 long reglen = 0;
12401
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012402 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 {
12404 strregname = get_tv_string_chk(&argvars[0]);
12405 if (strregname == NULL) /* type error; errmsg already given */
12406 {
12407 rettv->v_type = VAR_STRING;
12408 rettv->vval.v_string = NULL;
12409 return;
12410 }
12411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 else
12413 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012414 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415
12416 regname = (strregname == NULL ? '"' : *strregname);
12417 if (regname == 0)
12418 regname = '"';
12419
12420 buf[0] = NUL;
12421 buf[1] = NUL;
12422 switch (get_reg_type(regname, &reglen))
12423 {
12424 case MLINE: buf[0] = 'V'; break;
12425 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426 case MBLOCK:
12427 buf[0] = Ctrl_V;
12428 sprintf((char *)buf + 1, "%ld", reglen + 1);
12429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431 rettv->v_type = VAR_STRING;
12432 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433}
12434
12435/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012436 * "gettabvar()" function
12437 */
12438 static void
12439f_gettabvar(argvars, rettv)
12440 typval_T *argvars;
12441 typval_T *rettv;
12442{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012443 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012444 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012445 dictitem_T *v;
12446 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012447 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012448
12449 rettv->v_type = VAR_STRING;
12450 rettv->vval.v_string = NULL;
12451
12452 varname = get_tv_string_chk(&argvars[1]);
12453 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12454 if (tp != NULL && varname != NULL)
12455 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012456 /* Set tp to be our tabpage, temporarily. Also set the window to the
12457 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012458 if (switch_win(&oldcurwin, &oldtabpage,
12459 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012460 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012461 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012462 /* look up the variable */
12463 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12464 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12465 if (v != NULL)
12466 {
12467 copy_tv(&v->di_tv, rettv);
12468 done = TRUE;
12469 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012470 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012471
12472 /* restore previous notion of curwin */
12473 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012474 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012475
12476 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012477 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012478}
12479
12480/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012481 * "gettabwinvar()" function
12482 */
12483 static void
12484f_gettabwinvar(argvars, rettv)
12485 typval_T *argvars;
12486 typval_T *rettv;
12487{
12488 getwinvar(argvars, rettv, 1);
12489}
12490
12491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 * "getwinposx()" function
12493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012496 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012499 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500#ifdef FEAT_GUI
12501 if (gui.in_use)
12502 {
12503 int x, y;
12504
12505 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 }
12508#endif
12509}
12510
12511/*
12512 * "getwinposy()" function
12513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012516 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520#ifdef FEAT_GUI
12521 if (gui.in_use)
12522 {
12523 int x, y;
12524
12525 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 }
12528#endif
12529}
12530
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012531/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012532 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012533 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012534 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012535find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012536 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012537 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012538{
12539#ifdef FEAT_WINDOWS
12540 win_T *wp;
12541#endif
12542 int nr;
12543
12544 nr = get_tv_number_chk(vp, NULL);
12545
12546#ifdef FEAT_WINDOWS
12547 if (nr < 0)
12548 return NULL;
12549 if (nr == 0)
12550 return curwin;
12551
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012552 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12553 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012554 if (--nr <= 0)
12555 break;
12556 return wp;
12557#else
12558 if (nr == 0 || nr == 1)
12559 return curwin;
12560 return NULL;
12561#endif
12562}
12563
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564/*
12565 * "getwinvar()" function
12566 */
12567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012568f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012569 typval_T *argvars;
12570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012572 getwinvar(argvars, rettv, 0);
12573}
12574
12575/*
12576 * getwinvar() and gettabwinvar()
12577 */
12578 static void
12579getwinvar(argvars, rettv, off)
12580 typval_T *argvars;
12581 typval_T *rettv;
12582 int off; /* 1 for gettabwinvar() */
12583{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012584 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012586 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012587 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012589#ifdef FEAT_WINDOWS
12590 win_T *oldcurwin;
12591 tabpage_T *oldtabpage;
12592 int need_switch_win;
12593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012595#ifdef FEAT_WINDOWS
12596 if (off == 1)
12597 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12598 else
12599 tp = curtab;
12600#endif
12601 win = find_win_by_nr(&argvars[off], tp);
12602 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012603 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012605 rettv->v_type = VAR_STRING;
12606 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607
12608 if (win != NULL && varname != NULL)
12609 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012610#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012611 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012612 * otherwise the window is not valid. Only do this when needed,
12613 * autocommands get blocked. */
12614 need_switch_win = !(tp == curtab && win == curwin);
12615 if (!need_switch_win
12616 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12617#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012618 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012619 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012620 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012621 if (get_option_tv(&varname, rettv, 1) == OK)
12622 done = TRUE;
12623 }
12624 else
12625 {
12626 /* Look up the variable. */
12627 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12628 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12629 varname, FALSE);
12630 if (v != NULL)
12631 {
12632 copy_tv(&v->di_tv, rettv);
12633 done = TRUE;
12634 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012637
Bram Moolenaarba117c22015-09-29 16:53:22 +020012638#ifdef FEAT_WINDOWS
12639 if (need_switch_win)
12640 /* restore previous notion of curwin */
12641 restore_win(oldcurwin, oldtabpage, TRUE);
12642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 }
12644
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012645 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12646 /* use the default return value */
12647 copy_tv(&argvars[off + 2], rettv);
12648
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 --emsg_off;
12650}
12651
12652/*
12653 * "glob()" function
12654 */
12655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012657 typval_T *argvars;
12658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012660 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012662 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012664 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012665 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012667 if (argvars[1].v_type != VAR_UNKNOWN)
12668 {
12669 if (get_tv_number_chk(&argvars[1], &error))
12670 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012671 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012672 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012673 if (get_tv_number_chk(&argvars[2], &error))
12674 {
12675 rettv->v_type = VAR_LIST;
12676 rettv->vval.v_list = NULL;
12677 }
12678 if (argvars[3].v_type != VAR_UNKNOWN
12679 && get_tv_number_chk(&argvars[3], &error))
12680 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012681 }
12682 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012683 if (!error)
12684 {
12685 ExpandInit(&xpc);
12686 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012687 if (p_wic)
12688 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012689 if (rettv->v_type == VAR_STRING)
12690 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012691 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012692 else if (rettv_list_alloc(rettv) != FAIL)
12693 {
12694 int i;
12695
12696 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12697 NULL, options, WILD_ALL_KEEP);
12698 for (i = 0; i < xpc.xp_numfiles; i++)
12699 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12700
12701 ExpandCleanup(&xpc);
12702 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012703 }
12704 else
12705 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706}
12707
12708/*
12709 * "globpath()" function
12710 */
12711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012713 typval_T *argvars;
12714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012716 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012719 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012720 garray_T ga;
12721 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012723 /* When the optional second argument is non-zero, don't remove matches
12724 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012726 if (argvars[2].v_type != VAR_UNKNOWN)
12727 {
12728 if (get_tv_number_chk(&argvars[2], &error))
12729 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012730 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012731 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012732 if (get_tv_number_chk(&argvars[3], &error))
12733 {
12734 rettv->v_type = VAR_LIST;
12735 rettv->vval.v_list = NULL;
12736 }
12737 if (argvars[4].v_type != VAR_UNKNOWN
12738 && get_tv_number_chk(&argvars[4], &error))
12739 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012740 }
12741 }
12742 if (file != NULL && !error)
12743 {
12744 ga_init2(&ga, (int)sizeof(char_u *), 10);
12745 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12746 if (rettv->v_type == VAR_STRING)
12747 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12748 else if (rettv_list_alloc(rettv) != FAIL)
12749 for (i = 0; i < ga.ga_len; ++i)
12750 list_append_string(rettv->vval.v_list,
12751 ((char_u **)(ga.ga_data))[i], -1);
12752 ga_clear_strings(&ga);
12753 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756}
12757
12758/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012759 * "glob2regpat()" function
12760 */
12761 static void
12762f_glob2regpat(argvars, rettv)
12763 typval_T *argvars;
12764 typval_T *rettv;
12765{
12766 char_u *pat = get_tv_string_chk(&argvars[0]);
12767
12768 rettv->v_type = VAR_STRING;
12769 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12770}
12771
12772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 * "has()" function
12774 */
12775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012777 typval_T *argvars;
12778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779{
12780 int i;
12781 char_u *name;
12782 int n = FALSE;
12783 static char *(has_list[]) =
12784 {
12785#ifdef AMIGA
12786 "amiga",
12787# ifdef FEAT_ARP
12788 "arp",
12789# endif
12790#endif
12791#ifdef __BEOS__
12792 "beos",
12793#endif
12794#ifdef MSDOS
12795# ifdef DJGPP
12796 "dos32",
12797# else
12798 "dos16",
12799# endif
12800#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012801#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 "mac",
12803#endif
12804#if defined(MACOS_X_UNIX)
12805 "macunix",
12806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807#ifdef __QNX__
12808 "qnx",
12809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810#ifdef UNIX
12811 "unix",
12812#endif
12813#ifdef VMS
12814 "vms",
12815#endif
12816#ifdef WIN16
12817 "win16",
12818#endif
12819#ifdef WIN32
12820 "win32",
12821#endif
12822#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12823 "win32unix",
12824#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012825#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 "win64",
12827#endif
12828#ifdef EBCDIC
12829 "ebcdic",
12830#endif
12831#ifndef CASE_INSENSITIVE_FILENAME
12832 "fname_case",
12833#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012834#ifdef HAVE_ACL
12835 "acl",
12836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837#ifdef FEAT_ARABIC
12838 "arabic",
12839#endif
12840#ifdef FEAT_AUTOCMD
12841 "autocmd",
12842#endif
12843#ifdef FEAT_BEVAL
12844 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012845# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12846 "balloon_multiline",
12847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848#endif
12849#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12850 "builtin_terms",
12851# ifdef ALL_BUILTIN_TCAPS
12852 "all_builtin_terms",
12853# endif
12854#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012855#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12856 || defined(FEAT_GUI_W32) \
12857 || defined(FEAT_GUI_MOTIF))
12858 "browsefilter",
12859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860#ifdef FEAT_BYTEOFF
12861 "byte_offset",
12862#endif
12863#ifdef FEAT_CINDENT
12864 "cindent",
12865#endif
12866#ifdef FEAT_CLIENTSERVER
12867 "clientserver",
12868#endif
12869#ifdef FEAT_CLIPBOARD
12870 "clipboard",
12871#endif
12872#ifdef FEAT_CMDL_COMPL
12873 "cmdline_compl",
12874#endif
12875#ifdef FEAT_CMDHIST
12876 "cmdline_hist",
12877#endif
12878#ifdef FEAT_COMMENTS
12879 "comments",
12880#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012881#ifdef FEAT_CONCEAL
12882 "conceal",
12883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884#ifdef FEAT_CRYPT
12885 "cryptv",
12886#endif
12887#ifdef FEAT_CSCOPE
12888 "cscope",
12889#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012890#ifdef FEAT_CURSORBIND
12891 "cursorbind",
12892#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012893#ifdef CURSOR_SHAPE
12894 "cursorshape",
12895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896#ifdef DEBUG
12897 "debug",
12898#endif
12899#ifdef FEAT_CON_DIALOG
12900 "dialog_con",
12901#endif
12902#ifdef FEAT_GUI_DIALOG
12903 "dialog_gui",
12904#endif
12905#ifdef FEAT_DIFF
12906 "diff",
12907#endif
12908#ifdef FEAT_DIGRAPHS
12909 "digraphs",
12910#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012911#ifdef FEAT_DIRECTX
12912 "directx",
12913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914#ifdef FEAT_DND
12915 "dnd",
12916#endif
12917#ifdef FEAT_EMACS_TAGS
12918 "emacs_tags",
12919#endif
12920 "eval", /* always present, of course! */
12921#ifdef FEAT_EX_EXTRA
12922 "ex_extra",
12923#endif
12924#ifdef FEAT_SEARCH_EXTRA
12925 "extra_search",
12926#endif
12927#ifdef FEAT_FKMAP
12928 "farsi",
12929#endif
12930#ifdef FEAT_SEARCHPATH
12931 "file_in_path",
12932#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012933#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012934 "filterpipe",
12935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936#ifdef FEAT_FIND_ID
12937 "find_in_path",
12938#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012939#ifdef FEAT_FLOAT
12940 "float",
12941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942#ifdef FEAT_FOLDING
12943 "folding",
12944#endif
12945#ifdef FEAT_FOOTER
12946 "footer",
12947#endif
12948#if !defined(USE_SYSTEM) && defined(UNIX)
12949 "fork",
12950#endif
12951#ifdef FEAT_GETTEXT
12952 "gettext",
12953#endif
12954#ifdef FEAT_GUI
12955 "gui",
12956#endif
12957#ifdef FEAT_GUI_ATHENA
12958# ifdef FEAT_GUI_NEXTAW
12959 "gui_neXtaw",
12960# else
12961 "gui_athena",
12962# endif
12963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964#ifdef FEAT_GUI_GTK
12965 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012968#ifdef FEAT_GUI_GNOME
12969 "gui_gnome",
12970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971#ifdef FEAT_GUI_MAC
12972 "gui_mac",
12973#endif
12974#ifdef FEAT_GUI_MOTIF
12975 "gui_motif",
12976#endif
12977#ifdef FEAT_GUI_PHOTON
12978 "gui_photon",
12979#endif
12980#ifdef FEAT_GUI_W16
12981 "gui_win16",
12982#endif
12983#ifdef FEAT_GUI_W32
12984 "gui_win32",
12985#endif
12986#ifdef FEAT_HANGULIN
12987 "hangul_input",
12988#endif
12989#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12990 "iconv",
12991#endif
12992#ifdef FEAT_INS_EXPAND
12993 "insert_expand",
12994#endif
12995#ifdef FEAT_JUMPLIST
12996 "jumplist",
12997#endif
12998#ifdef FEAT_KEYMAP
12999 "keymap",
13000#endif
13001#ifdef FEAT_LANGMAP
13002 "langmap",
13003#endif
13004#ifdef FEAT_LIBCALL
13005 "libcall",
13006#endif
13007#ifdef FEAT_LINEBREAK
13008 "linebreak",
13009#endif
13010#ifdef FEAT_LISP
13011 "lispindent",
13012#endif
13013#ifdef FEAT_LISTCMDS
13014 "listcmds",
13015#endif
13016#ifdef FEAT_LOCALMAP
13017 "localmap",
13018#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013019#ifdef FEAT_LUA
13020# ifndef DYNAMIC_LUA
13021 "lua",
13022# endif
13023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024#ifdef FEAT_MENU
13025 "menu",
13026#endif
13027#ifdef FEAT_SESSION
13028 "mksession",
13029#endif
13030#ifdef FEAT_MODIFY_FNAME
13031 "modify_fname",
13032#endif
13033#ifdef FEAT_MOUSE
13034 "mouse",
13035#endif
13036#ifdef FEAT_MOUSESHAPE
13037 "mouseshape",
13038#endif
13039#if defined(UNIX) || defined(VMS)
13040# ifdef FEAT_MOUSE_DEC
13041 "mouse_dec",
13042# endif
13043# ifdef FEAT_MOUSE_GPM
13044 "mouse_gpm",
13045# endif
13046# ifdef FEAT_MOUSE_JSB
13047 "mouse_jsbterm",
13048# endif
13049# ifdef FEAT_MOUSE_NET
13050 "mouse_netterm",
13051# endif
13052# ifdef FEAT_MOUSE_PTERM
13053 "mouse_pterm",
13054# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013055# ifdef FEAT_MOUSE_SGR
13056 "mouse_sgr",
13057# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013058# ifdef FEAT_SYSMOUSE
13059 "mouse_sysmouse",
13060# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013061# ifdef FEAT_MOUSE_URXVT
13062 "mouse_urxvt",
13063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064# ifdef FEAT_MOUSE_XTERM
13065 "mouse_xterm",
13066# endif
13067#endif
13068#ifdef FEAT_MBYTE
13069 "multi_byte",
13070#endif
13071#ifdef FEAT_MBYTE_IME
13072 "multi_byte_ime",
13073#endif
13074#ifdef FEAT_MULTI_LANG
13075 "multi_lang",
13076#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013077#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013078#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013079 "mzscheme",
13080#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082#ifdef FEAT_OLE
13083 "ole",
13084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085#ifdef FEAT_PATH_EXTRA
13086 "path_extra",
13087#endif
13088#ifdef FEAT_PERL
13089#ifndef DYNAMIC_PERL
13090 "perl",
13091#endif
13092#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013093#ifdef FEAT_PERSISTENT_UNDO
13094 "persistent_undo",
13095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096#ifdef FEAT_PYTHON
13097#ifndef DYNAMIC_PYTHON
13098 "python",
13099#endif
13100#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013101#ifdef FEAT_PYTHON3
13102#ifndef DYNAMIC_PYTHON3
13103 "python3",
13104#endif
13105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106#ifdef FEAT_POSTSCRIPT
13107 "postscript",
13108#endif
13109#ifdef FEAT_PRINTER
13110 "printer",
13111#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013112#ifdef FEAT_PROFILE
13113 "profile",
13114#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013115#ifdef FEAT_RELTIME
13116 "reltime",
13117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118#ifdef FEAT_QUICKFIX
13119 "quickfix",
13120#endif
13121#ifdef FEAT_RIGHTLEFT
13122 "rightleft",
13123#endif
13124#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13125 "ruby",
13126#endif
13127#ifdef FEAT_SCROLLBIND
13128 "scrollbind",
13129#endif
13130#ifdef FEAT_CMDL_INFO
13131 "showcmd",
13132 "cmdline_info",
13133#endif
13134#ifdef FEAT_SIGNS
13135 "signs",
13136#endif
13137#ifdef FEAT_SMARTINDENT
13138 "smartindent",
13139#endif
13140#ifdef FEAT_SNIFF
13141 "sniff",
13142#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013143#ifdef STARTUPTIME
13144 "startuptime",
13145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146#ifdef FEAT_STL_OPT
13147 "statusline",
13148#endif
13149#ifdef FEAT_SUN_WORKSHOP
13150 "sun_workshop",
13151#endif
13152#ifdef FEAT_NETBEANS_INTG
13153 "netbeans_intg",
13154#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013155#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013156 "spell",
13157#endif
13158#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 "syntax",
13160#endif
13161#if defined(USE_SYSTEM) || !defined(UNIX)
13162 "system",
13163#endif
13164#ifdef FEAT_TAG_BINS
13165 "tag_binary",
13166#endif
13167#ifdef FEAT_TAG_OLDSTATIC
13168 "tag_old_static",
13169#endif
13170#ifdef FEAT_TAG_ANYWHITE
13171 "tag_any_white",
13172#endif
13173#ifdef FEAT_TCL
13174# ifndef DYNAMIC_TCL
13175 "tcl",
13176# endif
13177#endif
13178#ifdef TERMINFO
13179 "terminfo",
13180#endif
13181#ifdef FEAT_TERMRESPONSE
13182 "termresponse",
13183#endif
13184#ifdef FEAT_TEXTOBJ
13185 "textobjects",
13186#endif
13187#ifdef HAVE_TGETENT
13188 "tgetent",
13189#endif
13190#ifdef FEAT_TITLE
13191 "title",
13192#endif
13193#ifdef FEAT_TOOLBAR
13194 "toolbar",
13195#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013196#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13197 "unnamedplus",
13198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199#ifdef FEAT_USR_CMDS
13200 "user-commands", /* was accidentally included in 5.4 */
13201 "user_commands",
13202#endif
13203#ifdef FEAT_VIMINFO
13204 "viminfo",
13205#endif
13206#ifdef FEAT_VERTSPLIT
13207 "vertsplit",
13208#endif
13209#ifdef FEAT_VIRTUALEDIT
13210 "virtualedit",
13211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213#ifdef FEAT_VISUALEXTRA
13214 "visualextra",
13215#endif
13216#ifdef FEAT_VREPLACE
13217 "vreplace",
13218#endif
13219#ifdef FEAT_WILDIGN
13220 "wildignore",
13221#endif
13222#ifdef FEAT_WILDMENU
13223 "wildmenu",
13224#endif
13225#ifdef FEAT_WINDOWS
13226 "windows",
13227#endif
13228#ifdef FEAT_WAK
13229 "winaltkeys",
13230#endif
13231#ifdef FEAT_WRITEBACKUP
13232 "writebackup",
13233#endif
13234#ifdef FEAT_XIM
13235 "xim",
13236#endif
13237#ifdef FEAT_XFONTSET
13238 "xfontset",
13239#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013240#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013241 "xpm",
13242 "xpm_w32", /* for backward compatibility */
13243#else
13244# if defined(HAVE_XPM)
13245 "xpm",
13246# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248#ifdef USE_XSMP
13249 "xsmp",
13250#endif
13251#ifdef USE_XSMP_INTERACT
13252 "xsmp_interact",
13253#endif
13254#ifdef FEAT_XCLIPBOARD
13255 "xterm_clipboard",
13256#endif
13257#ifdef FEAT_XTERM_SAVE
13258 "xterm_save",
13259#endif
13260#if defined(UNIX) && defined(FEAT_X11)
13261 "X11",
13262#endif
13263 NULL
13264 };
13265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 for (i = 0; has_list[i] != NULL; ++i)
13268 if (STRICMP(name, has_list[i]) == 0)
13269 {
13270 n = TRUE;
13271 break;
13272 }
13273
13274 if (n == FALSE)
13275 {
13276 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013277 {
13278 if (name[5] == '-'
13279 && STRLEN(name) > 11
13280 && vim_isdigit(name[6])
13281 && vim_isdigit(name[8])
13282 && vim_isdigit(name[10]))
13283 {
13284 int major = atoi((char *)name + 6);
13285 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013286
13287 /* Expect "patch-9.9.01234". */
13288 n = (major < VIM_VERSION_MAJOR
13289 || (major == VIM_VERSION_MAJOR
13290 && (minor < VIM_VERSION_MINOR
13291 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013292 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013293 }
13294 else
13295 n = has_patch(atoi((char *)name + 5));
13296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 else if (STRICMP(name, "vim_starting") == 0)
13298 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013299#ifdef FEAT_MBYTE
13300 else if (STRICMP(name, "multi_byte_encoding") == 0)
13301 n = has_mbyte;
13302#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013303#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13304 else if (STRICMP(name, "balloon_multiline") == 0)
13305 n = multiline_balloon_available();
13306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307#ifdef DYNAMIC_TCL
13308 else if (STRICMP(name, "tcl") == 0)
13309 n = tcl_enabled(FALSE);
13310#endif
13311#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13312 else if (STRICMP(name, "iconv") == 0)
13313 n = iconv_enabled(FALSE);
13314#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013315#ifdef DYNAMIC_LUA
13316 else if (STRICMP(name, "lua") == 0)
13317 n = lua_enabled(FALSE);
13318#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013319#ifdef DYNAMIC_MZSCHEME
13320 else if (STRICMP(name, "mzscheme") == 0)
13321 n = mzscheme_enabled(FALSE);
13322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323#ifdef DYNAMIC_RUBY
13324 else if (STRICMP(name, "ruby") == 0)
13325 n = ruby_enabled(FALSE);
13326#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013327#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328#ifdef DYNAMIC_PYTHON
13329 else if (STRICMP(name, "python") == 0)
13330 n = python_enabled(FALSE);
13331#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013332#endif
13333#ifdef FEAT_PYTHON3
13334#ifdef DYNAMIC_PYTHON3
13335 else if (STRICMP(name, "python3") == 0)
13336 n = python3_enabled(FALSE);
13337#endif
13338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339#ifdef DYNAMIC_PERL
13340 else if (STRICMP(name, "perl") == 0)
13341 n = perl_enabled(FALSE);
13342#endif
13343#ifdef FEAT_GUI
13344 else if (STRICMP(name, "gui_running") == 0)
13345 n = (gui.in_use || gui.starting);
13346# ifdef FEAT_GUI_W32
13347 else if (STRICMP(name, "gui_win32s") == 0)
13348 n = gui_is_win32s();
13349# endif
13350# ifdef FEAT_BROWSE
13351 else if (STRICMP(name, "browse") == 0)
13352 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13353# endif
13354#endif
13355#ifdef FEAT_SYN_HL
13356 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013357 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358#endif
13359#if defined(WIN3264)
13360 else if (STRICMP(name, "win95") == 0)
13361 n = mch_windows95();
13362#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013363#ifdef FEAT_NETBEANS_INTG
13364 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013365 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 }
13368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013369 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370}
13371
13372/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013373 * "has_key()" function
13374 */
13375 static void
13376f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 typval_T *argvars;
13378 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013379{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013380 if (argvars[0].v_type != VAR_DICT)
13381 {
13382 EMSG(_(e_dictreq));
13383 return;
13384 }
13385 if (argvars[0].vval.v_dict == NULL)
13386 return;
13387
13388 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013389 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013390}
13391
13392/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013393 * "haslocaldir()" function
13394 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013395 static void
13396f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013397 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013398 typval_T *rettv;
13399{
13400 rettv->vval.v_number = (curwin->w_localdir != NULL);
13401}
13402
13403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 * "hasmapto()" function
13405 */
13406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013408 typval_T *argvars;
13409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410{
13411 char_u *name;
13412 char_u *mode;
13413 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013414 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013416 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013417 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 mode = (char_u *)"nvo";
13419 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013422 if (argvars[2].v_type != VAR_UNKNOWN)
13423 abbr = get_tv_number(&argvars[2]);
13424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425
Bram Moolenaar2c932302006-03-18 21:42:09 +000013426 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013427 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430}
13431
13432/*
13433 * "histadd()" function
13434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013436f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013437 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439{
13440#ifdef FEAT_CMDHIST
13441 int histype;
13442 char_u *str;
13443 char_u buf[NUMBUFLEN];
13444#endif
13445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013446 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 if (check_restricted() || check_secure())
13448 return;
13449#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013450 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13451 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 if (histype >= 0)
13453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013454 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 if (*str != NUL)
13456 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013457 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 return;
13461 }
13462 }
13463#endif
13464}
13465
13466/*
13467 * "histdel()" function
13468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013470f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013471 typval_T *argvars UNUSED;
13472 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473{
13474#ifdef FEAT_CMDHIST
13475 int n;
13476 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013479 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13480 if (str == NULL)
13481 n = 0;
13482 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013485 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 else
13490 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 get_tv_string_buf(&argvars[1], buf));
13493 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494#endif
13495}
13496
13497/*
13498 * "histget()" function
13499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013502 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504{
13505#ifdef FEAT_CMDHIST
13506 int type;
13507 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013508 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13511 if (str == NULL)
13512 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013514 {
13515 type = get_histtype(str);
13516 if (argvars[1].v_type == VAR_UNKNOWN)
13517 idx = get_history_idx(type);
13518 else
13519 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13520 /* -1 on type error */
13521 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013524 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013526 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527}
13528
13529/*
13530 * "histnr()" function
13531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013534 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536{
13537 int i;
13538
13539#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 char_u *history = get_tv_string_chk(&argvars[0]);
13541
13542 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 if (i >= HIST_CMD && i < HIST_COUNT)
13544 i = get_history_idx(i);
13545 else
13546#endif
13547 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549}
13550
13551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 * "highlightID(name)" function
13553 */
13554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560}
13561
13562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013563 * "highlight_exists()" function
13564 */
13565 static void
13566f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *argvars;
13568 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013569{
13570 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13571}
13572
13573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 * "hostname()" function
13575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013578 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580{
13581 char_u hostname[256];
13582
13583 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 rettv->v_type = VAR_STRING;
13585 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
13588/*
13589 * iconv() function
13590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
13596#ifdef FEAT_MBYTE
13597 char_u buf1[NUMBUFLEN];
13598 char_u buf2[NUMBUFLEN];
13599 char_u *from, *to, *str;
13600 vimconv_T vimconv;
13601#endif
13602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->v_type = VAR_STRING;
13604 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605
13606#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 str = get_tv_string(&argvars[0]);
13608 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13609 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 vimconv.vc_type = CONV_NONE;
13611 convert_setup(&vimconv, from, to);
13612
13613 /* If the encodings are equal, no conversion needed. */
13614 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618
13619 convert_setup(&vimconv, NULL, NULL);
13620 vim_free(from);
13621 vim_free(to);
13622#endif
13623}
13624
13625/*
13626 * "indent()" function
13627 */
13628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013630 typval_T *argvars;
13631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632{
13633 linenr_T lnum;
13634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640}
13641
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013642/*
13643 * "index()" function
13644 */
13645 static void
13646f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013647 typval_T *argvars;
13648 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013649{
Bram Moolenaar33570922005-01-25 22:26:29 +000013650 list_T *l;
13651 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013652 long idx = 0;
13653 int ic = FALSE;
13654
13655 rettv->vval.v_number = -1;
13656 if (argvars[0].v_type != VAR_LIST)
13657 {
13658 EMSG(_(e_listreq));
13659 return;
13660 }
13661 l = argvars[0].vval.v_list;
13662 if (l != NULL)
13663 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013664 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013665 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 int error = FALSE;
13668
Bram Moolenaar758711c2005-02-02 23:11:38 +000013669 /* Start at specified item. Use the cached index that list_find()
13670 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013672 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013673 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 ic = get_tv_number_chk(&argvars[3], &error);
13675 if (error)
13676 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013677 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013678
Bram Moolenaar758711c2005-02-02 23:11:38 +000013679 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013680 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013681 {
13682 rettv->vval.v_number = idx;
13683 break;
13684 }
13685 }
13686}
13687
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688static int inputsecret_flag = 0;
13689
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013690static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13691
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013693 * This function is used by f_input() and f_inputdialog() functions. The third
13694 * argument to f_input() specifies the type of completion to use at the
13695 * prompt. The third argument to f_inputdialog() specifies the value to return
13696 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 */
13698 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013699get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 typval_T *argvars;
13701 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013702 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 char_u *p = NULL;
13706 int c;
13707 char_u buf[NUMBUFLEN];
13708 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013710 int xp_type = EXPAND_NOTHING;
13711 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013714 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715
13716#ifdef NO_CONSOLE_INPUT
13717 /* While starting up, there is no place to enter text. */
13718 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#endif
13721
13722 cmd_silent = FALSE; /* Want to see the prompt. */
13723 if (prompt != NULL)
13724 {
13725 /* Only the part of the message after the last NL is considered as
13726 * prompt for the command line */
13727 p = vim_strrchr(prompt, '\n');
13728 if (p == NULL)
13729 p = prompt;
13730 else
13731 {
13732 ++p;
13733 c = *p;
13734 *p = NUL;
13735 msg_start();
13736 msg_clr_eos();
13737 msg_puts_attr(prompt, echo_attr);
13738 msg_didout = FALSE;
13739 msg_starthere();
13740 *p = c;
13741 }
13742 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013744 if (argvars[1].v_type != VAR_UNKNOWN)
13745 {
13746 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13747 if (defstr != NULL)
13748 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013750 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013751 {
13752 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013753 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013754 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013755
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013756 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013757 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013758
Bram Moolenaar4463f292005-09-25 22:20:24 +000013759 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13760 if (xp_name == NULL)
13761 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013762
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013763 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013764
Bram Moolenaar4463f292005-09-25 22:20:24 +000013765 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13766 &xp_arg) == FAIL)
13767 return;
13768 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013769 }
13770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013772 {
13773# ifdef FEAT_EX_EXTRA
13774 int save_ex_normal_busy = ex_normal_busy;
13775 ex_normal_busy = 0;
13776# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013778 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13779 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013780# ifdef FEAT_EX_EXTRA
13781 ex_normal_busy = save_ex_normal_busy;
13782# endif
13783 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013784 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013785 && argvars[1].v_type != VAR_UNKNOWN
13786 && argvars[2].v_type != VAR_UNKNOWN)
13787 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13788 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013789
13790 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792 /* since the user typed this, no need to wait for return */
13793 need_wait_return = FALSE;
13794 msg_didout = FALSE;
13795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 cmd_silent = cmd_silent_save;
13797}
13798
13799/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013800 * "input()" function
13801 * Also handles inputsecret() when inputsecret is set.
13802 */
13803 static void
13804f_input(argvars, rettv)
13805 typval_T *argvars;
13806 typval_T *rettv;
13807{
13808 get_user_input(argvars, rettv, FALSE);
13809}
13810
13811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 * "inputdialog()" function
13813 */
13814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013815f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013816 typval_T *argvars;
13817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818{
13819#if defined(FEAT_GUI_TEXTDIALOG)
13820 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13821 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13822 {
13823 char_u *message;
13824 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 message = get_tv_string_chk(&argvars[0]);
13828 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013829 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013830 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 else
13832 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013833 if (message != NULL && defstr != NULL
13834 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013835 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 else
13838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013839 if (message != NULL && defstr != NULL
13840 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013841 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013842 rettv->vval.v_string = vim_strsave(
13843 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013845 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013847 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 }
13849 else
13850#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013851 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852}
13853
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013854/*
13855 * "inputlist()" function
13856 */
13857 static void
13858f_inputlist(argvars, rettv)
13859 typval_T *argvars;
13860 typval_T *rettv;
13861{
13862 listitem_T *li;
13863 int selected;
13864 int mouse_used;
13865
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013866#ifdef NO_CONSOLE_INPUT
13867 /* While starting up, there is no place to enter text. */
13868 if (no_console_input())
13869 return;
13870#endif
13871 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13872 {
13873 EMSG2(_(e_listarg), "inputlist()");
13874 return;
13875 }
13876
13877 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013878 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013879 lines_left = Rows; /* avoid more prompt */
13880 msg_scroll = TRUE;
13881 msg_clr_eos();
13882
13883 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13884 {
13885 msg_puts(get_tv_string(&li->li_tv));
13886 msg_putchar('\n');
13887 }
13888
13889 /* Ask for choice. */
13890 selected = prompt_for_number(&mouse_used);
13891 if (mouse_used)
13892 selected -= lines_left;
13893
13894 rettv->vval.v_number = selected;
13895}
13896
13897
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13899
13900/*
13901 * "inputrestore()" function
13902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013904f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013905 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907{
13908 if (ga_userinput.ga_len > 0)
13909 {
13910 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13912 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013913 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 }
13915 else if (p_verbose > 1)
13916 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013917 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013918 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 }
13920}
13921
13922/*
13923 * "inputsave()" function
13924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013927 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013930 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 if (ga_grow(&ga_userinput, 1) == OK)
13932 {
13933 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13934 + ga_userinput.ga_len);
13935 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013936 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 }
13938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013939 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940}
13941
13942/*
13943 * "inputsecret()" function
13944 */
13945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013946f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013947 typval_T *argvars;
13948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949{
13950 ++cmdline_star;
13951 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013952 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 --cmdline_star;
13954 --inputsecret_flag;
13955}
13956
13957/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013958 * "insert()" function
13959 */
13960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013962 typval_T *argvars;
13963 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013964{
13965 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013966 listitem_T *item;
13967 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013969
13970 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013972 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013973 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013974 {
13975 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 before = get_tv_number_chk(&argvars[2], &error);
13977 if (error)
13978 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013979
Bram Moolenaar758711c2005-02-02 23:11:38 +000013980 if (before == l->lv_len)
13981 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013982 else
13983 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013984 item = list_find(l, before);
13985 if (item == NULL)
13986 {
13987 EMSGN(_(e_listidx), before);
13988 l = NULL;
13989 }
13990 }
13991 if (l != NULL)
13992 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013993 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013994 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013995 }
13996 }
13997}
13998
13999/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014000 * "invert(expr)" function
14001 */
14002 static void
14003f_invert(argvars, rettv)
14004 typval_T *argvars;
14005 typval_T *rettv;
14006{
14007 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14008}
14009
14010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 * "isdirectory()" function
14012 */
14013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014014f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014015 typval_T *argvars;
14016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014018 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019}
14020
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014021/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014022 * "islocked()" function
14023 */
14024 static void
14025f_islocked(argvars, rettv)
14026 typval_T *argvars;
14027 typval_T *rettv;
14028{
14029 lval_T lv;
14030 char_u *end;
14031 dictitem_T *di;
14032
14033 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014034 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14035 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014036 if (end != NULL && lv.ll_name != NULL)
14037 {
14038 if (*end != NUL)
14039 EMSG(_(e_trailing));
14040 else
14041 {
14042 if (lv.ll_tv == NULL)
14043 {
14044 if (check_changedtick(lv.ll_name))
14045 rettv->vval.v_number = 1; /* always locked */
14046 else
14047 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014048 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014049 if (di != NULL)
14050 {
14051 /* Consider a variable locked when:
14052 * 1. the variable itself is locked
14053 * 2. the value of the variable is locked.
14054 * 3. the List or Dict value is locked.
14055 */
14056 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14057 || tv_islocked(&di->di_tv));
14058 }
14059 }
14060 }
14061 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014062 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014063 else if (lv.ll_newkey != NULL)
14064 EMSG2(_(e_dictkey), lv.ll_newkey);
14065 else if (lv.ll_list != NULL)
14066 /* List item. */
14067 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14068 else
14069 /* Dictionary item. */
14070 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14071 }
14072 }
14073
14074 clear_lval(&lv);
14075}
14076
Bram Moolenaar33570922005-01-25 22:26:29 +000014077static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014078
14079/*
14080 * Turn a dict into a list:
14081 * "what" == 0: list of keys
14082 * "what" == 1: list of values
14083 * "what" == 2: list of items
14084 */
14085 static void
14086dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014087 typval_T *argvars;
14088 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014089 int what;
14090{
Bram Moolenaar33570922005-01-25 22:26:29 +000014091 list_T *l2;
14092 dictitem_T *di;
14093 hashitem_T *hi;
14094 listitem_T *li;
14095 listitem_T *li2;
14096 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014097 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014098
Bram Moolenaar8c711452005-01-14 21:53:12 +000014099 if (argvars[0].v_type != VAR_DICT)
14100 {
14101 EMSG(_(e_dictreq));
14102 return;
14103 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014104 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014105 return;
14106
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014107 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014108 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014109
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014110 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014112 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014113 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014115 --todo;
14116 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014117
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014118 li = listitem_alloc();
14119 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014120 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014121 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014122
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014123 if (what == 0)
14124 {
14125 /* keys() */
14126 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014127 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014128 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14129 }
14130 else if (what == 1)
14131 {
14132 /* values() */
14133 copy_tv(&di->di_tv, &li->li_tv);
14134 }
14135 else
14136 {
14137 /* items() */
14138 l2 = list_alloc();
14139 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014140 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014141 li->li_tv.vval.v_list = l2;
14142 if (l2 == NULL)
14143 break;
14144 ++l2->lv_refcount;
14145
14146 li2 = listitem_alloc();
14147 if (li2 == NULL)
14148 break;
14149 list_append(l2, li2);
14150 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014151 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014152 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14153
14154 li2 = listitem_alloc();
14155 if (li2 == NULL)
14156 break;
14157 list_append(l2, li2);
14158 copy_tv(&di->di_tv, &li2->li_tv);
14159 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014160 }
14161 }
14162}
14163
14164/*
14165 * "items(dict)" function
14166 */
14167 static void
14168f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 typval_T *argvars;
14170 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014171{
14172 dict_list(argvars, rettv, 2);
14173}
14174
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014176 * "join()" function
14177 */
14178 static void
14179f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014180 typval_T *argvars;
14181 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014182{
14183 garray_T ga;
14184 char_u *sep;
14185
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014186 if (argvars[0].v_type != VAR_LIST)
14187 {
14188 EMSG(_(e_listreq));
14189 return;
14190 }
14191 if (argvars[0].vval.v_list == NULL)
14192 return;
14193 if (argvars[1].v_type == VAR_UNKNOWN)
14194 sep = (char_u *)" ";
14195 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014197
14198 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199
14200 if (sep != NULL)
14201 {
14202 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014203 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 ga_append(&ga, NUL);
14205 rettv->vval.v_string = (char_u *)ga.ga_data;
14206 }
14207 else
14208 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014209}
14210
14211/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014212 * "keys()" function
14213 */
14214 static void
14215f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014216 typval_T *argvars;
14217 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014218{
14219 dict_list(argvars, rettv, 0);
14220}
14221
14222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 * "last_buffer_nr()" function.
14224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014227 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
14230 int n = 0;
14231 buf_T *buf;
14232
14233 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14234 if (n < buf->b_fnum)
14235 n = buf->b_fnum;
14236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014238}
14239
14240/*
14241 * "len()" function
14242 */
14243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014245 typval_T *argvars;
14246 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014247{
14248 switch (argvars[0].v_type)
14249 {
14250 case VAR_STRING:
14251 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252 rettv->vval.v_number = (varnumber_T)STRLEN(
14253 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014254 break;
14255 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014257 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014258 case VAR_DICT:
14259 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14260 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014261 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014262 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014263 break;
14264 }
14265}
14266
Bram Moolenaar33570922005-01-25 22:26:29 +000014267static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014268
14269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014271 typval_T *argvars;
14272 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014273 int type;
14274{
14275#ifdef FEAT_LIBCALL
14276 char_u *string_in;
14277 char_u **string_result;
14278 int nr_result;
14279#endif
14280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014281 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014282 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014283 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014284
14285 if (check_restricted() || check_secure())
14286 return;
14287
14288#ifdef FEAT_LIBCALL
14289 /* The first two args must be strings, otherwise its meaningless */
14290 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14291 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014292 string_in = NULL;
14293 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014294 string_in = argvars[2].vval.v_string;
14295 if (type == VAR_NUMBER)
14296 string_result = NULL;
14297 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014298 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014299 if (mch_libcall(argvars[0].vval.v_string,
14300 argvars[1].vval.v_string,
14301 string_in,
14302 argvars[2].vval.v_number,
14303 string_result,
14304 &nr_result) == OK
14305 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014306 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014307 }
14308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309}
14310
14311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014312 * "libcall()" function
14313 */
14314 static void
14315f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014316 typval_T *argvars;
14317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014318{
14319 libcall_common(argvars, rettv, VAR_STRING);
14320}
14321
14322/*
14323 * "libcallnr()" function
14324 */
14325 static void
14326f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014327 typval_T *argvars;
14328 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329{
14330 libcall_common(argvars, rettv, VAR_NUMBER);
14331}
14332
14333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 * "line(string)" function
14335 */
14336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014338 typval_T *argvars;
14339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340{
14341 linenr_T lnum = 0;
14342 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014343 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014345 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 if (fp != NULL)
14347 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349}
14350
14351/*
14352 * "line2byte(lnum)" function
14353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014355f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014356 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358{
14359#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361#else
14362 linenr_T lnum;
14363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14369 if (rettv->vval.v_number >= 0)
14370 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371#endif
14372}
14373
14374/*
14375 * "lispindent(lnum)" function
14376 */
14377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014378f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014379 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381{
14382#ifdef FEAT_LISP
14383 pos_T pos;
14384 linenr_T lnum;
14385
14386 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14389 {
14390 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 curwin->w_cursor = pos;
14393 }
14394 else
14395#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014396 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397}
14398
14399/*
14400 * "localtime()" function
14401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014404 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408}
14409
Bram Moolenaar33570922005-01-25 22:26:29 +000014410static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411
14412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014414 typval_T *argvars;
14415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 int exact;
14417{
14418 char_u *keys;
14419 char_u *which;
14420 char_u buf[NUMBUFLEN];
14421 char_u *keys_buf = NULL;
14422 char_u *rhs;
14423 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014424 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014425 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014426 mapblock_T *mp;
14427 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428
14429 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 rettv->v_type = VAR_STRING;
14431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 if (*keys == NUL)
14435 return;
14436
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014440 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014441 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014442 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014443 if (argvars[3].v_type != VAR_UNKNOWN)
14444 get_dict = get_tv_number(&argvars[3]);
14445 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 else
14448 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 if (which == NULL)
14450 return;
14451
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 mode = get_map_mode(&which, 0);
14453
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014454 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014455 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014457
14458 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014460 /* Return a string. */
14461 if (rhs != NULL)
14462 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463
Bram Moolenaarbd743252010-10-20 21:23:33 +020014464 }
14465 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14466 {
14467 /* Return a dictionary. */
14468 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14469 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14470 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471
Bram Moolenaarbd743252010-10-20 21:23:33 +020014472 dict_add_nr_str(dict, "lhs", 0L, lhs);
14473 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14474 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14475 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14476 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14477 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14478 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014479 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014480 dict_add_nr_str(dict, "mode", 0L, mapmode);
14481
14482 vim_free(lhs);
14483 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 }
14485}
14486
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014487#ifdef FEAT_FLOAT
14488/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014489 * "log()" function
14490 */
14491 static void
14492f_log(argvars, rettv)
14493 typval_T *argvars;
14494 typval_T *rettv;
14495{
14496 float_T f;
14497
14498 rettv->v_type = VAR_FLOAT;
14499 if (get_float_arg(argvars, &f) == OK)
14500 rettv->vval.v_float = log(f);
14501 else
14502 rettv->vval.v_float = 0.0;
14503}
14504
14505/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014506 * "log10()" function
14507 */
14508 static void
14509f_log10(argvars, rettv)
14510 typval_T *argvars;
14511 typval_T *rettv;
14512{
14513 float_T f;
14514
14515 rettv->v_type = VAR_FLOAT;
14516 if (get_float_arg(argvars, &f) == OK)
14517 rettv->vval.v_float = log10(f);
14518 else
14519 rettv->vval.v_float = 0.0;
14520}
14521#endif
14522
Bram Moolenaar1dced572012-04-05 16:54:08 +020014523#ifdef FEAT_LUA
14524/*
14525 * "luaeval()" function
14526 */
14527 static void
14528f_luaeval(argvars, rettv)
14529 typval_T *argvars;
14530 typval_T *rettv;
14531{
14532 char_u *str;
14533 char_u buf[NUMBUFLEN];
14534
14535 str = get_tv_string_buf(&argvars[0], buf);
14536 do_luaeval(str, argvars + 1, rettv);
14537}
14538#endif
14539
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014541 * "map()" function
14542 */
14543 static void
14544f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 typval_T *argvars;
14546 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014547{
14548 filter_map(argvars, rettv, TRUE);
14549}
14550
14551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014552 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 */
14554 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014556 typval_T *argvars;
14557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014559 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560}
14561
14562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 */
14565 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 typval_T *argvars;
14568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571}
14572
Bram Moolenaar33570922005-01-25 22:26:29 +000014573static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574
14575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 typval_T *argvars;
14578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 int type;
14580{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014581 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014582 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014583 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 char_u *pat;
14585 regmatch_T regmatch;
14586 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014587 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 char_u *save_cpo;
14589 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014590 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014591 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014592 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 list_T *l = NULL;
14594 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014595 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014596 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597
14598 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14599 save_cpo = p_cpo;
14600 p_cpo = (char_u *)"";
14601
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014602 rettv->vval.v_number = -1;
14603 if (type == 3)
14604 {
14605 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014606 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014607 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014608 }
14609 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 rettv->v_type = VAR_STRING;
14612 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014615 if (argvars[0].v_type == VAR_LIST)
14616 {
14617 if ((l = argvars[0].vval.v_list) == NULL)
14618 goto theend;
14619 li = l->lv_first;
14620 }
14621 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014622 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014623 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014624 len = (long)STRLEN(str);
14625 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14628 if (pat == NULL)
14629 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014630
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014631 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 int error = FALSE;
14634
14635 start = get_tv_number_chk(&argvars[2], &error);
14636 if (error)
14637 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014638 if (l != NULL)
14639 {
14640 li = list_find(l, start);
14641 if (li == NULL)
14642 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014643 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014644 }
14645 else
14646 {
14647 if (start < 0)
14648 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014649 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014650 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014651 /* When "count" argument is there ignore matches before "start",
14652 * otherwise skip part of the string. Differs when pattern is "^"
14653 * or "\<". */
14654 if (argvars[3].v_type != VAR_UNKNOWN)
14655 startcol = start;
14656 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014657 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014658 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014659 len -= start;
14660 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014661 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014662
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014663 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 nth = get_tv_number_chk(&argvars[3], &error);
14665 if (error)
14666 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667 }
14668
14669 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14670 if (regmatch.regprog != NULL)
14671 {
14672 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014673
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014674 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014675 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014676 if (l != NULL)
14677 {
14678 if (li == NULL)
14679 {
14680 match = FALSE;
14681 break;
14682 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014683 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014684 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014685 if (str == NULL)
14686 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014687 }
14688
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014689 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014690
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014691 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014693 if (l == NULL && !match)
14694 break;
14695
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014696 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014697 if (l != NULL)
14698 {
14699 li = li->li_next;
14700 ++idx;
14701 }
14702 else
14703 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014704#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014705 startcol = (colnr_T)(regmatch.startp[0]
14706 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014707#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014708 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014709#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014710 if (startcol > (colnr_T)len
14711 || str + startcol <= regmatch.startp[0])
14712 {
14713 match = FALSE;
14714 break;
14715 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014716 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014717 }
14718
14719 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014721 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014722 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014723 int i;
14724
14725 /* return list with matched string and submatches */
14726 for (i = 0; i < NSUBEXP; ++i)
14727 {
14728 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014729 {
14730 if (list_append_string(rettv->vval.v_list,
14731 (char_u *)"", 0) == FAIL)
14732 break;
14733 }
14734 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014735 regmatch.startp[i],
14736 (int)(regmatch.endp[i] - regmatch.startp[i]))
14737 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014738 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014739 }
14740 }
14741 else if (type == 2)
14742 {
14743 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014744 if (l != NULL)
14745 copy_tv(&li->li_tv, rettv);
14746 else
14747 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014749 }
14750 else if (l != NULL)
14751 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 else
14753 {
14754 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 (varnumber_T)(regmatch.startp[0] - str);
14757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014760 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 }
14762 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014763 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764 }
14765
14766theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014767 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768 p_cpo = save_cpo;
14769}
14770
14771/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014772 * "match()" function
14773 */
14774 static void
14775f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 typval_T *argvars;
14777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014778{
14779 find_some_match(argvars, rettv, 1);
14780}
14781
14782/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014783 * "matchadd()" function
14784 */
14785 static void
14786f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014787 typval_T *argvars UNUSED;
14788 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014789{
14790#ifdef FEAT_SEARCH_EXTRA
14791 char_u buf[NUMBUFLEN];
14792 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14793 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14794 int prio = 10; /* default priority */
14795 int id = -1;
14796 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014797 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014798
14799 rettv->vval.v_number = -1;
14800
14801 if (grp == NULL || pat == NULL)
14802 return;
14803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014804 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014805 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014806 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014807 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014808 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014809 if (argvars[4].v_type != VAR_UNKNOWN)
14810 {
14811 if (argvars[4].v_type != VAR_DICT)
14812 {
14813 EMSG(_(e_dictreq));
14814 return;
14815 }
14816 if (dict_find(argvars[4].vval.v_dict,
14817 (char_u *)"conceal", -1) != NULL)
14818 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14819 (char_u *)"conceal", FALSE);
14820 }
14821 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014822 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014823 if (error == TRUE)
14824 return;
14825 if (id >= 1 && id <= 3)
14826 {
14827 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14828 return;
14829 }
14830
Bram Moolenaar6561d522015-07-21 15:48:27 +020014831 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14832 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014833#endif
14834}
14835
14836/*
14837 * "matchaddpos()" function
14838 */
14839 static void
14840f_matchaddpos(argvars, rettv)
14841 typval_T *argvars UNUSED;
14842 typval_T *rettv UNUSED;
14843{
14844#ifdef FEAT_SEARCH_EXTRA
14845 char_u buf[NUMBUFLEN];
14846 char_u *group;
14847 int prio = 10;
14848 int id = -1;
14849 int error = FALSE;
14850 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014851 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014852
14853 rettv->vval.v_number = -1;
14854
14855 group = get_tv_string_buf_chk(&argvars[0], buf);
14856 if (group == NULL)
14857 return;
14858
14859 if (argvars[1].v_type != VAR_LIST)
14860 {
14861 EMSG2(_(e_listarg), "matchaddpos()");
14862 return;
14863 }
14864 l = argvars[1].vval.v_list;
14865 if (l == NULL)
14866 return;
14867
14868 if (argvars[2].v_type != VAR_UNKNOWN)
14869 {
14870 prio = get_tv_number_chk(&argvars[2], &error);
14871 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014872 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014873 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014874 if (argvars[4].v_type != VAR_UNKNOWN)
14875 {
14876 if (argvars[4].v_type != VAR_DICT)
14877 {
14878 EMSG(_(e_dictreq));
14879 return;
14880 }
14881 if (dict_find(argvars[4].vval.v_dict,
14882 (char_u *)"conceal", -1) != NULL)
14883 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14884 (char_u *)"conceal", FALSE);
14885 }
14886 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014887 }
14888 if (error == TRUE)
14889 return;
14890
14891 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14892 if (id == 1 || id == 2)
14893 {
14894 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14895 return;
14896 }
14897
Bram Moolenaar6561d522015-07-21 15:48:27 +020014898 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14899 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014900#endif
14901}
14902
14903/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014904 * "matcharg()" function
14905 */
14906 static void
14907f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014908 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014909 typval_T *rettv;
14910{
14911 if (rettv_list_alloc(rettv) == OK)
14912 {
14913#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014914 int id = get_tv_number(&argvars[0]);
14915 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014916
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014917 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014918 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014919 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14920 {
14921 list_append_string(rettv->vval.v_list,
14922 syn_id2name(m->hlg_id), -1);
14923 list_append_string(rettv->vval.v_list, m->pattern, -1);
14924 }
14925 else
14926 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014927 list_append_string(rettv->vval.v_list, NULL, -1);
14928 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014929 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014930 }
14931#endif
14932 }
14933}
14934
14935/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014936 * "matchdelete()" function
14937 */
14938 static void
14939f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014940 typval_T *argvars UNUSED;
14941 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014942{
14943#ifdef FEAT_SEARCH_EXTRA
14944 rettv->vval.v_number = match_delete(curwin,
14945 (int)get_tv_number(&argvars[0]), TRUE);
14946#endif
14947}
14948
14949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950 * "matchend()" function
14951 */
14952 static void
14953f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 typval_T *argvars;
14955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014956{
14957 find_some_match(argvars, rettv, 0);
14958}
14959
14960/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014961 * "matchlist()" function
14962 */
14963 static void
14964f_matchlist(argvars, rettv)
14965 typval_T *argvars;
14966 typval_T *rettv;
14967{
14968 find_some_match(argvars, rettv, 3);
14969}
14970
14971/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972 * "matchstr()" function
14973 */
14974 static void
14975f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *argvars;
14977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978{
14979 find_some_match(argvars, rettv, 2);
14980}
14981
Bram Moolenaar33570922005-01-25 22:26:29 +000014982static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014983
14984 static void
14985max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014986 typval_T *argvars;
14987 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014988 int domax;
14989{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014990 long n = 0;
14991 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014992 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014993
14994 if (argvars[0].v_type == VAR_LIST)
14995 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014996 list_T *l;
14997 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014998
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014999 l = argvars[0].vval.v_list;
15000 if (l != NULL)
15001 {
15002 li = l->lv_first;
15003 if (li != NULL)
15004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015005 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015006 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015007 {
15008 li = li->li_next;
15009 if (li == NULL)
15010 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015011 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015012 if (domax ? i > n : i < n)
15013 n = i;
15014 }
15015 }
15016 }
15017 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015018 else if (argvars[0].v_type == VAR_DICT)
15019 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015020 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015021 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015022 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015023 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015024
15025 d = argvars[0].vval.v_dict;
15026 if (d != NULL)
15027 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015028 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015029 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015030 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015031 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015032 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015033 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015034 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015035 if (first)
15036 {
15037 n = i;
15038 first = FALSE;
15039 }
15040 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015041 n = i;
15042 }
15043 }
15044 }
15045 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015046 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015047 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015048 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015049}
15050
15051/*
15052 * "max()" function
15053 */
15054 static void
15055f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015056 typval_T *argvars;
15057 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015058{
15059 max_min(argvars, rettv, TRUE);
15060}
15061
15062/*
15063 * "min()" function
15064 */
15065 static void
15066f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015067 typval_T *argvars;
15068 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015069{
15070 max_min(argvars, rettv, FALSE);
15071}
15072
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015073static int mkdir_recurse __ARGS((char_u *dir, int prot));
15074
15075/*
15076 * Create the directory in which "dir" is located, and higher levels when
15077 * needed.
15078 */
15079 static int
15080mkdir_recurse(dir, prot)
15081 char_u *dir;
15082 int prot;
15083{
15084 char_u *p;
15085 char_u *updir;
15086 int r = FAIL;
15087
15088 /* Get end of directory name in "dir".
15089 * We're done when it's "/" or "c:/". */
15090 p = gettail_sep(dir);
15091 if (p <= get_past_head(dir))
15092 return OK;
15093
15094 /* If the directory exists we're done. Otherwise: create it.*/
15095 updir = vim_strnsave(dir, (int)(p - dir));
15096 if (updir == NULL)
15097 return FAIL;
15098 if (mch_isdir(updir))
15099 r = OK;
15100 else if (mkdir_recurse(updir, prot) == OK)
15101 r = vim_mkdir_emsg(updir, prot);
15102 vim_free(updir);
15103 return r;
15104}
15105
15106#ifdef vim_mkdir
15107/*
15108 * "mkdir()" function
15109 */
15110 static void
15111f_mkdir(argvars, rettv)
15112 typval_T *argvars;
15113 typval_T *rettv;
15114{
15115 char_u *dir;
15116 char_u buf[NUMBUFLEN];
15117 int prot = 0755;
15118
15119 rettv->vval.v_number = FAIL;
15120 if (check_restricted() || check_secure())
15121 return;
15122
15123 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015124 if (*dir == NUL)
15125 rettv->vval.v_number = FAIL;
15126 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015127 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015128 if (*gettail(dir) == NUL)
15129 /* remove trailing slashes */
15130 *gettail_sep(dir) = NUL;
15131
15132 if (argvars[1].v_type != VAR_UNKNOWN)
15133 {
15134 if (argvars[2].v_type != VAR_UNKNOWN)
15135 prot = get_tv_number_chk(&argvars[2], NULL);
15136 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15137 mkdir_recurse(dir, prot);
15138 }
15139 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015140 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015141}
15142#endif
15143
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 * "mode()" function
15146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015148f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015149 typval_T *argvars;
15150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015152 char_u buf[3];
15153
15154 buf[1] = NUL;
15155 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 if (VIsual_active)
15158 {
15159 if (VIsual_select)
15160 buf[0] = VIsual_mode + 's' - 'v';
15161 else
15162 buf[0] = VIsual_mode;
15163 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015164 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015165 || State == CONFIRM)
15166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015168 if (State == ASKMORE)
15169 buf[1] = 'm';
15170 else if (State == CONFIRM)
15171 buf[1] = '?';
15172 }
15173 else if (State == EXTERNCMD)
15174 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 else if (State & INSERT)
15176 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015177#ifdef FEAT_VREPLACE
15178 if (State & VREPLACE_FLAG)
15179 {
15180 buf[0] = 'R';
15181 buf[1] = 'v';
15182 }
15183 else
15184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 if (State & REPLACE_FLAG)
15186 buf[0] = 'R';
15187 else
15188 buf[0] = 'i';
15189 }
15190 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015193 if (exmode_active)
15194 buf[1] = 'v';
15195 }
15196 else if (exmode_active)
15197 {
15198 buf[0] = 'c';
15199 buf[1] = 'e';
15200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015204 if (finish_op)
15205 buf[1] = 'o';
15206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015208 /* Clear out the minor mode when the argument is not a non-zero number or
15209 * non-empty string. */
15210 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015211 buf[1] = NUL;
15212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015213 rettv->vval.v_string = vim_strsave(buf);
15214 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215}
15216
Bram Moolenaar429fa852013-04-15 12:27:36 +020015217#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015218/*
15219 * "mzeval()" function
15220 */
15221 static void
15222f_mzeval(argvars, rettv)
15223 typval_T *argvars;
15224 typval_T *rettv;
15225{
15226 char_u *str;
15227 char_u buf[NUMBUFLEN];
15228
15229 str = get_tv_string_buf(&argvars[0], buf);
15230 do_mzeval(str, rettv);
15231}
Bram Moolenaar75676462013-01-30 14:55:42 +010015232
15233 void
15234mzscheme_call_vim(name, args, rettv)
15235 char_u *name;
15236 typval_T *args;
15237 typval_T *rettv;
15238{
15239 typval_T argvars[3];
15240
15241 argvars[0].v_type = VAR_STRING;
15242 argvars[0].vval.v_string = name;
15243 copy_tv(args, &argvars[1]);
15244 argvars[2].v_type = VAR_UNKNOWN;
15245 f_call(argvars, rettv);
15246 clear_tv(&argvars[1]);
15247}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015248#endif
15249
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015251 * "nextnonblank()" function
15252 */
15253 static void
15254f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015255 typval_T *argvars;
15256 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257{
15258 linenr_T lnum;
15259
15260 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015262 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263 {
15264 lnum = 0;
15265 break;
15266 }
15267 if (*skipwhite(ml_get(lnum)) != NUL)
15268 break;
15269 }
15270 rettv->vval.v_number = lnum;
15271}
15272
15273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 * "nr2char()" function
15275 */
15276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015277f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015278 typval_T *argvars;
15279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280{
15281 char_u buf[NUMBUFLEN];
15282
15283#ifdef FEAT_MBYTE
15284 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015285 {
15286 int utf8 = 0;
15287
15288 if (argvars[1].v_type != VAR_UNKNOWN)
15289 utf8 = get_tv_number_chk(&argvars[1], NULL);
15290 if (utf8)
15291 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15292 else
15293 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295 else
15296#endif
15297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015298 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299 buf[1] = NUL;
15300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 rettv->v_type = VAR_STRING;
15302 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015303}
15304
15305/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015306 * "or(expr, expr)" function
15307 */
15308 static void
15309f_or(argvars, rettv)
15310 typval_T *argvars;
15311 typval_T *rettv;
15312{
15313 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15314 | get_tv_number_chk(&argvars[1], NULL);
15315}
15316
15317/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015318 * "pathshorten()" function
15319 */
15320 static void
15321f_pathshorten(argvars, rettv)
15322 typval_T *argvars;
15323 typval_T *rettv;
15324{
15325 char_u *p;
15326
15327 rettv->v_type = VAR_STRING;
15328 p = get_tv_string_chk(&argvars[0]);
15329 if (p == NULL)
15330 rettv->vval.v_string = NULL;
15331 else
15332 {
15333 p = vim_strsave(p);
15334 rettv->vval.v_string = p;
15335 if (p != NULL)
15336 shorten_dir(p);
15337 }
15338}
15339
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015340#ifdef FEAT_FLOAT
15341/*
15342 * "pow()" function
15343 */
15344 static void
15345f_pow(argvars, rettv)
15346 typval_T *argvars;
15347 typval_T *rettv;
15348{
15349 float_T fx, fy;
15350
15351 rettv->v_type = VAR_FLOAT;
15352 if (get_float_arg(argvars, &fx) == OK
15353 && get_float_arg(&argvars[1], &fy) == OK)
15354 rettv->vval.v_float = pow(fx, fy);
15355 else
15356 rettv->vval.v_float = 0.0;
15357}
15358#endif
15359
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015361 * "prevnonblank()" function
15362 */
15363 static void
15364f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015365 typval_T *argvars;
15366 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015367{
15368 linenr_T lnum;
15369
15370 lnum = get_tv_lnum(argvars);
15371 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15372 lnum = 0;
15373 else
15374 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15375 --lnum;
15376 rettv->vval.v_number = lnum;
15377}
15378
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015379#ifdef HAVE_STDARG_H
15380/* This dummy va_list is here because:
15381 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15382 * - locally in the function results in a "used before set" warning
15383 * - using va_start() to initialize it gives "function with fixed args" error */
15384static va_list ap;
15385#endif
15386
Bram Moolenaar8c711452005-01-14 21:53:12 +000015387/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015388 * "printf()" function
15389 */
15390 static void
15391f_printf(argvars, rettv)
15392 typval_T *argvars;
15393 typval_T *rettv;
15394{
15395 rettv->v_type = VAR_STRING;
15396 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015397#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015398 {
15399 char_u buf[NUMBUFLEN];
15400 int len;
15401 char_u *s;
15402 int saved_did_emsg = did_emsg;
15403 char *fmt;
15404
15405 /* Get the required length, allocate the buffer and do it for real. */
15406 did_emsg = FALSE;
15407 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015408 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015409 if (!did_emsg)
15410 {
15411 s = alloc(len + 1);
15412 if (s != NULL)
15413 {
15414 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015415 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015416 }
15417 }
15418 did_emsg |= saved_did_emsg;
15419 }
15420#endif
15421}
15422
15423/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015424 * "pumvisible()" function
15425 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015426 static void
15427f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015428 typval_T *argvars UNUSED;
15429 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015430{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015431#ifdef FEAT_INS_EXPAND
15432 if (pum_visible())
15433 rettv->vval.v_number = 1;
15434#endif
15435}
15436
Bram Moolenaardb913952012-06-29 12:54:53 +020015437#ifdef FEAT_PYTHON3
15438/*
15439 * "py3eval()" function
15440 */
15441 static void
15442f_py3eval(argvars, rettv)
15443 typval_T *argvars;
15444 typval_T *rettv;
15445{
15446 char_u *str;
15447 char_u buf[NUMBUFLEN];
15448
15449 str = get_tv_string_buf(&argvars[0], buf);
15450 do_py3eval(str, rettv);
15451}
15452#endif
15453
15454#ifdef FEAT_PYTHON
15455/*
15456 * "pyeval()" function
15457 */
15458 static void
15459f_pyeval(argvars, rettv)
15460 typval_T *argvars;
15461 typval_T *rettv;
15462{
15463 char_u *str;
15464 char_u buf[NUMBUFLEN];
15465
15466 str = get_tv_string_buf(&argvars[0], buf);
15467 do_pyeval(str, rettv);
15468}
15469#endif
15470
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015471/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015472 * "range()" function
15473 */
15474 static void
15475f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015476 typval_T *argvars;
15477 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015478{
15479 long start;
15480 long end;
15481 long stride = 1;
15482 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015483 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015485 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015486 if (argvars[1].v_type == VAR_UNKNOWN)
15487 {
15488 end = start - 1;
15489 start = 0;
15490 }
15491 else
15492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015493 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015494 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015495 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015496 }
15497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015498 if (error)
15499 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015500 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015501 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015502 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015503 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015504 else
15505 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015506 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015507 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015508 if (list_append_number(rettv->vval.v_list,
15509 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015510 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015511 }
15512}
15513
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015514/*
15515 * "readfile()" function
15516 */
15517 static void
15518f_readfile(argvars, rettv)
15519 typval_T *argvars;
15520 typval_T *rettv;
15521{
15522 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015523 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015524 char_u *fname;
15525 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015526 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15527 int io_size = sizeof(buf);
15528 int readlen; /* size of last fread() */
15529 char_u *prev = NULL; /* previously read bytes, if any */
15530 long prevlen = 0; /* length of data in prev */
15531 long prevsize = 0; /* size of prev buffer */
15532 long maxline = MAXLNUM;
15533 long cnt = 0;
15534 char_u *p; /* position in buf */
15535 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015536
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015537 if (argvars[1].v_type != VAR_UNKNOWN)
15538 {
15539 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15540 binary = TRUE;
15541 if (argvars[2].v_type != VAR_UNKNOWN)
15542 maxline = get_tv_number(&argvars[2]);
15543 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015544
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015545 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015546 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015547
15548 /* Always open the file in binary mode, library functions have a mind of
15549 * their own about CR-LF conversion. */
15550 fname = get_tv_string(&argvars[0]);
15551 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15552 {
15553 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15554 return;
15555 }
15556
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015557 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015558 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015559 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015560
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015561 /* This for loop processes what was read, but is also entered at end
15562 * of file so that either:
15563 * - an incomplete line gets written
15564 * - a "binary" file gets an empty line at the end if it ends in a
15565 * newline. */
15566 for (p = buf, start = buf;
15567 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15568 ++p)
15569 {
15570 if (*p == '\n' || readlen <= 0)
15571 {
15572 listitem_T *li;
15573 char_u *s = NULL;
15574 long_u len = p - start;
15575
15576 /* Finished a line. Remove CRs before NL. */
15577 if (readlen > 0 && !binary)
15578 {
15579 while (len > 0 && start[len - 1] == '\r')
15580 --len;
15581 /* removal may cross back to the "prev" string */
15582 if (len == 0)
15583 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15584 --prevlen;
15585 }
15586 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015587 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015588 else
15589 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015590 /* Change "prev" buffer to be the right size. This way
15591 * the bytes are only copied once, and very long lines are
15592 * allocated only once. */
15593 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015594 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015595 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015596 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015597 prev = NULL; /* the list will own the string */
15598 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015599 }
15600 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015601 if (s == NULL)
15602 {
15603 do_outofmem_msg((long_u) prevlen + len + 1);
15604 failed = TRUE;
15605 break;
15606 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015607
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015608 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015609 {
15610 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015611 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015612 break;
15613 }
15614 li->li_tv.v_type = VAR_STRING;
15615 li->li_tv.v_lock = 0;
15616 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015617 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015618
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015619 start = p + 1; /* step over newline */
15620 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015621 break;
15622 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015623 else if (*p == NUL)
15624 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015625#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015626 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15627 * when finding the BF and check the previous two bytes. */
15628 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015629 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015630 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15631 * + 1, these may be in the "prev" string. */
15632 char_u back1 = p >= buf + 1 ? p[-1]
15633 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15634 char_u back2 = p >= buf + 2 ? p[-2]
15635 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15636 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015637
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015638 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015639 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015640 char_u *dest = p - 2;
15641
15642 /* Usually a BOM is at the beginning of a file, and so at
15643 * the beginning of a line; then we can just step over it.
15644 */
15645 if (start == dest)
15646 start = p + 1;
15647 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015648 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015649 /* have to shuffle buf to close gap */
15650 int adjust_prevlen = 0;
15651
15652 if (dest < buf)
15653 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015654 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015655 dest = buf;
15656 }
15657 if (readlen > p - buf + 1)
15658 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15659 readlen -= 3 - adjust_prevlen;
15660 prevlen -= adjust_prevlen;
15661 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015662 }
15663 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015664 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015665#endif
15666 } /* for */
15667
15668 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15669 break;
15670 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015671 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015672 /* There's part of a line in buf, store it in "prev". */
15673 if (p - start + prevlen >= prevsize)
15674 {
15675 /* need bigger "prev" buffer */
15676 char_u *newprev;
15677
15678 /* A common use case is ordinary text files and "prev" gets a
15679 * fragment of a line, so the first allocation is made
15680 * small, to avoid repeatedly 'allocing' large and
15681 * 'reallocing' small. */
15682 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015683 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015684 else
15685 {
15686 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015687 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015688 prevsize = grow50pc > growmin ? grow50pc : growmin;
15689 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015690 newprev = prev == NULL ? alloc(prevsize)
15691 : vim_realloc(prev, prevsize);
15692 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015693 {
15694 do_outofmem_msg((long_u)prevsize);
15695 failed = TRUE;
15696 break;
15697 }
15698 prev = newprev;
15699 }
15700 /* Add the line part to end of "prev". */
15701 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015702 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015703 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015704 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015705
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015706 /*
15707 * For a negative line count use only the lines at the end of the file,
15708 * free the rest.
15709 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015710 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015711 while (cnt > -maxline)
15712 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015713 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015714 --cnt;
15715 }
15716
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015717 if (failed)
15718 {
15719 list_free(rettv->vval.v_list, TRUE);
15720 /* readfile doc says an empty list is returned on error */
15721 rettv->vval.v_list = list_alloc();
15722 }
15723
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015724 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015725 fclose(fd);
15726}
15727
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015728#if defined(FEAT_RELTIME)
15729static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15730
15731/*
15732 * Convert a List to proftime_T.
15733 * Return FAIL when there is something wrong.
15734 */
15735 static int
15736list2proftime(arg, tm)
15737 typval_T *arg;
15738 proftime_T *tm;
15739{
15740 long n1, n2;
15741 int error = FALSE;
15742
15743 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15744 || arg->vval.v_list->lv_len != 2)
15745 return FAIL;
15746 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15747 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15748# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015749 tm->HighPart = n1;
15750 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015751# else
15752 tm->tv_sec = n1;
15753 tm->tv_usec = n2;
15754# endif
15755 return error ? FAIL : OK;
15756}
15757#endif /* FEAT_RELTIME */
15758
15759/*
15760 * "reltime()" function
15761 */
15762 static void
15763f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015764 typval_T *argvars UNUSED;
15765 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015766{
15767#ifdef FEAT_RELTIME
15768 proftime_T res;
15769 proftime_T start;
15770
15771 if (argvars[0].v_type == VAR_UNKNOWN)
15772 {
15773 /* No arguments: get current time. */
15774 profile_start(&res);
15775 }
15776 else if (argvars[1].v_type == VAR_UNKNOWN)
15777 {
15778 if (list2proftime(&argvars[0], &res) == FAIL)
15779 return;
15780 profile_end(&res);
15781 }
15782 else
15783 {
15784 /* Two arguments: compute the difference. */
15785 if (list2proftime(&argvars[0], &start) == FAIL
15786 || list2proftime(&argvars[1], &res) == FAIL)
15787 return;
15788 profile_sub(&res, &start);
15789 }
15790
15791 if (rettv_list_alloc(rettv) == OK)
15792 {
15793 long n1, n2;
15794
15795# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015796 n1 = res.HighPart;
15797 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015798# else
15799 n1 = res.tv_sec;
15800 n2 = res.tv_usec;
15801# endif
15802 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15803 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15804 }
15805#endif
15806}
15807
15808/*
15809 * "reltimestr()" function
15810 */
15811 static void
15812f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015813 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015814 typval_T *rettv;
15815{
15816#ifdef FEAT_RELTIME
15817 proftime_T tm;
15818#endif
15819
15820 rettv->v_type = VAR_STRING;
15821 rettv->vval.v_string = NULL;
15822#ifdef FEAT_RELTIME
15823 if (list2proftime(&argvars[0], &tm) == OK)
15824 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15825#endif
15826}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015827
Bram Moolenaar0d660222005-01-07 21:51:51 +000015828#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15829static void make_connection __ARGS((void));
15830static int check_connection __ARGS((void));
15831
15832 static void
15833make_connection()
15834{
15835 if (X_DISPLAY == NULL
15836# ifdef FEAT_GUI
15837 && !gui.in_use
15838# endif
15839 )
15840 {
15841 x_force_connect = TRUE;
15842 setup_term_clip();
15843 x_force_connect = FALSE;
15844 }
15845}
15846
15847 static int
15848check_connection()
15849{
15850 make_connection();
15851 if (X_DISPLAY == NULL)
15852 {
15853 EMSG(_("E240: No connection to Vim server"));
15854 return FAIL;
15855 }
15856 return OK;
15857}
15858#endif
15859
15860#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015861static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015862
15863 static void
15864remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015865 typval_T *argvars;
15866 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867 int expr;
15868{
15869 char_u *server_name;
15870 char_u *keys;
15871 char_u *r = NULL;
15872 char_u buf[NUMBUFLEN];
15873# ifdef WIN32
15874 HWND w;
15875# else
15876 Window w;
15877# endif
15878
15879 if (check_restricted() || check_secure())
15880 return;
15881
15882# ifdef FEAT_X11
15883 if (check_connection() == FAIL)
15884 return;
15885# endif
15886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015887 server_name = get_tv_string_chk(&argvars[0]);
15888 if (server_name == NULL)
15889 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015890 keys = get_tv_string_buf(&argvars[1], buf);
15891# ifdef WIN32
15892 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15893# else
15894 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15895 < 0)
15896# endif
15897 {
15898 if (r != NULL)
15899 EMSG(r); /* sending worked but evaluation failed */
15900 else
15901 EMSG2(_("E241: Unable to send to %s"), server_name);
15902 return;
15903 }
15904
15905 rettv->vval.v_string = r;
15906
15907 if (argvars[2].v_type != VAR_UNKNOWN)
15908 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015909 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015910 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015911 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015912
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015913 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015914 v.di_tv.v_type = VAR_STRING;
15915 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015916 idvar = get_tv_string_chk(&argvars[2]);
15917 if (idvar != NULL)
15918 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015919 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015920 }
15921}
15922#endif
15923
15924/*
15925 * "remote_expr()" function
15926 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015927 static void
15928f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015929 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015930 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015931{
15932 rettv->v_type = VAR_STRING;
15933 rettv->vval.v_string = NULL;
15934#ifdef FEAT_CLIENTSERVER
15935 remote_common(argvars, rettv, TRUE);
15936#endif
15937}
15938
15939/*
15940 * "remote_foreground()" function
15941 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942 static void
15943f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015944 typval_T *argvars UNUSED;
15945 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015947#ifdef FEAT_CLIENTSERVER
15948# ifdef WIN32
15949 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015950 {
15951 char_u *server_name = get_tv_string_chk(&argvars[0]);
15952
15953 if (server_name != NULL)
15954 serverForeground(server_name);
15955 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015956# else
15957 /* Send a foreground() expression to the server. */
15958 argvars[1].v_type = VAR_STRING;
15959 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15960 argvars[2].v_type = VAR_UNKNOWN;
15961 remote_common(argvars, rettv, TRUE);
15962 vim_free(argvars[1].vval.v_string);
15963# endif
15964#endif
15965}
15966
Bram Moolenaar0d660222005-01-07 21:51:51 +000015967 static void
15968f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015969 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015970 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971{
15972#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015973 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015974 char_u *s = NULL;
15975# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015976 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015977# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015978 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979
15980 if (check_restricted() || check_secure())
15981 {
15982 rettv->vval.v_number = -1;
15983 return;
15984 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015985 serverid = get_tv_string_chk(&argvars[0]);
15986 if (serverid == NULL)
15987 {
15988 rettv->vval.v_number = -1;
15989 return; /* type error; errmsg already given */
15990 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015991# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015992 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015993 if (n == 0)
15994 rettv->vval.v_number = -1;
15995 else
15996 {
15997 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15998 rettv->vval.v_number = (s != NULL);
15999 }
16000# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016001 if (check_connection() == FAIL)
16002 return;
16003
16004 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016005 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016006# endif
16007
16008 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016010 char_u *retvar;
16011
Bram Moolenaar33570922005-01-25 22:26:29 +000016012 v.di_tv.v_type = VAR_STRING;
16013 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016014 retvar = get_tv_string_chk(&argvars[1]);
16015 if (retvar != NULL)
16016 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016017 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016018 }
16019#else
16020 rettv->vval.v_number = -1;
16021#endif
16022}
16023
Bram Moolenaar0d660222005-01-07 21:51:51 +000016024 static void
16025f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016026 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016027 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016028{
16029 char_u *r = NULL;
16030
16031#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016032 char_u *serverid = get_tv_string_chk(&argvars[0]);
16033
16034 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016035 {
16036# ifdef WIN32
16037 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016038 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016039
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016040 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016041 if (n != 0)
16042 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16043 if (r == NULL)
16044# else
16045 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016046 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047# endif
16048 EMSG(_("E277: Unable to read a server reply"));
16049 }
16050#endif
16051 rettv->v_type = VAR_STRING;
16052 rettv->vval.v_string = r;
16053}
16054
16055/*
16056 * "remote_send()" function
16057 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058 static void
16059f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016060 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062{
16063 rettv->v_type = VAR_STRING;
16064 rettv->vval.v_string = NULL;
16065#ifdef FEAT_CLIENTSERVER
16066 remote_common(argvars, rettv, FALSE);
16067#endif
16068}
16069
16070/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016071 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016072 */
16073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016075 typval_T *argvars;
16076 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016077{
Bram Moolenaar33570922005-01-25 22:26:29 +000016078 list_T *l;
16079 listitem_T *item, *item2;
16080 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016081 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016082 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016083 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016084 dict_T *d;
16085 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016086 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016087
Bram Moolenaar8c711452005-01-14 21:53:12 +000016088 if (argvars[0].v_type == VAR_DICT)
16089 {
16090 if (argvars[2].v_type != VAR_UNKNOWN)
16091 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016092 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016093 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016095 key = get_tv_string_chk(&argvars[1]);
16096 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098 di = dict_find(d, key, -1);
16099 if (di == NULL)
16100 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016101 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16102 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016103 {
16104 *rettv = di->di_tv;
16105 init_tv(&di->di_tv);
16106 dictitem_remove(d, di);
16107 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016108 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016109 }
16110 }
16111 else if (argvars[0].v_type != VAR_LIST)
16112 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016113 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016114 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016116 int error = FALSE;
16117
16118 idx = get_tv_number_chk(&argvars[1], &error);
16119 if (error)
16120 ; /* type error: do nothing, errmsg already given */
16121 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016122 EMSGN(_(e_listidx), idx);
16123 else
16124 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016125 if (argvars[2].v_type == VAR_UNKNOWN)
16126 {
16127 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016128 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016129 *rettv = item->li_tv;
16130 vim_free(item);
16131 }
16132 else
16133 {
16134 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016135 end = get_tv_number_chk(&argvars[2], &error);
16136 if (error)
16137 ; /* type error: do nothing */
16138 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016139 EMSGN(_(e_listidx), end);
16140 else
16141 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016142 int cnt = 0;
16143
16144 for (li = item; li != NULL; li = li->li_next)
16145 {
16146 ++cnt;
16147 if (li == item2)
16148 break;
16149 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016150 if (li == NULL) /* didn't find "item2" after "item" */
16151 EMSG(_(e_invrange));
16152 else
16153 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016154 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016155 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016156 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016157 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016158 l->lv_first = item;
16159 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016160 item->li_prev = NULL;
16161 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016162 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016163 }
16164 }
16165 }
16166 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016167 }
16168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169}
16170
16171/*
16172 * "rename({from}, {to})" function
16173 */
16174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 typval_T *argvars;
16177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178{
16179 char_u buf[NUMBUFLEN];
16180
16181 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016184 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16185 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186}
16187
16188/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016189 * "repeat()" function
16190 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016191 static void
16192f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016193 typval_T *argvars;
16194 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016195{
16196 char_u *p;
16197 int n;
16198 int slen;
16199 int len;
16200 char_u *r;
16201 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016202
16203 n = get_tv_number(&argvars[1]);
16204 if (argvars[0].v_type == VAR_LIST)
16205 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016206 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016207 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016208 if (list_extend(rettv->vval.v_list,
16209 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016210 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016211 }
16212 else
16213 {
16214 p = get_tv_string(&argvars[0]);
16215 rettv->v_type = VAR_STRING;
16216 rettv->vval.v_string = NULL;
16217
16218 slen = (int)STRLEN(p);
16219 len = slen * n;
16220 if (len <= 0)
16221 return;
16222
16223 r = alloc(len + 1);
16224 if (r != NULL)
16225 {
16226 for (i = 0; i < n; i++)
16227 mch_memmove(r + i * slen, p, (size_t)slen);
16228 r[len] = NUL;
16229 }
16230
16231 rettv->vval.v_string = r;
16232 }
16233}
16234
16235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 * "resolve()" function
16237 */
16238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016239f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016240 typval_T *argvars;
16241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242{
16243 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016244#ifdef HAVE_READLINK
16245 char_u *buf = NULL;
16246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016248 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249#ifdef FEAT_SHORTCUT
16250 {
16251 char_u *v = NULL;
16252
16253 v = mch_resolve_shortcut(p);
16254 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016255 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 }
16259#else
16260# ifdef HAVE_READLINK
16261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 char_u *cpy;
16263 int len;
16264 char_u *remain = NULL;
16265 char_u *q;
16266 int is_relative_to_current = FALSE;
16267 int has_trailing_pathsep = FALSE;
16268 int limit = 100;
16269
16270 p = vim_strsave(p);
16271
16272 if (p[0] == '.' && (vim_ispathsep(p[1])
16273 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16274 is_relative_to_current = TRUE;
16275
16276 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016277 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016280 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282
16283 q = getnextcomp(p);
16284 if (*q != NUL)
16285 {
16286 /* Separate the first path component in "p", and keep the
16287 * remainder (beginning with the path separator). */
16288 remain = vim_strsave(q - 1);
16289 q[-1] = NUL;
16290 }
16291
Bram Moolenaard9462e32011-04-11 21:35:11 +020016292 buf = alloc(MAXPATHL + 1);
16293 if (buf == NULL)
16294 goto fail;
16295
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 for (;;)
16297 {
16298 for (;;)
16299 {
16300 len = readlink((char *)p, (char *)buf, MAXPATHL);
16301 if (len <= 0)
16302 break;
16303 buf[len] = NUL;
16304
16305 if (limit-- == 0)
16306 {
16307 vim_free(p);
16308 vim_free(remain);
16309 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016310 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 goto fail;
16312 }
16313
16314 /* Ensure that the result will have a trailing path separator
16315 * if the argument has one. */
16316 if (remain == NULL && has_trailing_pathsep)
16317 add_pathsep(buf);
16318
16319 /* Separate the first path component in the link value and
16320 * concatenate the remainders. */
16321 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16322 if (*q != NUL)
16323 {
16324 if (remain == NULL)
16325 remain = vim_strsave(q - 1);
16326 else
16327 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016328 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 if (cpy != NULL)
16330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 vim_free(remain);
16332 remain = cpy;
16333 }
16334 }
16335 q[-1] = NUL;
16336 }
16337
16338 q = gettail(p);
16339 if (q > p && *q == NUL)
16340 {
16341 /* Ignore trailing path separator. */
16342 q[-1] = NUL;
16343 q = gettail(p);
16344 }
16345 if (q > p && !mch_isFullName(buf))
16346 {
16347 /* symlink is relative to directory of argument */
16348 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16349 if (cpy != NULL)
16350 {
16351 STRCPY(cpy, p);
16352 STRCPY(gettail(cpy), buf);
16353 vim_free(p);
16354 p = cpy;
16355 }
16356 }
16357 else
16358 {
16359 vim_free(p);
16360 p = vim_strsave(buf);
16361 }
16362 }
16363
16364 if (remain == NULL)
16365 break;
16366
16367 /* Append the first path component of "remain" to "p". */
16368 q = getnextcomp(remain + 1);
16369 len = q - remain - (*q != NUL);
16370 cpy = vim_strnsave(p, STRLEN(p) + len);
16371 if (cpy != NULL)
16372 {
16373 STRNCAT(cpy, remain, len);
16374 vim_free(p);
16375 p = cpy;
16376 }
16377 /* Shorten "remain". */
16378 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016379 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 else
16381 {
16382 vim_free(remain);
16383 remain = NULL;
16384 }
16385 }
16386
16387 /* If the result is a relative path name, make it explicitly relative to
16388 * the current directory if and only if the argument had this form. */
16389 if (!vim_ispathsep(*p))
16390 {
16391 if (is_relative_to_current
16392 && *p != NUL
16393 && !(p[0] == '.'
16394 && (p[1] == NUL
16395 || vim_ispathsep(p[1])
16396 || (p[1] == '.'
16397 && (p[2] == NUL
16398 || vim_ispathsep(p[2]))))))
16399 {
16400 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016401 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 if (cpy != NULL)
16403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 vim_free(p);
16405 p = cpy;
16406 }
16407 }
16408 else if (!is_relative_to_current)
16409 {
16410 /* Strip leading "./". */
16411 q = p;
16412 while (q[0] == '.' && vim_ispathsep(q[1]))
16413 q += 2;
16414 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016415 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 }
16417 }
16418
16419 /* Ensure that the result will have no trailing path separator
16420 * if the argument had none. But keep "/" or "//". */
16421 if (!has_trailing_pathsep)
16422 {
16423 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016424 if (after_pathsep(p, q))
16425 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 }
16427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016428 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 }
16430# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016431 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432# endif
16433#endif
16434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016435 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436
16437#ifdef HAVE_READLINK
16438fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016439 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016441 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442}
16443
16444/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 */
16447 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016448f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 typval_T *argvars;
16450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451{
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 list_T *l;
16453 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455 if (argvars[0].v_type != VAR_LIST)
16456 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016457 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016458 && !tv_check_lock(l->lv_lock,
16459 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016460 {
16461 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016462 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016463 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464 while (li != NULL)
16465 {
16466 ni = li->li_prev;
16467 list_append(l, li);
16468 li = ni;
16469 }
16470 rettv->vval.v_list = l;
16471 rettv->v_type = VAR_LIST;
16472 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016473 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475}
16476
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016477#define SP_NOMOVE 0x01 /* don't move cursor */
16478#define SP_REPEAT 0x02 /* repeat to find outer pair */
16479#define SP_RETCOUNT 0x04 /* return matchcount */
16480#define SP_SETPCMARK 0x08 /* set previous context mark */
16481#define SP_START 0x10 /* accept match at start position */
16482#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16483#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016484#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016485
Bram Moolenaar33570922005-01-25 22:26:29 +000016486static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487
16488/*
16489 * Get flags for a search function.
16490 * Possibly sets "p_ws".
16491 * Returns BACKWARD, FORWARD or zero (for an error).
16492 */
16493 static int
16494get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016495 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016496 int *flagsp;
16497{
16498 int dir = FORWARD;
16499 char_u *flags;
16500 char_u nbuf[NUMBUFLEN];
16501 int mask;
16502
16503 if (varp->v_type != VAR_UNKNOWN)
16504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016505 flags = get_tv_string_buf_chk(varp, nbuf);
16506 if (flags == NULL)
16507 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508 while (*flags != NUL)
16509 {
16510 switch (*flags)
16511 {
16512 case 'b': dir = BACKWARD; break;
16513 case 'w': p_ws = TRUE; break;
16514 case 'W': p_ws = FALSE; break;
16515 default: mask = 0;
16516 if (flagsp != NULL)
16517 switch (*flags)
16518 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016519 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016520 case 'e': mask = SP_END; break;
16521 case 'm': mask = SP_RETCOUNT; break;
16522 case 'n': mask = SP_NOMOVE; break;
16523 case 'p': mask = SP_SUBPAT; break;
16524 case 'r': mask = SP_REPEAT; break;
16525 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016526 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016527 }
16528 if (mask == 0)
16529 {
16530 EMSG2(_(e_invarg2), flags);
16531 dir = 0;
16532 }
16533 else
16534 *flagsp |= mask;
16535 }
16536 if (dir == 0)
16537 break;
16538 ++flags;
16539 }
16540 }
16541 return dir;
16542}
16543
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016545 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016547 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016548search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016549 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016550 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016551 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016553 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 char_u *pat;
16555 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016556 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 int save_p_ws = p_ws;
16558 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016559 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016560 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016561 proftime_T tm;
16562#ifdef FEAT_RELTIME
16563 long time_limit = 0;
16564#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016565 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016566 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016568 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016569 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016570 if (dir == 0)
16571 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016572 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016573 if (flags & SP_START)
16574 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016575 if (flags & SP_END)
16576 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016577 if (flags & SP_COLUMN)
16578 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016579
Bram Moolenaar76929292008-01-06 19:07:36 +000016580 /* Optional arguments: line number to stop searching and timeout. */
16581 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016582 {
16583 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16584 if (lnum_stop < 0)
16585 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016586#ifdef FEAT_RELTIME
16587 if (argvars[3].v_type != VAR_UNKNOWN)
16588 {
16589 time_limit = get_tv_number_chk(&argvars[3], NULL);
16590 if (time_limit < 0)
16591 goto theend;
16592 }
16593#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016594 }
16595
Bram Moolenaar76929292008-01-06 19:07:36 +000016596#ifdef FEAT_RELTIME
16597 /* Set the time limit, if there is one. */
16598 profile_setlimit(time_limit, &tm);
16599#endif
16600
Bram Moolenaar231334e2005-07-25 20:46:57 +000016601 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016602 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016603 * Check to make sure only those flags are set.
16604 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16605 * flags cannot be set. Check for that condition also.
16606 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016607 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016608 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016610 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016611 goto theend;
16612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016614 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016615 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016616 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016617 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016619 if (flags & SP_SUBPAT)
16620 retval = subpatnum;
16621 else
16622 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016623 if (flags & SP_SETPCMARK)
16624 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016626 if (match_pos != NULL)
16627 {
16628 /* Store the match cursor position */
16629 match_pos->lnum = pos.lnum;
16630 match_pos->col = pos.col + 1;
16631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 /* "/$" will put the cursor after the end of the line, may need to
16633 * correct that here */
16634 check_cursor();
16635 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016636
16637 /* If 'n' flag is used: restore cursor position. */
16638 if (flags & SP_NOMOVE)
16639 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016640 else
16641 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016642theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016644
16645 return retval;
16646}
16647
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016648#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016649
16650/*
16651 * round() is not in C90, use ceil() or floor() instead.
16652 */
16653 float_T
16654vim_round(f)
16655 float_T f;
16656{
16657 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16658}
16659
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016660/*
16661 * "round({float})" function
16662 */
16663 static void
16664f_round(argvars, rettv)
16665 typval_T *argvars;
16666 typval_T *rettv;
16667{
16668 float_T f;
16669
16670 rettv->v_type = VAR_FLOAT;
16671 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016672 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016673 else
16674 rettv->vval.v_float = 0.0;
16675}
16676#endif
16677
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016678/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016679 * "screenattr()" function
16680 */
16681 static void
16682f_screenattr(argvars, rettv)
16683 typval_T *argvars UNUSED;
16684 typval_T *rettv;
16685{
16686 int row;
16687 int col;
16688 int c;
16689
16690 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16691 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16692 if (row < 0 || row >= screen_Rows
16693 || col < 0 || col >= screen_Columns)
16694 c = -1;
16695 else
16696 c = ScreenAttrs[LineOffset[row] + col];
16697 rettv->vval.v_number = c;
16698}
16699
16700/*
16701 * "screenchar()" function
16702 */
16703 static void
16704f_screenchar(argvars, rettv)
16705 typval_T *argvars UNUSED;
16706 typval_T *rettv;
16707{
16708 int row;
16709 int col;
16710 int off;
16711 int c;
16712
16713 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16714 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16715 if (row < 0 || row >= screen_Rows
16716 || col < 0 || col >= screen_Columns)
16717 c = -1;
16718 else
16719 {
16720 off = LineOffset[row] + col;
16721#ifdef FEAT_MBYTE
16722 if (enc_utf8 && ScreenLinesUC[off] != 0)
16723 c = ScreenLinesUC[off];
16724 else
16725#endif
16726 c = ScreenLines[off];
16727 }
16728 rettv->vval.v_number = c;
16729}
16730
16731/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016732 * "screencol()" function
16733 *
16734 * First column is 1 to be consistent with virtcol().
16735 */
16736 static void
16737f_screencol(argvars, rettv)
16738 typval_T *argvars UNUSED;
16739 typval_T *rettv;
16740{
16741 rettv->vval.v_number = screen_screencol() + 1;
16742}
16743
16744/*
16745 * "screenrow()" function
16746 */
16747 static void
16748f_screenrow(argvars, rettv)
16749 typval_T *argvars UNUSED;
16750 typval_T *rettv;
16751{
16752 rettv->vval.v_number = screen_screenrow() + 1;
16753}
16754
16755/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016756 * "search()" function
16757 */
16758 static void
16759f_search(argvars, rettv)
16760 typval_T *argvars;
16761 typval_T *rettv;
16762{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016763 int flags = 0;
16764
16765 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766}
16767
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016769 * "searchdecl()" function
16770 */
16771 static void
16772f_searchdecl(argvars, rettv)
16773 typval_T *argvars;
16774 typval_T *rettv;
16775{
16776 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016777 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016778 int error = FALSE;
16779 char_u *name;
16780
16781 rettv->vval.v_number = 1; /* default: FAIL */
16782
16783 name = get_tv_string_chk(&argvars[0]);
16784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016785 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016786 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016787 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16788 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16789 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016790 if (!error && name != NULL)
16791 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016792 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016793}
16794
16795/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016796 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016798 static int
16799searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016800 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016801 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802{
16803 char_u *spat, *mpat, *epat;
16804 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806 int dir;
16807 int flags = 0;
16808 char_u nbuf1[NUMBUFLEN];
16809 char_u nbuf2[NUMBUFLEN];
16810 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016811 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016812 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016813 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016816 spat = get_tv_string_chk(&argvars[0]);
16817 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16818 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16819 if (spat == NULL || mpat == NULL || epat == NULL)
16820 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 /* Handle the optional fourth argument: flags */
16823 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016824 if (dir == 0)
16825 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016826
16827 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016828 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16829 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016830 if ((flags & (SP_END | SP_SUBPAT)) != 0
16831 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016832 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016833 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016834 goto theend;
16835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016837 /* Using 'r' implies 'W', otherwise it doesn't work. */
16838 if (flags & SP_REPEAT)
16839 p_ws = FALSE;
16840
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016841 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016842 if (argvars[3].v_type == VAR_UNKNOWN
16843 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 skip = (char_u *)"";
16845 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016847 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016848 if (argvars[5].v_type != VAR_UNKNOWN)
16849 {
16850 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16851 if (lnum_stop < 0)
16852 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016853#ifdef FEAT_RELTIME
16854 if (argvars[6].v_type != VAR_UNKNOWN)
16855 {
16856 time_limit = get_tv_number_chk(&argvars[6], NULL);
16857 if (time_limit < 0)
16858 goto theend;
16859 }
16860#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016861 }
16862 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016863 if (skip == NULL)
16864 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016866 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016867 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016868
16869theend:
16870 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016871
16872 return retval;
16873}
16874
16875/*
16876 * "searchpair()" function
16877 */
16878 static void
16879f_searchpair(argvars, rettv)
16880 typval_T *argvars;
16881 typval_T *rettv;
16882{
16883 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16884}
16885
16886/*
16887 * "searchpairpos()" function
16888 */
16889 static void
16890f_searchpairpos(argvars, rettv)
16891 typval_T *argvars;
16892 typval_T *rettv;
16893{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016894 pos_T match_pos;
16895 int lnum = 0;
16896 int col = 0;
16897
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016898 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016899 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016900
16901 if (searchpair_cmn(argvars, &match_pos) > 0)
16902 {
16903 lnum = match_pos.lnum;
16904 col = match_pos.col;
16905 }
16906
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016907 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16908 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016909}
16910
16911/*
16912 * Search for a start/middle/end thing.
16913 * Used by searchpair(), see its documentation for the details.
16914 * Returns 0 or -1 for no match,
16915 */
16916 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016917do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16918 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016919 char_u *spat; /* start pattern */
16920 char_u *mpat; /* middle pattern */
16921 char_u *epat; /* end pattern */
16922 int dir; /* BACKWARD or FORWARD */
16923 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016924 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016925 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016926 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016927 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016928{
16929 char_u *save_cpo;
16930 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16931 long retval = 0;
16932 pos_T pos;
16933 pos_T firstpos;
16934 pos_T foundpos;
16935 pos_T save_cursor;
16936 pos_T save_pos;
16937 int n;
16938 int r;
16939 int nest = 1;
16940 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016941 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016942 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016943
16944 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16945 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016946 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016947
Bram Moolenaar76929292008-01-06 19:07:36 +000016948#ifdef FEAT_RELTIME
16949 /* Set the time limit, if there is one. */
16950 profile_setlimit(time_limit, &tm);
16951#endif
16952
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016953 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16954 * start/middle/end (pat3, for the top pair). */
16955 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16956 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16957 if (pat2 == NULL || pat3 == NULL)
16958 goto theend;
16959 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16960 if (*mpat == NUL)
16961 STRCPY(pat3, pat2);
16962 else
16963 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16964 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016965 if (flags & SP_START)
16966 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016967
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 save_cursor = curwin->w_cursor;
16969 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016970 clearpos(&firstpos);
16971 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 pat = pat3;
16973 for (;;)
16974 {
16975 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016976 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16978 /* didn't find it or found the first match again: FAIL */
16979 break;
16980
16981 if (firstpos.lnum == 0)
16982 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016983 if (equalpos(pos, foundpos))
16984 {
16985 /* Found the same position again. Can happen with a pattern that
16986 * has "\zs" at the end and searching backwards. Advance one
16987 * character and try again. */
16988 if (dir == BACKWARD)
16989 decl(&pos);
16990 else
16991 incl(&pos);
16992 }
16993 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016995 /* clear the start flag to avoid getting stuck here */
16996 options &= ~SEARCH_START;
16997
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 /* If the skip pattern matches, ignore this match. */
16999 if (*skip != NUL)
17000 {
17001 save_pos = curwin->w_cursor;
17002 curwin->w_cursor = pos;
17003 r = eval_to_bool(skip, &err, NULL, FALSE);
17004 curwin->w_cursor = save_pos;
17005 if (err)
17006 {
17007 /* Evaluating {skip} caused an error, break here. */
17008 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017009 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 break;
17011 }
17012 if (r)
17013 continue;
17014 }
17015
17016 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17017 {
17018 /* Found end when searching backwards or start when searching
17019 * forward: nested pair. */
17020 ++nest;
17021 pat = pat2; /* nested, don't search for middle */
17022 }
17023 else
17024 {
17025 /* Found end when searching forward or start when searching
17026 * backward: end of (nested) pair; or found middle in outer pair. */
17027 if (--nest == 1)
17028 pat = pat3; /* outer level, search for middle */
17029 }
17030
17031 if (nest == 0)
17032 {
17033 /* Found the match: return matchcount or line number. */
17034 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017035 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017037 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017038 if (flags & SP_SETPCMARK)
17039 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 curwin->w_cursor = pos;
17041 if (!(flags & SP_REPEAT))
17042 break;
17043 nest = 1; /* search for next unmatched */
17044 }
17045 }
17046
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017047 if (match_pos != NULL)
17048 {
17049 /* Store the match cursor position */
17050 match_pos->lnum = curwin->w_cursor.lnum;
17051 match_pos->col = curwin->w_cursor.col + 1;
17052 }
17053
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017055 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 curwin->w_cursor = save_cursor;
17057
17058theend:
17059 vim_free(pat2);
17060 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017061 if (p_cpo == empty_option)
17062 p_cpo = save_cpo;
17063 else
17064 /* Darn, evaluating the {skip} expression changed the value. */
17065 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017066
17067 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068}
17069
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017070/*
17071 * "searchpos()" function
17072 */
17073 static void
17074f_searchpos(argvars, rettv)
17075 typval_T *argvars;
17076 typval_T *rettv;
17077{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017078 pos_T match_pos;
17079 int lnum = 0;
17080 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017081 int n;
17082 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017084 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017085 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017086
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017087 n = search_cmn(argvars, &match_pos, &flags);
17088 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017089 {
17090 lnum = match_pos.lnum;
17091 col = match_pos.col;
17092 }
17093
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017094 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17095 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017096 if (flags & SP_SUBPAT)
17097 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017098}
17099
17100
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 static void
17102f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017103 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106#ifdef FEAT_CLIENTSERVER
17107 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017108 char_u *server = get_tv_string_chk(&argvars[0]);
17109 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110
Bram Moolenaar0d660222005-01-07 21:51:51 +000017111 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017112 if (server == NULL || reply == NULL)
17113 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114 if (check_restricted() || check_secure())
17115 return;
17116# ifdef FEAT_X11
17117 if (check_connection() == FAIL)
17118 return;
17119# endif
17120
17121 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123 EMSG(_("E258: Unable to send to client"));
17124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126 rettv->vval.v_number = 0;
17127#else
17128 rettv->vval.v_number = -1;
17129#endif
17130}
17131
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132 static void
17133f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017136{
17137 char_u *r = NULL;
17138
17139#ifdef FEAT_CLIENTSERVER
17140# ifdef WIN32
17141 r = serverGetVimNames();
17142# else
17143 make_connection();
17144 if (X_DISPLAY != NULL)
17145 r = serverGetVimNames(X_DISPLAY);
17146# endif
17147#endif
17148 rettv->v_type = VAR_STRING;
17149 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150}
17151
17152/*
17153 * "setbufvar()" function
17154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017156f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017157 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017158 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159{
17160 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017163 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 char_u nbuf[NUMBUFLEN];
17165
17166 if (check_restricted() || check_secure())
17167 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017168 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17169 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017170 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 varp = &argvars[2];
17172
17173 if (buf != NULL && varname != NULL && varp != NULL)
17174 {
17175 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177
17178 if (*varname == '&')
17179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017180 long numval;
17181 char_u *strval;
17182 int error = FALSE;
17183
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017185 numval = get_tv_number_chk(varp, &error);
17186 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017187 if (!error && strval != NULL)
17188 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 }
17190 else
17191 {
17192 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17193 if (bufvarname != NULL)
17194 {
17195 STRCPY(bufvarname, "b:");
17196 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017197 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 vim_free(bufvarname);
17199 }
17200 }
17201
17202 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205}
17206
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017207 static void
17208f_setcharsearch(argvars, rettv)
17209 typval_T *argvars;
17210 typval_T *rettv UNUSED;
17211{
17212 dict_T *d;
17213 dictitem_T *di;
17214 char_u *csearch;
17215
17216 if (argvars[0].v_type != VAR_DICT)
17217 {
17218 EMSG(_(e_dictreq));
17219 return;
17220 }
17221
17222 if ((d = argvars[0].vval.v_dict) != NULL)
17223 {
17224 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17225 if (csearch != NULL)
17226 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017227#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017228 if (enc_utf8)
17229 {
17230 int pcc[MAX_MCO];
17231 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017232
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017233 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17234 }
17235 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017236#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017237 set_last_csearch(PTR2CHAR(csearch),
17238 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017239 }
17240
17241 di = dict_find(d, (char_u *)"forward", -1);
17242 if (di != NULL)
17243 set_csearch_direction(get_tv_number(&di->di_tv)
17244 ? FORWARD : BACKWARD);
17245
17246 di = dict_find(d, (char_u *)"until", -1);
17247 if (di != NULL)
17248 set_csearch_until(!!get_tv_number(&di->di_tv));
17249 }
17250}
17251
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252/*
17253 * "setcmdpos()" function
17254 */
17255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017256f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017257 typval_T *argvars;
17258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017260 int pos = (int)get_tv_number(&argvars[0]) - 1;
17261
17262 if (pos >= 0)
17263 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264}
17265
17266/*
17267 * "setline()" function
17268 */
17269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017270f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017271 typval_T *argvars;
17272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273{
17274 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017275 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017276 list_T *l = NULL;
17277 listitem_T *li = NULL;
17278 long added = 0;
17279 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017281 lnum = get_tv_lnum(&argvars[0]);
17282 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017284 l = argvars[1].vval.v_list;
17285 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017287 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017288 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017289
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017290 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017291 for (;;)
17292 {
17293 if (l != NULL)
17294 {
17295 /* list argument, get next string */
17296 if (li == NULL)
17297 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017298 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017299 li = li->li_next;
17300 }
17301
17302 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017303 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017304 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017305
17306 /* When coming here from Insert mode, sync undo, so that this can be
17307 * undone separately from what was previously inserted. */
17308 if (u_sync_once == 2)
17309 {
17310 u_sync_once = 1; /* notify that u_sync() was called */
17311 u_sync(TRUE);
17312 }
17313
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017314 if (lnum <= curbuf->b_ml.ml_line_count)
17315 {
17316 /* existing line, replace it */
17317 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17318 {
17319 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017320 if (lnum == curwin->w_cursor.lnum)
17321 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017322 rettv->vval.v_number = 0; /* OK */
17323 }
17324 }
17325 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17326 {
17327 /* lnum is one past the last line, append the line */
17328 ++added;
17329 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17330 rettv->vval.v_number = 0; /* OK */
17331 }
17332
17333 if (l == NULL) /* only one string argument */
17334 break;
17335 ++lnum;
17336 }
17337
17338 if (added > 0)
17339 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340}
17341
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017342static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17343
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017345 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017346 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017347 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017348set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017349 win_T *wp UNUSED;
17350 typval_T *list_arg UNUSED;
17351 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017352 typval_T *rettv;
17353{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017354#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017355 char_u *act;
17356 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017357#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017358
Bram Moolenaar2641f772005-03-25 21:58:17 +000017359 rettv->vval.v_number = -1;
17360
17361#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017362 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017363 EMSG(_(e_listreq));
17364 else
17365 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017366 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017367
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017368 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017369 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017370 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 if (act == NULL)
17372 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017373 if (*act == 'a' || *act == 'r')
17374 action = *act;
17375 }
17376
Bram Moolenaar81484f42012-12-05 15:16:47 +010017377 if (l != NULL && set_errorlist(wp, l, action,
17378 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017379 rettv->vval.v_number = 0;
17380 }
17381#endif
17382}
17383
17384/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017385 * "setloclist()" function
17386 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017387 static void
17388f_setloclist(argvars, rettv)
17389 typval_T *argvars;
17390 typval_T *rettv;
17391{
17392 win_T *win;
17393
17394 rettv->vval.v_number = -1;
17395
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017396 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017397 if (win != NULL)
17398 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17399}
17400
17401/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017402 * "setmatches()" function
17403 */
17404 static void
17405f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017406 typval_T *argvars UNUSED;
17407 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017408{
17409#ifdef FEAT_SEARCH_EXTRA
17410 list_T *l;
17411 listitem_T *li;
17412 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017413 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017414
17415 rettv->vval.v_number = -1;
17416 if (argvars[0].v_type != VAR_LIST)
17417 {
17418 EMSG(_(e_listreq));
17419 return;
17420 }
17421 if ((l = argvars[0].vval.v_list) != NULL)
17422 {
17423
17424 /* To some extent make sure that we are dealing with a list from
17425 * "getmatches()". */
17426 li = l->lv_first;
17427 while (li != NULL)
17428 {
17429 if (li->li_tv.v_type != VAR_DICT
17430 || (d = li->li_tv.vval.v_dict) == NULL)
17431 {
17432 EMSG(_(e_invarg));
17433 return;
17434 }
17435 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017436 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17437 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017438 && dict_find(d, (char_u *)"priority", -1) != NULL
17439 && dict_find(d, (char_u *)"id", -1) != NULL))
17440 {
17441 EMSG(_(e_invarg));
17442 return;
17443 }
17444 li = li->li_next;
17445 }
17446
17447 clear_matches(curwin);
17448 li = l->lv_first;
17449 while (li != NULL)
17450 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017451 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017452 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017453 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017454 char_u *group;
17455 int priority;
17456 int id;
17457 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017458
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017459 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017460 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17461 {
17462 if (s == NULL)
17463 {
17464 s = list_alloc();
17465 if (s == NULL)
17466 return;
17467 }
17468
17469 /* match from matchaddpos() */
17470 for (i = 1; i < 9; i++)
17471 {
17472 sprintf((char *)buf, (char *)"pos%d", i);
17473 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17474 {
17475 if (di->di_tv.v_type != VAR_LIST)
17476 return;
17477
17478 list_append_tv(s, &di->di_tv);
17479 s->lv_refcount++;
17480 }
17481 else
17482 break;
17483 }
17484 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017485
17486 group = get_dict_string(d, (char_u *)"group", FALSE);
17487 priority = (int)get_dict_number(d, (char_u *)"priority");
17488 id = (int)get_dict_number(d, (char_u *)"id");
17489 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17490 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17491 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017492 if (i == 0)
17493 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017494 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017495 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017496 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017497 }
17498 else
17499 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017500 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017501 list_unref(s);
17502 s = NULL;
17503 }
17504
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017505 li = li->li_next;
17506 }
17507 rettv->vval.v_number = 0;
17508 }
17509#endif
17510}
17511
17512/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017513 * "setpos()" function
17514 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017515 static void
17516f_setpos(argvars, rettv)
17517 typval_T *argvars;
17518 typval_T *rettv;
17519{
17520 pos_T pos;
17521 int fnum;
17522 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017523 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017524
Bram Moolenaar08250432008-02-13 11:42:46 +000017525 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017526 name = get_tv_string_chk(argvars);
17527 if (name != NULL)
17528 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017529 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017530 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017531 if (--pos.col < 0)
17532 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017533 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017534 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017535 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017536 if (fnum == curbuf->b_fnum)
17537 {
17538 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017539 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017540 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017541 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017542 curwin->w_set_curswant = FALSE;
17543 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017544 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017545 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017546 }
17547 else
17548 EMSG(_(e_invarg));
17549 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017550 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17551 {
17552 /* set mark */
17553 if (setmark_pos(name[1], &pos, fnum) == OK)
17554 rettv->vval.v_number = 0;
17555 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017556 else
17557 EMSG(_(e_invarg));
17558 }
17559 }
17560}
17561
17562/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017563 * "setqflist()" function
17564 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017565 static void
17566f_setqflist(argvars, rettv)
17567 typval_T *argvars;
17568 typval_T *rettv;
17569{
17570 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17571}
17572
17573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 * "setreg()" function
17575 */
17576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017577f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017578 typval_T *argvars;
17579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580{
17581 int regname;
17582 char_u *strregname;
17583 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017584 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 int append;
17586 char_u yank_type;
17587 long block_len;
17588
17589 block_len = -1;
17590 yank_type = MAUTO;
17591 append = FALSE;
17592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017593 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017594 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017596 if (strregname == NULL)
17597 return; /* type error; errmsg already given */
17598 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 if (regname == 0 || regname == '@')
17600 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017602 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017604 stropt = get_tv_string_chk(&argvars[2]);
17605 if (stropt == NULL)
17606 return; /* type error */
17607 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 switch (*stropt)
17609 {
17610 case 'a': case 'A': /* append */
17611 append = TRUE;
17612 break;
17613 case 'v': case 'c': /* character-wise selection */
17614 yank_type = MCHAR;
17615 break;
17616 case 'V': case 'l': /* line-wise selection */
17617 yank_type = MLINE;
17618 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 case 'b': case Ctrl_V: /* block-wise selection */
17620 yank_type = MBLOCK;
17621 if (VIM_ISDIGIT(stropt[1]))
17622 {
17623 ++stropt;
17624 block_len = getdigits(&stropt) - 1;
17625 --stropt;
17626 }
17627 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 }
17629 }
17630
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017631 if (argvars[1].v_type == VAR_LIST)
17632 {
17633 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017634 char_u **allocval;
17635 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017636 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017637 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017638 int len = argvars[1].vval.v_list->lv_len;
17639 listitem_T *li;
17640
Bram Moolenaar7d647822014-04-05 21:28:56 +020017641 /* First half: use for pointers to result lines; second half: use for
17642 * pointers to allocated copies. */
17643 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017644 if (lstval == NULL)
17645 return;
17646 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017647 allocval = lstval + len + 2;
17648 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017649
17650 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17651 li = li->li_next)
17652 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017653 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017654 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017655 goto free_lstval;
17656 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017657 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017658 /* Need to make a copy, next get_tv_string_buf_chk() will
17659 * overwrite the string. */
17660 strval = vim_strsave(buf);
17661 if (strval == NULL)
17662 goto free_lstval;
17663 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017664 }
17665 *curval++ = strval;
17666 }
17667 *curval++ = NULL;
17668
17669 write_reg_contents_lst(regname, lstval, -1,
17670 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017671free_lstval:
17672 while (curallocval > allocval)
17673 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017674 vim_free(lstval);
17675 }
17676 else
17677 {
17678 strval = get_tv_string_chk(&argvars[1]);
17679 if (strval == NULL)
17680 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017684 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685}
17686
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017687/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017688 * "settabvar()" function
17689 */
17690 static void
17691f_settabvar(argvars, rettv)
17692 typval_T *argvars;
17693 typval_T *rettv;
17694{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017695#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017696 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017697 tabpage_T *tp;
17698#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017699 char_u *varname, *tabvarname;
17700 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017701
17702 rettv->vval.v_number = 0;
17703
17704 if (check_restricted() || check_secure())
17705 return;
17706
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017707#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017708 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017709#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017710 varname = get_tv_string_chk(&argvars[1]);
17711 varp = &argvars[2];
17712
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017713 if (varname != NULL && varp != NULL
17714#ifdef FEAT_WINDOWS
17715 && tp != NULL
17716#endif
17717 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017718 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017719#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017720 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017721 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017722#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017723
17724 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17725 if (tabvarname != NULL)
17726 {
17727 STRCPY(tabvarname, "t:");
17728 STRCPY(tabvarname + 2, varname);
17729 set_var(tabvarname, varp, TRUE);
17730 vim_free(tabvarname);
17731 }
17732
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017733#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017734 /* Restore current tabpage */
17735 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017736 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017737#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017738 }
17739}
17740
17741/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017742 * "settabwinvar()" function
17743 */
17744 static void
17745f_settabwinvar(argvars, rettv)
17746 typval_T *argvars;
17747 typval_T *rettv;
17748{
17749 setwinvar(argvars, rettv, 1);
17750}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751
17752/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017753 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017756f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017757 typval_T *argvars;
17758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017760 setwinvar(argvars, rettv, 0);
17761}
17762
17763/*
17764 * "setwinvar()" and "settabwinvar()" functions
17765 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017766
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017767 static void
17768setwinvar(argvars, rettv, off)
17769 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017770 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017771 int off;
17772{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 win_T *win;
17774#ifdef FEAT_WINDOWS
17775 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017776 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017777 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778#endif
17779 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017780 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017782 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783
17784 if (check_restricted() || check_secure())
17785 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017786
17787#ifdef FEAT_WINDOWS
17788 if (off == 1)
17789 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17790 else
17791 tp = curtab;
17792#endif
17793 win = find_win_by_nr(&argvars[off], tp);
17794 varname = get_tv_string_chk(&argvars[off + 1]);
17795 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796
17797 if (win != NULL && varname != NULL && varp != NULL)
17798 {
17799#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017800 need_switch_win = !(tp == curtab && win == curwin);
17801 if (!need_switch_win
17802 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017805 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017807 long numval;
17808 char_u *strval;
17809 int error = FALSE;
17810
17811 ++varname;
17812 numval = get_tv_number_chk(varp, &error);
17813 strval = get_tv_string_buf_chk(varp, nbuf);
17814 if (!error && strval != NULL)
17815 set_option_value(varname, numval, strval, OPT_LOCAL);
17816 }
17817 else
17818 {
17819 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17820 if (winvarname != NULL)
17821 {
17822 STRCPY(winvarname, "w:");
17823 STRCPY(winvarname + 2, varname);
17824 set_var(winvarname, varp, TRUE);
17825 vim_free(winvarname);
17826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 }
17828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017830 if (need_switch_win)
17831 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832#endif
17833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834}
17835
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017836#ifdef FEAT_CRYPT
17837/*
17838 * "sha256({string})" function
17839 */
17840 static void
17841f_sha256(argvars, rettv)
17842 typval_T *argvars;
17843 typval_T *rettv;
17844{
17845 char_u *p;
17846
17847 p = get_tv_string(&argvars[0]);
17848 rettv->vval.v_string = vim_strsave(
17849 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17850 rettv->v_type = VAR_STRING;
17851}
17852#endif /* FEAT_CRYPT */
17853
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017855 * "shellescape({string})" function
17856 */
17857 static void
17858f_shellescape(argvars, rettv)
17859 typval_T *argvars;
17860 typval_T *rettv;
17861{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017862 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017863 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017864 rettv->v_type = VAR_STRING;
17865}
17866
17867/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017868 * shiftwidth() function
17869 */
17870 static void
17871f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017872 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017873 typval_T *rettv;
17874{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017875 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017876}
17877
17878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017879 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 */
17881 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017882f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017883 typval_T *argvars;
17884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017886 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887
Bram Moolenaar0d660222005-01-07 21:51:51 +000017888 p = get_tv_string(&argvars[0]);
17889 rettv->vval.v_string = vim_strsave(p);
17890 simplify_filename(rettv->vval.v_string); /* simplify in place */
17891 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892}
17893
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017894#ifdef FEAT_FLOAT
17895/*
17896 * "sin()" function
17897 */
17898 static void
17899f_sin(argvars, rettv)
17900 typval_T *argvars;
17901 typval_T *rettv;
17902{
17903 float_T f;
17904
17905 rettv->v_type = VAR_FLOAT;
17906 if (get_float_arg(argvars, &f) == OK)
17907 rettv->vval.v_float = sin(f);
17908 else
17909 rettv->vval.v_float = 0.0;
17910}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017911
17912/*
17913 * "sinh()" function
17914 */
17915 static void
17916f_sinh(argvars, rettv)
17917 typval_T *argvars;
17918 typval_T *rettv;
17919{
17920 float_T f;
17921
17922 rettv->v_type = VAR_FLOAT;
17923 if (get_float_arg(argvars, &f) == OK)
17924 rettv->vval.v_float = sinh(f);
17925 else
17926 rettv->vval.v_float = 0.0;
17927}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017928#endif
17929
Bram Moolenaar0d660222005-01-07 21:51:51 +000017930static int
17931#ifdef __BORLANDC__
17932 _RTLENTRYF
17933#endif
17934 item_compare __ARGS((const void *s1, const void *s2));
17935static int
17936#ifdef __BORLANDC__
17937 _RTLENTRYF
17938#endif
17939 item_compare2 __ARGS((const void *s1, const void *s2));
17940
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017941/* struct used in the array that's given to qsort() */
17942typedef struct
17943{
17944 listitem_T *item;
17945 int idx;
17946} sortItem_T;
17947
Bram Moolenaar0d660222005-01-07 21:51:51 +000017948static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017949static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017950static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017952static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017953static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017954static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017955static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956#define ITEM_COMPARE_FAIL 999
17957
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017959 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017961 static int
17962#ifdef __BORLANDC__
17963_RTLENTRYF
17964#endif
17965item_compare(s1, s2)
17966 const void *s1;
17967 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017969 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017970 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017971 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017972 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017973 int res;
17974 char_u numbuf1[NUMBUFLEN];
17975 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017977 si1 = (sortItem_T *)s1;
17978 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017979 tv1 = &si1->item->li_tv;
17980 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017981
17982 if (item_compare_numbers)
17983 {
17984 long v1 = get_tv_number(tv1);
17985 long v2 = get_tv_number(tv2);
17986
17987 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17988 }
17989
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017990 /* tv2string() puts quotes around a string and allocates memory. Don't do
17991 * that for string variables. Use a single quote when comparing with a
17992 * non-string to do what the docs promise. */
17993 if (tv1->v_type == VAR_STRING)
17994 {
17995 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17996 p1 = (char_u *)"'";
17997 else
17998 p1 = tv1->vval.v_string;
17999 }
18000 else
18001 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18002 if (tv2->v_type == VAR_STRING)
18003 {
18004 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18005 p2 = (char_u *)"'";
18006 else
18007 p2 = tv2->vval.v_string;
18008 }
18009 else
18010 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018011 if (p1 == NULL)
18012 p1 = (char_u *)"";
18013 if (p2 == NULL)
18014 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018015 if (!item_compare_numeric)
18016 {
18017 if (item_compare_ic)
18018 res = STRICMP(p1, p2);
18019 else
18020 res = STRCMP(p1, p2);
18021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018023 {
18024 double n1, n2;
18025 n1 = strtod((char *)p1, (char **)&p1);
18026 n2 = strtod((char *)p2, (char **)&p2);
18027 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18028 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018029
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018030 /* When the result would be zero, compare the item indexes. Makes the
18031 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018032 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018033 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018034
Bram Moolenaar0d660222005-01-07 21:51:51 +000018035 vim_free(tofree1);
18036 vim_free(tofree2);
18037 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038}
18039
18040 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018041#ifdef __BORLANDC__
18042_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018044item_compare2(s1, s2)
18045 const void *s1;
18046 const void *s2;
18047{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018048 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018049 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018050 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018051 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018052 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018054 /* shortcut after failure in previous call; compare all items equal */
18055 if (item_compare_func_err)
18056 return 0;
18057
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018058 si1 = (sortItem_T *)s1;
18059 si2 = (sortItem_T *)s2;
18060
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018061 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018062 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018063 copy_tv(&si1->item->li_tv, &argv[0]);
18064 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018065
18066 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018067 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018068 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18069 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018070 clear_tv(&argv[0]);
18071 clear_tv(&argv[1]);
18072
18073 if (res == FAIL)
18074 res = ITEM_COMPARE_FAIL;
18075 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018076 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18077 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018078 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018080
18081 /* When the result would be zero, compare the pointers themselves. Makes
18082 * the sort stable. */
18083 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018084 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018085
Bram Moolenaar0d660222005-01-07 21:51:51 +000018086 return res;
18087}
18088
18089/*
18090 * "sort({list})" function
18091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018093do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018094 typval_T *argvars;
18095 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018096 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097{
Bram Moolenaar33570922005-01-25 22:26:29 +000018098 list_T *l;
18099 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018100 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018101 long len;
18102 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103
Bram Moolenaar0d660222005-01-07 21:51:51 +000018104 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018105 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 else
18107 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018108 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018109 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018110 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18111 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018112 return;
18113 rettv->vval.v_list = l;
18114 rettv->v_type = VAR_LIST;
18115 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116
Bram Moolenaar0d660222005-01-07 21:51:51 +000018117 len = list_len(l);
18118 if (len <= 1)
18119 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120
Bram Moolenaar0d660222005-01-07 21:51:51 +000018121 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018122 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018123 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018124 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018125 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018126 if (argvars[1].v_type != VAR_UNKNOWN)
18127 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018128 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018129 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018130 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018131 else
18132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018133 int error = FALSE;
18134
18135 i = get_tv_number_chk(&argvars[1], &error);
18136 if (error)
18137 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018138 if (i == 1)
18139 item_compare_ic = TRUE;
18140 else
18141 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018142 if (item_compare_func != NULL)
18143 {
18144 if (STRCMP(item_compare_func, "n") == 0)
18145 {
18146 item_compare_func = NULL;
18147 item_compare_numeric = TRUE;
18148 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018149 else if (STRCMP(item_compare_func, "N") == 0)
18150 {
18151 item_compare_func = NULL;
18152 item_compare_numbers = TRUE;
18153 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018154 else if (STRCMP(item_compare_func, "i") == 0)
18155 {
18156 item_compare_func = NULL;
18157 item_compare_ic = TRUE;
18158 }
18159 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018161
18162 if (argvars[2].v_type != VAR_UNKNOWN)
18163 {
18164 /* optional third argument: {dict} */
18165 if (argvars[2].v_type != VAR_DICT)
18166 {
18167 EMSG(_(e_dictreq));
18168 return;
18169 }
18170 item_compare_selfdict = argvars[2].vval.v_dict;
18171 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173
Bram Moolenaar0d660222005-01-07 21:51:51 +000018174 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018175 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018176 if (ptrs == NULL)
18177 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178
Bram Moolenaar327aa022014-03-25 18:24:23 +010018179 i = 0;
18180 if (sort)
18181 {
18182 /* sort(): ptrs will be the list to sort */
18183 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018184 {
18185 ptrs[i].item = li;
18186 ptrs[i].idx = i;
18187 ++i;
18188 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018189
18190 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018191 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018192 /* test the compare function */
18193 if (item_compare_func != NULL
18194 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018195 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018196 EMSG(_("E702: Sort compare function failed"));
18197 else
18198 {
18199 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018200 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018201 item_compare_func == NULL ? item_compare : item_compare2);
18202
18203 if (!item_compare_func_err)
18204 {
18205 /* Clear the List and append the items in sorted order. */
18206 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18207 l->lv_len = 0;
18208 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018209 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018210 }
18211 }
18212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018215 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18216
18217 /* f_uniq(): ptrs will be a stack of items to remove */
18218 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018219 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018220 item_compare_func_ptr = item_compare_func
18221 ? item_compare2 : item_compare;
18222
18223 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18224 li = li->li_next)
18225 {
18226 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18227 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018228 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018229 if (item_compare_func_err)
18230 {
18231 EMSG(_("E882: Uniq compare function failed"));
18232 break;
18233 }
18234 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018236 if (!item_compare_func_err)
18237 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018238 while (--i >= 0)
18239 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018240 li = ptrs[i].item->li_next;
18241 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018242 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018243 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018244 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018245 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018246 list_fix_watch(l, li);
18247 listitem_free(li);
18248 l->lv_len--;
18249 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018250 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251 }
18252
18253 vim_free(ptrs);
18254 }
18255}
18256
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018257/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018258 * "sort({list})" function
18259 */
18260 static void
18261f_sort(argvars, rettv)
18262 typval_T *argvars;
18263 typval_T *rettv;
18264{
18265 do_sort_uniq(argvars, rettv, TRUE);
18266}
18267
18268/*
18269 * "uniq({list})" function
18270 */
18271 static void
18272f_uniq(argvars, rettv)
18273 typval_T *argvars;
18274 typval_T *rettv;
18275{
18276 do_sort_uniq(argvars, rettv, FALSE);
18277}
18278
18279/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018280 * "soundfold({word})" function
18281 */
18282 static void
18283f_soundfold(argvars, rettv)
18284 typval_T *argvars;
18285 typval_T *rettv;
18286{
18287 char_u *s;
18288
18289 rettv->v_type = VAR_STRING;
18290 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018291#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018292 rettv->vval.v_string = eval_soundfold(s);
18293#else
18294 rettv->vval.v_string = vim_strsave(s);
18295#endif
18296}
18297
18298/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018299 * "spellbadword()" function
18300 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018301 static void
18302f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018303 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018304 typval_T *rettv;
18305{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018306 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018307 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018308 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018309
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018310 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018311 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018312
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018313#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018314 if (argvars[0].v_type == VAR_UNKNOWN)
18315 {
18316 /* Find the start and length of the badly spelled word. */
18317 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18318 if (len != 0)
18319 word = ml_get_cursor();
18320 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018321 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018322 {
18323 char_u *str = get_tv_string_chk(&argvars[0]);
18324 int capcol = -1;
18325
18326 if (str != NULL)
18327 {
18328 /* Check the argument for spelling. */
18329 while (*str != NUL)
18330 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018331 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018332 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018333 {
18334 word = str;
18335 break;
18336 }
18337 str += len;
18338 }
18339 }
18340 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018341#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018342
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018343 list_append_string(rettv->vval.v_list, word, len);
18344 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018345 attr == HLF_SPB ? "bad" :
18346 attr == HLF_SPR ? "rare" :
18347 attr == HLF_SPL ? "local" :
18348 attr == HLF_SPC ? "caps" :
18349 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018350}
18351
18352/*
18353 * "spellsuggest()" function
18354 */
18355 static void
18356f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018357 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018358 typval_T *rettv;
18359{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018360#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018361 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018362 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018363 int maxcount;
18364 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018365 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018366 listitem_T *li;
18367 int need_capital = FALSE;
18368#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018369
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018370 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018371 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018372
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018373#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018374 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018375 {
18376 str = get_tv_string(&argvars[0]);
18377 if (argvars[1].v_type != VAR_UNKNOWN)
18378 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018379 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018380 if (maxcount <= 0)
18381 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018382 if (argvars[2].v_type != VAR_UNKNOWN)
18383 {
18384 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18385 if (typeerr)
18386 return;
18387 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018388 }
18389 else
18390 maxcount = 25;
18391
Bram Moolenaar4770d092006-01-12 23:22:24 +000018392 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018393
18394 for (i = 0; i < ga.ga_len; ++i)
18395 {
18396 str = ((char_u **)ga.ga_data)[i];
18397
18398 li = listitem_alloc();
18399 if (li == NULL)
18400 vim_free(str);
18401 else
18402 {
18403 li->li_tv.v_type = VAR_STRING;
18404 li->li_tv.v_lock = 0;
18405 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018406 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018407 }
18408 }
18409 ga_clear(&ga);
18410 }
18411#endif
18412}
18413
Bram Moolenaar0d660222005-01-07 21:51:51 +000018414 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018415f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018416 typval_T *argvars;
18417 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418{
18419 char_u *str;
18420 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018421 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018422 regmatch_T regmatch;
18423 char_u patbuf[NUMBUFLEN];
18424 char_u *save_cpo;
18425 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018426 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018427 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018428 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018429
18430 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18431 save_cpo = p_cpo;
18432 p_cpo = (char_u *)"";
18433
18434 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018435 if (argvars[1].v_type != VAR_UNKNOWN)
18436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018437 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18438 if (pat == NULL)
18439 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018440 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018441 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018442 }
18443 if (pat == NULL || *pat == NUL)
18444 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018445
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018446 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018448 if (typeerr)
18449 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450
Bram Moolenaar0d660222005-01-07 21:51:51 +000018451 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18452 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018454 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018455 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018457 if (*str == NUL)
18458 match = FALSE; /* empty item at the end */
18459 else
18460 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018461 if (match)
18462 end = regmatch.startp[0];
18463 else
18464 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018465 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18466 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018467 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018468 if (list_append_string(rettv->vval.v_list, str,
18469 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018470 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018471 }
18472 if (!match)
18473 break;
18474 /* Advance to just after the match. */
18475 if (regmatch.endp[0] > str)
18476 col = 0;
18477 else
18478 {
18479 /* Don't get stuck at the same match. */
18480#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018481 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018482#else
18483 col = 1;
18484#endif
18485 }
18486 str = regmatch.endp[0];
18487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488
Bram Moolenaar473de612013-06-08 18:19:48 +020018489 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491
Bram Moolenaar0d660222005-01-07 21:51:51 +000018492 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493}
18494
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018495#ifdef FEAT_FLOAT
18496/*
18497 * "sqrt()" function
18498 */
18499 static void
18500f_sqrt(argvars, rettv)
18501 typval_T *argvars;
18502 typval_T *rettv;
18503{
18504 float_T f;
18505
18506 rettv->v_type = VAR_FLOAT;
18507 if (get_float_arg(argvars, &f) == OK)
18508 rettv->vval.v_float = sqrt(f);
18509 else
18510 rettv->vval.v_float = 0.0;
18511}
18512
18513/*
18514 * "str2float()" function
18515 */
18516 static void
18517f_str2float(argvars, rettv)
18518 typval_T *argvars;
18519 typval_T *rettv;
18520{
18521 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18522
18523 if (*p == '+')
18524 p = skipwhite(p + 1);
18525 (void)string2float(p, &rettv->vval.v_float);
18526 rettv->v_type = VAR_FLOAT;
18527}
18528#endif
18529
Bram Moolenaar2c932302006-03-18 21:42:09 +000018530/*
18531 * "str2nr()" function
18532 */
18533 static void
18534f_str2nr(argvars, rettv)
18535 typval_T *argvars;
18536 typval_T *rettv;
18537{
18538 int base = 10;
18539 char_u *p;
18540 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018541 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018542
18543 if (argvars[1].v_type != VAR_UNKNOWN)
18544 {
18545 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018546 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018547 {
18548 EMSG(_(e_invarg));
18549 return;
18550 }
18551 }
18552
18553 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018554 if (*p == '+')
18555 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018556 switch (base)
18557 {
18558 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18559 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18560 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18561 default: what = 0;
18562 }
18563 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018564 rettv->vval.v_number = n;
18565}
18566
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567#ifdef HAVE_STRFTIME
18568/*
18569 * "strftime({format}[, {time}])" function
18570 */
18571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018573 typval_T *argvars;
18574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575{
18576 char_u result_buf[256];
18577 struct tm *curtime;
18578 time_t seconds;
18579 char_u *p;
18580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018581 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018584 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 seconds = time(NULL);
18586 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588 curtime = localtime(&seconds);
18589 /* MSVC returns NULL for an invalid value of seconds. */
18590 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018591 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 else
18593 {
18594# ifdef FEAT_MBYTE
18595 vimconv_T conv;
18596 char_u *enc;
18597
18598 conv.vc_type = CONV_NONE;
18599 enc = enc_locale();
18600 convert_setup(&conv, p_enc, enc);
18601 if (conv.vc_type != CONV_NONE)
18602 p = string_convert(&conv, p, NULL);
18603# endif
18604 if (p != NULL)
18605 (void)strftime((char *)result_buf, sizeof(result_buf),
18606 (char *)p, curtime);
18607 else
18608 result_buf[0] = NUL;
18609
18610# ifdef FEAT_MBYTE
18611 if (conv.vc_type != CONV_NONE)
18612 vim_free(p);
18613 convert_setup(&conv, enc, p_enc);
18614 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 else
18617# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018618 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619
18620# ifdef FEAT_MBYTE
18621 /* Release conversion descriptors */
18622 convert_setup(&conv, NULL, NULL);
18623 vim_free(enc);
18624# endif
18625 }
18626}
18627#endif
18628
18629/*
18630 * "stridx()" function
18631 */
18632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 typval_T *argvars;
18635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636{
18637 char_u buf[NUMBUFLEN];
18638 char_u *needle;
18639 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018640 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018642 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018644 needle = get_tv_string_chk(&argvars[1]);
18645 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018646 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018647 if (needle == NULL || haystack == NULL)
18648 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649
Bram Moolenaar33570922005-01-25 22:26:29 +000018650 if (argvars[2].v_type != VAR_UNKNOWN)
18651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018652 int error = FALSE;
18653
18654 start_idx = get_tv_number_chk(&argvars[2], &error);
18655 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018656 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018657 if (start_idx >= 0)
18658 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018659 }
18660
18661 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18662 if (pos != NULL)
18663 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664}
18665
18666/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018667 * "string()" function
18668 */
18669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018670f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018671 typval_T *argvars;
18672 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018673{
18674 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018675 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018677 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018678 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018679 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018680 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018681 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682}
18683
18684/*
18685 * "strlen()" function
18686 */
18687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018689 typval_T *argvars;
18690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692 rettv->vval.v_number = (varnumber_T)(STRLEN(
18693 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694}
18695
18696/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018697 * "strchars()" function
18698 */
18699 static void
18700f_strchars(argvars, rettv)
18701 typval_T *argvars;
18702 typval_T *rettv;
18703{
18704 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018705 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018706#ifdef FEAT_MBYTE
18707 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018708 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018709#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018710
18711 if (argvars[1].v_type != VAR_UNKNOWN)
18712 skipcc = get_tv_number_chk(&argvars[1], NULL);
18713 if (skipcc < 0 || skipcc > 1)
18714 EMSG(_(e_invarg));
18715 else
18716 {
18717#ifdef FEAT_MBYTE
18718 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18719 while (*s != NUL)
18720 {
18721 func_mb_ptr2char_adv(&s);
18722 ++len;
18723 }
18724 rettv->vval.v_number = len;
18725#else
18726 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18727#endif
18728 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018729}
18730
18731/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018732 * "strdisplaywidth()" function
18733 */
18734 static void
18735f_strdisplaywidth(argvars, rettv)
18736 typval_T *argvars;
18737 typval_T *rettv;
18738{
18739 char_u *s = get_tv_string(&argvars[0]);
18740 int col = 0;
18741
18742 if (argvars[1].v_type != VAR_UNKNOWN)
18743 col = get_tv_number(&argvars[1]);
18744
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018745 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018746}
18747
18748/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018749 * "strwidth()" function
18750 */
18751 static void
18752f_strwidth(argvars, rettv)
18753 typval_T *argvars;
18754 typval_T *rettv;
18755{
18756 char_u *s = get_tv_string(&argvars[0]);
18757
18758 rettv->vval.v_number = (varnumber_T)(
18759#ifdef FEAT_MBYTE
18760 mb_string2cells(s, -1)
18761#else
18762 STRLEN(s)
18763#endif
18764 );
18765}
18766
18767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 * "strpart()" function
18769 */
18770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018771f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018772 typval_T *argvars;
18773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774{
18775 char_u *p;
18776 int n;
18777 int len;
18778 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018779 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018781 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 slen = (int)STRLEN(p);
18783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018784 n = get_tv_number_chk(&argvars[1], &error);
18785 if (error)
18786 len = 0;
18787 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018788 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 else
18790 len = slen - n; /* default len: all bytes that are available. */
18791
18792 /*
18793 * Only return the overlap between the specified part and the actual
18794 * string.
18795 */
18796 if (n < 0)
18797 {
18798 len += n;
18799 n = 0;
18800 }
18801 else if (n > slen)
18802 n = slen;
18803 if (len < 0)
18804 len = 0;
18805 else if (n + len > slen)
18806 len = slen - n;
18807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808 rettv->v_type = VAR_STRING;
18809 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810}
18811
18812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018813 * "strridx()" function
18814 */
18815 static void
18816f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018817 typval_T *argvars;
18818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018819{
18820 char_u buf[NUMBUFLEN];
18821 char_u *needle;
18822 char_u *haystack;
18823 char_u *rest;
18824 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018825 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018827 needle = get_tv_string_chk(&argvars[1]);
18828 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018829
18830 rettv->vval.v_number = -1;
18831 if (needle == NULL || haystack == NULL)
18832 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018833
18834 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018835 if (argvars[2].v_type != VAR_UNKNOWN)
18836 {
18837 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018838 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018839 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018840 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018841 }
18842 else
18843 end_idx = haystack_len;
18844
Bram Moolenaar0d660222005-01-07 21:51:51 +000018845 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018846 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018847 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018848 lastmatch = haystack + end_idx;
18849 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018850 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018851 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018852 for (rest = haystack; *rest != '\0'; ++rest)
18853 {
18854 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018855 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018856 break;
18857 lastmatch = rest;
18858 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018859 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018860
18861 if (lastmatch == NULL)
18862 rettv->vval.v_number = -1;
18863 else
18864 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18865}
18866
18867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 * "strtrans()" function
18869 */
18870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018871f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018872 typval_T *argvars;
18873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875 rettv->v_type = VAR_STRING;
18876 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877}
18878
18879/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018880 * "submatch()" function
18881 */
18882 static void
18883f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 typval_T *argvars;
18885 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018886{
Bram Moolenaar41571762014-04-02 19:00:58 +020018887 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018888 int no;
18889 int retList = 0;
18890
18891 no = (int)get_tv_number_chk(&argvars[0], &error);
18892 if (error)
18893 return;
18894 error = FALSE;
18895 if (argvars[1].v_type != VAR_UNKNOWN)
18896 retList = get_tv_number_chk(&argvars[1], &error);
18897 if (error)
18898 return;
18899
18900 if (retList == 0)
18901 {
18902 rettv->v_type = VAR_STRING;
18903 rettv->vval.v_string = reg_submatch(no);
18904 }
18905 else
18906 {
18907 rettv->v_type = VAR_LIST;
18908 rettv->vval.v_list = reg_submatch_list(no);
18909 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018910}
18911
18912/*
18913 * "substitute()" function
18914 */
18915 static void
18916f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018917 typval_T *argvars;
18918 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018919{
18920 char_u patbuf[NUMBUFLEN];
18921 char_u subbuf[NUMBUFLEN];
18922 char_u flagsbuf[NUMBUFLEN];
18923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018924 char_u *str = get_tv_string_chk(&argvars[0]);
18925 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18926 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18927 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18928
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018930 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18931 rettv->vval.v_string = NULL;
18932 else
18933 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018934}
18935
18936/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018937 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018940f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018941 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943{
18944 int id = 0;
18945#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018946 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947 long col;
18948 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018949 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018951 lnum = get_tv_lnum(argvars); /* -1 on type error */
18952 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18953 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018955 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018956 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018957 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958#endif
18959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018960 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961}
18962
18963/*
18964 * "synIDattr(id, what [, mode])" function
18965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018967f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018968 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970{
18971 char_u *p = NULL;
18972#ifdef FEAT_SYN_HL
18973 int id;
18974 char_u *what;
18975 char_u *mode;
18976 char_u modebuf[NUMBUFLEN];
18977 int modec;
18978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018979 id = get_tv_number(&argvars[0]);
18980 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018981 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018983 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018985 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 modec = 0; /* replace invalid with current */
18987 }
18988 else
18989 {
18990#ifdef FEAT_GUI
18991 if (gui.in_use)
18992 modec = 'g';
18993 else
18994#endif
18995 if (t_colors > 1)
18996 modec = 'c';
18997 else
18998 modec = 't';
18999 }
19000
19001
19002 switch (TOLOWER_ASC(what[0]))
19003 {
19004 case 'b':
19005 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19006 p = highlight_color(id, what, modec);
19007 else /* bold */
19008 p = highlight_has_attr(id, HL_BOLD, modec);
19009 break;
19010
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019011 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 p = highlight_color(id, what, modec);
19013 break;
19014
19015 case 'i':
19016 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19017 p = highlight_has_attr(id, HL_INVERSE, modec);
19018 else /* italic */
19019 p = highlight_has_attr(id, HL_ITALIC, modec);
19020 break;
19021
19022 case 'n': /* name */
19023 p = get_highlight_name(NULL, id - 1);
19024 break;
19025
19026 case 'r': /* reverse */
19027 p = highlight_has_attr(id, HL_INVERSE, modec);
19028 break;
19029
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019030 case 's':
19031 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19032 p = highlight_color(id, what, modec);
19033 else /* standout */
19034 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 break;
19036
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019037 case 'u':
19038 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19039 /* underline */
19040 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19041 else
19042 /* undercurl */
19043 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 break;
19045 }
19046
19047 if (p != NULL)
19048 p = vim_strsave(p);
19049#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019050 rettv->v_type = VAR_STRING;
19051 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052}
19053
19054/*
19055 * "synIDtrans(id)" function
19056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019058f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019059 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061{
19062 int id;
19063
19064#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019065 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066
19067 if (id > 0)
19068 id = syn_get_final_id(id);
19069 else
19070#endif
19071 id = 0;
19072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019073 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074}
19075
19076/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019077 * "synconcealed(lnum, col)" function
19078 */
19079 static void
19080f_synconcealed(argvars, rettv)
19081 typval_T *argvars UNUSED;
19082 typval_T *rettv;
19083{
19084#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19085 long lnum;
19086 long col;
19087 int syntax_flags = 0;
19088 int cchar;
19089 int matchid = 0;
19090 char_u str[NUMBUFLEN];
19091#endif
19092
19093 rettv->v_type = VAR_LIST;
19094 rettv->vval.v_list = NULL;
19095
19096#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19097 lnum = get_tv_lnum(argvars); /* -1 on type error */
19098 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19099
19100 vim_memset(str, NUL, sizeof(str));
19101
19102 if (rettv_list_alloc(rettv) != FAIL)
19103 {
19104 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19105 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19106 && curwin->w_p_cole > 0)
19107 {
19108 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19109 syntax_flags = get_syntax_info(&matchid);
19110
19111 /* get the conceal character */
19112 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19113 {
19114 cchar = syn_get_sub_char();
19115 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19116 cchar = lcs_conceal;
19117 if (cchar != NUL)
19118 {
19119# ifdef FEAT_MBYTE
19120 if (has_mbyte)
19121 (*mb_char2bytes)(cchar, str);
19122 else
19123# endif
19124 str[0] = cchar;
19125 }
19126 }
19127 }
19128
19129 list_append_number(rettv->vval.v_list,
19130 (syntax_flags & HL_CONCEAL) != 0);
19131 /* -1 to auto-determine strlen */
19132 list_append_string(rettv->vval.v_list, str, -1);
19133 list_append_number(rettv->vval.v_list, matchid);
19134 }
19135#endif
19136}
19137
19138/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019139 * "synstack(lnum, col)" function
19140 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019141 static void
19142f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019143 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019144 typval_T *rettv;
19145{
19146#ifdef FEAT_SYN_HL
19147 long lnum;
19148 long col;
19149 int i;
19150 int id;
19151#endif
19152
19153 rettv->v_type = VAR_LIST;
19154 rettv->vval.v_list = NULL;
19155
19156#ifdef FEAT_SYN_HL
19157 lnum = get_tv_lnum(argvars); /* -1 on type error */
19158 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19159
19160 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019161 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019162 && rettv_list_alloc(rettv) != FAIL)
19163 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019164 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019165 for (i = 0; ; ++i)
19166 {
19167 id = syn_get_stack_item(i);
19168 if (id < 0)
19169 break;
19170 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19171 break;
19172 }
19173 }
19174#endif
19175}
19176
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019178get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019179 typval_T *argvars;
19180 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019181 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019183 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019185 char_u *infile = NULL;
19186 char_u buf[NUMBUFLEN];
19187 int err = FALSE;
19188 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019189 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019190 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019192 rettv->v_type = VAR_STRING;
19193 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019194 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019195 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019196
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019197 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019198 {
19199 /*
19200 * Write the string to a temp file, to be used for input of the shell
19201 * command.
19202 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019203 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019204 {
19205 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019206 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019207 }
19208
19209 fd = mch_fopen((char *)infile, WRITEBIN);
19210 if (fd == NULL)
19211 {
19212 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019213 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019214 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019215 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019216 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019217 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19218 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019219 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019220 else
19221 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019222 size_t len;
19223
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019224 p = get_tv_string_buf_chk(&argvars[1], buf);
19225 if (p == NULL)
19226 {
19227 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019228 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019229 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019230 len = STRLEN(p);
19231 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019232 err = TRUE;
19233 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019234 if (fclose(fd) != 0)
19235 err = TRUE;
19236 if (err)
19237 {
19238 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019239 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019240 }
19241 }
19242
Bram Moolenaar52a72462014-08-29 15:53:52 +020019243 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19244 * echoes typeahead, that messes up the display. */
19245 if (!msg_silent)
19246 flags += SHELL_COOKED;
19247
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019248 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019250 int len;
19251 listitem_T *li;
19252 char_u *s = NULL;
19253 char_u *start;
19254 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019255 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256
Bram Moolenaar52a72462014-08-29 15:53:52 +020019257 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019258 if (res == NULL)
19259 goto errret;
19260
19261 list = list_alloc();
19262 if (list == NULL)
19263 goto errret;
19264
19265 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019267 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019268 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019269 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019270 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019271
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019272 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019273 if (s == NULL)
19274 goto errret;
19275
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019276 for (p = s; start < end; ++p, ++start)
19277 *p = *start == NUL ? NL : *start;
19278 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019279
19280 li = listitem_alloc();
19281 if (li == NULL)
19282 {
19283 vim_free(s);
19284 goto errret;
19285 }
19286 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019287 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019288 li->li_tv.vval.v_string = s;
19289 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019291
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019292 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019293 rettv->v_type = VAR_LIST;
19294 rettv->vval.v_list = list;
19295 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019297 else
19298 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019299 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019300#ifdef USE_CR
19301 /* translate <CR> into <NL> */
19302 if (res != NULL)
19303 {
19304 char_u *s;
19305
19306 for (s = res; *s; ++s)
19307 {
19308 if (*s == CAR)
19309 *s = NL;
19310 }
19311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312#else
19313# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019314 /* translate <CR><NL> into <NL> */
19315 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019317 char_u *s, *d;
19318
19319 d = res;
19320 for (s = res; *s; ++s)
19321 {
19322 if (s[0] == CAR && s[1] == NL)
19323 ++s;
19324 *d++ = *s;
19325 }
19326 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328# endif
19329#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019330 rettv->vval.v_string = res;
19331 res = NULL;
19332 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019333
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019334errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019335 if (infile != NULL)
19336 {
19337 mch_remove(infile);
19338 vim_free(infile);
19339 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019340 if (res != NULL)
19341 vim_free(res);
19342 if (list != NULL)
19343 list_free(list, TRUE);
19344}
19345
19346/*
19347 * "system()" function
19348 */
19349 static void
19350f_system(argvars, rettv)
19351 typval_T *argvars;
19352 typval_T *rettv;
19353{
19354 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19355}
19356
19357/*
19358 * "systemlist()" function
19359 */
19360 static void
19361f_systemlist(argvars, rettv)
19362 typval_T *argvars;
19363 typval_T *rettv;
19364{
19365 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366}
19367
19368/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019369 * "tabpagebuflist()" function
19370 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019371 static void
19372f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019373 typval_T *argvars UNUSED;
19374 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019375{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019376#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019377 tabpage_T *tp;
19378 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019379
19380 if (argvars[0].v_type == VAR_UNKNOWN)
19381 wp = firstwin;
19382 else
19383 {
19384 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19385 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019386 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019387 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019388 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019389 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019390 for (; wp != NULL; wp = wp->w_next)
19391 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019392 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019393 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019394 }
19395#endif
19396}
19397
19398
19399/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019400 * "tabpagenr()" function
19401 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019402 static void
19403f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019404 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019405 typval_T *rettv;
19406{
19407 int nr = 1;
19408#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019409 char_u *arg;
19410
19411 if (argvars[0].v_type != VAR_UNKNOWN)
19412 {
19413 arg = get_tv_string_chk(&argvars[0]);
19414 nr = 0;
19415 if (arg != NULL)
19416 {
19417 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019418 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019419 else
19420 EMSG2(_(e_invexpr2), arg);
19421 }
19422 }
19423 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019424 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019425#endif
19426 rettv->vval.v_number = nr;
19427}
19428
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019429
19430#ifdef FEAT_WINDOWS
19431static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19432
19433/*
19434 * Common code for tabpagewinnr() and winnr().
19435 */
19436 static int
19437get_winnr(tp, argvar)
19438 tabpage_T *tp;
19439 typval_T *argvar;
19440{
19441 win_T *twin;
19442 int nr = 1;
19443 win_T *wp;
19444 char_u *arg;
19445
19446 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19447 if (argvar->v_type != VAR_UNKNOWN)
19448 {
19449 arg = get_tv_string_chk(argvar);
19450 if (arg == NULL)
19451 nr = 0; /* type error; errmsg already given */
19452 else if (STRCMP(arg, "$") == 0)
19453 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19454 else if (STRCMP(arg, "#") == 0)
19455 {
19456 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19457 if (twin == NULL)
19458 nr = 0;
19459 }
19460 else
19461 {
19462 EMSG2(_(e_invexpr2), arg);
19463 nr = 0;
19464 }
19465 }
19466
19467 if (nr > 0)
19468 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19469 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019470 {
19471 if (wp == NULL)
19472 {
19473 /* didn't find it in this tabpage */
19474 nr = 0;
19475 break;
19476 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019477 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019478 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019479 return nr;
19480}
19481#endif
19482
19483/*
19484 * "tabpagewinnr()" function
19485 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019486 static void
19487f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019488 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019489 typval_T *rettv;
19490{
19491 int nr = 1;
19492#ifdef FEAT_WINDOWS
19493 tabpage_T *tp;
19494
19495 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19496 if (tp == NULL)
19497 nr = 0;
19498 else
19499 nr = get_winnr(tp, &argvars[1]);
19500#endif
19501 rettv->vval.v_number = nr;
19502}
19503
19504
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019505/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019506 * "tagfiles()" function
19507 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019508 static void
19509f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019510 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019511 typval_T *rettv;
19512{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019513 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019514 tagname_T tn;
19515 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019516
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019517 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019518 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019519 fname = alloc(MAXPATHL);
19520 if (fname == NULL)
19521 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019522
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019523 for (first = TRUE; ; first = FALSE)
19524 if (get_tagfname(&tn, first, fname) == FAIL
19525 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019526 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019527 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019528 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019529}
19530
19531/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019532 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019533 */
19534 static void
19535f_taglist(argvars, rettv)
19536 typval_T *argvars;
19537 typval_T *rettv;
19538{
19539 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019540
19541 tag_pattern = get_tv_string(&argvars[0]);
19542
19543 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019544 if (*tag_pattern == NUL)
19545 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019546
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019547 if (rettv_list_alloc(rettv) == OK)
19548 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019549}
19550
19551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 * "tempname()" function
19553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019555f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019556 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558{
19559 static int x = 'A';
19560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019561 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019562 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563
19564 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19565 * names. Skip 'I' and 'O', they are used for shell redirection. */
19566 do
19567 {
19568 if (x == 'Z')
19569 x = '0';
19570 else if (x == '9')
19571 x = 'A';
19572 else
19573 {
19574#ifdef EBCDIC
19575 if (x == 'I')
19576 x = 'J';
19577 else if (x == 'R')
19578 x = 'S';
19579 else
19580#endif
19581 ++x;
19582 }
19583 } while (x == 'I' || x == 'O');
19584}
19585
19586/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019587 * "test(list)" function: Just checking the walls...
19588 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019589 static void
19590f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019591 typval_T *argvars UNUSED;
19592 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019593{
19594 /* Used for unit testing. Change the code below to your liking. */
19595#if 0
19596 listitem_T *li;
19597 list_T *l;
19598 char_u *bad, *good;
19599
19600 if (argvars[0].v_type != VAR_LIST)
19601 return;
19602 l = argvars[0].vval.v_list;
19603 if (l == NULL)
19604 return;
19605 li = l->lv_first;
19606 if (li == NULL)
19607 return;
19608 bad = get_tv_string(&li->li_tv);
19609 li = li->li_next;
19610 if (li == NULL)
19611 return;
19612 good = get_tv_string(&li->li_tv);
19613 rettv->vval.v_number = test_edit_score(bad, good);
19614#endif
19615}
19616
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019617#ifdef FEAT_FLOAT
19618/*
19619 * "tan()" function
19620 */
19621 static void
19622f_tan(argvars, rettv)
19623 typval_T *argvars;
19624 typval_T *rettv;
19625{
19626 float_T f;
19627
19628 rettv->v_type = VAR_FLOAT;
19629 if (get_float_arg(argvars, &f) == OK)
19630 rettv->vval.v_float = tan(f);
19631 else
19632 rettv->vval.v_float = 0.0;
19633}
19634
19635/*
19636 * "tanh()" function
19637 */
19638 static void
19639f_tanh(argvars, rettv)
19640 typval_T *argvars;
19641 typval_T *rettv;
19642{
19643 float_T f;
19644
19645 rettv->v_type = VAR_FLOAT;
19646 if (get_float_arg(argvars, &f) == OK)
19647 rettv->vval.v_float = tanh(f);
19648 else
19649 rettv->vval.v_float = 0.0;
19650}
19651#endif
19652
Bram Moolenaard52d9742005-08-21 22:20:28 +000019653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 * "tolower(string)" function
19655 */
19656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019657f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019658 typval_T *argvars;
19659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660{
19661 char_u *p;
19662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019663 p = vim_strsave(get_tv_string(&argvars[0]));
19664 rettv->v_type = VAR_STRING;
19665 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666
19667 if (p != NULL)
19668 while (*p != NUL)
19669 {
19670#ifdef FEAT_MBYTE
19671 int l;
19672
19673 if (enc_utf8)
19674 {
19675 int c, lc;
19676
19677 c = utf_ptr2char(p);
19678 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019679 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 /* TODO: reallocate string when byte count changes. */
19681 if (utf_char2len(lc) == l)
19682 utf_char2bytes(lc, p);
19683 p += l;
19684 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019685 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 p += l; /* skip multi-byte character */
19687 else
19688#endif
19689 {
19690 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19691 ++p;
19692 }
19693 }
19694}
19695
19696/*
19697 * "toupper(string)" function
19698 */
19699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019700f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019701 typval_T *argvars;
19702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019704 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019705 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706}
19707
19708/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019709 * "tr(string, fromstr, tostr)" function
19710 */
19711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019712f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019713 typval_T *argvars;
19714 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019715{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019716 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019717 char_u *fromstr;
19718 char_u *tostr;
19719 char_u *p;
19720#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019721 int inlen;
19722 int fromlen;
19723 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019724 int idx;
19725 char_u *cpstr;
19726 int cplen;
19727 int first = TRUE;
19728#endif
19729 char_u buf[NUMBUFLEN];
19730 char_u buf2[NUMBUFLEN];
19731 garray_T ga;
19732
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019733 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019734 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19735 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019736
19737 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019738 rettv->v_type = VAR_STRING;
19739 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019740 if (fromstr == NULL || tostr == NULL)
19741 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019742 ga_init2(&ga, (int)sizeof(char), 80);
19743
19744#ifdef FEAT_MBYTE
19745 if (!has_mbyte)
19746#endif
19747 /* not multi-byte: fromstr and tostr must be the same length */
19748 if (STRLEN(fromstr) != STRLEN(tostr))
19749 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019750#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019751error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019752#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019753 EMSG2(_(e_invarg2), fromstr);
19754 ga_clear(&ga);
19755 return;
19756 }
19757
19758 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019759 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019760 {
19761#ifdef FEAT_MBYTE
19762 if (has_mbyte)
19763 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019764 inlen = (*mb_ptr2len)(in_str);
19765 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019766 cplen = inlen;
19767 idx = 0;
19768 for (p = fromstr; *p != NUL; p += fromlen)
19769 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019770 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019771 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019772 {
19773 for (p = tostr; *p != NUL; p += tolen)
19774 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019775 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019776 if (idx-- == 0)
19777 {
19778 cplen = tolen;
19779 cpstr = p;
19780 break;
19781 }
19782 }
19783 if (*p == NUL) /* tostr is shorter than fromstr */
19784 goto error;
19785 break;
19786 }
19787 ++idx;
19788 }
19789
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019790 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019791 {
19792 /* Check that fromstr and tostr have the same number of
19793 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019794 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019795 first = FALSE;
19796 for (p = tostr; *p != NUL; p += tolen)
19797 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019798 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019799 --idx;
19800 }
19801 if (idx != 0)
19802 goto error;
19803 }
19804
Bram Moolenaarcde88542015-08-11 19:14:00 +020019805 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019806 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019807 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019808
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019809 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019810 }
19811 else
19812#endif
19813 {
19814 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019815 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019816 if (p != NULL)
19817 ga_append(&ga, tostr[p - fromstr]);
19818 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019819 ga_append(&ga, *in_str);
19820 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019821 }
19822 }
19823
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019824 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019825 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019826 ga_append(&ga, NUL);
19827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019829}
19830
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019831#ifdef FEAT_FLOAT
19832/*
19833 * "trunc({float})" function
19834 */
19835 static void
19836f_trunc(argvars, rettv)
19837 typval_T *argvars;
19838 typval_T *rettv;
19839{
19840 float_T f;
19841
19842 rettv->v_type = VAR_FLOAT;
19843 if (get_float_arg(argvars, &f) == OK)
19844 /* trunc() is not in C90, use floor() or ceil() instead. */
19845 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19846 else
19847 rettv->vval.v_float = 0.0;
19848}
19849#endif
19850
Bram Moolenaar8299df92004-07-10 09:47:34 +000019851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 * "type(expr)" function
19853 */
19854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 typval_T *argvars;
19857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019859 int n;
19860
19861 switch (argvars[0].v_type)
19862 {
19863 case VAR_NUMBER: n = 0; break;
19864 case VAR_STRING: n = 1; break;
19865 case VAR_FUNC: n = 2; break;
19866 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019867 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019868#ifdef FEAT_FLOAT
19869 case VAR_FLOAT: n = 5; break;
19870#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019871 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19872 }
19873 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874}
19875
19876/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019877 * "undofile(name)" function
19878 */
19879 static void
19880f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019881 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019882 typval_T *rettv;
19883{
19884 rettv->v_type = VAR_STRING;
19885#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019886 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019887 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019888
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019889 if (*fname == NUL)
19890 {
19891 /* If there is no file name there will be no undo file. */
19892 rettv->vval.v_string = NULL;
19893 }
19894 else
19895 {
19896 char_u *ffname = FullName_save(fname, FALSE);
19897
19898 if (ffname != NULL)
19899 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19900 vim_free(ffname);
19901 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019902 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019903#else
19904 rettv->vval.v_string = NULL;
19905#endif
19906}
19907
19908/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019909 * "undotree()" function
19910 */
19911 static void
19912f_undotree(argvars, rettv)
19913 typval_T *argvars UNUSED;
19914 typval_T *rettv;
19915{
19916 if (rettv_dict_alloc(rettv) == OK)
19917 {
19918 dict_T *dict = rettv->vval.v_dict;
19919 list_T *list;
19920
Bram Moolenaar730cde92010-06-27 05:18:54 +020019921 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019922 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019923 dict_add_nr_str(dict, "save_last",
19924 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019925 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19926 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019927 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019928
19929 list = list_alloc();
19930 if (list != NULL)
19931 {
19932 u_eval_tree(curbuf->b_u_oldhead, list);
19933 dict_add_list(dict, "entries", list);
19934 }
19935 }
19936}
19937
19938/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019939 * "values(dict)" function
19940 */
19941 static void
19942f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 typval_T *argvars;
19944 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019945{
19946 dict_list(argvars, rettv, 1);
19947}
19948
19949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 * "virtcol(string)" function
19951 */
19952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019953f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019954 typval_T *argvars;
19955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956{
19957 colnr_T vcol = 0;
19958 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019959 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019961 fp = var2fpos(&argvars[0], FALSE, &fnum);
19962 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19963 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 {
19965 getvvcol(curwin, fp, NULL, NULL, &vcol);
19966 ++vcol;
19967 }
19968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970}
19971
19972/*
19973 * "visualmode()" function
19974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019977 typval_T *argvars UNUSED;
19978 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 char_u str[2];
19981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 str[0] = curbuf->b_visual_mode_eval;
19984 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986
19987 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019988 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990}
19991
19992/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019993 * "wildmenumode()" function
19994 */
19995 static void
19996f_wildmenumode(argvars, rettv)
19997 typval_T *argvars UNUSED;
19998 typval_T *rettv UNUSED;
19999{
20000#ifdef FEAT_WILDMENU
20001 if (wild_menu_showing)
20002 rettv->vval.v_number = 1;
20003#endif
20004}
20005
20006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 * "winbufnr(nr)" function
20008 */
20009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020011 typval_T *argvars;
20012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013{
20014 win_T *wp;
20015
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020016 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021}
20022
20023/*
20024 * "wincol()" function
20025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020027f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020028 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030{
20031 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020032 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033}
20034
20035/*
20036 * "winheight(nr)" function
20037 */
20038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020039f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 typval_T *argvars;
20041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042{
20043 win_T *wp;
20044
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020045 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050}
20051
20052/*
20053 * "winline()" function
20054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020056f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059{
20060 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062}
20063
20064/*
20065 * "winnr()" function
20066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020069 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071{
20072 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020073
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020075 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078}
20079
20080/*
20081 * "winrestcmd()" function
20082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020084f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020085 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087{
20088#ifdef FEAT_WINDOWS
20089 win_T *wp;
20090 int winnr = 1;
20091 garray_T ga;
20092 char_u buf[50];
20093
20094 ga_init2(&ga, (int)sizeof(char), 70);
20095 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20096 {
20097 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20098 ga_concat(&ga, buf);
20099# ifdef FEAT_VERTSPLIT
20100 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20101 ga_concat(&ga, buf);
20102# endif
20103 ++winnr;
20104 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020105 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020109 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020111 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112}
20113
20114/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020115 * "winrestview()" function
20116 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020117 static void
20118f_winrestview(argvars, rettv)
20119 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020120 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020121{
20122 dict_T *dict;
20123
20124 if (argvars[0].v_type != VAR_DICT
20125 || (dict = argvars[0].vval.v_dict) == NULL)
20126 EMSG(_(e_invarg));
20127 else
20128 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020129 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20130 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20131 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20132 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020133#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020134 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20135 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020136#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020137 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20138 {
20139 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20140 curwin->w_set_curswant = FALSE;
20141 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020142
Bram Moolenaar82c25852014-05-28 16:47:16 +020020143 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20144 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020145#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020146 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20147 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020148#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020149 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20150 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20151 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20152 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020153
20154 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020155 win_new_height(curwin, curwin->w_height);
20156# ifdef FEAT_VERTSPLIT
20157 win_new_width(curwin, W_WIDTH(curwin));
20158# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020159 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020160
Bram Moolenaarb851a962014-10-31 15:45:52 +010020161 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020162 curwin->w_topline = 1;
20163 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20164 curwin->w_topline = curbuf->b_ml.ml_line_count;
20165#ifdef FEAT_DIFF
20166 check_topfill(curwin, TRUE);
20167#endif
20168 }
20169}
20170
20171/*
20172 * "winsaveview()" function
20173 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020174 static void
20175f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020176 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020177 typval_T *rettv;
20178{
20179 dict_T *dict;
20180
Bram Moolenaara800b422010-06-27 01:15:55 +020020181 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020182 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020183 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020184
20185 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20186 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20187#ifdef FEAT_VIRTUALEDIT
20188 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20189#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020190 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020191 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20192
20193 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20194#ifdef FEAT_DIFF
20195 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20196#endif
20197 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20198 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20199}
20200
20201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 * "winwidth(nr)" function
20203 */
20204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020205f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020206 typval_T *argvars;
20207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208{
20209 win_T *wp;
20210
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020211 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 else
20215#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020216 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219#endif
20220}
20221
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020223 * Write list of strings to file
20224 */
20225 static int
20226write_list(fd, list, binary)
20227 FILE *fd;
20228 list_T *list;
20229 int binary;
20230{
20231 listitem_T *li;
20232 int c;
20233 int ret = OK;
20234 char_u *s;
20235
20236 for (li = list->lv_first; li != NULL; li = li->li_next)
20237 {
20238 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20239 {
20240 if (*s == '\n')
20241 c = putc(NUL, fd);
20242 else
20243 c = putc(*s, fd);
20244 if (c == EOF)
20245 {
20246 ret = FAIL;
20247 break;
20248 }
20249 }
20250 if (!binary || li->li_next != NULL)
20251 if (putc('\n', fd) == EOF)
20252 {
20253 ret = FAIL;
20254 break;
20255 }
20256 if (ret == FAIL)
20257 {
20258 EMSG(_(e_write));
20259 break;
20260 }
20261 }
20262 return ret;
20263}
20264
20265/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020266 * "writefile()" function
20267 */
20268 static void
20269f_writefile(argvars, rettv)
20270 typval_T *argvars;
20271 typval_T *rettv;
20272{
20273 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020274 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020275 char_u *fname;
20276 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020277 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020278
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020279 if (check_restricted() || check_secure())
20280 return;
20281
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020282 if (argvars[0].v_type != VAR_LIST)
20283 {
20284 EMSG2(_(e_listarg), "writefile()");
20285 return;
20286 }
20287 if (argvars[0].vval.v_list == NULL)
20288 return;
20289
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020290 if (argvars[2].v_type != VAR_UNKNOWN)
20291 {
20292 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20293 binary = TRUE;
20294 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20295 append = TRUE;
20296 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020297
20298 /* Always open the file in binary mode, library functions have a mind of
20299 * their own about CR-LF conversion. */
20300 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020301 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20302 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020303 {
20304 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20305 ret = -1;
20306 }
20307 else
20308 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020309 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20310 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020311 fclose(fd);
20312 }
20313
20314 rettv->vval.v_number = ret;
20315}
20316
20317/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020318 * "xor(expr, expr)" function
20319 */
20320 static void
20321f_xor(argvars, rettv)
20322 typval_T *argvars;
20323 typval_T *rettv;
20324{
20325 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20326 ^ get_tv_number_chk(&argvars[1], NULL);
20327}
20328
20329
20330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020332 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 */
20334 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020335var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020336 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020337 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020338 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020340 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020342 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343
Bram Moolenaara5525202006-03-02 22:52:09 +000020344 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020345 if (varp->v_type == VAR_LIST)
20346 {
20347 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020348 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020349 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020350 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020351
20352 l = varp->vval.v_list;
20353 if (l == NULL)
20354 return NULL;
20355
20356 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020357 pos.lnum = list_find_nr(l, 0L, &error);
20358 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020359 return NULL; /* invalid line number */
20360
20361 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020362 pos.col = list_find_nr(l, 1L, &error);
20363 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020364 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020365 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020366
20367 /* We accept "$" for the column number: last column. */
20368 li = list_find(l, 1L);
20369 if (li != NULL && li->li_tv.v_type == VAR_STRING
20370 && li->li_tv.vval.v_string != NULL
20371 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20372 pos.col = len + 1;
20373
Bram Moolenaara5525202006-03-02 22:52:09 +000020374 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020375 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020376 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020377 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020378
Bram Moolenaara5525202006-03-02 22:52:09 +000020379#ifdef FEAT_VIRTUALEDIT
20380 /* Get the virtual offset. Defaults to zero. */
20381 pos.coladd = list_find_nr(l, 2L, &error);
20382 if (error)
20383 pos.coladd = 0;
20384#endif
20385
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020386 return &pos;
20387 }
20388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020389 name = get_tv_string_chk(varp);
20390 if (name == NULL)
20391 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020392 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020394 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20395 {
20396 if (VIsual_active)
20397 return &VIsual;
20398 return &curwin->w_cursor;
20399 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020400 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020402 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20404 return NULL;
20405 return pp;
20406 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020407
20408#ifdef FEAT_VIRTUALEDIT
20409 pos.coladd = 0;
20410#endif
20411
Bram Moolenaar477933c2007-07-17 14:32:23 +000020412 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020413 {
20414 pos.col = 0;
20415 if (name[1] == '0') /* "w0": first visible line */
20416 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020417 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020418 pos.lnum = curwin->w_topline;
20419 return &pos;
20420 }
20421 else if (name[1] == '$') /* "w$": last visible line */
20422 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020423 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020424 pos.lnum = curwin->w_botline - 1;
20425 return &pos;
20426 }
20427 }
20428 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020430 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 {
20432 pos.lnum = curbuf->b_ml.ml_line_count;
20433 pos.col = 0;
20434 }
20435 else
20436 {
20437 pos.lnum = curwin->w_cursor.lnum;
20438 pos.col = (colnr_T)STRLEN(ml_get_curline());
20439 }
20440 return &pos;
20441 }
20442 return NULL;
20443}
20444
20445/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020446 * Convert list in "arg" into a position and optional file number.
20447 * When "fnump" is NULL there is no file number, only 3 items.
20448 * Note that the column is passed on as-is, the caller may want to decrement
20449 * it to use 1 for the first column.
20450 * Return FAIL when conversion is not possible, doesn't check the position for
20451 * validity.
20452 */
20453 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020454list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020455 typval_T *arg;
20456 pos_T *posp;
20457 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020458 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020459{
20460 list_T *l = arg->vval.v_list;
20461 long i = 0;
20462 long n;
20463
Bram Moolenaar493c1782014-05-28 14:34:46 +020020464 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20465 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020466 if (arg->v_type != VAR_LIST
20467 || l == NULL
20468 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020469 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020470 return FAIL;
20471
20472 if (fnump != NULL)
20473 {
20474 n = list_find_nr(l, i++, NULL); /* fnum */
20475 if (n < 0)
20476 return FAIL;
20477 if (n == 0)
20478 n = curbuf->b_fnum; /* current buffer */
20479 *fnump = n;
20480 }
20481
20482 n = list_find_nr(l, i++, NULL); /* lnum */
20483 if (n < 0)
20484 return FAIL;
20485 posp->lnum = n;
20486
20487 n = list_find_nr(l, i++, NULL); /* col */
20488 if (n < 0)
20489 return FAIL;
20490 posp->col = n;
20491
20492#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020493 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020494 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020495 posp->coladd = 0;
20496 else
20497 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020498#endif
20499
Bram Moolenaar493c1782014-05-28 14:34:46 +020020500 if (curswantp != NULL)
20501 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20502
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020503 return OK;
20504}
20505
20506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 * Get the length of an environment variable name.
20508 * Advance "arg" to the first character after the name.
20509 * Return 0 for error.
20510 */
20511 static int
20512get_env_len(arg)
20513 char_u **arg;
20514{
20515 char_u *p;
20516 int len;
20517
20518 for (p = *arg; vim_isIDc(*p); ++p)
20519 ;
20520 if (p == *arg) /* no name found */
20521 return 0;
20522
20523 len = (int)(p - *arg);
20524 *arg = p;
20525 return len;
20526}
20527
20528/*
20529 * Get the length of the name of a function or internal variable.
20530 * "arg" is advanced to the first non-white character after the name.
20531 * Return 0 if something is wrong.
20532 */
20533 static int
20534get_id_len(arg)
20535 char_u **arg;
20536{
20537 char_u *p;
20538 int len;
20539
20540 /* Find the end of the name. */
20541 for (p = *arg; eval_isnamec(*p); ++p)
20542 ;
20543 if (p == *arg) /* no name found */
20544 return 0;
20545
20546 len = (int)(p - *arg);
20547 *arg = skipwhite(p);
20548
20549 return len;
20550}
20551
20552/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020553 * Get the length of the name of a variable or function.
20554 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020556 * Return -1 if curly braces expansion failed.
20557 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558 * If the name contains 'magic' {}'s, expand them and return the
20559 * expanded name in an allocated string via 'alias' - caller must free.
20560 */
20561 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020562get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 char_u **arg;
20564 char_u **alias;
20565 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020566 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567{
20568 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 char_u *p;
20570 char_u *expr_start;
20571 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572
20573 *alias = NULL; /* default to no alias */
20574
20575 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20576 && (*arg)[2] == (int)KE_SNR)
20577 {
20578 /* hard coded <SNR>, already translated */
20579 *arg += 3;
20580 return get_id_len(arg) + 3;
20581 }
20582 len = eval_fname_script(*arg);
20583 if (len > 0)
20584 {
20585 /* literal "<SID>", "s:" or "<SNR>" */
20586 *arg += len;
20587 }
20588
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020590 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020592 p = find_name_end(*arg, &expr_start, &expr_end,
20593 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594 if (expr_start != NULL)
20595 {
20596 char_u *temp_string;
20597
20598 if (!evaluate)
20599 {
20600 len += (int)(p - *arg);
20601 *arg = skipwhite(p);
20602 return len;
20603 }
20604
20605 /*
20606 * Include any <SID> etc in the expanded string:
20607 * Thus the -len here.
20608 */
20609 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20610 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020611 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 *alias = temp_string;
20613 *arg = skipwhite(p);
20614 return (int)STRLEN(temp_string);
20615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616
20617 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020618 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 EMSG2(_(e_invexpr2), *arg);
20620
20621 return len;
20622}
20623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020624/*
20625 * Find the end of a variable or function name, taking care of magic braces.
20626 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20627 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020628 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020629 * Return a pointer to just after the name. Equal to "arg" if there is no
20630 * valid name.
20631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020633find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 char_u *arg;
20635 char_u **expr_start;
20636 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020637 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639 int mb_nest = 0;
20640 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 char_u *p;
20642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020643 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020645 *expr_start = NULL;
20646 *expr_end = NULL;
20647 }
20648
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020649 /* Quick check for valid starting character. */
20650 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20651 return arg;
20652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020653 for (p = arg; *p != NUL
20654 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020655 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020656 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020658 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020659 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020660 if (*p == '\'')
20661 {
20662 /* skip over 'string' to avoid counting [ and ] inside it. */
20663 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20664 ;
20665 if (*p == NUL)
20666 break;
20667 }
20668 else if (*p == '"')
20669 {
20670 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20671 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20672 if (*p == '\\' && p[1] != NUL)
20673 ++p;
20674 if (*p == NUL)
20675 break;
20676 }
20677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020678 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020680 if (*p == '[')
20681 ++br_nest;
20682 else if (*p == ']')
20683 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020686 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020688 if (*p == '{')
20689 {
20690 mb_nest++;
20691 if (expr_start != NULL && *expr_start == NULL)
20692 *expr_start = p;
20693 }
20694 else if (*p == '}')
20695 {
20696 mb_nest--;
20697 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20698 *expr_end = p;
20699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 }
20702
20703 return p;
20704}
20705
20706/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020707 * Expands out the 'magic' {}'s in a variable/function name.
20708 * Note that this can call itself recursively, to deal with
20709 * constructs like foo{bar}{baz}{bam}
20710 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20711 * "in_start" ^
20712 * "expr_start" ^
20713 * "expr_end" ^
20714 * "in_end" ^
20715 *
20716 * Returns a new allocated string, which the caller must free.
20717 * Returns NULL for failure.
20718 */
20719 static char_u *
20720make_expanded_name(in_start, expr_start, expr_end, in_end)
20721 char_u *in_start;
20722 char_u *expr_start;
20723 char_u *expr_end;
20724 char_u *in_end;
20725{
20726 char_u c1;
20727 char_u *retval = NULL;
20728 char_u *temp_result;
20729 char_u *nextcmd = NULL;
20730
20731 if (expr_end == NULL || in_end == NULL)
20732 return NULL;
20733 *expr_start = NUL;
20734 *expr_end = NUL;
20735 c1 = *in_end;
20736 *in_end = NUL;
20737
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020738 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020739 if (temp_result != NULL && nextcmd == NULL)
20740 {
20741 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20742 + (in_end - expr_end) + 1));
20743 if (retval != NULL)
20744 {
20745 STRCPY(retval, in_start);
20746 STRCAT(retval, temp_result);
20747 STRCAT(retval, expr_end + 1);
20748 }
20749 }
20750 vim_free(temp_result);
20751
20752 *in_end = c1; /* put char back for error messages */
20753 *expr_start = '{';
20754 *expr_end = '}';
20755
20756 if (retval != NULL)
20757 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020758 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020759 if (expr_start != NULL)
20760 {
20761 /* Further expansion! */
20762 temp_result = make_expanded_name(retval, expr_start,
20763 expr_end, temp_result);
20764 vim_free(retval);
20765 retval = temp_result;
20766 }
20767 }
20768
20769 return retval;
20770}
20771
20772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020774 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 */
20776 static int
20777eval_isnamec(c)
20778 int c;
20779{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020780 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20781}
20782
20783/*
20784 * Return TRUE if character "c" can be used as the first character in a
20785 * variable or function name (excluding '{' and '}').
20786 */
20787 static int
20788eval_isnamec1(c)
20789 int c;
20790{
20791 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792}
20793
20794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 * Set number v: variable to "val".
20796 */
20797 void
20798set_vim_var_nr(idx, val)
20799 int idx;
20800 long val;
20801{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020802 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803}
20804
20805/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020806 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 */
20808 long
20809get_vim_var_nr(idx)
20810 int idx;
20811{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020812 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813}
20814
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020815/*
20816 * Get string v: variable value. Uses a static buffer, can only be used once.
20817 */
20818 char_u *
20819get_vim_var_str(idx)
20820 int idx;
20821{
20822 return get_tv_string(&vimvars[idx].vv_tv);
20823}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020824
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020826 * Get List v: variable value. Caller must take care of reference count when
20827 * needed.
20828 */
20829 list_T *
20830get_vim_var_list(idx)
20831 int idx;
20832{
20833 return vimvars[idx].vv_list;
20834}
20835
20836/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020837 * Set v:char to character "c".
20838 */
20839 void
20840set_vim_var_char(c)
20841 int c;
20842{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020843 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020844
20845#ifdef FEAT_MBYTE
20846 if (has_mbyte)
20847 buf[(*mb_char2bytes)(c, buf)] = NUL;
20848 else
20849#endif
20850 {
20851 buf[0] = c;
20852 buf[1] = NUL;
20853 }
20854 set_vim_var_string(VV_CHAR, buf, -1);
20855}
20856
20857/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020858 * Set v:count to "count" and v:count1 to "count1".
20859 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 */
20861 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020862set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 long count;
20864 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020865 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020867 if (set_prevcount)
20868 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020869 vimvars[VV_COUNT].vv_nr = count;
20870 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871}
20872
20873/*
20874 * Set string v: variable to a copy of "val".
20875 */
20876 void
20877set_vim_var_string(idx, val, len)
20878 int idx;
20879 char_u *val;
20880 int len; /* length of "val" to use or -1 (whole string) */
20881{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020882 /* Need to do this (at least) once, since we can't initialize a union.
20883 * Will always be invoked when "v:progname" is set. */
20884 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20885
Bram Moolenaare9a41262005-01-15 22:18:47 +000020886 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020888 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020890 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020892 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893}
20894
20895/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020896 * Set List v: variable to "val".
20897 */
20898 void
20899set_vim_var_list(idx, val)
20900 int idx;
20901 list_T *val;
20902{
20903 list_unref(vimvars[idx].vv_list);
20904 vimvars[idx].vv_list = val;
20905 if (val != NULL)
20906 ++val->lv_refcount;
20907}
20908
20909/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020910 * Set Dictionary v: variable to "val".
20911 */
20912 void
20913set_vim_var_dict(idx, val)
20914 int idx;
20915 dict_T *val;
20916{
20917 int todo;
20918 hashitem_T *hi;
20919
20920 dict_unref(vimvars[idx].vv_dict);
20921 vimvars[idx].vv_dict = val;
20922 if (val != NULL)
20923 {
20924 ++val->dv_refcount;
20925
20926 /* Set readonly */
20927 todo = (int)val->dv_hashtab.ht_used;
20928 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20929 {
20930 if (HASHITEM_EMPTY(hi))
20931 continue;
20932 --todo;
20933 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20934 }
20935 }
20936}
20937
20938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 * Set v:register if needed.
20940 */
20941 void
20942set_reg_var(c)
20943 int c;
20944{
20945 char_u regname;
20946
20947 if (c == 0 || c == ' ')
20948 regname = '"';
20949 else
20950 regname = c;
20951 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020952 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 set_vim_var_string(VV_REG, &regname, 1);
20954}
20955
20956/*
20957 * Get or set v:exception. If "oldval" == NULL, return the current value.
20958 * Otherwise, restore the value to "oldval" and return NULL.
20959 * Must always be called in pairs to save and restore v:exception! Does not
20960 * take care of memory allocations.
20961 */
20962 char_u *
20963v_exception(oldval)
20964 char_u *oldval;
20965{
20966 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020967 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968
Bram Moolenaare9a41262005-01-15 22:18:47 +000020969 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 return NULL;
20971}
20972
20973/*
20974 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20975 * Otherwise, restore the value to "oldval" and return NULL.
20976 * Must always be called in pairs to save and restore v:throwpoint! Does not
20977 * take care of memory allocations.
20978 */
20979 char_u *
20980v_throwpoint(oldval)
20981 char_u *oldval;
20982{
20983 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020984 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985
Bram Moolenaare9a41262005-01-15 22:18:47 +000020986 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 return NULL;
20988}
20989
20990#if defined(FEAT_AUTOCMD) || defined(PROTO)
20991/*
20992 * Set v:cmdarg.
20993 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20994 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20995 * Must always be called in pairs!
20996 */
20997 char_u *
20998set_cmdarg(eap, oldarg)
20999 exarg_T *eap;
21000 char_u *oldarg;
21001{
21002 char_u *oldval;
21003 char_u *newval;
21004 unsigned len;
21005
Bram Moolenaare9a41262005-01-15 22:18:47 +000021006 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021007 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021009 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021010 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021011 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 }
21013
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021014 if (eap->force_bin == FORCE_BIN)
21015 len = 6;
21016 else if (eap->force_bin == FORCE_NOBIN)
21017 len = 8;
21018 else
21019 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021020
21021 if (eap->read_edit)
21022 len += 7;
21023
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021024 if (eap->force_ff != 0)
21025 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21026# ifdef FEAT_MBYTE
21027 if (eap->force_enc != 0)
21028 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021029 if (eap->bad_char != 0)
21030 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021031# endif
21032
21033 newval = alloc(len + 1);
21034 if (newval == NULL)
21035 return NULL;
21036
21037 if (eap->force_bin == FORCE_BIN)
21038 sprintf((char *)newval, " ++bin");
21039 else if (eap->force_bin == FORCE_NOBIN)
21040 sprintf((char *)newval, " ++nobin");
21041 else
21042 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021043
21044 if (eap->read_edit)
21045 STRCAT(newval, " ++edit");
21046
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021047 if (eap->force_ff != 0)
21048 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21049 eap->cmd + eap->force_ff);
21050# ifdef FEAT_MBYTE
21051 if (eap->force_enc != 0)
21052 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21053 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021054 if (eap->bad_char == BAD_KEEP)
21055 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21056 else if (eap->bad_char == BAD_DROP)
21057 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21058 else if (eap->bad_char != 0)
21059 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021060# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021061 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021062 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063}
21064#endif
21065
21066/*
21067 * Get the value of internal variable "name".
21068 * Return OK or FAIL.
21069 */
21070 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021071get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 char_u *name;
21073 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021074 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021075 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021076 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021077 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078{
21079 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021080 typval_T *tv = NULL;
21081 typval_T atv;
21082 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084
21085 /* truncate the name, so that we can use strcmp() */
21086 cc = name[len];
21087 name[len] = NUL;
21088
21089 /*
21090 * Check for "b:changedtick".
21091 */
21092 if (STRCMP(name, "b:changedtick") == 0)
21093 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021094 atv.v_type = VAR_NUMBER;
21095 atv.vval.v_number = curbuf->b_changedtick;
21096 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 }
21098
21099 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 * Check for user-defined variables.
21101 */
21102 else
21103 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021104 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021106 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021107 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021108 if (dip != NULL)
21109 *dip = v;
21110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 }
21112
Bram Moolenaare9a41262005-01-15 22:18:47 +000021113 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021115 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021116 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 ret = FAIL;
21118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021119 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021120 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121
21122 name[len] = cc;
21123
21124 return ret;
21125}
21126
21127/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021128 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21129 * Also handle function call with Funcref variable: func(expr)
21130 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21131 */
21132 static int
21133handle_subscript(arg, rettv, evaluate, verbose)
21134 char_u **arg;
21135 typval_T *rettv;
21136 int evaluate; /* do more than finding the end */
21137 int verbose; /* give error messages */
21138{
21139 int ret = OK;
21140 dict_T *selfdict = NULL;
21141 char_u *s;
21142 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021143 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021144
21145 while (ret == OK
21146 && (**arg == '['
21147 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021148 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021149 && !vim_iswhite(*(*arg - 1)))
21150 {
21151 if (**arg == '(')
21152 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021153 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021154 if (evaluate)
21155 {
21156 functv = *rettv;
21157 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021158
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021159 /* Invoke the function. Recursive! */
21160 s = functv.vval.v_string;
21161 }
21162 else
21163 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021164 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021165 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21166 &len, evaluate, selfdict);
21167
21168 /* Clear the funcref afterwards, so that deleting it while
21169 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021170 if (evaluate)
21171 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021172
21173 /* Stop the expression evaluation when immediately aborting on
21174 * error, or when an interrupt occurred or an exception was thrown
21175 * but not caught. */
21176 if (aborting())
21177 {
21178 if (ret == OK)
21179 clear_tv(rettv);
21180 ret = FAIL;
21181 }
21182 dict_unref(selfdict);
21183 selfdict = NULL;
21184 }
21185 else /* **arg == '[' || **arg == '.' */
21186 {
21187 dict_unref(selfdict);
21188 if (rettv->v_type == VAR_DICT)
21189 {
21190 selfdict = rettv->vval.v_dict;
21191 if (selfdict != NULL)
21192 ++selfdict->dv_refcount;
21193 }
21194 else
21195 selfdict = NULL;
21196 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21197 {
21198 clear_tv(rettv);
21199 ret = FAIL;
21200 }
21201 }
21202 }
21203 dict_unref(selfdict);
21204 return ret;
21205}
21206
21207/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021208 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021209 * value).
21210 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021211 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021212alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021213{
Bram Moolenaar33570922005-01-25 22:26:29 +000021214 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021215}
21216
21217/*
21218 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 * The string "s" must have been allocated, it is consumed.
21220 * Return NULL for out of memory, the variable otherwise.
21221 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021222 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021223alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 char_u *s;
21225{
Bram Moolenaar33570922005-01-25 22:26:29 +000021226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021228 rettv = alloc_tv();
21229 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021231 rettv->v_type = VAR_STRING;
21232 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 }
21234 else
21235 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021236 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237}
21238
21239/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021240 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021242 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021243free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021244 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245{
21246 if (varp != NULL)
21247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021248 switch (varp->v_type)
21249 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021250 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021251 func_unref(varp->vval.v_string);
21252 /*FALLTHROUGH*/
21253 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021254 vim_free(varp->vval.v_string);
21255 break;
21256 case VAR_LIST:
21257 list_unref(varp->vval.v_list);
21258 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021259 case VAR_DICT:
21260 dict_unref(varp->vval.v_dict);
21261 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021262 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021263#ifdef FEAT_FLOAT
21264 case VAR_FLOAT:
21265#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021266 case VAR_UNKNOWN:
21267 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021268 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021269 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021270 break;
21271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 vim_free(varp);
21273 }
21274}
21275
21276/*
21277 * Free the memory for a variable value and set the value to NULL or 0.
21278 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021279 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021280clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021281 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282{
21283 if (varp != NULL)
21284 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021285 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021287 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021288 func_unref(varp->vval.v_string);
21289 /*FALLTHROUGH*/
21290 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021291 vim_free(varp->vval.v_string);
21292 varp->vval.v_string = NULL;
21293 break;
21294 case VAR_LIST:
21295 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021296 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021297 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021298 case VAR_DICT:
21299 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021300 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021301 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021302 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021303 varp->vval.v_number = 0;
21304 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021305#ifdef FEAT_FLOAT
21306 case VAR_FLOAT:
21307 varp->vval.v_float = 0.0;
21308 break;
21309#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021310 case VAR_UNKNOWN:
21311 break;
21312 default:
21313 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021315 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 }
21317}
21318
21319/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021320 * Set the value of a variable to NULL without freeing items.
21321 */
21322 static void
21323init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021324 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021325{
21326 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021327 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021328}
21329
21330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 * Get the number value of a variable.
21332 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021333 * For incompatible types, return 0.
21334 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21335 * caller of incompatible types: it sets *denote to TRUE if "denote"
21336 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 */
21338 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021339get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021340 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021342 int error = FALSE;
21343
21344 return get_tv_number_chk(varp, &error); /* return 0L on error */
21345}
21346
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021347 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021348get_tv_number_chk(varp, denote)
21349 typval_T *varp;
21350 int *denote;
21351{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021352 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021354 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021356 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021357 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021358#ifdef FEAT_FLOAT
21359 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021360 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021361 break;
21362#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021363 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021364 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021365 break;
21366 case VAR_STRING:
21367 if (varp->vval.v_string != NULL)
21368 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021369 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021370 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021371 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021372 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021373 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021374 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021375 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021376 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021377 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021378 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021381 if (denote == NULL) /* useful for values that must be unsigned */
21382 n = -1;
21383 else
21384 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021385 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386}
21387
21388/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021389 * Get the lnum from the first argument.
21390 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021391 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 */
21393 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021394get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021395 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396{
Bram Moolenaar33570922005-01-25 22:26:29 +000021397 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 linenr_T lnum;
21399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021400 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 if (lnum == 0) /* no valid number, try using line() */
21402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021403 rettv.v_type = VAR_NUMBER;
21404 f_line(argvars, &rettv);
21405 lnum = rettv.vval.v_number;
21406 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 }
21408 return lnum;
21409}
21410
21411/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021412 * Get the lnum from the first argument.
21413 * Also accepts "$", then "buf" is used.
21414 * Returns 0 on error.
21415 */
21416 static linenr_T
21417get_tv_lnum_buf(argvars, buf)
21418 typval_T *argvars;
21419 buf_T *buf;
21420{
21421 if (argvars[0].v_type == VAR_STRING
21422 && argvars[0].vval.v_string != NULL
21423 && argvars[0].vval.v_string[0] == '$'
21424 && buf != NULL)
21425 return buf->b_ml.ml_line_count;
21426 return get_tv_number_chk(&argvars[0], NULL);
21427}
21428
21429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 * Get the string value of a variable.
21431 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021432 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21433 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 * If the String variable has never been set, return an empty string.
21435 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021436 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21437 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 */
21439 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021440get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021441 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442{
21443 static char_u mybuf[NUMBUFLEN];
21444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021445 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446}
21447
21448 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021449get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021450 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021451 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021453 char_u *res = get_tv_string_buf_chk(varp, buf);
21454
21455 return res != NULL ? res : (char_u *)"";
21456}
21457
Bram Moolenaar7d647822014-04-05 21:28:56 +020021458/*
21459 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21460 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021461 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021462get_tv_string_chk(varp)
21463 typval_T *varp;
21464{
21465 static char_u mybuf[NUMBUFLEN];
21466
21467 return get_tv_string_buf_chk(varp, mybuf);
21468}
21469
21470 static char_u *
21471get_tv_string_buf_chk(varp, buf)
21472 typval_T *varp;
21473 char_u *buf;
21474{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021475 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021477 case VAR_NUMBER:
21478 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21479 return buf;
21480 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021481 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021482 break;
21483 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021484 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021485 break;
21486 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021487 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021488 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021489#ifdef FEAT_FLOAT
21490 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021491 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021492 break;
21493#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021494 case VAR_STRING:
21495 if (varp->vval.v_string != NULL)
21496 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021497 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021498 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021499 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021502 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503}
21504
21505/*
21506 * Find variable "name" in the list of variables.
21507 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021508 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021509 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021510 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021512 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021513find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021515 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021516 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021519 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520
Bram Moolenaara7043832005-01-21 11:56:39 +000021521 ht = find_var_ht(name, &varname);
21522 if (htp != NULL)
21523 *htp = ht;
21524 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021526 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527}
21528
21529/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021530 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021531 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021533 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021534find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021536 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021537 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021538 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021539{
Bram Moolenaar33570922005-01-25 22:26:29 +000021540 hashitem_T *hi;
21541
21542 if (*varname == NUL)
21543 {
21544 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021545 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021546 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021547 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021548 case 'g': return &globvars_var;
21549 case 'v': return &vimvars_var;
21550 case 'b': return &curbuf->b_bufvar;
21551 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021552#ifdef FEAT_WINDOWS
21553 case 't': return &curtab->tp_winvar;
21554#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021555 case 'l': return current_funccal == NULL
21556 ? NULL : &current_funccal->l_vars_var;
21557 case 'a': return current_funccal == NULL
21558 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021559 }
21560 return NULL;
21561 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021562
21563 hi = hash_find(ht, varname);
21564 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021565 {
21566 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021567 * worked find the variable again. Don't auto-load a script if it was
21568 * loaded already, otherwise it would be loaded every time when
21569 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021570 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021571 {
21572 /* Note: script_autoload() may make "hi" invalid. It must either
21573 * be obtained again or not used. */
21574 if (!script_autoload(varname, FALSE) || aborting())
21575 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021576 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021577 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021578 if (HASHITEM_EMPTY(hi))
21579 return NULL;
21580 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021581 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021582}
21583
21584/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021585 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021586 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021587 * Set "varname" to the start of name without ':'.
21588 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021589 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021590find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 char_u *name;
21592 char_u **varname;
21593{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021594 hashitem_T *hi;
21595
Bram Moolenaar73627d02015-08-11 15:46:09 +020021596 if (name[0] == NUL)
21597 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 if (name[1] != ':')
21599 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021600 /* The name must not start with a colon or #. */
21601 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 return NULL;
21603 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021604
21605 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021606 hi = hash_find(&compat_hashtab, name);
21607 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021608 return &compat_hashtab;
21609
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021611 return &globvarht; /* global variable */
21612 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 }
21614 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021615 if (*name == 'g') /* global variable */
21616 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021617 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21618 */
21619 if (vim_strchr(name + 2, ':') != NULL
21620 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021621 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021623 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021625 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021626#ifdef FEAT_WINDOWS
21627 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021628 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021630 if (*name == 'v') /* v: variable */
21631 return &vimvarht;
21632 if (*name == 'a' && current_funccal != NULL) /* function argument */
21633 return &current_funccal->l_avars.dv_hashtab;
21634 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21635 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 if (*name == 's' /* script variable */
21637 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21638 return &SCRIPT_VARS(current_SID);
21639 return NULL;
21640}
21641
21642/*
21643 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021644 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 * Returns NULL when it doesn't exist.
21646 */
21647 char_u *
21648get_var_value(name)
21649 char_u *name;
21650{
Bram Moolenaar33570922005-01-25 22:26:29 +000021651 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021653 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 if (v == NULL)
21655 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657}
21658
21659/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 * sourcing this script and when executing functions defined in the script.
21662 */
21663 void
21664new_script_vars(id)
21665 scid_T id;
21666{
Bram Moolenaara7043832005-01-21 11:56:39 +000021667 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021668 hashtab_T *ht;
21669 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021670
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21672 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021673 /* Re-allocating ga_data means that an ht_array pointing to
21674 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021676 for (i = 1; i <= ga_scripts.ga_len; ++i)
21677 {
21678 ht = &SCRIPT_VARS(i);
21679 if (ht->ht_mask == HT_INIT_SIZE - 1)
21680 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021681 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021682 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021683 }
21684
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 while (ga_scripts.ga_len < id)
21686 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021687 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021688 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021689 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 }
21692 }
21693}
21694
21695/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021696 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21697 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 */
21699 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021700init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021701 dict_T *dict;
21702 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021703 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704{
Bram Moolenaar33570922005-01-25 22:26:29 +000021705 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021706 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021707 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021708 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021709 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021710 dict_var->di_tv.vval.v_dict = dict;
21711 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021712 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021713 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21714 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715}
21716
21717/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021718 * Unreference a dictionary initialized by init_var_dict().
21719 */
21720 void
21721unref_var_dict(dict)
21722 dict_T *dict;
21723{
21724 /* Now the dict needs to be freed if no one else is using it, go back to
21725 * normal reference counting. */
21726 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21727 dict_unref(dict);
21728}
21729
21730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021732 * Frees all allocated variables and the value they contain.
21733 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 */
21735 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021736vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021737 hashtab_T *ht;
21738{
21739 vars_clear_ext(ht, TRUE);
21740}
21741
21742/*
21743 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21744 */
21745 static void
21746vars_clear_ext(ht, free_val)
21747 hashtab_T *ht;
21748 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749{
Bram Moolenaara7043832005-01-21 11:56:39 +000021750 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 hashitem_T *hi;
21752 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021755 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021756 for (hi = ht->ht_array; todo > 0; ++hi)
21757 {
21758 if (!HASHITEM_EMPTY(hi))
21759 {
21760 --todo;
21761
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021763 * ht_array might change then. hash_clear() takes care of it
21764 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021765 v = HI2DI(hi);
21766 if (free_val)
21767 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021768 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021770 }
21771 }
21772 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021773 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774}
21775
Bram Moolenaara7043832005-01-21 11:56:39 +000021776/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021777 * Delete a variable from hashtab "ht" at item "hi".
21778 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021781delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021782 hashtab_T *ht;
21783 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784{
Bram Moolenaar33570922005-01-25 22:26:29 +000021785 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021786
21787 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 clear_tv(&di->di_tv);
21789 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790}
21791
21792/*
21793 * List the value of one internal variable.
21794 */
21795 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021796list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021797 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021799 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021801 char_u *tofree;
21802 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021803 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021804
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021805 current_copyID += COPYID_INC;
21806 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021808 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021809 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810}
21811
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021813list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814 char_u *prefix;
21815 char_u *name;
21816 int type;
21817 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021818 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819{
Bram Moolenaar31859182007-08-14 20:41:13 +000021820 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21821 msg_start();
21822 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 if (name != NULL) /* "a:" vars don't have a name stored */
21824 msg_puts(name);
21825 msg_putchar(' ');
21826 msg_advance(22);
21827 if (type == VAR_NUMBER)
21828 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021829 else if (type == VAR_FUNC)
21830 msg_putchar('*');
21831 else if (type == VAR_LIST)
21832 {
21833 msg_putchar('[');
21834 if (*string == '[')
21835 ++string;
21836 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021837 else if (type == VAR_DICT)
21838 {
21839 msg_putchar('{');
21840 if (*string == '{')
21841 ++string;
21842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 else
21844 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021845
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021847
21848 if (type == VAR_FUNC)
21849 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021850 if (*first)
21851 {
21852 msg_clr_eos();
21853 *first = FALSE;
21854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855}
21856
21857/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021858 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 * If the variable already exists, the value is updated.
21860 * Otherwise the variable is created.
21861 */
21862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021863set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021866 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867{
Bram Moolenaar33570922005-01-25 22:26:29 +000021868 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021870 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021872 ht = find_var_ht(name, &varname);
21873 if (ht == NULL || *varname == NUL)
21874 {
21875 EMSG2(_(e_illvar), name);
21876 return;
21877 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021878 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021879
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021880 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21881 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021882
Bram Moolenaar33570922005-01-25 22:26:29 +000021883 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021886 if (var_check_ro(v->di_flags, name, FALSE)
21887 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 return;
21889 if (v->di_tv.v_type != tv->v_type
21890 && !((v->di_tv.v_type == VAR_STRING
21891 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021892 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021893 || tv->v_type == VAR_NUMBER))
21894#ifdef FEAT_FLOAT
21895 && !((v->di_tv.v_type == VAR_NUMBER
21896 || v->di_tv.v_type == VAR_FLOAT)
21897 && (tv->v_type == VAR_NUMBER
21898 || tv->v_type == VAR_FLOAT))
21899#endif
21900 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021901 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021902 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021903 return;
21904 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021905
21906 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021907 * Handle setting internal v: variables separately where needed to
21908 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021909 */
21910 if (ht == &vimvarht)
21911 {
21912 if (v->di_tv.v_type == VAR_STRING)
21913 {
21914 vim_free(v->di_tv.vval.v_string);
21915 if (copy || tv->v_type != VAR_STRING)
21916 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21917 else
21918 {
21919 /* Take over the string to avoid an extra alloc/free. */
21920 v->di_tv.vval.v_string = tv->vval.v_string;
21921 tv->vval.v_string = NULL;
21922 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021923 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021924 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021925 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021926 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021928 if (STRCMP(varname, "searchforward") == 0)
21929 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021930#ifdef FEAT_SEARCH_EXTRA
21931 else if (STRCMP(varname, "hlsearch") == 0)
21932 {
21933 no_hlsearch = !v->di_tv.vval.v_number;
21934 redraw_all_later(SOME_VALID);
21935 }
21936#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021937 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021938 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021939 else if (v->di_tv.v_type != tv->v_type)
21940 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021941 }
21942
21943 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 }
21945 else /* add a new variable */
21946 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021947 /* Can't add "v:" variable. */
21948 if (ht == &vimvarht)
21949 {
21950 EMSG2(_(e_illvar), name);
21951 return;
21952 }
21953
Bram Moolenaar92124a32005-06-17 22:03:40 +000021954 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021955 if (!valid_varname(varname))
21956 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021957
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021958 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21959 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021960 if (v == NULL)
21961 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021962 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021965 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 return;
21967 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021968 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021970
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021971 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021972 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021973 else
21974 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021975 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021976 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021977 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979}
21980
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021981/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021982 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021983 * Also give an error message.
21984 */
21985 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021986var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 int flags;
21988 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021989 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021990{
21991 if (flags & DI_FLAGS_RO)
21992 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021993 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 return TRUE;
21995 }
21996 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21997 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021998 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 return TRUE;
22000 }
22001 return FALSE;
22002}
22003
22004/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022005 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22006 * Also give an error message.
22007 */
22008 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022009var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022010 int flags;
22011 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022012 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022013{
22014 if (flags & DI_FLAGS_FIX)
22015 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022016 EMSG2(_("E795: Cannot delete variable %s"),
22017 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022018 return TRUE;
22019 }
22020 return FALSE;
22021}
22022
22023/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022024 * Check if a funcref is assigned to a valid variable name.
22025 * Return TRUE and give an error if not.
22026 */
22027 static int
22028var_check_func_name(name, new_var)
22029 char_u *name; /* points to start of variable name */
22030 int new_var; /* TRUE when creating the variable */
22031{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022032 /* Allow for w: b: s: and t:. */
22033 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022034 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22035 ? name[2] : name[0]))
22036 {
22037 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22038 name);
22039 return TRUE;
22040 }
22041 /* Don't allow hiding a function. When "v" is not NULL we might be
22042 * assigning another function to the same var, the type is checked
22043 * below. */
22044 if (new_var && function_exists(name))
22045 {
22046 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22047 name);
22048 return TRUE;
22049 }
22050 return FALSE;
22051}
22052
22053/*
22054 * Check if a variable name is valid.
22055 * Return FALSE and give an error if not.
22056 */
22057 static int
22058valid_varname(varname)
22059 char_u *varname;
22060{
22061 char_u *p;
22062
22063 for (p = varname; *p != NUL; ++p)
22064 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22065 && *p != AUTOLOAD_CHAR)
22066 {
22067 EMSG2(_(e_illvar), varname);
22068 return FALSE;
22069 }
22070 return TRUE;
22071}
22072
22073/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022074 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022075 * Also give an error message, using "name" or _("name") when use_gettext is
22076 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022077 */
22078 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022079tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022080 int lock;
22081 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022082 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022083{
22084 if (lock & VAR_LOCKED)
22085 {
22086 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022087 name == NULL ? (char_u *)_("Unknown")
22088 : use_gettext ? (char_u *)_(name)
22089 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022090 return TRUE;
22091 }
22092 if (lock & VAR_FIXED)
22093 {
22094 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022095 name == NULL ? (char_u *)_("Unknown")
22096 : use_gettext ? (char_u *)_(name)
22097 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022098 return TRUE;
22099 }
22100 return FALSE;
22101}
22102
22103/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022104 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022105 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022106 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022107 * It is OK for "from" and "to" to point to the same item. This is used to
22108 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022109 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022110 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022111copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022112 typval_T *from;
22113 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022115 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022116 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022117 switch (from->v_type)
22118 {
22119 case VAR_NUMBER:
22120 to->vval.v_number = from->vval.v_number;
22121 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022122#ifdef FEAT_FLOAT
22123 case VAR_FLOAT:
22124 to->vval.v_float = from->vval.v_float;
22125 break;
22126#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022127 case VAR_STRING:
22128 case VAR_FUNC:
22129 if (from->vval.v_string == NULL)
22130 to->vval.v_string = NULL;
22131 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022132 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022133 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022134 if (from->v_type == VAR_FUNC)
22135 func_ref(to->vval.v_string);
22136 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022137 break;
22138 case VAR_LIST:
22139 if (from->vval.v_list == NULL)
22140 to->vval.v_list = NULL;
22141 else
22142 {
22143 to->vval.v_list = from->vval.v_list;
22144 ++to->vval.v_list->lv_refcount;
22145 }
22146 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022147 case VAR_DICT:
22148 if (from->vval.v_dict == NULL)
22149 to->vval.v_dict = NULL;
22150 else
22151 {
22152 to->vval.v_dict = from->vval.v_dict;
22153 ++to->vval.v_dict->dv_refcount;
22154 }
22155 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022156 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022157 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022158 break;
22159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160}
22161
22162/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022163 * Make a copy of an item.
22164 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022165 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22166 * reference to an already copied list/dict can be used.
22167 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022168 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022169 static int
22170item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022171 typval_T *from;
22172 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022173 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022174 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022175{
22176 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022177 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022178
Bram Moolenaar33570922005-01-25 22:26:29 +000022179 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022180 {
22181 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022182 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022183 }
22184 ++recurse;
22185
22186 switch (from->v_type)
22187 {
22188 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022189#ifdef FEAT_FLOAT
22190 case VAR_FLOAT:
22191#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022192 case VAR_STRING:
22193 case VAR_FUNC:
22194 copy_tv(from, to);
22195 break;
22196 case VAR_LIST:
22197 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022198 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022199 if (from->vval.v_list == NULL)
22200 to->vval.v_list = NULL;
22201 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22202 {
22203 /* use the copy made earlier */
22204 to->vval.v_list = from->vval.v_list->lv_copylist;
22205 ++to->vval.v_list->lv_refcount;
22206 }
22207 else
22208 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22209 if (to->vval.v_list == NULL)
22210 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022211 break;
22212 case VAR_DICT:
22213 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022214 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022215 if (from->vval.v_dict == NULL)
22216 to->vval.v_dict = NULL;
22217 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22218 {
22219 /* use the copy made earlier */
22220 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22221 ++to->vval.v_dict->dv_refcount;
22222 }
22223 else
22224 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22225 if (to->vval.v_dict == NULL)
22226 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022227 break;
22228 default:
22229 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022230 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022231 }
22232 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022233 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022234}
22235
22236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 * ":echo expr1 ..." print each argument separated with a space, add a
22238 * newline at the end.
22239 * ":echon expr1 ..." print each argument plain.
22240 */
22241 void
22242ex_echo(eap)
22243 exarg_T *eap;
22244{
22245 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022246 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022247 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 char_u *p;
22249 int needclr = TRUE;
22250 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022251 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252
22253 if (eap->skip)
22254 ++emsg_skip;
22255 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22256 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022257 /* If eval1() causes an error message the text from the command may
22258 * still need to be cleared. E.g., "echo 22,44". */
22259 need_clr_eos = needclr;
22260
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022262 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 {
22264 /*
22265 * Report the invalid expression unless the expression evaluation
22266 * has been cancelled due to an aborting error, an interrupt, or an
22267 * exception.
22268 */
22269 if (!aborting())
22270 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022271 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 break;
22273 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022274 need_clr_eos = FALSE;
22275
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 if (!eap->skip)
22277 {
22278 if (atstart)
22279 {
22280 atstart = FALSE;
22281 /* Call msg_start() after eval1(), evaluating the expression
22282 * may cause a message to appear. */
22283 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022284 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022285 /* Mark the saved text as finishing the line, so that what
22286 * follows is displayed on a new line when scrolling back
22287 * at the more prompt. */
22288 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 }
22292 else if (eap->cmdidx == CMD_echo)
22293 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022294 current_copyID += COPYID_INC;
22295 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022296 if (p != NULL)
22297 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022299 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022301 if (*p != TAB && needclr)
22302 {
22303 /* remove any text still there from the command */
22304 msg_clr_eos();
22305 needclr = FALSE;
22306 }
22307 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 }
22309 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022310 {
22311#ifdef FEAT_MBYTE
22312 if (has_mbyte)
22313 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022314 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022315
22316 (void)msg_outtrans_len_attr(p, i, echo_attr);
22317 p += i - 1;
22318 }
22319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022321 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022324 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022326 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 arg = skipwhite(arg);
22328 }
22329 eap->nextcmd = check_nextcmd(arg);
22330
22331 if (eap->skip)
22332 --emsg_skip;
22333 else
22334 {
22335 /* remove text that may still be there from the command */
22336 if (needclr)
22337 msg_clr_eos();
22338 if (eap->cmdidx == CMD_echo)
22339 msg_end();
22340 }
22341}
22342
22343/*
22344 * ":echohl {name}".
22345 */
22346 void
22347ex_echohl(eap)
22348 exarg_T *eap;
22349{
22350 int id;
22351
22352 id = syn_name2id(eap->arg);
22353 if (id == 0)
22354 echo_attr = 0;
22355 else
22356 echo_attr = syn_id2attr(id);
22357}
22358
22359/*
22360 * ":execute expr1 ..." execute the result of an expression.
22361 * ":echomsg expr1 ..." Print a message
22362 * ":echoerr expr1 ..." Print an error
22363 * Each gets spaces around each argument and a newline at the end for
22364 * echo commands
22365 */
22366 void
22367ex_execute(eap)
22368 exarg_T *eap;
22369{
22370 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022371 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 int ret = OK;
22373 char_u *p;
22374 garray_T ga;
22375 int len;
22376 int save_did_emsg;
22377
22378 ga_init2(&ga, 1, 80);
22379
22380 if (eap->skip)
22381 ++emsg_skip;
22382 while (*arg != NUL && *arg != '|' && *arg != '\n')
22383 {
22384 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022385 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 {
22387 /*
22388 * Report the invalid expression unless the expression evaluation
22389 * has been cancelled due to an aborting error, an interrupt, or an
22390 * exception.
22391 */
22392 if (!aborting())
22393 EMSG2(_(e_invexpr2), p);
22394 ret = FAIL;
22395 break;
22396 }
22397
22398 if (!eap->skip)
22399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022400 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 len = (int)STRLEN(p);
22402 if (ga_grow(&ga, len + 2) == FAIL)
22403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022404 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 ret = FAIL;
22406 break;
22407 }
22408 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 ga.ga_len += len;
22412 }
22413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022414 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 arg = skipwhite(arg);
22416 }
22417
22418 if (ret != FAIL && ga.ga_data != NULL)
22419 {
22420 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022423 out_flush();
22424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 else if (eap->cmdidx == CMD_echoerr)
22426 {
22427 /* We don't want to abort following commands, restore did_emsg. */
22428 save_did_emsg = did_emsg;
22429 EMSG((char_u *)ga.ga_data);
22430 if (!force_abort)
22431 did_emsg = save_did_emsg;
22432 }
22433 else if (eap->cmdidx == CMD_execute)
22434 do_cmdline((char_u *)ga.ga_data,
22435 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22436 }
22437
22438 ga_clear(&ga);
22439
22440 if (eap->skip)
22441 --emsg_skip;
22442
22443 eap->nextcmd = check_nextcmd(arg);
22444}
22445
22446/*
22447 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22448 * "arg" points to the "&" or '+' when called, to "option" when returning.
22449 * Returns NULL when no option name found. Otherwise pointer to the char
22450 * after the option name.
22451 */
22452 static char_u *
22453find_option_end(arg, opt_flags)
22454 char_u **arg;
22455 int *opt_flags;
22456{
22457 char_u *p = *arg;
22458
22459 ++p;
22460 if (*p == 'g' && p[1] == ':')
22461 {
22462 *opt_flags = OPT_GLOBAL;
22463 p += 2;
22464 }
22465 else if (*p == 'l' && p[1] == ':')
22466 {
22467 *opt_flags = OPT_LOCAL;
22468 p += 2;
22469 }
22470 else
22471 *opt_flags = 0;
22472
22473 if (!ASCII_ISALPHA(*p))
22474 return NULL;
22475 *arg = p;
22476
22477 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22478 p += 4; /* termcap option */
22479 else
22480 while (ASCII_ISALPHA(*p))
22481 ++p;
22482 return p;
22483}
22484
22485/*
22486 * ":function"
22487 */
22488 void
22489ex_function(eap)
22490 exarg_T *eap;
22491{
22492 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022493 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 int j;
22495 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022497 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 char_u *name = NULL;
22499 char_u *p;
22500 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022501 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 garray_T newargs;
22503 garray_T newlines;
22504 int varargs = FALSE;
22505 int mustend = FALSE;
22506 int flags = 0;
22507 ufunc_T *fp;
22508 int indent;
22509 int nesting;
22510 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022511 dictitem_T *v;
22512 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022513 static int func_nr = 0; /* number for nameless function */
22514 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022515 hashtab_T *ht;
22516 int todo;
22517 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022518 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519
22520 /*
22521 * ":function" without argument: list functions.
22522 */
22523 if (ends_excmd(*eap->arg))
22524 {
22525 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022526 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022527 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022528 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022529 {
22530 if (!HASHITEM_EMPTY(hi))
22531 {
22532 --todo;
22533 fp = HI2UF(hi);
22534 if (!isdigit(*fp->uf_name))
22535 list_func_head(fp, FALSE);
22536 }
22537 }
22538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 eap->nextcmd = check_nextcmd(eap->arg);
22540 return;
22541 }
22542
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022543 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022544 * ":function /pat": list functions matching pattern.
22545 */
22546 if (*eap->arg == '/')
22547 {
22548 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22549 if (!eap->skip)
22550 {
22551 regmatch_T regmatch;
22552
22553 c = *p;
22554 *p = NUL;
22555 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22556 *p = c;
22557 if (regmatch.regprog != NULL)
22558 {
22559 regmatch.rm_ic = p_ic;
22560
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022561 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022562 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22563 {
22564 if (!HASHITEM_EMPTY(hi))
22565 {
22566 --todo;
22567 fp = HI2UF(hi);
22568 if (!isdigit(*fp->uf_name)
22569 && vim_regexec(&regmatch, fp->uf_name, 0))
22570 list_func_head(fp, FALSE);
22571 }
22572 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022573 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022574 }
22575 }
22576 if (*p == '/')
22577 ++p;
22578 eap->nextcmd = check_nextcmd(p);
22579 return;
22580 }
22581
22582 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022583 * Get the function name. There are these situations:
22584 * func normal function name
22585 * "name" == func, "fudi.fd_dict" == NULL
22586 * dict.func new dictionary entry
22587 * "name" == NULL, "fudi.fd_dict" set,
22588 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22589 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022590 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022591 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22592 * dict.func existing dict entry that's not a Funcref
22593 * "name" == NULL, "fudi.fd_dict" set,
22594 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022595 * s:func script-local function name
22596 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022599 name = trans_function_name(&p, eap->skip, 0, &fudi);
22600 paren = (vim_strchr(p, '(') != NULL);
22601 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 {
22603 /*
22604 * Return on an invalid expression in braces, unless the expression
22605 * evaluation has been cancelled due to an aborting error, an
22606 * interrupt, or an exception.
22607 */
22608 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022610 if (!eap->skip && fudi.fd_newkey != NULL)
22611 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022612 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 else
22616 eap->skip = TRUE;
22617 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022618
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 /* An error in a function call during evaluation of an expression in magic
22620 * braces should not cause the function not to be defined. */
22621 saved_did_emsg = did_emsg;
22622 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623
22624 /*
22625 * ":function func" with only function name: list function.
22626 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022627 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 {
22629 if (!ends_excmd(*skipwhite(p)))
22630 {
22631 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022632 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 }
22634 eap->nextcmd = check_nextcmd(p);
22635 if (eap->nextcmd != NULL)
22636 *p = NUL;
22637 if (!eap->skip && !got_int)
22638 {
22639 fp = find_func(name);
22640 if (fp != NULL)
22641 {
22642 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022643 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022645 if (FUNCLINE(fp, j) == NULL)
22646 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 msg_putchar('\n');
22648 msg_outnum((long)(j + 1));
22649 if (j < 9)
22650 msg_putchar(' ');
22651 if (j < 99)
22652 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022653 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 out_flush(); /* show a line at a time */
22655 ui_breakcheck();
22656 }
22657 if (!got_int)
22658 {
22659 msg_putchar('\n');
22660 msg_puts((char_u *)" endfunction");
22661 }
22662 }
22663 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022664 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022666 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 }
22668
22669 /*
22670 * ":function name(arg1, arg2)" Define function.
22671 */
22672 p = skipwhite(p);
22673 if (*p != '(')
22674 {
22675 if (!eap->skip)
22676 {
22677 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022678 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 }
22680 /* attempt to continue by skipping some text */
22681 if (vim_strchr(p, '(') != NULL)
22682 p = vim_strchr(p, '(');
22683 }
22684 p = skipwhite(p + 1);
22685
22686 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22687 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22688
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022689 if (!eap->skip)
22690 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022691 /* Check the name of the function. Unless it's a dictionary function
22692 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022693 if (name != NULL)
22694 arg = name;
22695 else
22696 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022697 if (arg != NULL && (fudi.fd_di == NULL
22698 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022699 {
22700 if (*arg == K_SPECIAL)
22701 j = 3;
22702 else
22703 j = 0;
22704 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22705 : eval_isnamec(arg[j])))
22706 ++j;
22707 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022708 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022709 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022710 /* Disallow using the g: dict. */
22711 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22712 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022713 }
22714
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 /*
22716 * Isolate the arguments: "arg1, arg2, ...)"
22717 */
22718 while (*p != ')')
22719 {
22720 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22721 {
22722 varargs = TRUE;
22723 p += 3;
22724 mustend = TRUE;
22725 }
22726 else
22727 {
22728 arg = p;
22729 while (ASCII_ISALNUM(*p) || *p == '_')
22730 ++p;
22731 if (arg == p || isdigit(*arg)
22732 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22733 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22734 {
22735 if (!eap->skip)
22736 EMSG2(_("E125: Illegal argument: %s"), arg);
22737 break;
22738 }
22739 if (ga_grow(&newargs, 1) == FAIL)
22740 goto erret;
22741 c = *p;
22742 *p = NUL;
22743 arg = vim_strsave(arg);
22744 if (arg == NULL)
22745 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022746
22747 /* Check for duplicate argument name. */
22748 for (i = 0; i < newargs.ga_len; ++i)
22749 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22750 {
22751 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022752 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022753 goto erret;
22754 }
22755
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22757 *p = c;
22758 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 if (*p == ',')
22760 ++p;
22761 else
22762 mustend = TRUE;
22763 }
22764 p = skipwhite(p);
22765 if (mustend && *p != ')')
22766 {
22767 if (!eap->skip)
22768 EMSG2(_(e_invarg2), eap->arg);
22769 break;
22770 }
22771 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022772 if (*p != ')')
22773 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 ++p; /* skip the ')' */
22775
Bram Moolenaare9a41262005-01-15 22:18:47 +000022776 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 for (;;)
22778 {
22779 p = skipwhite(p);
22780 if (STRNCMP(p, "range", 5) == 0)
22781 {
22782 flags |= FC_RANGE;
22783 p += 5;
22784 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022785 else if (STRNCMP(p, "dict", 4) == 0)
22786 {
22787 flags |= FC_DICT;
22788 p += 4;
22789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 else if (STRNCMP(p, "abort", 5) == 0)
22791 {
22792 flags |= FC_ABORT;
22793 p += 5;
22794 }
22795 else
22796 break;
22797 }
22798
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022799 /* When there is a line break use what follows for the function body.
22800 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22801 if (*p == '\n')
22802 line_arg = p + 1;
22803 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 EMSG(_(e_trailing));
22805
22806 /*
22807 * Read the body of the function, until ":endfunction" is found.
22808 */
22809 if (KeyTyped)
22810 {
22811 /* Check if the function already exists, don't let the user type the
22812 * whole function before telling him it doesn't work! For a script we
22813 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022814 if (!eap->skip && !eap->forceit)
22815 {
22816 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22817 EMSG(_(e_funcdict));
22818 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022819 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022822 if (!eap->skip && did_emsg)
22823 goto erret;
22824
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 msg_putchar('\n'); /* don't overwrite the function name */
22826 cmdline_row = msg_row;
22827 }
22828
22829 indent = 2;
22830 nesting = 0;
22831 for (;;)
22832 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022833 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022834 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022835 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022836 saved_wait_return = FALSE;
22837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022839 sourcing_lnum_off = sourcing_lnum;
22840
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022841 if (line_arg != NULL)
22842 {
22843 /* Use eap->arg, split up in parts by line breaks. */
22844 theline = line_arg;
22845 p = vim_strchr(theline, '\n');
22846 if (p == NULL)
22847 line_arg += STRLEN(line_arg);
22848 else
22849 {
22850 *p = NUL;
22851 line_arg = p + 1;
22852 }
22853 }
22854 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 theline = getcmdline(':', 0L, indent);
22856 else
22857 theline = eap->getline(':', eap->cookie, indent);
22858 if (KeyTyped)
22859 lines_left = Rows - 1;
22860 if (theline == NULL)
22861 {
22862 EMSG(_("E126: Missing :endfunction"));
22863 goto erret;
22864 }
22865
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022866 /* Detect line continuation: sourcing_lnum increased more than one. */
22867 if (sourcing_lnum > sourcing_lnum_off + 1)
22868 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22869 else
22870 sourcing_lnum_off = 0;
22871
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 if (skip_until != NULL)
22873 {
22874 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22875 * don't check for ":endfunc". */
22876 if (STRCMP(theline, skip_until) == 0)
22877 {
22878 vim_free(skip_until);
22879 skip_until = NULL;
22880 }
22881 }
22882 else
22883 {
22884 /* skip ':' and blanks*/
22885 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22886 ;
22887
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022888 /* Check for "endfunction". */
22889 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022891 if (line_arg == NULL)
22892 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 break;
22894 }
22895
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022896 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 * at "end". */
22898 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22899 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022900 else if (STRNCMP(p, "if", 2) == 0
22901 || STRNCMP(p, "wh", 2) == 0
22902 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 || STRNCMP(p, "try", 3) == 0)
22904 indent += 2;
22905
22906 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022907 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022909 if (*p == '!')
22910 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022912 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22913 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022915 ++nesting;
22916 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 }
22918 }
22919
22920 /* Check for ":append" or ":insert". */
22921 p = skip_range(p, NULL);
22922 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22923 || (p[0] == 'i'
22924 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22925 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22926 skip_until = vim_strsave((char_u *)".");
22927
22928 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22929 arg = skipwhite(skiptowhite(p));
22930 if (arg[0] == '<' && arg[1] =='<'
22931 && ((p[0] == 'p' && p[1] == 'y'
22932 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22933 || (p[0] == 'p' && p[1] == 'e'
22934 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22935 || (p[0] == 't' && p[1] == 'c'
22936 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022937 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22938 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22940 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022941 || (p[0] == 'm' && p[1] == 'z'
22942 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 ))
22944 {
22945 /* ":python <<" continues until a dot, like ":append" */
22946 p = skipwhite(arg + 2);
22947 if (*p == NUL)
22948 skip_until = vim_strsave((char_u *)".");
22949 else
22950 skip_until = vim_strsave(p);
22951 }
22952 }
22953
22954 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022955 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022956 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022957 if (line_arg == NULL)
22958 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022960 }
22961
22962 /* Copy the line to newly allocated memory. get_one_sourceline()
22963 * allocates 250 bytes per line, this saves 80% on average. The cost
22964 * is an extra alloc/free. */
22965 p = vim_strsave(theline);
22966 if (p != NULL)
22967 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022968 if (line_arg == NULL)
22969 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022970 theline = p;
22971 }
22972
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022973 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22974
22975 /* Add NULL lines for continuation lines, so that the line count is
22976 * equal to the index in the growarray. */
22977 while (sourcing_lnum_off-- > 0)
22978 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022979
22980 /* Check for end of eap->arg. */
22981 if (line_arg != NULL && *line_arg == NUL)
22982 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 }
22984
22985 /* Don't define the function when skipping commands or when an error was
22986 * detected. */
22987 if (eap->skip || did_emsg)
22988 goto erret;
22989
22990 /*
22991 * If there are no errors, add the function
22992 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022993 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022994 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022995 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022996 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022997 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022998 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022999 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023000 goto erret;
23001 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023002
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023003 fp = find_func(name);
23004 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023006 if (!eap->forceit)
23007 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023008 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023009 goto erret;
23010 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023011 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023012 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023013 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023014 name);
23015 goto erret;
23016 }
23017 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023018 ga_clear_strings(&(fp->uf_args));
23019 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023020 vim_free(name);
23021 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 }
23024 else
23025 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023026 char numbuf[20];
23027
23028 fp = NULL;
23029 if (fudi.fd_newkey == NULL && !eap->forceit)
23030 {
23031 EMSG(_(e_funcdict));
23032 goto erret;
23033 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023034 if (fudi.fd_di == NULL)
23035 {
23036 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023037 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023038 goto erret;
23039 }
23040 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023041 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023042 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023043
23044 /* Give the function a sequential number. Can only be used with a
23045 * Funcref! */
23046 vim_free(name);
23047 sprintf(numbuf, "%d", ++func_nr);
23048 name = vim_strsave((char_u *)numbuf);
23049 if (name == NULL)
23050 goto erret;
23051 }
23052
23053 if (fp == NULL)
23054 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023055 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023056 {
23057 int slen, plen;
23058 char_u *scriptname;
23059
23060 /* Check that the autoload name matches the script name. */
23061 j = FAIL;
23062 if (sourcing_name != NULL)
23063 {
23064 scriptname = autoload_name(name);
23065 if (scriptname != NULL)
23066 {
23067 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023068 plen = (int)STRLEN(p);
23069 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023070 if (slen > plen && fnamecmp(p,
23071 sourcing_name + slen - plen) == 0)
23072 j = OK;
23073 vim_free(scriptname);
23074 }
23075 }
23076 if (j == FAIL)
23077 {
23078 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23079 goto erret;
23080 }
23081 }
23082
23083 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 if (fp == NULL)
23085 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023086
23087 if (fudi.fd_dict != NULL)
23088 {
23089 if (fudi.fd_di == NULL)
23090 {
23091 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023092 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023093 if (fudi.fd_di == NULL)
23094 {
23095 vim_free(fp);
23096 goto erret;
23097 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023098 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23099 {
23100 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023101 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023102 goto erret;
23103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023104 }
23105 else
23106 /* overwrite existing dict entry */
23107 clear_tv(&fudi.fd_di->di_tv);
23108 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023109 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023110 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023111 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023112
23113 /* behave like "dict" was used */
23114 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023115 }
23116
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023118 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023119 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23120 {
23121 vim_free(fp);
23122 goto erret;
23123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023125 fp->uf_args = newargs;
23126 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023127#ifdef FEAT_PROFILE
23128 fp->uf_tml_count = NULL;
23129 fp->uf_tml_total = NULL;
23130 fp->uf_tml_self = NULL;
23131 fp->uf_profiling = FALSE;
23132 if (prof_def_func())
23133 func_do_profile(fp);
23134#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023135 fp->uf_varargs = varargs;
23136 fp->uf_flags = flags;
23137 fp->uf_calls = 0;
23138 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023139 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140
23141erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 ga_clear_strings(&newargs);
23143 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023144ret_free:
23145 vim_free(skip_until);
23146 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023149 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150}
23151
23152/*
23153 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023154 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023156 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023157 * TFN_INT: internal function name OK
23158 * TFN_QUIET: be quiet
23159 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160 * Advances "pp" to just after the function name (if no error).
23161 */
23162 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023163trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 char_u **pp;
23165 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023166 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023167 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023169 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 char_u *start;
23171 char_u *end;
23172 int lead;
23173 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023175 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023176
23177 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023178 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023180
23181 /* Check for hard coded <SNR>: already translated function ID (from a user
23182 * command). */
23183 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23184 && (*pp)[2] == (int)KE_SNR)
23185 {
23186 *pp += 3;
23187 len = get_id_len(pp) + 3;
23188 return vim_strnsave(start, len);
23189 }
23190
23191 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23192 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023194 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023196
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023197 /* Note that TFN_ flags use the same values as GLV_ flags. */
23198 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023199 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023200 if (end == start)
23201 {
23202 if (!skip)
23203 EMSG(_("E129: Function name required"));
23204 goto theend;
23205 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023206 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023207 {
23208 /*
23209 * Report an invalid expression in braces, unless the expression
23210 * evaluation has been cancelled due to an aborting error, an
23211 * interrupt, or an exception.
23212 */
23213 if (!aborting())
23214 {
23215 if (end != NULL)
23216 EMSG2(_(e_invarg2), start);
23217 }
23218 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023219 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023220 goto theend;
23221 }
23222
23223 if (lv.ll_tv != NULL)
23224 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023225 if (fdp != NULL)
23226 {
23227 fdp->fd_dict = lv.ll_dict;
23228 fdp->fd_newkey = lv.ll_newkey;
23229 lv.ll_newkey = NULL;
23230 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023231 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023232 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23233 {
23234 name = vim_strsave(lv.ll_tv->vval.v_string);
23235 *pp = end;
23236 }
23237 else
23238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023239 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23240 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023241 EMSG(_(e_funcref));
23242 else
23243 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023244 name = NULL;
23245 }
23246 goto theend;
23247 }
23248
23249 if (lv.ll_name == NULL)
23250 {
23251 /* Error found, but continue after the function name. */
23252 *pp = end;
23253 goto theend;
23254 }
23255
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023256 /* Check if the name is a Funcref. If so, use the value. */
23257 if (lv.ll_exp_name != NULL)
23258 {
23259 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023260 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023261 if (name == lv.ll_exp_name)
23262 name = NULL;
23263 }
23264 else
23265 {
23266 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023267 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023268 if (name == *pp)
23269 name = NULL;
23270 }
23271 if (name != NULL)
23272 {
23273 name = vim_strsave(name);
23274 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023275 if (STRNCMP(name, "<SNR>", 5) == 0)
23276 {
23277 /* Change "<SNR>" to the byte sequence. */
23278 name[0] = K_SPECIAL;
23279 name[1] = KS_EXTRA;
23280 name[2] = (int)KE_SNR;
23281 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23282 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023283 goto theend;
23284 }
23285
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023286 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023287 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023288 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023289 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23290 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23291 {
23292 /* When there was "s:" already or the name expanded to get a
23293 * leading "s:" then remove it. */
23294 lv.ll_name += 2;
23295 len -= 2;
23296 lead = 2;
23297 }
23298 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023299 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023300 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023301 /* skip over "s:" and "g:" */
23302 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023303 lv.ll_name += 2;
23304 len = (int)(end - lv.ll_name);
23305 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023306
23307 /*
23308 * Copy the function name to allocated memory.
23309 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23310 * Accept <SNR>123_name() outside a script.
23311 */
23312 if (skip)
23313 lead = 0; /* do nothing */
23314 else if (lead > 0)
23315 {
23316 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023317 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23318 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023319 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023320 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023321 if (current_SID <= 0)
23322 {
23323 EMSG(_(e_usingsid));
23324 goto theend;
23325 }
23326 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23327 lead += (int)STRLEN(sid_buf);
23328 }
23329 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023330 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023331 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023332 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023333 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023334 goto theend;
23335 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023336 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023337 {
23338 char_u *cp = vim_strchr(lv.ll_name, ':');
23339
23340 if (cp != NULL && cp < end)
23341 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023342 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023343 goto theend;
23344 }
23345 }
23346
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023347 name = alloc((unsigned)(len + lead + 1));
23348 if (name != NULL)
23349 {
23350 if (lead > 0)
23351 {
23352 name[0] = K_SPECIAL;
23353 name[1] = KS_EXTRA;
23354 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023355 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023356 STRCPY(name + 3, sid_buf);
23357 }
23358 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023359 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023360 }
23361 *pp = end;
23362
23363theend:
23364 clear_lval(&lv);
23365 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366}
23367
23368/*
23369 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23370 * Return 2 if "p" starts with "s:".
23371 * Return 0 otherwise.
23372 */
23373 static int
23374eval_fname_script(p)
23375 char_u *p;
23376{
23377 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23378 || STRNICMP(p + 1, "SNR>", 4) == 0))
23379 return 5;
23380 if (p[0] == 's' && p[1] == ':')
23381 return 2;
23382 return 0;
23383}
23384
23385/*
23386 * Return TRUE if "p" starts with "<SID>" or "s:".
23387 * Only works if eval_fname_script() returned non-zero for "p"!
23388 */
23389 static int
23390eval_fname_sid(p)
23391 char_u *p;
23392{
23393 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23394}
23395
23396/*
23397 * List the head of the function: "name(arg1, arg2)".
23398 */
23399 static void
23400list_func_head(fp, indent)
23401 ufunc_T *fp;
23402 int indent;
23403{
23404 int j;
23405
23406 msg_start();
23407 if (indent)
23408 MSG_PUTS(" ");
23409 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023410 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 {
23412 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023413 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 }
23415 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023416 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023418 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 {
23420 if (j)
23421 MSG_PUTS(", ");
23422 msg_puts(FUNCARG(fp, j));
23423 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023424 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 {
23426 if (j)
23427 MSG_PUTS(", ");
23428 MSG_PUTS("...");
23429 }
23430 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023431 if (fp->uf_flags & FC_ABORT)
23432 MSG_PUTS(" abort");
23433 if (fp->uf_flags & FC_RANGE)
23434 MSG_PUTS(" range");
23435 if (fp->uf_flags & FC_DICT)
23436 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023437 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023438 if (p_verbose > 0)
23439 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440}
23441
23442/*
23443 * Find a function by name, return pointer to it in ufuncs.
23444 * Return NULL for unknown function.
23445 */
23446 static ufunc_T *
23447find_func(name)
23448 char_u *name;
23449{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023450 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023452 hi = hash_find(&func_hashtab, name);
23453 if (!HASHITEM_EMPTY(hi))
23454 return HI2UF(hi);
23455 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456}
23457
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023458#if defined(EXITFREE) || defined(PROTO)
23459 void
23460free_all_functions()
23461{
23462 hashitem_T *hi;
23463
23464 /* Need to start all over every time, because func_free() may change the
23465 * hash table. */
23466 while (func_hashtab.ht_used > 0)
23467 for (hi = func_hashtab.ht_array; ; ++hi)
23468 if (!HASHITEM_EMPTY(hi))
23469 {
23470 func_free(HI2UF(hi));
23471 break;
23472 }
23473}
23474#endif
23475
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023476 int
23477translated_function_exists(name)
23478 char_u *name;
23479{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023480 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023481 return find_internal_func(name) >= 0;
23482 return find_func(name) != NULL;
23483}
23484
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023485/*
23486 * Return TRUE if a function "name" exists.
23487 */
23488 static int
23489function_exists(name)
23490 char_u *name;
23491{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023492 char_u *nm = name;
23493 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023494 int n = FALSE;
23495
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023496 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23497 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023498 nm = skipwhite(nm);
23499
23500 /* Only accept "funcname", "funcname ", "funcname (..." and
23501 * "funcname(...", not "funcname!...". */
23502 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023503 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023504 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023505 return n;
23506}
23507
Bram Moolenaara1544c02013-05-30 12:35:52 +020023508 char_u *
23509get_expanded_name(name, check)
23510 char_u *name;
23511 int check;
23512{
23513 char_u *nm = name;
23514 char_u *p;
23515
23516 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23517
23518 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023519 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023520 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023521
Bram Moolenaara1544c02013-05-30 12:35:52 +020023522 vim_free(p);
23523 return NULL;
23524}
23525
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023526/*
23527 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023528 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23529 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023530 */
23531 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023532builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023533 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023534 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023535{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023536 char_u *p;
23537
23538 if (!ASCII_ISLOWER(name[0]))
23539 return FALSE;
23540 p = vim_strchr(name, AUTOLOAD_CHAR);
23541 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023542}
23543
Bram Moolenaar05159a02005-02-26 23:04:13 +000023544#if defined(FEAT_PROFILE) || defined(PROTO)
23545/*
23546 * Start profiling function "fp".
23547 */
23548 static void
23549func_do_profile(fp)
23550 ufunc_T *fp;
23551{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023552 int len = fp->uf_lines.ga_len;
23553
23554 if (len == 0)
23555 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023556 fp->uf_tm_count = 0;
23557 profile_zero(&fp->uf_tm_self);
23558 profile_zero(&fp->uf_tm_total);
23559 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023560 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023561 if (fp->uf_tml_total == NULL)
23562 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023563 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023564 if (fp->uf_tml_self == NULL)
23565 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023566 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023567 fp->uf_tml_idx = -1;
23568 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23569 || fp->uf_tml_self == NULL)
23570 return; /* out of memory */
23571
23572 fp->uf_profiling = TRUE;
23573}
23574
23575/*
23576 * Dump the profiling results for all functions in file "fd".
23577 */
23578 void
23579func_dump_profile(fd)
23580 FILE *fd;
23581{
23582 hashitem_T *hi;
23583 int todo;
23584 ufunc_T *fp;
23585 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023586 ufunc_T **sorttab;
23587 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023588
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023589 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023590 if (todo == 0)
23591 return; /* nothing to dump */
23592
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023593 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023594
Bram Moolenaar05159a02005-02-26 23:04:13 +000023595 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23596 {
23597 if (!HASHITEM_EMPTY(hi))
23598 {
23599 --todo;
23600 fp = HI2UF(hi);
23601 if (fp->uf_profiling)
23602 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023603 if (sorttab != NULL)
23604 sorttab[st_len++] = fp;
23605
Bram Moolenaar05159a02005-02-26 23:04:13 +000023606 if (fp->uf_name[0] == K_SPECIAL)
23607 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23608 else
23609 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23610 if (fp->uf_tm_count == 1)
23611 fprintf(fd, "Called 1 time\n");
23612 else
23613 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23614 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23615 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23616 fprintf(fd, "\n");
23617 fprintf(fd, "count total (s) self (s)\n");
23618
23619 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23620 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023621 if (FUNCLINE(fp, i) == NULL)
23622 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023623 prof_func_line(fd, fp->uf_tml_count[i],
23624 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023625 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23626 }
23627 fprintf(fd, "\n");
23628 }
23629 }
23630 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023631
23632 if (sorttab != NULL && st_len > 0)
23633 {
23634 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23635 prof_total_cmp);
23636 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23637 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23638 prof_self_cmp);
23639 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23640 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023641
23642 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023643}
Bram Moolenaar73830342005-02-28 22:48:19 +000023644
23645 static void
23646prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23647 FILE *fd;
23648 ufunc_T **sorttab;
23649 int st_len;
23650 char *title;
23651 int prefer_self; /* when equal print only self time */
23652{
23653 int i;
23654 ufunc_T *fp;
23655
23656 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23657 fprintf(fd, "count total (s) self (s) function\n");
23658 for (i = 0; i < 20 && i < st_len; ++i)
23659 {
23660 fp = sorttab[i];
23661 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23662 prefer_self);
23663 if (fp->uf_name[0] == K_SPECIAL)
23664 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23665 else
23666 fprintf(fd, " %s()\n", fp->uf_name);
23667 }
23668 fprintf(fd, "\n");
23669}
23670
23671/*
23672 * Print the count and times for one function or function line.
23673 */
23674 static void
23675prof_func_line(fd, count, total, self, prefer_self)
23676 FILE *fd;
23677 int count;
23678 proftime_T *total;
23679 proftime_T *self;
23680 int prefer_self; /* when equal print only self time */
23681{
23682 if (count > 0)
23683 {
23684 fprintf(fd, "%5d ", count);
23685 if (prefer_self && profile_equal(total, self))
23686 fprintf(fd, " ");
23687 else
23688 fprintf(fd, "%s ", profile_msg(total));
23689 if (!prefer_self && profile_equal(total, self))
23690 fprintf(fd, " ");
23691 else
23692 fprintf(fd, "%s ", profile_msg(self));
23693 }
23694 else
23695 fprintf(fd, " ");
23696}
23697
23698/*
23699 * Compare function for total time sorting.
23700 */
23701 static int
23702#ifdef __BORLANDC__
23703_RTLENTRYF
23704#endif
23705prof_total_cmp(s1, s2)
23706 const void *s1;
23707 const void *s2;
23708{
23709 ufunc_T *p1, *p2;
23710
23711 p1 = *(ufunc_T **)s1;
23712 p2 = *(ufunc_T **)s2;
23713 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23714}
23715
23716/*
23717 * Compare function for self time sorting.
23718 */
23719 static int
23720#ifdef __BORLANDC__
23721_RTLENTRYF
23722#endif
23723prof_self_cmp(s1, s2)
23724 const void *s1;
23725 const void *s2;
23726{
23727 ufunc_T *p1, *p2;
23728
23729 p1 = *(ufunc_T **)s1;
23730 p2 = *(ufunc_T **)s2;
23731 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23732}
23733
Bram Moolenaar05159a02005-02-26 23:04:13 +000023734#endif
23735
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023736/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023737 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023738 * Return TRUE if a package was loaded.
23739 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023740 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023741script_autoload(name, reload)
23742 char_u *name;
23743 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023744{
23745 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023746 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023747 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023748 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023749
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023750 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023751 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023752 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023753 return FALSE;
23754
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023755 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023756
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023757 /* Find the name in the list of previously loaded package names. Skip
23758 * "autoload/", it's always the same. */
23759 for (i = 0; i < ga_loaded.ga_len; ++i)
23760 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23761 break;
23762 if (!reload && i < ga_loaded.ga_len)
23763 ret = FALSE; /* was loaded already */
23764 else
23765 {
23766 /* Remember the name if it wasn't loaded already. */
23767 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23768 {
23769 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23770 tofree = NULL;
23771 }
23772
23773 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023774 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023775 ret = TRUE;
23776 }
23777
23778 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023779 return ret;
23780}
23781
23782/*
23783 * Return the autoload script name for a function or variable name.
23784 * Returns NULL when out of memory.
23785 */
23786 static char_u *
23787autoload_name(name)
23788 char_u *name;
23789{
23790 char_u *p;
23791 char_u *scriptname;
23792
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023793 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023794 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23795 if (scriptname == NULL)
23796 return FALSE;
23797 STRCPY(scriptname, "autoload/");
23798 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023799 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023800 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023801 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023802 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023803 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023804}
23805
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23807
23808/*
23809 * Function given to ExpandGeneric() to obtain the list of user defined
23810 * function names.
23811 */
23812 char_u *
23813get_user_func_name(xp, idx)
23814 expand_T *xp;
23815 int idx;
23816{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023817 static long_u done;
23818 static hashitem_T *hi;
23819 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820
23821 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023823 done = 0;
23824 hi = func_hashtab.ht_array;
23825 }
23826 if (done < func_hashtab.ht_used)
23827 {
23828 if (done++ > 0)
23829 ++hi;
23830 while (HASHITEM_EMPTY(hi))
23831 ++hi;
23832 fp = HI2UF(hi);
23833
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023834 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023835 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023836
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023837 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23838 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839
23840 cat_func_name(IObuff, fp);
23841 if (xp->xp_context != EXPAND_USER_FUNC)
23842 {
23843 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023844 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 STRCAT(IObuff, ")");
23846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 return IObuff;
23848 }
23849 return NULL;
23850}
23851
23852#endif /* FEAT_CMDL_COMPL */
23853
23854/*
23855 * Copy the function name of "fp" to buffer "buf".
23856 * "buf" must be able to hold the function name plus three bytes.
23857 * Takes care of script-local function names.
23858 */
23859 static void
23860cat_func_name(buf, fp)
23861 char_u *buf;
23862 ufunc_T *fp;
23863{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023864 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 {
23866 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023867 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 }
23869 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023870 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871}
23872
23873/*
23874 * ":delfunction {name}"
23875 */
23876 void
23877ex_delfunction(eap)
23878 exarg_T *eap;
23879{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023880 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 char_u *p;
23882 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023883 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884
23885 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023886 name = trans_function_name(&p, eap->skip, 0, &fudi);
23887 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023889 {
23890 if (fudi.fd_dict != NULL && !eap->skip)
23891 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 if (!ends_excmd(*skipwhite(p)))
23895 {
23896 vim_free(name);
23897 EMSG(_(e_trailing));
23898 return;
23899 }
23900 eap->nextcmd = check_nextcmd(p);
23901 if (eap->nextcmd != NULL)
23902 *p = NUL;
23903
23904 if (!eap->skip)
23905 fp = find_func(name);
23906 vim_free(name);
23907
23908 if (!eap->skip)
23909 {
23910 if (fp == NULL)
23911 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023912 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913 return;
23914 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023915 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 {
23917 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23918 return;
23919 }
23920
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023921 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023923 /* Delete the dict item that refers to the function, it will
23924 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023925 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023927 else
23928 func_free(fp);
23929 }
23930}
23931
23932/*
23933 * Free a function and remove it from the list of functions.
23934 */
23935 static void
23936func_free(fp)
23937 ufunc_T *fp;
23938{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023939 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023940
23941 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023942 ga_clear_strings(&(fp->uf_args));
23943 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023944#ifdef FEAT_PROFILE
23945 vim_free(fp->uf_tml_count);
23946 vim_free(fp->uf_tml_total);
23947 vim_free(fp->uf_tml_self);
23948#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023949
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023950 /* remove the function from the function hashtable */
23951 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23952 if (HASHITEM_EMPTY(hi))
23953 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023954 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023955 hash_remove(&func_hashtab, hi);
23956
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023957 vim_free(fp);
23958}
23959
23960/*
23961 * Unreference a Function: decrement the reference count and free it when it
23962 * becomes zero. Only for numbered functions.
23963 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023964 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023965func_unref(name)
23966 char_u *name;
23967{
23968 ufunc_T *fp;
23969
23970 if (name != NULL && isdigit(*name))
23971 {
23972 fp = find_func(name);
23973 if (fp == NULL)
23974 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023975 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023976 {
23977 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023978 * when "uf_calls" becomes zero. */
23979 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023980 func_free(fp);
23981 }
23982 }
23983}
23984
23985/*
23986 * Count a reference to a Function.
23987 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023988 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023989func_ref(name)
23990 char_u *name;
23991{
23992 ufunc_T *fp;
23993
23994 if (name != NULL && isdigit(*name))
23995 {
23996 fp = find_func(name);
23997 if (fp == NULL)
23998 EMSG2(_(e_intern2), "func_ref()");
23999 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024000 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 }
24002}
24003
24004/*
24005 * Call a user function.
24006 */
24007 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024008call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 ufunc_T *fp; /* pointer to function */
24010 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024011 typval_T *argvars; /* arguments */
24012 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 linenr_T firstline; /* first line of range */
24014 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024015 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016{
Bram Moolenaar33570922005-01-25 22:26:29 +000024017 char_u *save_sourcing_name;
24018 linenr_T save_sourcing_lnum;
24019 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024020 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024021 int save_did_emsg;
24022 static int depth = 0;
24023 dictitem_T *v;
24024 int fixvar_idx = 0; /* index in fixvar[] */
24025 int i;
24026 int ai;
24027 char_u numbuf[NUMBUFLEN];
24028 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024029 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024030#ifdef FEAT_PROFILE
24031 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024032 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034
24035 /* If depth of calling is getting too high, don't execute the function */
24036 if (depth >= p_mfd)
24037 {
24038 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024039 rettv->v_type = VAR_NUMBER;
24040 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 return;
24042 }
24043 ++depth;
24044
24045 line_breakcheck(); /* check for CTRL-C hit */
24046
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024047 fc = (funccall_T *)alloc(sizeof(funccall_T));
24048 fc->caller = current_funccal;
24049 current_funccal = fc;
24050 fc->func = fp;
24051 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024052 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024053 fc->linenr = 0;
24054 fc->returned = FALSE;
24055 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024057 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24058 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059
Bram Moolenaar33570922005-01-25 22:26:29 +000024060 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024061 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024062 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24063 * each argument variable and saves a lot of time.
24064 */
24065 /*
24066 * Init l: variables.
24067 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024068 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024069 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024070 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024071 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24072 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024073 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024074 name = v->di_key;
24075 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024076 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024077 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024078 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024079 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024080 v->di_tv.vval.v_dict = selfdict;
24081 ++selfdict->dv_refcount;
24082 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024083
Bram Moolenaar33570922005-01-25 22:26:29 +000024084 /*
24085 * Init a: variables.
24086 * Set a:0 to "argcount".
24087 * Set a:000 to a list with room for the "..." arguments.
24088 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024089 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024090 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024091 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024092 /* Use "name" to avoid a warning from some compiler that checks the
24093 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024094 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024095 name = v->di_key;
24096 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024097 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024098 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024099 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024100 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024101 v->di_tv.vval.v_list = &fc->l_varlist;
24102 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24103 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24104 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024105
24106 /*
24107 * Set a:firstline to "firstline" and a:lastline to "lastline".
24108 * Set a:name to named arguments.
24109 * Set a:N to the "..." arguments.
24110 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024111 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024112 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024113 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024114 (varnumber_T)lastline);
24115 for (i = 0; i < argcount; ++i)
24116 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024117 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024118 if (ai < 0)
24119 /* named argument a:name */
24120 name = FUNCARG(fp, i);
24121 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024122 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024123 /* "..." argument a:1, a:2, etc. */
24124 sprintf((char *)numbuf, "%d", ai + 1);
24125 name = numbuf;
24126 }
24127 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24128 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024129 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024130 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24131 }
24132 else
24133 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024134 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24135 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024136 if (v == NULL)
24137 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024138 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024139 }
24140 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024141 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024142
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024143 /* Note: the values are copied directly to avoid alloc/free.
24144 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024145 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024146 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024147
24148 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24149 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024150 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24151 fc->l_listitems[ai].li_tv = argvars[i];
24152 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024153 }
24154 }
24155
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156 /* Don't redraw while executing the function. */
24157 ++RedrawingDisabled;
24158 save_sourcing_name = sourcing_name;
24159 save_sourcing_lnum = sourcing_lnum;
24160 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024161 /* need space for function name + ("function " + 3) or "[number]" */
24162 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24163 + STRLEN(fp->uf_name) + 20;
24164 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 if (sourcing_name != NULL)
24166 {
24167 if (save_sourcing_name != NULL
24168 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024169 sprintf((char *)sourcing_name, "%s[%d]..",
24170 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 else
24172 STRCPY(sourcing_name, "function ");
24173 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24174
24175 if (p_verbose >= 12)
24176 {
24177 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024178 verbose_enter_scroll();
24179
Bram Moolenaar555b2802005-05-19 21:08:39 +000024180 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 if (p_verbose >= 14)
24182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024184 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024185 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024186 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187
24188 msg_puts((char_u *)"(");
24189 for (i = 0; i < argcount; ++i)
24190 {
24191 if (i > 0)
24192 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024193 if (argvars[i].v_type == VAR_NUMBER)
24194 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 else
24196 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024197 /* Do not want errors such as E724 here. */
24198 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024199 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024200 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024201 if (s != NULL)
24202 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024203 if (vim_strsize(s) > MSG_BUF_CLEN)
24204 {
24205 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24206 s = buf;
24207 }
24208 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024209 vim_free(tofree);
24210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 }
24212 }
24213 msg_puts((char_u *)")");
24214 }
24215 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024216
24217 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 --no_wait_return;
24219 }
24220 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024221#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024222 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024223 {
24224 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24225 func_do_profile(fp);
24226 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024227 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024228 {
24229 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024230 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024231 profile_zero(&fp->uf_tm_children);
24232 }
24233 script_prof_save(&wait_start);
24234 }
24235#endif
24236
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024238 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 save_did_emsg = did_emsg;
24240 did_emsg = FALSE;
24241
24242 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024243 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24245
24246 --RedrawingDisabled;
24247
24248 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024249 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024251 clear_tv(rettv);
24252 rettv->v_type = VAR_NUMBER;
24253 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 }
24255
Bram Moolenaar05159a02005-02-26 23:04:13 +000024256#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024257 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024258 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024259 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024260 profile_end(&call_start);
24261 profile_sub_wait(&wait_start, &call_start);
24262 profile_add(&fp->uf_tm_total, &call_start);
24263 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024264 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024265 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024266 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24267 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024268 }
24269 }
24270#endif
24271
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 /* when being verbose, mention the return value */
24273 if (p_verbose >= 12)
24274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024276 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024279 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024280 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024281 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024282 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024285 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024286 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024287 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024288 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024289
Bram Moolenaar555b2802005-05-19 21:08:39 +000024290 /* The value may be very long. Skip the middle part, so that we
24291 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024292 * truncate it at the end. Don't want errors such as E724 here. */
24293 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024294 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024295 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024296 if (s != NULL)
24297 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024298 if (vim_strsize(s) > MSG_BUF_CLEN)
24299 {
24300 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24301 s = buf;
24302 }
24303 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024304 vim_free(tofree);
24305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306 }
24307 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024308
24309 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 --no_wait_return;
24311 }
24312
24313 vim_free(sourcing_name);
24314 sourcing_name = save_sourcing_name;
24315 sourcing_lnum = save_sourcing_lnum;
24316 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024317#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024318 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024319 script_prof_restore(&wait_start);
24320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321
24322 if (p_verbose >= 12 && sourcing_name != NULL)
24323 {
24324 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024325 verbose_enter_scroll();
24326
Bram Moolenaar555b2802005-05-19 21:08:39 +000024327 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024329
24330 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 --no_wait_return;
24332 }
24333
24334 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024335 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024337
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024338 /* If the a:000 list and the l: and a: dicts are not referenced we can
24339 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024340 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24341 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24342 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24343 {
24344 free_funccal(fc, FALSE);
24345 }
24346 else
24347 {
24348 hashitem_T *hi;
24349 listitem_T *li;
24350 int todo;
24351
24352 /* "fc" is still in use. This can happen when returning "a:000" or
24353 * assigning "l:" to a global variable.
24354 * Link "fc" in the list for garbage collection later. */
24355 fc->caller = previous_funccal;
24356 previous_funccal = fc;
24357
24358 /* Make a copy of the a: variables, since we didn't do that above. */
24359 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24360 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24361 {
24362 if (!HASHITEM_EMPTY(hi))
24363 {
24364 --todo;
24365 v = HI2DI(hi);
24366 copy_tv(&v->di_tv, &v->di_tv);
24367 }
24368 }
24369
24370 /* Make a copy of the a:000 items, since we didn't do that above. */
24371 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24372 copy_tv(&li->li_tv, &li->li_tv);
24373 }
24374}
24375
24376/*
24377 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024378 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024379 */
24380 static int
24381can_free_funccal(fc, copyID)
24382 funccall_T *fc;
24383 int copyID;
24384{
24385 return (fc->l_varlist.lv_copyID != copyID
24386 && fc->l_vars.dv_copyID != copyID
24387 && fc->l_avars.dv_copyID != copyID);
24388}
24389
24390/*
24391 * Free "fc" and what it contains.
24392 */
24393 static void
24394free_funccal(fc, free_val)
24395 funccall_T *fc;
24396 int free_val; /* a: vars were allocated */
24397{
24398 listitem_T *li;
24399
24400 /* The a: variables typevals may not have been allocated, only free the
24401 * allocated variables. */
24402 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24403
24404 /* free all l: variables */
24405 vars_clear(&fc->l_vars.dv_hashtab);
24406
24407 /* Free the a:000 variables if they were allocated. */
24408 if (free_val)
24409 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24410 clear_tv(&li->li_tv);
24411
24412 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413}
24414
24415/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024416 * Add a number variable "name" to dict "dp" with value "nr".
24417 */
24418 static void
24419add_nr_var(dp, v, name, nr)
24420 dict_T *dp;
24421 dictitem_T *v;
24422 char *name;
24423 varnumber_T nr;
24424{
24425 STRCPY(v->di_key, name);
24426 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24427 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24428 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024429 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024430 v->di_tv.vval.v_number = nr;
24431}
24432
24433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434 * ":return [expr]"
24435 */
24436 void
24437ex_return(eap)
24438 exarg_T *eap;
24439{
24440 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024441 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 int returning = FALSE;
24443
24444 if (current_funccal == NULL)
24445 {
24446 EMSG(_("E133: :return not inside a function"));
24447 return;
24448 }
24449
24450 if (eap->skip)
24451 ++emsg_skip;
24452
24453 eap->nextcmd = NULL;
24454 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024455 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024456 {
24457 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024458 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461 }
24462 /* It's safer to return also on error. */
24463 else if (!eap->skip)
24464 {
24465 /*
24466 * Return unless the expression evaluation has been cancelled due to an
24467 * aborting error, an interrupt, or an exception.
24468 */
24469 if (!aborting())
24470 returning = do_return(eap, FALSE, TRUE, NULL);
24471 }
24472
24473 /* When skipping or the return gets pending, advance to the next command
24474 * in this line (!returning). Otherwise, ignore the rest of the line.
24475 * Following lines will be ignored by get_func_line(). */
24476 if (returning)
24477 eap->nextcmd = NULL;
24478 else if (eap->nextcmd == NULL) /* no argument */
24479 eap->nextcmd = check_nextcmd(arg);
24480
24481 if (eap->skip)
24482 --emsg_skip;
24483}
24484
24485/*
24486 * Return from a function. Possibly makes the return pending. Also called
24487 * for a pending return at the ":endtry" or after returning from an extra
24488 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024489 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024490 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491 * FALSE when the return gets pending.
24492 */
24493 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024494do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 exarg_T *eap;
24496 int reanimate;
24497 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024498 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499{
24500 int idx;
24501 struct condstack *cstack = eap->cstack;
24502
24503 if (reanimate)
24504 /* Undo the return. */
24505 current_funccal->returned = FALSE;
24506
24507 /*
24508 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24509 * not in its finally clause (which then is to be executed next) is found.
24510 * In this case, make the ":return" pending for execution at the ":endtry".
24511 * Otherwise, return normally.
24512 */
24513 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24514 if (idx >= 0)
24515 {
24516 cstack->cs_pending[idx] = CSTP_RETURN;
24517
24518 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024519 /* A pending return again gets pending. "rettv" points to an
24520 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024522 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523 else
24524 {
24525 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024526 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024528 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024530 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 {
24532 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024533 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024534 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 else
24536 EMSG(_(e_outofmem));
24537 }
24538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024539 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540
24541 if (reanimate)
24542 {
24543 /* The pending return value could be overwritten by a ":return"
24544 * without argument in a finally clause; reset the default
24545 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024546 current_funccal->rettv->v_type = VAR_NUMBER;
24547 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548 }
24549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024550 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024551 }
24552 else
24553 {
24554 current_funccal->returned = TRUE;
24555
24556 /* If the return is carried out now, store the return value. For
24557 * a return immediately after reanimation, the value is already
24558 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024559 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024561 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024562 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024564 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565 }
24566 }
24567
24568 return idx < 0;
24569}
24570
24571/*
24572 * Free the variable with a pending return value.
24573 */
24574 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024575discard_pending_return(rettv)
24576 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577{
Bram Moolenaar33570922005-01-25 22:26:29 +000024578 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579}
24580
24581/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024582 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 * is an allocated string. Used by report_pending() for verbose messages.
24584 */
24585 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024586get_return_cmd(rettv)
24587 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024589 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024590 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024591 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024592
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024593 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024594 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024595 if (s == NULL)
24596 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024597
24598 STRCPY(IObuff, ":return ");
24599 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24600 if (STRLEN(s) + 8 >= IOSIZE)
24601 STRCPY(IObuff + IOSIZE - 4, "...");
24602 vim_free(tofree);
24603 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604}
24605
24606/*
24607 * Get next function line.
24608 * Called by do_cmdline() to get the next line.
24609 * Returns allocated string, or NULL for end of function.
24610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611 char_u *
24612get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024613 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024615 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616{
Bram Moolenaar33570922005-01-25 22:26:29 +000024617 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024618 ufunc_T *fp = fcp->func;
24619 char_u *retval;
24620 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621
24622 /* If breakpoints have been added/deleted need to check for it. */
24623 if (fcp->dbg_tick != debug_tick)
24624 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024625 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 sourcing_lnum);
24627 fcp->dbg_tick = debug_tick;
24628 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024629#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024630 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024631 func_line_end(cookie);
24632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633
Bram Moolenaar05159a02005-02-26 23:04:13 +000024634 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024635 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24636 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 retval = NULL;
24638 else
24639 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024640 /* Skip NULL lines (continuation lines). */
24641 while (fcp->linenr < gap->ga_len
24642 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24643 ++fcp->linenr;
24644 if (fcp->linenr >= gap->ga_len)
24645 retval = NULL;
24646 else
24647 {
24648 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24649 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024650#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024651 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024652 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024653#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 }
24656
24657 /* Did we encounter a breakpoint? */
24658 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24659 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024660 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024662 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 sourcing_lnum);
24664 fcp->dbg_tick = debug_tick;
24665 }
24666
24667 return retval;
24668}
24669
Bram Moolenaar05159a02005-02-26 23:04:13 +000024670#if defined(FEAT_PROFILE) || defined(PROTO)
24671/*
24672 * Called when starting to read a function line.
24673 * "sourcing_lnum" must be correct!
24674 * When skipping lines it may not actually be executed, but we won't find out
24675 * until later and we need to store the time now.
24676 */
24677 void
24678func_line_start(cookie)
24679 void *cookie;
24680{
24681 funccall_T *fcp = (funccall_T *)cookie;
24682 ufunc_T *fp = fcp->func;
24683
24684 if (fp->uf_profiling && sourcing_lnum >= 1
24685 && sourcing_lnum <= fp->uf_lines.ga_len)
24686 {
24687 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024688 /* Skip continuation lines. */
24689 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24690 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024691 fp->uf_tml_execed = FALSE;
24692 profile_start(&fp->uf_tml_start);
24693 profile_zero(&fp->uf_tml_children);
24694 profile_get_wait(&fp->uf_tml_wait);
24695 }
24696}
24697
24698/*
24699 * Called when actually executing a function line.
24700 */
24701 void
24702func_line_exec(cookie)
24703 void *cookie;
24704{
24705 funccall_T *fcp = (funccall_T *)cookie;
24706 ufunc_T *fp = fcp->func;
24707
24708 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24709 fp->uf_tml_execed = TRUE;
24710}
24711
24712/*
24713 * Called when done with a function line.
24714 */
24715 void
24716func_line_end(cookie)
24717 void *cookie;
24718{
24719 funccall_T *fcp = (funccall_T *)cookie;
24720 ufunc_T *fp = fcp->func;
24721
24722 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24723 {
24724 if (fp->uf_tml_execed)
24725 {
24726 ++fp->uf_tml_count[fp->uf_tml_idx];
24727 profile_end(&fp->uf_tml_start);
24728 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024729 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024730 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24731 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024732 }
24733 fp->uf_tml_idx = -1;
24734 }
24735}
24736#endif
24737
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738/*
24739 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024740 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741 */
24742 int
24743func_has_ended(cookie)
24744 void *cookie;
24745{
Bram Moolenaar33570922005-01-25 22:26:29 +000024746 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747
24748 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24749 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024750 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 || fcp->returned);
24752}
24753
24754/*
24755 * return TRUE if cookie indicates a function which "abort"s on errors.
24756 */
24757 int
24758func_has_abort(cookie)
24759 void *cookie;
24760{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024761 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762}
24763
24764#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24765typedef enum
24766{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024767 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24768 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24769 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770} var_flavour_T;
24771
24772static var_flavour_T var_flavour __ARGS((char_u *varname));
24773
24774 static var_flavour_T
24775var_flavour(varname)
24776 char_u *varname;
24777{
24778 char_u *p = varname;
24779
24780 if (ASCII_ISUPPER(*p))
24781 {
24782 while (*(++p))
24783 if (ASCII_ISLOWER(*p))
24784 return VAR_FLAVOUR_SESSION;
24785 return VAR_FLAVOUR_VIMINFO;
24786 }
24787 else
24788 return VAR_FLAVOUR_DEFAULT;
24789}
24790#endif
24791
24792#if defined(FEAT_VIMINFO) || defined(PROTO)
24793/*
24794 * Restore global vars that start with a capital from the viminfo file
24795 */
24796 int
24797read_viminfo_varlist(virp, writing)
24798 vir_T *virp;
24799 int writing;
24800{
24801 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024802 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804
24805 if (!writing && (find_viminfo_parameter('!') != NULL))
24806 {
24807 tab = vim_strchr(virp->vir_line + 1, '\t');
24808 if (tab != NULL)
24809 {
24810 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024811 switch (*tab)
24812 {
24813 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024814#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024815 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024816#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024817 case 'D': type = VAR_DICT; break;
24818 case 'L': type = VAR_LIST; break;
24819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820
24821 tab = vim_strchr(tab, '\t');
24822 if (tab != NULL)
24823 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024824 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024825 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024826 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024827 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024828#ifdef FEAT_FLOAT
24829 else if (type == VAR_FLOAT)
24830 (void)string2float(tab + 1, &tv.vval.v_float);
24831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024833 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024834 if (type == VAR_DICT || type == VAR_LIST)
24835 {
24836 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24837
24838 if (etv == NULL)
24839 /* Failed to parse back the dict or list, use it as a
24840 * string. */
24841 tv.v_type = VAR_STRING;
24842 else
24843 {
24844 vim_free(tv.vval.v_string);
24845 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024846 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024847 }
24848 }
24849
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024850 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024851
24852 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024853 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024854 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24855 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856 }
24857 }
24858 }
24859
24860 return viminfo_readline(virp);
24861}
24862
24863/*
24864 * Write global vars that start with a capital to the viminfo file
24865 */
24866 void
24867write_viminfo_varlist(fp)
24868 FILE *fp;
24869{
Bram Moolenaar33570922005-01-25 22:26:29 +000024870 hashitem_T *hi;
24871 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024872 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024873 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024874 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024875 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024876 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877
24878 if (find_viminfo_parameter('!') == NULL)
24879 return;
24880
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024881 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024882
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024883 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024884 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024886 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024888 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024889 this_var = HI2DI(hi);
24890 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024891 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024892 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024893 {
24894 case VAR_STRING: s = "STR"; break;
24895 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024896#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024897 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024898#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024899 case VAR_DICT: s = "DIC"; break;
24900 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024901 default: continue;
24902 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024903 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024904 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024905 if (p != NULL)
24906 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024907 vim_free(tofree);
24908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 }
24910 }
24911}
24912#endif
24913
24914#if defined(FEAT_SESSION) || defined(PROTO)
24915 int
24916store_session_globals(fd)
24917 FILE *fd;
24918{
Bram Moolenaar33570922005-01-25 22:26:29 +000024919 hashitem_T *hi;
24920 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024921 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 char_u *p, *t;
24923
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024924 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024925 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024926 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024927 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024929 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024930 this_var = HI2DI(hi);
24931 if ((this_var->di_tv.v_type == VAR_NUMBER
24932 || this_var->di_tv.v_type == VAR_STRING)
24933 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024934 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024935 /* Escape special characters with a backslash. Turn a LF and
24936 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024937 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024938 (char_u *)"\\\"\n\r");
24939 if (p == NULL) /* out of memory */
24940 break;
24941 for (t = p; *t != NUL; ++t)
24942 if (*t == '\n')
24943 *t = 'n';
24944 else if (*t == '\r')
24945 *t = 'r';
24946 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024947 this_var->di_key,
24948 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24949 : ' ',
24950 p,
24951 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24952 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024953 || put_eol(fd) == FAIL)
24954 {
24955 vim_free(p);
24956 return FAIL;
24957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958 vim_free(p);
24959 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024960#ifdef FEAT_FLOAT
24961 else if (this_var->di_tv.v_type == VAR_FLOAT
24962 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24963 {
24964 float_T f = this_var->di_tv.vval.v_float;
24965 int sign = ' ';
24966
24967 if (f < 0)
24968 {
24969 f = -f;
24970 sign = '-';
24971 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024972 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024973 this_var->di_key, sign, f) < 0)
24974 || put_eol(fd) == FAIL)
24975 return FAIL;
24976 }
24977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978 }
24979 }
24980 return OK;
24981}
24982#endif
24983
Bram Moolenaar661b1822005-07-28 22:36:45 +000024984/*
24985 * Display script name where an item was last set.
24986 * Should only be invoked when 'verbose' is non-zero.
24987 */
24988 void
24989last_set_msg(scriptID)
24990 scid_T scriptID;
24991{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024992 char_u *p;
24993
Bram Moolenaar661b1822005-07-28 22:36:45 +000024994 if (scriptID != 0)
24995 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024996 p = home_replace_save(NULL, get_scriptname(scriptID));
24997 if (p != NULL)
24998 {
24999 verbose_enter();
25000 MSG_PUTS(_("\n\tLast set from "));
25001 MSG_PUTS(p);
25002 vim_free(p);
25003 verbose_leave();
25004 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025005 }
25006}
25007
Bram Moolenaard812df62008-11-09 12:46:09 +000025008/*
25009 * List v:oldfiles in a nice way.
25010 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025011 void
25012ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025013 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025014{
25015 list_T *l = vimvars[VV_OLDFILES].vv_list;
25016 listitem_T *li;
25017 int nr = 0;
25018
25019 if (l == NULL)
25020 msg((char_u *)_("No old files"));
25021 else
25022 {
25023 msg_start();
25024 msg_scroll = TRUE;
25025 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25026 {
25027 msg_outnum((long)++nr);
25028 MSG_PUTS(": ");
25029 msg_outtrans(get_tv_string(&li->li_tv));
25030 msg_putchar('\n');
25031 out_flush(); /* output one line at a time */
25032 ui_breakcheck();
25033 }
25034 /* Assume "got_int" was set to truncate the listing. */
25035 got_int = FALSE;
25036
25037#ifdef FEAT_BROWSE_CMD
25038 if (cmdmod.browse)
25039 {
25040 quit_more = FALSE;
25041 nr = prompt_for_number(FALSE);
25042 msg_starthere();
25043 if (nr > 0)
25044 {
25045 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25046 (long)nr);
25047
25048 if (p != NULL)
25049 {
25050 p = expand_env_save(p);
25051 eap->arg = p;
25052 eap->cmdidx = CMD_edit;
25053 cmdmod.browse = FALSE;
25054 do_exedit(eap, NULL);
25055 vim_free(p);
25056 }
25057 }
25058 }
25059#endif
25060 }
25061}
25062
Bram Moolenaar53744302015-07-17 17:38:22 +020025063/* reset v:option_new, v:option_old and v:option_type */
25064 void
25065reset_v_option_vars()
25066{
25067 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25068 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25069 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25070}
25071
25072
Bram Moolenaar071d4272004-06-13 20:20:40 +000025073#endif /* FEAT_EVAL */
25074
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025076#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077
25078#ifdef WIN3264
25079/*
25080 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25081 */
25082static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25083static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25084static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25085
25086/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025087 * Get the short path (8.3) for the filename in "fnamep".
25088 * Only works for a valid file name.
25089 * When the path gets longer "fnamep" is changed and the allocated buffer
25090 * is put in "bufp".
25091 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25092 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 */
25094 static int
25095get_short_pathname(fnamep, bufp, fnamelen)
25096 char_u **fnamep;
25097 char_u **bufp;
25098 int *fnamelen;
25099{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025100 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101 char_u *newbuf;
25102
25103 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 l = GetShortPathName(*fnamep, *fnamep, len);
25105 if (l > len - 1)
25106 {
25107 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025108 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 newbuf = vim_strnsave(*fnamep, l);
25110 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025111 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112
25113 vim_free(*bufp);
25114 *fnamep = *bufp = newbuf;
25115
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025116 /* Really should always succeed, as the buffer is big enough. */
25117 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025118 }
25119
25120 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025121 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122}
25123
25124/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025125 * Get the short path (8.3) for the filename in "fname". The converted
25126 * path is returned in "bufp".
25127 *
25128 * Some of the directories specified in "fname" may not exist. This function
25129 * will shorten the existing directories at the beginning of the path and then
25130 * append the remaining non-existing path.
25131 *
25132 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025133 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025134 * bufp - Pointer to an allocated buffer for the filename.
25135 * fnamelen - Length of the filename pointed to by fname
25136 *
25137 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 */
25139 static int
25140shortpath_for_invalid_fname(fname, bufp, fnamelen)
25141 char_u **fname;
25142 char_u **bufp;
25143 int *fnamelen;
25144{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025145 char_u *short_fname, *save_fname, *pbuf_unused;
25146 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025147 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025148 int old_len, len;
25149 int new_len, sfx_len;
25150 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151
25152 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025153 old_len = *fnamelen;
25154 save_fname = vim_strnsave(*fname, old_len);
25155 pbuf_unused = NULL;
25156 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025158 endp = save_fname + old_len - 1; /* Find the end of the copy */
25159 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025161 /*
25162 * Try shortening the supplied path till it succeeds by removing one
25163 * directory at a time from the tail of the path.
25164 */
25165 len = 0;
25166 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025168 /* go back one path-separator */
25169 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25170 --endp;
25171 if (endp <= save_fname)
25172 break; /* processed the complete path */
25173
25174 /*
25175 * Replace the path separator with a NUL and try to shorten the
25176 * resulting path.
25177 */
25178 ch = *endp;
25179 *endp = 0;
25180 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025181 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025182 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25183 {
25184 retval = FAIL;
25185 goto theend;
25186 }
25187 *endp = ch; /* preserve the string */
25188
25189 if (len > 0)
25190 break; /* successfully shortened the path */
25191
25192 /* failed to shorten the path. Skip the path separator */
25193 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194 }
25195
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025196 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025198 /*
25199 * Succeeded in shortening the path. Now concatenate the shortened
25200 * path with the remaining path at the tail.
25201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025203 /* Compute the length of the new path. */
25204 sfx_len = (int)(save_endp - endp) + 1;
25205 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025207 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025209 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025211 /* There is not enough space in the currently allocated string,
25212 * copy it to a buffer big enough. */
25213 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025215 {
25216 retval = FAIL;
25217 goto theend;
25218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219 }
25220 else
25221 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025222 /* Transfer short_fname to the main buffer (it's big enough),
25223 * unless get_short_pathname() did its work in-place. */
25224 *fname = *bufp = save_fname;
25225 if (short_fname != save_fname)
25226 vim_strncpy(save_fname, short_fname, len);
25227 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025229
25230 /* concat the not-shortened part of the path */
25231 vim_strncpy(*fname + len, endp, sfx_len);
25232 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025234
25235theend:
25236 vim_free(pbuf_unused);
25237 vim_free(save_fname);
25238
25239 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240}
25241
25242/*
25243 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025244 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 */
25246 static int
25247shortpath_for_partial(fnamep, bufp, fnamelen)
25248 char_u **fnamep;
25249 char_u **bufp;
25250 int *fnamelen;
25251{
25252 int sepcount, len, tflen;
25253 char_u *p;
25254 char_u *pbuf, *tfname;
25255 int hasTilde;
25256
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025257 /* Count up the path separators from the RHS.. so we know which part
25258 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025260 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 if (vim_ispathsep(*p))
25262 ++sepcount;
25263
25264 /* Need full path first (use expand_env() to remove a "~/") */
25265 hasTilde = (**fnamep == '~');
25266 if (hasTilde)
25267 pbuf = tfname = expand_env_save(*fnamep);
25268 else
25269 pbuf = tfname = FullName_save(*fnamep, FALSE);
25270
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025271 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025273 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275
25276 if (len == 0)
25277 {
25278 /* Don't have a valid filename, so shorten the rest of the
25279 * path if we can. This CAN give us invalid 8.3 filenames, but
25280 * there's not a lot of point in guessing what it might be.
25281 */
25282 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025283 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285 }
25286
25287 /* Count the paths backward to find the beginning of the desired string. */
25288 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025289 {
25290#ifdef FEAT_MBYTE
25291 if (has_mbyte)
25292 p -= mb_head_off(tfname, p);
25293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 if (vim_ispathsep(*p))
25295 {
25296 if (sepcount == 0 || (hasTilde && sepcount == 1))
25297 break;
25298 else
25299 sepcount --;
25300 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302 if (hasTilde)
25303 {
25304 --p;
25305 if (p >= tfname)
25306 *p = '~';
25307 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025308 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025309 }
25310 else
25311 ++p;
25312
25313 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25314 vim_free(*bufp);
25315 *fnamelen = (int)STRLEN(p);
25316 *bufp = pbuf;
25317 *fnamep = p;
25318
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025319 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320}
25321#endif /* WIN3264 */
25322
25323/*
25324 * Adjust a filename, according to a string of modifiers.
25325 * *fnamep must be NUL terminated when called. When returning, the length is
25326 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025327 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025328 * When there is an error, *fnamep is set to NULL.
25329 */
25330 int
25331modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25332 char_u *src; /* string with modifiers */
25333 int *usedlen; /* characters after src that are used */
25334 char_u **fnamep; /* file name so far */
25335 char_u **bufp; /* buffer for allocated file name or NULL */
25336 int *fnamelen; /* length of fnamep */
25337{
25338 int valid = 0;
25339 char_u *tail;
25340 char_u *s, *p, *pbuf;
25341 char_u dirname[MAXPATHL];
25342 int c;
25343 int has_fullname = 0;
25344#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025345 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346 int has_shortname = 0;
25347#endif
25348
25349repeat:
25350 /* ":p" - full path/file_name */
25351 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25352 {
25353 has_fullname = 1;
25354
25355 valid |= VALID_PATH;
25356 *usedlen += 2;
25357
25358 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25359 if ((*fnamep)[0] == '~'
25360#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25361 && ((*fnamep)[1] == '/'
25362# ifdef BACKSLASH_IN_FILENAME
25363 || (*fnamep)[1] == '\\'
25364# endif
25365 || (*fnamep)[1] == NUL)
25366
25367#endif
25368 )
25369 {
25370 *fnamep = expand_env_save(*fnamep);
25371 vim_free(*bufp); /* free any allocated file name */
25372 *bufp = *fnamep;
25373 if (*fnamep == NULL)
25374 return -1;
25375 }
25376
25377 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025378 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 {
25380 if (vim_ispathsep(*p)
25381 && p[1] == '.'
25382 && (p[2] == NUL
25383 || vim_ispathsep(p[2])
25384 || (p[2] == '.'
25385 && (p[3] == NUL || vim_ispathsep(p[3])))))
25386 break;
25387 }
25388
25389 /* FullName_save() is slow, don't use it when not needed. */
25390 if (*p != NUL || !vim_isAbsName(*fnamep))
25391 {
25392 *fnamep = FullName_save(*fnamep, *p != NUL);
25393 vim_free(*bufp); /* free any allocated file name */
25394 *bufp = *fnamep;
25395 if (*fnamep == NULL)
25396 return -1;
25397 }
25398
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025399#ifdef WIN3264
25400# if _WIN32_WINNT >= 0x0500
25401 if (vim_strchr(*fnamep, '~') != NULL)
25402 {
25403 /* Expand 8.3 filename to full path. Needed to make sure the same
25404 * file does not have two different names.
25405 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25406 p = alloc(_MAX_PATH + 1);
25407 if (p != NULL)
25408 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025409 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025410 {
25411 vim_free(*bufp);
25412 *bufp = *fnamep = p;
25413 }
25414 else
25415 vim_free(p);
25416 }
25417 }
25418# endif
25419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420 /* Append a path separator to a directory. */
25421 if (mch_isdir(*fnamep))
25422 {
25423 /* Make room for one or two extra characters. */
25424 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25425 vim_free(*bufp); /* free any allocated file name */
25426 *bufp = *fnamep;
25427 if (*fnamep == NULL)
25428 return -1;
25429 add_pathsep(*fnamep);
25430 }
25431 }
25432
25433 /* ":." - path relative to the current directory */
25434 /* ":~" - path relative to the home directory */
25435 /* ":8" - shortname path - postponed till after */
25436 while (src[*usedlen] == ':'
25437 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25438 {
25439 *usedlen += 2;
25440 if (c == '8')
25441 {
25442#ifdef WIN3264
25443 has_shortname = 1; /* Postpone this. */
25444#endif
25445 continue;
25446 }
25447 pbuf = NULL;
25448 /* Need full path first (use expand_env() to remove a "~/") */
25449 if (!has_fullname)
25450 {
25451 if (c == '.' && **fnamep == '~')
25452 p = pbuf = expand_env_save(*fnamep);
25453 else
25454 p = pbuf = FullName_save(*fnamep, FALSE);
25455 }
25456 else
25457 p = *fnamep;
25458
25459 has_fullname = 0;
25460
25461 if (p != NULL)
25462 {
25463 if (c == '.')
25464 {
25465 mch_dirname(dirname, MAXPATHL);
25466 s = shorten_fname(p, dirname);
25467 if (s != NULL)
25468 {
25469 *fnamep = s;
25470 if (pbuf != NULL)
25471 {
25472 vim_free(*bufp); /* free any allocated file name */
25473 *bufp = pbuf;
25474 pbuf = NULL;
25475 }
25476 }
25477 }
25478 else
25479 {
25480 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25481 /* Only replace it when it starts with '~' */
25482 if (*dirname == '~')
25483 {
25484 s = vim_strsave(dirname);
25485 if (s != NULL)
25486 {
25487 *fnamep = s;
25488 vim_free(*bufp);
25489 *bufp = s;
25490 }
25491 }
25492 }
25493 vim_free(pbuf);
25494 }
25495 }
25496
25497 tail = gettail(*fnamep);
25498 *fnamelen = (int)STRLEN(*fnamep);
25499
25500 /* ":h" - head, remove "/file_name", can be repeated */
25501 /* Don't remove the first "/" or "c:\" */
25502 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25503 {
25504 valid |= VALID_HEAD;
25505 *usedlen += 2;
25506 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025507 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025508 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509 *fnamelen = (int)(tail - *fnamep);
25510#ifdef VMS
25511 if (*fnamelen > 0)
25512 *fnamelen += 1; /* the path separator is part of the path */
25513#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025514 if (*fnamelen == 0)
25515 {
25516 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25517 p = vim_strsave((char_u *)".");
25518 if (p == NULL)
25519 return -1;
25520 vim_free(*bufp);
25521 *bufp = *fnamep = tail = p;
25522 *fnamelen = 1;
25523 }
25524 else
25525 {
25526 while (tail > s && !after_pathsep(s, tail))
25527 mb_ptr_back(*fnamep, tail);
25528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529 }
25530
25531 /* ":8" - shortname */
25532 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25533 {
25534 *usedlen += 2;
25535#ifdef WIN3264
25536 has_shortname = 1;
25537#endif
25538 }
25539
25540#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025541 /*
25542 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543 */
25544 if (has_shortname)
25545 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025546 /* Copy the string if it is shortened by :h and when it wasn't copied
25547 * yet, because we are going to change it in place. Avoids changing
25548 * the buffer name for "%:8". */
25549 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025550 {
25551 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025552 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 return -1;
25554 vim_free(*bufp);
25555 *bufp = *fnamep = p;
25556 }
25557
25558 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025559 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025560 if (!has_fullname && !vim_isAbsName(*fnamep))
25561 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025562 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 return -1;
25564 }
25565 else
25566 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025567 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568
Bram Moolenaardc935552011-08-17 15:23:23 +020025569 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025571 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025572 return -1;
25573
25574 if (l == 0)
25575 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025576 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025578 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025579 return -1;
25580 }
25581 *fnamelen = l;
25582 }
25583 }
25584#endif /* WIN3264 */
25585
25586 /* ":t" - tail, just the basename */
25587 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25588 {
25589 *usedlen += 2;
25590 *fnamelen -= (int)(tail - *fnamep);
25591 *fnamep = tail;
25592 }
25593
25594 /* ":e" - extension, can be repeated */
25595 /* ":r" - root, without extension, can be repeated */
25596 while (src[*usedlen] == ':'
25597 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25598 {
25599 /* find a '.' in the tail:
25600 * - for second :e: before the current fname
25601 * - otherwise: The last '.'
25602 */
25603 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25604 s = *fnamep - 2;
25605 else
25606 s = *fnamep + *fnamelen - 1;
25607 for ( ; s > tail; --s)
25608 if (s[0] == '.')
25609 break;
25610 if (src[*usedlen + 1] == 'e') /* :e */
25611 {
25612 if (s > tail)
25613 {
25614 *fnamelen += (int)(*fnamep - (s + 1));
25615 *fnamep = s + 1;
25616#ifdef VMS
25617 /* cut version from the extension */
25618 s = *fnamep + *fnamelen - 1;
25619 for ( ; s > *fnamep; --s)
25620 if (s[0] == ';')
25621 break;
25622 if (s > *fnamep)
25623 *fnamelen = s - *fnamep;
25624#endif
25625 }
25626 else if (*fnamep <= tail)
25627 *fnamelen = 0;
25628 }
25629 else /* :r */
25630 {
25631 if (s > tail) /* remove one extension */
25632 *fnamelen = (int)(s - *fnamep);
25633 }
25634 *usedlen += 2;
25635 }
25636
25637 /* ":s?pat?foo?" - substitute */
25638 /* ":gs?pat?foo?" - global substitute */
25639 if (src[*usedlen] == ':'
25640 && (src[*usedlen + 1] == 's'
25641 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25642 {
25643 char_u *str;
25644 char_u *pat;
25645 char_u *sub;
25646 int sep;
25647 char_u *flags;
25648 int didit = FALSE;
25649
25650 flags = (char_u *)"";
25651 s = src + *usedlen + 2;
25652 if (src[*usedlen + 1] == 'g')
25653 {
25654 flags = (char_u *)"g";
25655 ++s;
25656 }
25657
25658 sep = *s++;
25659 if (sep)
25660 {
25661 /* find end of pattern */
25662 p = vim_strchr(s, sep);
25663 if (p != NULL)
25664 {
25665 pat = vim_strnsave(s, (int)(p - s));
25666 if (pat != NULL)
25667 {
25668 s = p + 1;
25669 /* find end of substitution */
25670 p = vim_strchr(s, sep);
25671 if (p != NULL)
25672 {
25673 sub = vim_strnsave(s, (int)(p - s));
25674 str = vim_strnsave(*fnamep, *fnamelen);
25675 if (sub != NULL && str != NULL)
25676 {
25677 *usedlen = (int)(p + 1 - src);
25678 s = do_string_sub(str, pat, sub, flags);
25679 if (s != NULL)
25680 {
25681 *fnamep = s;
25682 *fnamelen = (int)STRLEN(s);
25683 vim_free(*bufp);
25684 *bufp = s;
25685 didit = TRUE;
25686 }
25687 }
25688 vim_free(sub);
25689 vim_free(str);
25690 }
25691 vim_free(pat);
25692 }
25693 }
25694 /* after using ":s", repeat all the modifiers */
25695 if (didit)
25696 goto repeat;
25697 }
25698 }
25699
Bram Moolenaar26df0922014-02-23 23:39:13 +010025700 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25701 {
25702 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25703 if (p == NULL)
25704 return -1;
25705 vim_free(*bufp);
25706 *bufp = *fnamep = p;
25707 *fnamelen = (int)STRLEN(p);
25708 *usedlen += 2;
25709 }
25710
Bram Moolenaar071d4272004-06-13 20:20:40 +000025711 return valid;
25712}
25713
25714/*
25715 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25716 * "flags" can be "g" to do a global substitute.
25717 * Returns an allocated string, NULL for error.
25718 */
25719 char_u *
25720do_string_sub(str, pat, sub, flags)
25721 char_u *str;
25722 char_u *pat;
25723 char_u *sub;
25724 char_u *flags;
25725{
25726 int sublen;
25727 regmatch_T regmatch;
25728 int i;
25729 int do_all;
25730 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025731 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732 garray_T ga;
25733 char_u *ret;
25734 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025735 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025736
25737 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25738 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025739 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740
25741 ga_init2(&ga, 1, 200);
25742
25743 do_all = (flags[0] == 'g');
25744
25745 regmatch.rm_ic = p_ic;
25746 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25747 if (regmatch.regprog != NULL)
25748 {
25749 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025750 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25752 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025753 /* Skip empty match except for first match. */
25754 if (regmatch.startp[0] == regmatch.endp[0])
25755 {
25756 if (zero_width == regmatch.startp[0])
25757 {
25758 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025759 i = MB_PTR2LEN(tail);
25760 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25761 (size_t)i);
25762 ga.ga_len += i;
25763 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025764 continue;
25765 }
25766 zero_width = regmatch.startp[0];
25767 }
25768
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 /*
25770 * Get some space for a temporary buffer to do the substitution
25771 * into. It will contain:
25772 * - The text up to where the match is.
25773 * - The substituted text.
25774 * - The text after the match.
25775 */
25776 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025777 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25779 {
25780 ga_clear(&ga);
25781 break;
25782 }
25783
25784 /* copy the text up to where the match is */
25785 i = (int)(regmatch.startp[0] - tail);
25786 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25787 /* add the substituted text */
25788 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25789 + ga.ga_len + i, TRUE, TRUE, FALSE);
25790 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025791 tail = regmatch.endp[0];
25792 if (*tail == NUL)
25793 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025794 if (!do_all)
25795 break;
25796 }
25797
25798 if (ga.ga_data != NULL)
25799 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25800
Bram Moolenaar473de612013-06-08 18:19:48 +020025801 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 }
25803
25804 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25805 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025806 if (p_cpo == empty_option)
25807 p_cpo = save_cpo;
25808 else
25809 /* Darn, evaluating {sub} expression changed the value. */
25810 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025811
25812 return ret;
25813}
25814
25815#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */