blob: e97ddc47812b69f5df17718b48227c630d58e85c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000372};
373
374/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_type vv_di.di_tv.v_type
376#define vv_nr vv_di.di_tv.vval.v_number
377#define vv_float vv_di.di_tv.vval.v_float
378#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000379#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200380#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000382
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200383static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000384#define vimvarht vimvardict.dv_hashtab
385
Bram Moolenaara40058a2005-07-11 22:42:07 +0000386static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
387static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100404static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000434static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
438static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100441static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000443static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200444static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
452static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static int string2float __ARGS((char_u *text, float_T *value));
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200461static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100470static void f_alloc_fail __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100471static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200475static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100477static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara803c7f2016-01-15 15:31:39 +0100478static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara260b872016-01-15 20:48:22 +0100479static void f_assert_fails __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100480static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200485static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100496static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100498static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
502#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000503static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000506static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000508#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000509static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000510static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
512#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000515#ifdef FEAT_FLOAT
516static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200517static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000518#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
522static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200532static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000533static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534#ifdef FEAT_FLOAT
535static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
536#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000539static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#ifdef FEAT_FLOAT
546static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000549#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000550static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000559static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000561static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200565static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000568static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200569static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000577static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000578static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200579static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000580static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000581static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200584static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000585static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100591static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000594static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000608static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100613static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000615static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000627#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200628static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000629static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200631#ifdef FEAT_LUA
632static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000638static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200639static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000640static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000641static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000643static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000647#ifdef vim_mkdir
648static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100651#ifdef FEAT_MZSCHEME
652static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100656static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000657static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
659static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
660#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000662static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000663static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200664#ifdef FEAT_PYTHON3
665static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
667#ifdef FEAT_PYTHON
668static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
669#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000671static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000672static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000684#ifdef FEAT_FLOAT
685static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
686#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200687static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100689static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000692static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000694static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200699static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000702static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000703static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000704static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000705static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200707static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000708static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100710#ifdef FEAT_CRYPT
711static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
712#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000713static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200714static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#ifdef FEAT_FLOAT
717static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200718static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000721static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000722static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000725#ifdef FEAT_FLOAT
726static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
728#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000729static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200730static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000731#ifdef HAVE_STRFTIME
732static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
734static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200740static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200741static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000747static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200748static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200750static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000751static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000752static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000753static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000754static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000755static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000757static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200758#ifdef FEAT_FLOAT
759static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
761#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#ifdef FEAT_FLOAT
766static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
767#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200769static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200770static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100771static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
773static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
774static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100775static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
781static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000782static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
783static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000785static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100786static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100787static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788
Bram Moolenaar493c1782014-05-28 14:34:46 +0200789static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000790static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static int get_env_len __ARGS((char_u **arg));
792static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000793static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000794static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
795#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
796#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
797 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000798static 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 +0000799static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000800static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200801static 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 +0000802static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static typval_T *alloc_tv __ARGS((void));
804static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static void init_tv __ARGS((typval_T *varp));
806static long get_tv_number __ARGS((typval_T *varp));
807static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000808static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static char_u *get_tv_string __ARGS((typval_T *varp));
810static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000811static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100812static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
813static 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 +0000814static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
815static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
816static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000817static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
818static 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 +0000819static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200820static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
821static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200822static int var_check_func_name __ARGS((char_u *name, int new_var));
823static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200824static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000825static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
827static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
828static int eval_fname_script __ARGS((char_u *p));
829static int eval_fname_sid __ARGS((char_u *p));
830static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static ufunc_T *find_func __ARGS((char_u *name));
832static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200833static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834#ifdef FEAT_PROFILE
835static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000836static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
837static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
838static int
839# ifdef __BORLANDC__
840 _RTLENTRYF
841# endif
842 prof_total_cmp __ARGS((const void *s1, const void *s2));
843static int
844# ifdef __BORLANDC__
845 _RTLENTRYF
846# endif
847 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200849static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000850static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000851static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000852static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000853static 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 +0000854static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
855static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000856static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000857static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
858static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000859static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000860static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000861static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200862static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200863static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000864
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200865
866#ifdef EBCDIC
867static int compare_func_name __ARGS((const void *s1, const void *s2));
868static void sortFunctions __ARGS(());
869#endif
870
Bram Moolenaar33570922005-01-25 22:26:29 +0000871/*
872 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000873 */
874 void
875eval_init()
876{
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 int i;
878 struct vimvar *p;
879
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200880 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
881 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200882 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000883 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000884 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885
886 for (i = 0; i < VV_LEN; ++i)
887 {
888 p = &vimvars[i];
889 STRCPY(p->vv_di.di_key, p->vv_name);
890 if (p->vv_flags & VV_RO)
891 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
892 else if (p->vv_flags & VV_RO_SBX)
893 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
894 else
895 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000896
897 /* add to v: scope dict, unless the value is not always available */
898 if (p->vv_type != VAR_UNKNOWN)
899 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000900 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000901 /* add to compat scope dict */
902 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000904 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100905 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200906 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100907 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200908 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909
910#ifdef EBCDIC
911 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100912 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200913 */
914 sortFunctions();
915#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000916}
917
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918#if defined(EXITFREE) || defined(PROTO)
919 void
920eval_clear()
921{
922 int i;
923 struct vimvar *p;
924
925 for (i = 0; i < VV_LEN; ++i)
926 {
927 p = &vimvars[i];
928 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000929 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000930 vim_free(p->vv_str);
931 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000932 }
933 else if (p->vv_di.di_tv.v_type == VAR_LIST)
934 {
935 list_unref(p->vv_list);
936 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000937 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000938 }
939 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000940 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000941 hash_clear(&compat_hashtab);
942
Bram Moolenaard9fba312005-06-26 22:34:35 +0000943 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100944# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200945 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100946# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000947
948 /* global variables */
949 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000950
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000951 /* autoloaded script names */
952 ga_clear_strings(&ga_loaded);
953
Bram Moolenaarcca74132013-09-25 21:00:28 +0200954 /* Script-local variables. First clear all the variables and in a second
955 * loop free the scriptvar_T, because a variable in one script might hold
956 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200957 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200958 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200959 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200960 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200961 ga_clear(&ga_scripts);
962
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000963 /* unreferenced lists and dicts */
964 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000965
966 /* functions */
967 free_all_functions();
968 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969}
970#endif
971
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 * Return the name of the executed function.
974 */
975 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100976func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000978 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980
981/*
982 * Return the address holding the next breakpoint line for a funccall cookie.
983 */
984 linenr_T *
985func_breakpoint(cookie)
986 void *cookie;
987{
Bram Moolenaar33570922005-01-25 22:26:29 +0000988 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991/*
992 * Return the address holding the debug tick for a funccall cookie.
993 */
994 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100995func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996{
Bram Moolenaar33570922005-01-25 22:26:29 +0000997 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998}
999
1000/*
1001 * Return the nesting level for a funccall cookie.
1002 */
1003 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001004func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
Bram Moolenaar33570922005-01-25 22:26:29 +00001006 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007}
1008
1009/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001010funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001012/* pointer to list of previously used funccal, still around because some
1013 * item in it is still being used. */
1014funccall_T *previous_funccal = NULL;
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016/*
1017 * Return TRUE when a function was ended by a ":return" command.
1018 */
1019 int
1020current_func_returned()
1021{
1022 return current_funccal->returned;
1023}
1024
1025
1026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Set an internal variable to a string value. Creates the variable if it does
1028 * not already exist.
1029 */
1030 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001031set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001033 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 val = vim_strsave(value);
1037 if (val != NULL)
1038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001040 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001042 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001043 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045 }
1046}
1047
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001048static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001049static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050static char_u *redir_endp = NULL;
1051static char_u *redir_varname = NULL;
1052
1053/*
1054 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001055 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 * Returns OK if successfully completed the setup. FAIL otherwise.
1057 */
1058 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060{
1061 int save_emsg;
1062 int err;
1063 typval_T tv;
1064
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001065 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001066 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 {
1068 EMSG(_(e_invarg));
1069 return FAIL;
1070 }
1071
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001072 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 redir_varname = vim_strsave(name);
1074 if (redir_varname == NULL)
1075 return FAIL;
1076
1077 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1078 if (redir_lval == NULL)
1079 {
1080 var_redir_stop();
1081 return FAIL;
1082 }
1083
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001084 /* The output is stored in growarray "redir_ga" until redirection ends. */
1085 ga_init2(&redir_ga, (int)sizeof(char), 500);
1086
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001088 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001089 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1091 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001092 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 if (redir_endp != NULL && *redir_endp != NUL)
1094 /* Trailing characters are present after the variable name */
1095 EMSG(_(e_trailing));
1096 else
1097 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001098 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099 var_redir_stop();
1100 return FAIL;
1101 }
1102
1103 /* check if we can write to the variable: set it to or append an empty
1104 * string */
1105 save_emsg = did_emsg;
1106 did_emsg = FALSE;
1107 tv.v_type = VAR_STRING;
1108 tv.vval.v_string = (char_u *)"";
1109 if (append)
1110 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1111 else
1112 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001113 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001115 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 if (err)
1117 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 var_redir_stop();
1120 return FAIL;
1121 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122
1123 return OK;
1124}
1125
1126/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 * Append "value[value_len]" to the variable set by var_redir_start().
1128 * The actual appending is postponed until redirection ends, because the value
1129 * appended may in fact be the string we write to, changing it may cause freed
1130 * memory to be used:
1131 * :redir => foo
1132 * :let foo
1133 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 */
1135 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001136var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001138 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139
1140 if (redir_lval == NULL)
1141 return;
1142
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001144 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001146 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001148 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149 {
1150 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152 }
1153 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155}
1156
1157/*
1158 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 */
1161 void
1162var_redir_stop()
1163{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164 typval_T tv;
1165
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (redir_lval != NULL)
1167 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001168 /* If there was no error: assign the text to the variable. */
1169 if (redir_endp != NULL)
1170 {
1171 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1172 tv.v_type = VAR_STRING;
1173 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001174 /* Call get_lval() again, if it's inside a Dict or List it may
1175 * have changed. */
1176 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001177 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001178 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1179 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1180 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001181 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001182
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001183 /* free the collected output */
1184 vim_free(redir_ga.ga_data);
1185 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001186
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 vim_free(redir_lval);
1188 redir_lval = NULL;
1189 }
1190 vim_free(redir_varname);
1191 redir_varname = NULL;
1192}
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194# if defined(FEAT_MBYTE) || defined(PROTO)
1195 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001196eval_charconvert(
1197 char_u *enc_from,
1198 char_u *enc_to,
1199 char_u *fname_from,
1200 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
1202 int err = FALSE;
1203
1204 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1205 set_vim_var_string(VV_CC_TO, enc_to, -1);
1206 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1207 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1208 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1209 err = TRUE;
1210 set_vim_var_string(VV_CC_FROM, NULL, -1);
1211 set_vim_var_string(VV_CC_TO, NULL, -1);
1212 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1213 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1214
1215 if (err)
1216 return FAIL;
1217 return OK;
1218}
1219# endif
1220
1221# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1222 int
1223eval_printexpr(fname, args)
1224 char_u *fname;
1225 char_u *args;
1226{
1227 int err = FALSE;
1228
1229 set_vim_var_string(VV_FNAME_IN, fname, -1);
1230 set_vim_var_string(VV_CMDARG, args, -1);
1231 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1232 err = TRUE;
1233 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1234 set_vim_var_string(VV_CMDARG, NULL, -1);
1235
1236 if (err)
1237 {
1238 mch_remove(fname);
1239 return FAIL;
1240 }
1241 return OK;
1242}
1243# endif
1244
1245# if defined(FEAT_DIFF) || defined(PROTO)
1246 void
1247eval_diff(origfile, newfile, outfile)
1248 char_u *origfile;
1249 char_u *newfile;
1250 char_u *outfile;
1251{
1252 int err = FALSE;
1253
1254 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1255 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1256 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1257 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1258 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1259 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1260 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1261}
1262
1263 void
1264eval_patch(origfile, difffile, outfile)
1265 char_u *origfile;
1266 char_u *difffile;
1267 char_u *outfile;
1268{
1269 int err;
1270
1271 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1272 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1273 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1274 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1275 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1276 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1277 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1278}
1279# endif
1280
1281/*
1282 * Top level evaluation function, returning a boolean.
1283 * Sets "error" to TRUE if there was an error.
1284 * Return TRUE or FALSE.
1285 */
1286 int
1287eval_to_bool(arg, error, nextcmd, skip)
1288 char_u *arg;
1289 int *error;
1290 char_u **nextcmd;
1291 int skip; /* only parse, don't execute */
1292{
Bram Moolenaar33570922005-01-25 22:26:29 +00001293 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 int retval = FALSE;
1295
1296 if (skip)
1297 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001298 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 else
1301 {
1302 *error = FALSE;
1303 if (!skip)
1304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001305 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
1316 * Top level evaluation function, returning a string. If "skip" is TRUE,
1317 * only parsing to "nextcmd" is done, without reporting errors. Return
1318 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1319 */
1320 char_u *
1321eval_to_string_skip(arg, nextcmd, skip)
1322 char_u *arg;
1323 char_u **nextcmd;
1324 int skip; /* only parse, don't execute */
1325{
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 char_u *retval;
1328
1329 if (skip)
1330 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001331 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 retval = NULL;
1333 else
1334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 retval = vim_strsave(get_tv_string(&tv));
1336 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338 if (skip)
1339 --emsg_skip;
1340
1341 return retval;
1342}
1343
1344/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001345 * Skip over an expression at "*pp".
1346 * Return FAIL for an error, OK otherwise.
1347 */
1348 int
1349skip_expr(pp)
1350 char_u **pp;
1351{
Bram Moolenaar33570922005-01-25 22:26:29 +00001352 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001353
1354 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001356}
1357
1358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001360 * When "convert" is TRUE convert a List into a sequence of lines and convert
1361 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 * Return pointer to allocated memory, or NULL for failure.
1363 */
1364 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 char_u *arg;
1367 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001368 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
Bram Moolenaar33570922005-01-25 22:26:29 +00001370 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001372 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001373#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001374 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 retval = NULL;
1379 else
1380 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001381 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001382 {
1383 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001384 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001385 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001386 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001387 if (tv.vval.v_list->lv_len > 0)
1388 ga_append(&ga, NL);
1389 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001390 ga_append(&ga, NUL);
1391 retval = (char_u *)ga.ga_data;
1392 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001393#ifdef FEAT_FLOAT
1394 else if (convert && tv.v_type == VAR_FLOAT)
1395 {
1396 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1397 retval = vim_strsave(numbuf);
1398 }
1399#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001400 else
1401 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404
1405 return retval;
1406}
1407
1408/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001409 * Call eval_to_string() without using current local variables and using
1410 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 */
1412 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 char_u *arg;
1415 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
1418 char_u *retval;
1419 void *save_funccalp;
1420
1421 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001422 if (use_sandbox)
1423 ++sandbox;
1424 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001425 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001426 if (use_sandbox)
1427 --sandbox;
1428 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 restore_funccal(save_funccalp);
1430 return retval;
1431}
1432
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433/*
1434 * Top level evaluation function, returning a number.
1435 * Evaluates "expr" silently.
1436 * Returns -1 for an error.
1437 */
1438 int
1439eval_to_number(expr)
1440 char_u *expr;
1441{
Bram Moolenaar33570922005-01-25 22:26:29 +00001442 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001444 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 ++emsg_off;
1447
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001448 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 retval = -1;
1450 else
1451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001452 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001453 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 }
1455 --emsg_off;
1456
1457 return retval;
1458}
1459
Bram Moolenaara40058a2005-07-11 22:42:07 +00001460/*
1461 * Prepare v: variable "idx" to be used.
1462 * Save the current typeval in "save_tv".
1463 * When not used yet add the variable to the v: hashtable.
1464 */
1465 static void
1466prepare_vimvar(idx, save_tv)
1467 int idx;
1468 typval_T *save_tv;
1469{
1470 *save_tv = vimvars[idx].vv_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1473}
1474
1475/*
1476 * Restore v: variable "idx" to typeval "save_tv".
1477 * When no longer defined, remove the variable from the v: hashtable.
1478 */
1479 static void
1480restore_vimvar(idx, save_tv)
1481 int idx;
1482 typval_T *save_tv;
1483{
1484 hashitem_T *hi;
1485
Bram Moolenaara40058a2005-07-11 22:42:07 +00001486 vimvars[idx].vv_tv = *save_tv;
1487 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1488 {
1489 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1490 if (HASHITEM_EMPTY(hi))
1491 EMSG2(_(e_intern2), "restore_vimvar()");
1492 else
1493 hash_remove(&vimvarht, hi);
1494 }
1495}
1496
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001497#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001498/*
1499 * Evaluate an expression to a list with suggestions.
1500 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001501 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001502 */
1503 list_T *
1504eval_spell_expr(badword, expr)
1505 char_u *badword;
1506 char_u *expr;
1507{
1508 typval_T save_val;
1509 typval_T rettv;
1510 list_T *list = NULL;
1511 char_u *p = skipwhite(expr);
1512
1513 /* Set "v:val" to the bad word. */
1514 prepare_vimvar(VV_VAL, &save_val);
1515 vimvars[VV_VAL].vv_type = VAR_STRING;
1516 vimvars[VV_VAL].vv_str = badword;
1517 if (p_verbose == 0)
1518 ++emsg_off;
1519
1520 if (eval1(&p, &rettv, TRUE) == OK)
1521 {
1522 if (rettv.v_type != VAR_LIST)
1523 clear_tv(&rettv);
1524 else
1525 list = rettv.vval.v_list;
1526 }
1527
1528 if (p_verbose == 0)
1529 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001530 restore_vimvar(VV_VAL, &save_val);
1531
1532 return list;
1533}
1534
1535/*
1536 * "list" is supposed to contain two items: a word and a number. Return the
1537 * word in "pp" and the number as the return value.
1538 * Return -1 if anything isn't right.
1539 * Used to get the good word and score from the eval_spell_expr() result.
1540 */
1541 int
1542get_spellword(list, pp)
1543 list_T *list;
1544 char_u **pp;
1545{
1546 listitem_T *li;
1547
1548 li = list->lv_first;
1549 if (li == NULL)
1550 return -1;
1551 *pp = get_tv_string(&li->li_tv);
1552
1553 li = li->li_next;
1554 if (li == NULL)
1555 return -1;
1556 return get_tv_number(&li->li_tv);
1557}
1558#endif
1559
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001560/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001561 * Top level evaluation function.
1562 * Returns an allocated typval_T with the result.
1563 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001564 */
1565 typval_T *
1566eval_expr(arg, nextcmd)
1567 char_u *arg;
1568 char_u **nextcmd;
1569{
1570 typval_T *tv;
1571
1572 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001573 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001574 {
1575 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001576 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001577 }
1578
1579 return tv;
1580}
1581
1582
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001585 * Uses argv[argc] for the function arguments. Only Number and String
1586 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001589 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001590call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 char_u *func;
1592 int argc;
1593 char_u **argv;
1594 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001595 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
Bram Moolenaar33570922005-01-25 22:26:29 +00001598 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 long n;
1600 int len;
1601 int i;
1602 int doesrange;
1603 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001604 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001606 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 for (i = 0; i < argc; i++)
1611 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001612 /* Pass a NULL or empty argument as an empty string */
1613 if (argv[i] == NULL || *argv[i] == NUL)
1614 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001615 argvars[i].v_type = VAR_STRING;
1616 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001617 continue;
1618 }
1619
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001620 if (str_arg_only)
1621 len = 0;
1622 else
1623 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001624 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (len != 0 && len == (int)STRLEN(argv[i]))
1626 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001627 argvars[i].v_type = VAR_NUMBER;
1628 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
1630 else
1631 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001632 argvars[i].v_type = VAR_STRING;
1633 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 }
1636
1637 if (safe)
1638 {
1639 save_funccalp = save_funccal();
1640 ++sandbox;
1641 }
1642
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1644 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (safe)
1648 {
1649 --sandbox;
1650 restore_funccal(save_funccalp);
1651 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 vim_free(argvars);
1653
1654 if (ret == FAIL)
1655 clear_tv(rettv);
1656
1657 return ret;
1658}
1659
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001660/*
1661 * Call vimL function "func" and return the result as a number.
1662 * Returns -1 when calling the function fails.
1663 * Uses argv[argc] for the function arguments.
1664 */
1665 long
1666call_func_retnr(func, argc, argv, safe)
1667 char_u *func;
1668 int argc;
1669 char_u **argv;
1670 int safe; /* use the sandbox */
1671{
1672 typval_T rettv;
1673 long retval;
1674
1675 /* All arguments are passed as strings, no conversion to number. */
1676 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1677 return -1;
1678
1679 retval = get_tv_number_chk(&rettv, NULL);
1680 clear_tv(&rettv);
1681 return retval;
1682}
1683
1684#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1685 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1686
Bram Moolenaar4f688582007-07-24 12:34:30 +00001687# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001689 * Call vimL function "func" and return the result as a string.
1690 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 * Uses argv[argc] for the function arguments.
1692 */
1693 void *
1694call_func_retstr(func, argc, argv, safe)
1695 char_u *func;
1696 int argc;
1697 char_u **argv;
1698 int safe; /* use the sandbox */
1699{
1700 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001701 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001703 /* All arguments are passed as strings, no conversion to number. */
1704 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 return NULL;
1706
1707 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 return retval;
1710}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001711# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001713/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001714 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001716 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717 */
1718 void *
1719call_func_retlist(func, argc, argv, safe)
1720 char_u *func;
1721 int argc;
1722 char_u **argv;
1723 int safe; /* use the sandbox */
1724{
1725 typval_T rettv;
1726
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001727 /* All arguments are passed as strings, no conversion to number. */
1728 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729 return NULL;
1730
1731 if (rettv.v_type != VAR_LIST)
1732 {
1733 clear_tv(&rettv);
1734 return NULL;
1735 }
1736
1737 return rettv.vval.v_list;
1738}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#endif
1740
1741/*
1742 * Save the current function call pointer, and set it to NULL.
1743 * Used when executing autocommands and for ":source".
1744 */
1745 void *
1746save_funccal()
1747{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001748 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 current_funccal = NULL;
1751 return (void *)fc;
1752}
1753
1754 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001755restore_funccal(vfc)
1756 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001758 funccall_T *fc = (funccall_T *)vfc;
1759
1760 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761}
1762
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763#if defined(FEAT_PROFILE) || defined(PROTO)
1764/*
1765 * Prepare profiling for entering a child or something else that is not
1766 * counted for the script/function itself.
1767 * Should always be called in pair with prof_child_exit().
1768 */
1769 void
1770prof_child_enter(tm)
1771 proftime_T *tm; /* place to store waittime */
1772{
1773 funccall_T *fc = current_funccal;
1774
1775 if (fc != NULL && fc->func->uf_profiling)
1776 profile_start(&fc->prof_child);
1777 script_prof_save(tm);
1778}
1779
1780/*
1781 * Take care of time spent in a child.
1782 * Should always be called after prof_child_enter().
1783 */
1784 void
1785prof_child_exit(tm)
1786 proftime_T *tm; /* where waittime was stored */
1787{
1788 funccall_T *fc = current_funccal;
1789
1790 if (fc != NULL && fc->func->uf_profiling)
1791 {
1792 profile_end(&fc->prof_child);
1793 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1794 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1795 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1796 }
1797 script_prof_restore(tm);
1798}
1799#endif
1800
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802#ifdef FEAT_FOLDING
1803/*
1804 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1805 * it in "*cp". Doesn't give error messages.
1806 */
1807 int
1808eval_foldexpr(arg, cp)
1809 char_u *arg;
1810 int *cp;
1811{
Bram Moolenaar33570922005-01-25 22:26:29 +00001812 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 int retval;
1814 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001815 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1816 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001819 if (use_sandbox)
1820 ++sandbox;
1821 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 retval = 0;
1825 else
1826 {
1827 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 if (tv.v_type == VAR_NUMBER)
1829 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001830 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 retval = 0;
1832 else
1833 {
1834 /* If the result is a string, check if there is a non-digit before
1835 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (!VIM_ISDIGIT(*s) && *s != '-')
1838 *cp = *s++;
1839 retval = atol((char *)s);
1840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001844 if (use_sandbox)
1845 --sandbox;
1846 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848 return retval;
1849}
1850#endif
1851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 * ":let" list all variable values
1854 * ":let var1 var2" list variable values
1855 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001856 * ":let var += expr" assignment command.
1857 * ":let var -= expr" assignment command.
1858 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 */
1861 void
1862ex_let(eap)
1863 exarg_T *eap;
1864{
1865 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001867 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 int var_count = 0;
1870 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001871 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001872 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001873 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
Bram Moolenaardb552d602006-03-23 22:59:57 +00001875 argend = skip_var_list(arg, &var_count, &semicolon);
1876 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001877 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001878 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1879 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001880 expr = skipwhite(argend);
1881 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1882 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001884 /*
1885 * ":let" without "=": list variables
1886 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 if (*arg == '[')
1888 EMSG(_(e_invarg));
1889 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001891 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001895 list_glob_vars(&first);
1896 list_buf_vars(&first);
1897 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001898#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001899 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001900#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 list_script_vars(&first);
1902 list_func_vars(&first);
1903 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 eap->nextcmd = check_nextcmd(arg);
1906 }
1907 else
1908 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 op[0] = '=';
1910 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001911 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001913 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1914 op[0] = *expr; /* +=, -= or .= */
1915 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001917 else
1918 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (eap->skip)
1921 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (eap->skip)
1924 {
1925 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 --emsg_skip;
1928 }
1929 else if (i != FAIL)
1930 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 }
1935 }
1936}
1937
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938/*
1939 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1940 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 * When "nextchars" is not NULL it points to a string with characters that
1942 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1943 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 * Returns OK or FAIL;
1945 */
1946 static int
1947ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1948 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 int copy; /* copy values from "tv", don't move */
1951 int semicolon; /* from skip_var_list() */
1952 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954{
1955 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001958 listitem_T *item;
1959 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960
1961 if (*arg != '[')
1962 {
1963 /*
1964 * ":let var = expr" or ":for var in list"
1965 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001966 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 return FAIL;
1968 return OK;
1969 }
1970
1971 /*
1972 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1973 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001974 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 {
1976 EMSG(_(e_listreq));
1977 return FAIL;
1978 }
1979
1980 i = list_len(l);
1981 if (semicolon == 0 && var_count < i)
1982 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001983 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 return FAIL;
1985 }
1986 if (var_count - semicolon > i)
1987 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001988 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 return FAIL;
1990 }
1991
1992 item = l->lv_first;
1993 while (*arg != ']')
1994 {
1995 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 item = item->li_next;
1998 if (arg == NULL)
1999 return FAIL;
2000
2001 arg = skipwhite(arg);
2002 if (*arg == ';')
2003 {
2004 /* Put the rest of the list (may be empty) in the var after ';'.
2005 * Create a new list for this. */
2006 l = list_alloc();
2007 if (l == NULL)
2008 return FAIL;
2009 while (item != NULL)
2010 {
2011 list_append_tv(l, &item->li_tv);
2012 item = item->li_next;
2013 }
2014
2015 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002016 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 ltv.vval.v_list = l;
2018 l->lv_refcount = 1;
2019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2021 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 clear_tv(&ltv);
2023 if (arg == NULL)
2024 return FAIL;
2025 break;
2026 }
2027 else if (*arg != ',' && *arg != ']')
2028 {
2029 EMSG2(_(e_intern2), "ex_let_vars()");
2030 return FAIL;
2031 }
2032 }
2033
2034 return OK;
2035}
2036
2037/*
2038 * Skip over assignable variable "var" or list of variables "[var, var]".
2039 * Used for ":let varvar = expr" and ":for varvar in expr".
2040 * For "[var, var]" increment "*var_count" for each variable.
2041 * for "[var, var; var]" set "semicolon".
2042 * Return NULL for an error.
2043 */
2044 static char_u *
2045skip_var_list(arg, var_count, semicolon)
2046 char_u *arg;
2047 int *var_count;
2048 int *semicolon;
2049{
2050 char_u *p, *s;
2051
2052 if (*arg == '[')
2053 {
2054 /* "[var, var]": find the matching ']'. */
2055 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002056 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 {
2058 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2059 s = skip_var_one(p);
2060 if (s == p)
2061 {
2062 EMSG2(_(e_invarg2), p);
2063 return NULL;
2064 }
2065 ++*var_count;
2066
2067 p = skipwhite(s);
2068 if (*p == ']')
2069 break;
2070 else if (*p == ';')
2071 {
2072 if (*semicolon == 1)
2073 {
2074 EMSG(_("Double ; in list of variables"));
2075 return NULL;
2076 }
2077 *semicolon = 1;
2078 }
2079 else if (*p != ',')
2080 {
2081 EMSG2(_(e_invarg2), p);
2082 return NULL;
2083 }
2084 }
2085 return p + 1;
2086 }
2087 else
2088 return skip_var_one(arg);
2089}
2090
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002091/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002092 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002093 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002094 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002095 static char_u *
2096skip_var_one(arg)
2097 char_u *arg;
2098{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002099 if (*arg == '@' && arg[1] != NUL)
2100 return arg + 2;
2101 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2102 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002103}
2104
Bram Moolenaara7043832005-01-21 11:56:39 +00002105/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002106 * List variables for hashtab "ht" with prefix "prefix".
2107 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002108 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002111 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 hashitem_T *hi;
2117 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118 int todo;
2119
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002120 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2122 {
2123 if (!HASHITEM_EMPTY(hi))
2124 {
2125 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002126 di = HI2DI(hi);
2127 if (empty || di->di_tv.v_type != VAR_STRING
2128 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002130 }
2131 }
2132}
2133
2134/*
2135 * List global variables.
2136 */
2137 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_glob_vars(first)
2139 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002142}
2143
2144/*
2145 * List buffer variables.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_buf_vars(first)
2149 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002150{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002151 char_u numbuf[NUMBUFLEN];
2152
Bram Moolenaar429fa852013-04-15 12:27:36 +02002153 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155
2156 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2158 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002159}
2160
2161/*
2162 * List window variables.
2163 */
2164 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165list_win_vars(first)
2166 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002167{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002168 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002170}
2171
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002172#ifdef FEAT_WINDOWS
2173/*
2174 * List tab page variables.
2175 */
2176 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177list_tab_vars(first)
2178 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002180 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002182}
2183#endif
2184
Bram Moolenaara7043832005-01-21 11:56:39 +00002185/*
2186 * List Vim variables.
2187 */
2188 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189list_vim_vars(first)
2190 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193}
2194
2195/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196 * List script-local variables, if there is a script.
2197 */
2198 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199list_script_vars(first)
2200 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201{
2202 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2204 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002205}
2206
2207/*
2208 * List function variables, if there is a function.
2209 */
2210 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211list_func_vars(first)
2212 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002213{
2214 if (current_funccal != NULL)
2215 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002217}
2218
2219/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 * List variables in "arg".
2221 */
2222 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 exarg_T *eap;
2225 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227{
2228 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 char_u *name_start;
2232 char_u *arg_subsc;
2233 char_u *tofree;
2234 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235
2236 while (!ends_excmd(*arg) && !got_int)
2237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002240 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2242 {
2243 emsg_severe = TRUE;
2244 EMSG(_(e_trailing));
2245 break;
2246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 /* get_name_len() takes care of expanding curly braces */
2251 name_start = name = arg;
2252 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2253 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 /* This is mainly to keep test 49 working: when expanding
2256 * curly braces fails overrule the exception error message. */
2257 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 emsg_severe = TRUE;
2260 EMSG2(_(e_invarg2), arg);
2261 break;
2262 }
2263 error = TRUE;
2264 }
2265 else
2266 {
2267 if (tofree != NULL)
2268 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002269 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 else
2272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 /* handle d.key, l[idx], f(expr) */
2274 arg_subsc = arg;
2275 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 case 'g': list_glob_vars(first); break;
2284 case 'b': list_buf_vars(first); break;
2285 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002286#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002287 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002288#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 case 'v': list_vim_vars(first); break;
2290 case 's': list_script_vars(first); break;
2291 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 default:
2293 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 }
2296 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 {
2298 char_u numbuf[NUMBUFLEN];
2299 char_u *tf;
2300 int c;
2301 char_u *s;
2302
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002303 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 c = *arg;
2305 *arg = NUL;
2306 list_one_var_a((char_u *)"",
2307 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002308 tv.v_type,
2309 s == NULL ? (char_u *)"" : s,
2310 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 *arg = c;
2312 vim_free(tf);
2313 }
2314 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002315 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 }
2317 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318
2319 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321
2322 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
2324
2325 return arg;
2326}
2327
2328/*
2329 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2330 * Returns a pointer to the char just after the var name.
2331 * Returns NULL if there is an error.
2332 */
2333 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002336 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002338 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340{
2341 int c1;
2342 char_u *name;
2343 char_u *p;
2344 char_u *arg_end = NULL;
2345 int len;
2346 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348
2349 /*
2350 * ":let $VAR = expr": Set environment variable.
2351 */
2352 if (*arg == '$')
2353 {
2354 /* Find the end of the name. */
2355 ++arg;
2356 name = arg;
2357 len = get_env_len(&arg);
2358 if (len == 0)
2359 EMSG2(_(e_invarg2), name - 1);
2360 else
2361 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 if (op != NULL && (*op == '+' || *op == '-'))
2363 EMSG2(_(e_letwrong), op);
2364 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002365 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002367 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002368 {
2369 c1 = name[len];
2370 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 p = get_tv_string_chk(tv);
2372 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 {
2374 int mustfree = FALSE;
2375 char_u *s = vim_getenv(name, &mustfree);
2376
2377 if (s != NULL)
2378 {
2379 p = tofree = concat_str(s, p);
2380 if (mustfree)
2381 vim_free(s);
2382 }
2383 }
2384 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002387 if (STRICMP(name, "HOME") == 0)
2388 init_homedir();
2389 else if (didset_vim && STRICMP(name, "VIM") == 0)
2390 didset_vim = FALSE;
2391 else if (didset_vimruntime
2392 && STRICMP(name, "VIMRUNTIME") == 0)
2393 didset_vimruntime = FALSE;
2394 arg_end = arg;
2395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398 }
2399 }
2400 }
2401
2402 /*
2403 * ":let &option = expr": Set option value.
2404 * ":let &l:option = expr": Set local option value.
2405 * ":let &g:option = expr": Set global option value.
2406 */
2407 else if (*arg == '&')
2408 {
2409 /* Find the end of the name. */
2410 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002411 if (p == NULL || (endchars != NULL
2412 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 EMSG(_(e_letunexp));
2414 else
2415 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 long n;
2417 int opt_type;
2418 long numval;
2419 char_u *stringval = NULL;
2420 char_u *s;
2421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 c1 = *p;
2423 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424
2425 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 s = get_tv_string_chk(tv); /* != NULL if number or string */
2427 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 {
2429 opt_type = get_option_value(arg, &numval,
2430 &stringval, opt_flags);
2431 if ((opt_type == 1 && *op == '.')
2432 || (opt_type == 0 && *op != '.'))
2433 EMSG2(_(e_letwrong), op);
2434 else
2435 {
2436 if (opt_type == 1) /* number */
2437 {
2438 if (*op == '+')
2439 n = numval + n;
2440 else
2441 n = numval - n;
2442 }
2443 else if (opt_type == 0 && stringval != NULL) /* string */
2444 {
2445 s = concat_str(stringval, s);
2446 vim_free(stringval);
2447 stringval = s;
2448 }
2449 }
2450 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 if (s != NULL)
2452 {
2453 set_option_value(arg, n, s, opt_flags);
2454 arg_end = p;
2455 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 }
2459 }
2460
2461 /*
2462 * ":let @r = expr": Set register contents.
2463 */
2464 else if (*arg == '@')
2465 {
2466 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 if (op != NULL && (*op == '+' || *op == '-'))
2468 EMSG2(_(e_letwrong), op);
2469 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002470 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 char_u *s;
2476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 p = get_tv_string_chk(tv);
2478 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002480 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 if (s != NULL)
2482 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 vim_free(s);
2485 }
2486 }
2487 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002488 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002490 arg_end = arg + 1;
2491 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002492 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 }
2494 }
2495
2496 /*
2497 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002499 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002500 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002502 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002504 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2508 EMSG(_(e_letunexp));
2509 else
2510 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 arg_end = p;
2513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 }
2517
2518 else
2519 EMSG2(_(e_invarg2), arg);
2520
2521 return arg_end;
2522}
2523
2524/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2526 */
2527 static int
2528check_changedtick(arg)
2529 char_u *arg;
2530{
2531 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2532 {
2533 EMSG2(_(e_readonlyvar), arg);
2534 return TRUE;
2535 }
2536 return FALSE;
2537}
2538
2539/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 * Get an lval: variable, Dict item or List item that can be assigned a value
2541 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2542 * "name.key", "name.key[expr]" etc.
2543 * Indexing only works if "name" is an existing List or Dictionary.
2544 * "name" points to the start of the name.
2545 * If "rettv" is not NULL it points to the value to be assigned.
2546 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2547 * wrong; must end in space or cmd separator.
2548 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549 * flags:
2550 * GLV_QUIET: do not give error messages
2551 * GLV_NO_AUTOLOAD: do not use script autoloading
2552 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002554 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 */
2557 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002558get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 typval_T *rettv;
2561 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 int unlet;
2563 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002564 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002565 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 char_u *expr_start, *expr_end;
2569 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 dictitem_T *v;
2571 typval_T var1;
2572 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002578 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582
2583 if (skip)
2584 {
2585 /* When skipping just find the end of the name. */
2586 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002587 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 }
2589
2590 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002591 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (expr_start != NULL)
2593 {
2594 /* Don't expand the name when we already know there is an error. */
2595 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2596 && *p != '[' && *p != '.')
2597 {
2598 EMSG(_(e_trailing));
2599 return NULL;
2600 }
2601
2602 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2603 if (lp->ll_exp_name == NULL)
2604 {
2605 /* Report an invalid expression in braces, unless the
2606 * expression evaluation has been cancelled due to an
2607 * aborting error, an interrupt, or an exception. */
2608 if (!aborting() && !quiet)
2609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002610 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 EMSG2(_(e_invarg2), name);
2612 return NULL;
2613 }
2614 }
2615 lp->ll_name = lp->ll_exp_name;
2616 }
2617 else
2618 lp->ll_name = name;
2619
2620 /* Without [idx] or .key we are done. */
2621 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2622 return p;
2623
2624 cc = *p;
2625 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002626 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (v == NULL && !quiet)
2628 EMSG2(_(e_undefvar), lp->ll_name);
2629 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 if (v == NULL)
2631 return NULL;
2632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 /*
2634 * Loop until no more [idx] or .key is following.
2635 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002636 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2640 && !(lp->ll_tv->v_type == VAR_DICT
2641 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
2644 EMSG(_("E689: Can only index a List or Dictionary"));
2645 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
2650 EMSG(_("E708: [:] must come last"));
2651 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 len = -1;
2655 if (*p == '.')
2656 {
2657 key = p + 1;
2658 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2659 ;
2660 if (len == 0)
2661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!quiet)
2663 EMSG(_(e_emptykey));
2664 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 }
2666 p = key + len;
2667 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668 else
2669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (*p == ':')
2673 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 else
2675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 empty1 = FALSE;
2677 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002679 if (get_tv_string_chk(&var1) == NULL)
2680 {
2681 /* not a number or string */
2682 clear_tv(&var1);
2683 return NULL;
2684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686
2687 /* Optionally get the second index [ :expr]. */
2688 if (*p == ':')
2689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (rettv != NULL && (rettv->v_type != VAR_LIST
2699 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (!empty1)
2704 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707 p = skipwhite(p + 1);
2708 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 else
2711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (!empty1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002719 if (get_tv_string_chk(&var2) == NULL)
2720 {
2721 /* not a number or string */
2722 if (!empty1)
2723 clear_tv(&var1);
2724 clear_tv(&var2);
2725 return NULL;
2726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002732
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (!quiet)
2736 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (!empty1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
2743
2744 /* Skip to past ']'. */
2745 ++p;
2746 }
2747
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 {
2750 if (len == -1)
2751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (*key == NUL)
2755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (!quiet)
2757 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
2761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 lp->ll_list = NULL;
2763 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002764 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002765
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002766 /* When assigning to a scope dictionary check that a function and
2767 * variable name is valid (only variable name unless it is l: or
2768 * g: dictionary). Disallow overwriting a builtin function. */
2769 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002770 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 int prevval;
2772 int wrong;
2773
2774 if (len != -1)
2775 {
2776 prevval = key[len];
2777 key[len] = NUL;
2778 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002779 else
2780 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2782 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 || !valid_varname(key);
2785 if (len != -1)
2786 key[len] = prevval;
2787 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788 return NULL;
2789 }
2790
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 /* Can't add "v:" variable. */
2794 if (lp->ll_dict == &vimvardict)
2795 {
2796 EMSG2(_(e_illvar), name);
2797 return NULL;
2798 }
2799
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002800 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002804 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 if (len == -1)
2806 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 }
2809 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 p = NULL;
2817 break;
2818 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002819 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002820 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002821 return NULL;
2822
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 }
2827 else
2828 {
2829 /*
2830 * Get the number and item for the only or first index of the List.
2831 */
2832 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 else
2835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002836 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 clear_tv(&var1);
2838 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002839 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_list = lp->ll_tv->vval.v_list;
2841 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2842 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002844 if (lp->ll_n1 < 0)
2845 {
2846 lp->ll_n1 = 0;
2847 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2848 }
2849 }
2850 if (lp->ll_li == NULL)
2851 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002854 if (!quiet)
2855 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 }
2858
2859 /*
2860 * May need to find the item or absolute index for the second
2861 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 * When no index given: "lp->ll_empty2" is TRUE.
2863 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002867 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002873 {
2874 if (!quiet)
2875 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002877 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 }
2880
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2882 if (lp->ll_n1 < 0)
2883 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2884 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002885 {
2886 if (!quiet)
2887 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002890 }
2891
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 return p;
2897}
2898
2899/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002900 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 */
2902 static void
2903clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905{
2906 vim_free(lp->ll_exp_name);
2907 vim_free(lp->ll_newkey);
2908}
2909
2910/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002913 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 */
2915 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002916set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922{
2923 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 listitem_T *ri;
2925 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926
2927 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002928 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 cc = *endp;
2932 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933 if (op != NULL && *op != '=')
2934 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002935 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002937 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002938 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002939 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002940 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002941 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002942 if ((di == NULL
2943 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2944 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2945 FALSE)))
2946 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 set_var(lp->ll_name, &tv, FALSE);
2948 clear_tv(&tv);
2949 }
2950 }
2951 else
2952 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002954 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 else if (tv_check_lock(lp->ll_newkey == NULL
2957 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002958 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 else if (lp->ll_range)
2961 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002962 listitem_T *ll_li = lp->ll_li;
2963 int ll_n1 = lp->ll_n1;
2964
2965 /*
2966 * Check whether any of the list items is locked
2967 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002968 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002969 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002970 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002971 return;
2972 ri = ri->li_next;
2973 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2974 break;
2975 ll_li = ll_li->li_next;
2976 ++ll_n1;
2977 }
2978
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 /*
2980 * Assign the List values to the list items.
2981 */
2982 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002983 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002984 if (op != NULL && *op != '=')
2985 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2986 else
2987 {
2988 clear_tv(&lp->ll_li->li_tv);
2989 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2990 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 ri = ri->li_next;
2992 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2993 break;
2994 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002997 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 ri = NULL;
3000 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 lp->ll_li = lp->ll_li->li_next;
3004 ++lp->ll_n1;
3005 }
3006 if (ri != NULL)
3007 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 else if (lp->ll_empty2
3009 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 : lp->ll_n1 != lp->ll_n2)
3011 EMSG(_("E711: List value has not enough items"));
3012 }
3013 else
3014 {
3015 /*
3016 * Assign to a List or Dictionary item.
3017 */
3018 if (lp->ll_newkey != NULL)
3019 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 if (op != NULL && *op != '=')
3021 {
3022 EMSG2(_(e_letwrong), op);
3023 return;
3024 }
3025
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003027 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 if (di == NULL)
3029 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003030 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3031 {
3032 vim_free(di);
3033 return;
3034 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003036 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 else if (op != NULL && *op != '=')
3038 {
3039 tv_op(lp->ll_tv, rettv, op);
3040 return;
3041 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003042 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003044
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /*
3046 * Assign the value to the variable or list item.
3047 */
3048 if (copy)
3049 copy_tv(rettv, lp->ll_tv);
3050 else
3051 {
3052 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003053 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003055 }
3056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057}
3058
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3061 * Returns OK or FAIL.
3062 */
3063 static int
3064tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003065 typval_T *tv1;
3066 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 char_u *op;
3068{
3069 long n;
3070 char_u numbuf[NUMBUFLEN];
3071 char_u *s;
3072
3073 /* Can't do anything with a Funcref or a Dict on the right. */
3074 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3075 {
3076 switch (tv1->v_type)
3077 {
3078 case VAR_DICT:
3079 case VAR_FUNC:
3080 break;
3081
3082 case VAR_LIST:
3083 if (*op != '+' || tv2->v_type != VAR_LIST)
3084 break;
3085 /* List += List */
3086 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3087 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3088 return OK;
3089
3090 case VAR_NUMBER:
3091 case VAR_STRING:
3092 if (tv2->v_type == VAR_LIST)
3093 break;
3094 if (*op == '+' || *op == '-')
3095 {
3096 /* nr += nr or nr -= nr*/
3097 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098#ifdef FEAT_FLOAT
3099 if (tv2->v_type == VAR_FLOAT)
3100 {
3101 float_T f = n;
3102
3103 if (*op == '+')
3104 f += tv2->vval.v_float;
3105 else
3106 f -= tv2->vval.v_float;
3107 clear_tv(tv1);
3108 tv1->v_type = VAR_FLOAT;
3109 tv1->vval.v_float = f;
3110 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003111 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003112#endif
3113 {
3114 if (*op == '+')
3115 n += get_tv_number(tv2);
3116 else
3117 n -= get_tv_number(tv2);
3118 clear_tv(tv1);
3119 tv1->v_type = VAR_NUMBER;
3120 tv1->vval.v_number = n;
3121 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 }
3123 else
3124 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 if (tv2->v_type == VAR_FLOAT)
3126 break;
3127
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 /* str .= str */
3129 s = get_tv_string(tv1);
3130 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3131 clear_tv(tv1);
3132 tv1->v_type = VAR_STRING;
3133 tv1->vval.v_string = s;
3134 }
3135 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136
3137#ifdef FEAT_FLOAT
3138 case VAR_FLOAT:
3139 {
3140 float_T f;
3141
3142 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3143 && tv2->v_type != VAR_NUMBER
3144 && tv2->v_type != VAR_STRING))
3145 break;
3146 if (tv2->v_type == VAR_FLOAT)
3147 f = tv2->vval.v_float;
3148 else
3149 f = get_tv_number(tv2);
3150 if (*op == '+')
3151 tv1->vval.v_float += f;
3152 else
3153 tv1->vval.v_float -= f;
3154 }
3155 return OK;
3156#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003157 }
3158 }
3159
3160 EMSG2(_(e_letwrong), op);
3161 return FAIL;
3162}
3163
3164/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165 * Add a watcher to a list.
3166 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003167 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 list_T *l;
3170 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171{
3172 lw->lw_next = l->lv_watch;
3173 l->lv_watch = lw;
3174}
3175
3176/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003177 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 * No warning when it isn't found...
3179 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003180 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003182 list_T *l;
3183 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184{
Bram Moolenaar33570922005-01-25 22:26:29 +00003185 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186
3187 lwp = &l->lv_watch;
3188 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3189 {
3190 if (lw == lwrem)
3191 {
3192 *lwp = lw->lw_next;
3193 break;
3194 }
3195 lwp = &lw->lw_next;
3196 }
3197}
3198
3199/*
3200 * Just before removing an item from a list: advance watchers to the next
3201 * item.
3202 */
3203 static void
3204list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 list_T *l;
3206 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207{
Bram Moolenaar33570922005-01-25 22:26:29 +00003208 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209
3210 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3211 if (lw->lw_item == item)
3212 lw->lw_item = item->li_next;
3213}
3214
3215/*
3216 * Evaluate the expression used in a ":for var in expr" command.
3217 * "arg" points to "var".
3218 * Set "*errp" to TRUE for an error, FALSE otherwise;
3219 * Return a pointer that holds the info. Null when there is an error.
3220 */
3221 void *
3222eval_for_line(arg, errp, nextcmdp, skip)
3223 char_u *arg;
3224 int *errp;
3225 char_u **nextcmdp;
3226 int skip;
3227{
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 typval_T tv;
3231 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232
3233 *errp = TRUE; /* default: there is an error */
3234
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 if (fi == NULL)
3237 return NULL;
3238
3239 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3240 if (expr == NULL)
3241 return fi;
3242
3243 expr = skipwhite(expr);
3244 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3245 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003246 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 return fi;
3248 }
3249
3250 if (skip)
3251 ++emsg_skip;
3252 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3253 {
3254 *errp = FALSE;
3255 if (!skip)
3256 {
3257 l = tv.vval.v_list;
3258 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003261 clear_tv(&tv);
3262 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 else
3264 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003265 /* No need to increment the refcount, it's already set for the
3266 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 fi->fi_list = l;
3268 list_add_watch(l, &fi->fi_lw);
3269 fi->fi_lw.lw_item = l->lv_first;
3270 }
3271 }
3272 }
3273 if (skip)
3274 --emsg_skip;
3275
3276 return fi;
3277}
3278
3279/*
3280 * Use the first item in a ":for" list. Advance to the next.
3281 * Assign the values to the variable (list). "arg" points to the first one.
3282 * Return TRUE when a valid item was found, FALSE when at end of list or
3283 * something wrong.
3284 */
3285 int
3286next_for_item(fi_void, arg)
3287 void *fi_void;
3288 char_u *arg;
3289{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003290 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293
3294 item = fi->fi_lw.lw_item;
3295 if (item == NULL)
3296 result = FALSE;
3297 else
3298 {
3299 fi->fi_lw.lw_item = item->li_next;
3300 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3301 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3302 }
3303 return result;
3304}
3305
3306/*
3307 * Free the structure used to store info used by ":for".
3308 */
3309 void
3310free_for_info(fi_void)
3311 void *fi_void;
3312{
Bram Moolenaar33570922005-01-25 22:26:29 +00003313 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003315 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003316 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003318 list_unref(fi->fi_list);
3319 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320 vim_free(fi);
3321}
3322
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3324
3325 void
3326set_context_for_expression(xp, arg, cmdidx)
3327 expand_T *xp;
3328 char_u *arg;
3329 cmdidx_T cmdidx;
3330{
3331 int got_eq = FALSE;
3332 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335 if (cmdidx == CMD_let)
3336 {
3337 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003338 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 {
3340 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003341 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 {
3343 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003344 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 if (vim_iswhite(*p))
3346 break;
3347 }
3348 return;
3349 }
3350 }
3351 else
3352 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3353 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 while ((xp->xp_pattern = vim_strpbrk(arg,
3355 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3356 {
3357 c = *xp->xp_pattern;
3358 if (c == '&')
3359 {
3360 c = xp->xp_pattern[1];
3361 if (c == '&')
3362 {
3363 ++xp->xp_pattern;
3364 xp->xp_context = cmdidx != CMD_let || got_eq
3365 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3366 }
3367 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003370 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3371 xp->xp_pattern += 2;
3372
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375 else if (c == '$')
3376 {
3377 /* environment variable */
3378 xp->xp_context = EXPAND_ENV_VARS;
3379 }
3380 else if (c == '=')
3381 {
3382 got_eq = TRUE;
3383 xp->xp_context = EXPAND_EXPRESSION;
3384 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003385 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 && xp->xp_context == EXPAND_FUNCTIONS
3387 && vim_strchr(xp->xp_pattern, '(') == NULL)
3388 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003389 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 break;
3391 }
3392 else if (cmdidx != CMD_let || got_eq)
3393 {
3394 if (c == '"') /* string */
3395 {
3396 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3397 if (c == '\\' && xp->xp_pattern[1] != NUL)
3398 ++xp->xp_pattern;
3399 xp->xp_context = EXPAND_NOTHING;
3400 }
3401 else if (c == '\'') /* literal string */
3402 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003403 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3405 /* skip */ ;
3406 xp->xp_context = EXPAND_NOTHING;
3407 }
3408 else if (c == '|')
3409 {
3410 if (xp->xp_pattern[1] == '|')
3411 {
3412 ++xp->xp_pattern;
3413 xp->xp_context = EXPAND_EXPRESSION;
3414 }
3415 else
3416 xp->xp_context = EXPAND_COMMANDS;
3417 }
3418 else
3419 xp->xp_context = EXPAND_EXPRESSION;
3420 }
3421 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003422 /* Doesn't look like something valid, expand as an expression
3423 * anyway. */
3424 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 arg = xp->xp_pattern;
3426 if (*arg != NUL)
3427 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3428 /* skip */ ;
3429 }
3430 xp->xp_pattern = arg;
3431}
3432
3433#endif /* FEAT_CMDL_COMPL */
3434
3435/*
3436 * ":1,25call func(arg1, arg2)" function call.
3437 */
3438 void
3439ex_call(eap)
3440 exarg_T *eap;
3441{
3442 char_u *arg = eap->arg;
3443 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003445 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003447 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 linenr_T lnum;
3449 int doesrange;
3450 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003453 if (eap->skip)
3454 {
3455 /* trans_function_name() doesn't work well when skipping, use eval0()
3456 * instead to skip to any following command, e.g. for:
3457 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003458 ++emsg_skip;
3459 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3460 clear_tv(&rettv);
3461 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003462 return;
3463 }
3464
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003465 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003466 if (fudi.fd_newkey != NULL)
3467 {
3468 /* Still need to give an error message for missing key. */
3469 EMSG2(_(e_dictkey), fudi.fd_newkey);
3470 vim_free(fudi.fd_newkey);
3471 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003472 if (tofree == NULL)
3473 return;
3474
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003475 /* Increase refcount on dictionary, it could get deleted when evaluating
3476 * the arguments. */
3477 if (fudi.fd_dict != NULL)
3478 ++fudi.fd_dict->dv_refcount;
3479
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003480 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003481 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003482 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaar532c7802005-01-27 14:44:31 +00003484 /* Skip white space to allow ":call func ()". Not good, but required for
3485 * backward compatibility. */
3486 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 if (*startarg != '(')
3490 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003491 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 goto end;
3493 }
3494
3495 /*
3496 * When skipping, evaluate the function once, to find the end of the
3497 * arguments.
3498 * When the function takes a range, this is discovered after the first
3499 * call, and the loop is broken.
3500 */
3501 if (eap->skip)
3502 {
3503 ++emsg_skip;
3504 lnum = eap->line2; /* do it once, also with an invalid range */
3505 }
3506 else
3507 lnum = eap->line1;
3508 for ( ; lnum <= eap->line2; ++lnum)
3509 {
3510 if (!eap->skip && eap->addr_count > 0)
3511 {
3512 curwin->w_cursor.lnum = lnum;
3513 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003514#ifdef FEAT_VIRTUALEDIT
3515 curwin->w_cursor.coladd = 0;
3516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003519 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003520 eap->line1, eap->line2, &doesrange,
3521 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
3523 failed = TRUE;
3524 break;
3525 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003526
3527 /* Handle a function returning a Funcref, Dictionary or List. */
3528 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3529 {
3530 failed = TRUE;
3531 break;
3532 }
3533
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (doesrange || eap->skip)
3536 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003537
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003539 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003541 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (aborting())
3543 break;
3544 }
3545 if (eap->skip)
3546 --emsg_skip;
3547
3548 if (!failed)
3549 {
3550 /* Check for trailing illegal characters and a following command. */
3551 if (!ends_excmd(*arg))
3552 {
3553 emsg_severe = TRUE;
3554 EMSG(_(e_trailing));
3555 }
3556 else
3557 eap->nextcmd = check_nextcmd(arg);
3558 }
3559
3560end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003561 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003562 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563}
3564
3565/*
3566 * ":unlet[!] var1 ... " command.
3567 */
3568 void
3569ex_unlet(eap)
3570 exarg_T *eap;
3571{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 ex_unletlock(eap, eap->arg, 0);
3573}
3574
3575/*
3576 * ":lockvar" and ":unlockvar" commands
3577 */
3578 void
3579ex_lockvar(eap)
3580 exarg_T *eap;
3581{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 int deep = 2;
3584
3585 if (eap->forceit)
3586 deep = -1;
3587 else if (vim_isdigit(*arg))
3588 {
3589 deep = getdigits(&arg);
3590 arg = skipwhite(arg);
3591 }
3592
3593 ex_unletlock(eap, arg, deep);
3594}
3595
3596/*
3597 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3598 */
3599 static void
3600ex_unletlock(eap, argstart, deep)
3601 exarg_T *eap;
3602 char_u *argstart;
3603 int deep;
3604{
3605 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003608 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
3610 do
3611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003613 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003614 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 if (lv.ll_name == NULL)
3616 error = TRUE; /* error but continue parsing */
3617 if (name_end == NULL || (!vim_iswhite(*name_end)
3618 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 if (name_end != NULL)
3621 {
3622 emsg_severe = TRUE;
3623 EMSG(_(e_trailing));
3624 }
3625 if (!(eap->skip || error))
3626 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 break;
3628 }
3629
3630 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003631 {
3632 if (eap->cmdidx == CMD_unlet)
3633 {
3634 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3635 error = TRUE;
3636 }
3637 else
3638 {
3639 if (do_lock_var(&lv, name_end, deep,
3640 eap->cmdidx == CMD_lockvar) == FAIL)
3641 error = TRUE;
3642 }
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (!eap->skip)
3646 clear_lval(&lv);
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 arg = skipwhite(name_end);
3649 } while (!ends_excmd(*arg));
3650
3651 eap->nextcmd = check_nextcmd(arg);
3652}
3653
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003656 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 int forceit;
3659{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 int ret = OK;
3661 int cc;
3662
3663 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 cc = *name_end;
3666 *name_end = NUL;
3667
3668 /* Normal name or expanded name. */
3669 if (check_changedtick(lp->ll_name))
3670 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003671 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003674 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003675 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003676 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003677 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003678 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 else if (lp->ll_range)
3681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003683 listitem_T *ll_li = lp->ll_li;
3684 int ll_n1 = lp->ll_n1;
3685
3686 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3687 {
3688 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003689 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 return FAIL;
3691 ll_li = li;
3692 ++ll_n1;
3693 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694
3695 /* Delete a range of List items. */
3696 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3697 {
3698 li = lp->ll_li->li_next;
3699 listitem_remove(lp->ll_list, lp->ll_li);
3700 lp->ll_li = li;
3701 ++lp->ll_n1;
3702 }
3703 }
3704 else
3705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 /* unlet a List item. */
3708 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003711 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 }
3713
3714 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003715}
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717/*
3718 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003719 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 */
3721 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003724 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
Bram Moolenaar33570922005-01-25 22:26:29 +00003726 hashtab_T *ht;
3727 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003728 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003729 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003730 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
Bram Moolenaar33570922005-01-25 22:26:29 +00003732 ht = find_var_ht(name, &varname);
3733 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003735 if (ht == &globvarht)
3736 d = &globvardict;
3737 else if (current_funccal != NULL
3738 && ht == &current_funccal->l_vars.dv_hashtab)
3739 d = &current_funccal->l_vars;
3740 else if (ht == &compat_hashtab)
3741 d = &vimvardict;
3742 else
3743 {
3744 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3745 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3746 }
3747 if (d == NULL)
3748 {
3749 EMSG2(_(e_intern2), "do_unlet()");
3750 return FAIL;
3751 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003752 hi = hash_find(ht, varname);
3753 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003754 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003755 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003756 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003757 || var_check_ro(di->di_flags, name, FALSE)
3758 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003759 return FAIL;
3760
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003761 delete_var(ht, hi);
3762 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003765 if (forceit)
3766 return OK;
3767 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return FAIL;
3769}
3770
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771/*
3772 * Lock or unlock variable indicated by "lp".
3773 * "deep" is the levels to go (-1 for unlimited);
3774 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3775 */
3776 static int
3777do_lock_var(lp, name_end, deep, lock)
3778 lval_T *lp;
3779 char_u *name_end;
3780 int deep;
3781 int lock;
3782{
3783 int ret = OK;
3784 int cc;
3785 dictitem_T *di;
3786
3787 if (deep == 0) /* nothing to do */
3788 return OK;
3789
3790 if (lp->ll_tv == NULL)
3791 {
3792 cc = *name_end;
3793 *name_end = NUL;
3794
3795 /* Normal name or expanded name. */
3796 if (check_changedtick(lp->ll_name))
3797 ret = FAIL;
3798 else
3799 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003800 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801 if (di == NULL)
3802 ret = FAIL;
3803 else
3804 {
3805 if (lock)
3806 di->di_flags |= DI_FLAGS_LOCK;
3807 else
3808 di->di_flags &= ~DI_FLAGS_LOCK;
3809 item_lock(&di->di_tv, deep, lock);
3810 }
3811 }
3812 *name_end = cc;
3813 }
3814 else if (lp->ll_range)
3815 {
3816 listitem_T *li = lp->ll_li;
3817
3818 /* (un)lock a range of List items. */
3819 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3820 {
3821 item_lock(&li->li_tv, deep, lock);
3822 li = li->li_next;
3823 ++lp->ll_n1;
3824 }
3825 }
3826 else if (lp->ll_list != NULL)
3827 /* (un)lock a List item. */
3828 item_lock(&lp->ll_li->li_tv, deep, lock);
3829 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003830 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003831 item_lock(&lp->ll_di->di_tv, deep, lock);
3832
3833 return ret;
3834}
3835
3836/*
3837 * Lock or unlock an item. "deep" is nr of levels to go.
3838 */
3839 static void
3840item_lock(tv, deep, lock)
3841 typval_T *tv;
3842 int deep;
3843 int lock;
3844{
3845 static int recurse = 0;
3846 list_T *l;
3847 listitem_T *li;
3848 dict_T *d;
3849 hashitem_T *hi;
3850 int todo;
3851
3852 if (recurse >= DICT_MAXNEST)
3853 {
3854 EMSG(_("E743: variable nested too deep for (un)lock"));
3855 return;
3856 }
3857 if (deep == 0)
3858 return;
3859 ++recurse;
3860
3861 /* lock/unlock the item itself */
3862 if (lock)
3863 tv->v_lock |= VAR_LOCKED;
3864 else
3865 tv->v_lock &= ~VAR_LOCKED;
3866
3867 switch (tv->v_type)
3868 {
3869 case VAR_LIST:
3870 if ((l = tv->vval.v_list) != NULL)
3871 {
3872 if (lock)
3873 l->lv_lock |= VAR_LOCKED;
3874 else
3875 l->lv_lock &= ~VAR_LOCKED;
3876 if (deep < 0 || deep > 1)
3877 /* recursive: lock/unlock the items the List contains */
3878 for (li = l->lv_first; li != NULL; li = li->li_next)
3879 item_lock(&li->li_tv, deep - 1, lock);
3880 }
3881 break;
3882 case VAR_DICT:
3883 if ((d = tv->vval.v_dict) != NULL)
3884 {
3885 if (lock)
3886 d->dv_lock |= VAR_LOCKED;
3887 else
3888 d->dv_lock &= ~VAR_LOCKED;
3889 if (deep < 0 || deep > 1)
3890 {
3891 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003892 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003893 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3894 {
3895 if (!HASHITEM_EMPTY(hi))
3896 {
3897 --todo;
3898 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3899 }
3900 }
3901 }
3902 }
3903 }
3904 --recurse;
3905}
3906
Bram Moolenaara40058a2005-07-11 22:42:07 +00003907/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003908 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3909 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003910 */
3911 static int
3912tv_islocked(tv)
3913 typval_T *tv;
3914{
3915 return (tv->v_lock & VAR_LOCKED)
3916 || (tv->v_type == VAR_LIST
3917 && tv->vval.v_list != NULL
3918 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3919 || (tv->v_type == VAR_DICT
3920 && tv->vval.v_dict != NULL
3921 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3922}
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3925/*
3926 * Delete all "menutrans_" variables.
3927 */
3928 void
3929del_menutrans_vars()
3930{
Bram Moolenaar33570922005-01-25 22:26:29 +00003931 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003935 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 {
3938 if (!HASHITEM_EMPTY(hi))
3939 {
3940 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3942 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 }
3944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946}
3947#endif
3948
3949#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3950
3951/*
3952 * Local string buffer for the next two functions to store a variable name
3953 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3954 * get_user_var_name().
3955 */
3956
3957static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3958
3959static char_u *varnamebuf = NULL;
3960static int varnamebuflen = 0;
3961
3962/*
3963 * Function to concatenate a prefix and a variable name.
3964 */
3965 static char_u *
3966cat_prefix_varname(prefix, name)
3967 int prefix;
3968 char_u *name;
3969{
3970 int len;
3971
3972 len = (int)STRLEN(name) + 3;
3973 if (len > varnamebuflen)
3974 {
3975 vim_free(varnamebuf);
3976 len += 10; /* some additional space */
3977 varnamebuf = alloc(len);
3978 if (varnamebuf == NULL)
3979 {
3980 varnamebuflen = 0;
3981 return NULL;
3982 }
3983 varnamebuflen = len;
3984 }
3985 *varnamebuf = prefix;
3986 varnamebuf[1] = ':';
3987 STRCPY(varnamebuf + 2, name);
3988 return varnamebuf;
3989}
3990
3991/*
3992 * Function given to ExpandGeneric() to obtain the list of user defined
3993 * (global/buffer/window/built-in) variable names.
3994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 char_u *
3996get_user_var_name(xp, idx)
3997 expand_T *xp;
3998 int idx;
3999{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004000 static long_u gdone;
4001 static long_u bdone;
4002 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004003#ifdef FEAT_WINDOWS
4004 static long_u tdone;
4005#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 static int vidx;
4007 static hashitem_T *hi;
4008 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004013#ifdef FEAT_WINDOWS
4014 tdone = 0;
4015#endif
4016 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004017
4018 /* Global variables */
4019 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004022 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004023 else
4024 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 while (HASHITEM_EMPTY(hi))
4026 ++hi;
4027 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4028 return cat_prefix_varname('g', hi->hi_key);
4029 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004031
4032 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004033 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004038 else
4039 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004040 while (HASHITEM_EMPTY(hi))
4041 ++hi;
4042 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 return (char_u *)"b:changedtick";
4048 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004049
4050 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004051 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004054 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 else
4057 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 while (HASHITEM_EMPTY(hi))
4059 ++hi;
4060 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063#ifdef FEAT_WINDOWS
4064 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004065 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066 if (tdone < ht->ht_used)
4067 {
4068 if (tdone++ == 0)
4069 hi = ht->ht_array;
4070 else
4071 ++hi;
4072 while (HASHITEM_EMPTY(hi))
4073 ++hi;
4074 return cat_prefix_varname('t', hi->hi_key);
4075 }
4076#endif
4077
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 /* v: variables */
4079 if (vidx < VV_LEN)
4080 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
4082 vim_free(varnamebuf);
4083 varnamebuf = NULL;
4084 varnamebuflen = 0;
4085 return NULL;
4086}
4087
4088#endif /* FEAT_CMDL_COMPL */
4089
4090/*
4091 * types for expressions.
4092 */
4093typedef enum
4094{
4095 TYPE_UNKNOWN = 0
4096 , TYPE_EQUAL /* == */
4097 , TYPE_NEQUAL /* != */
4098 , TYPE_GREATER /* > */
4099 , TYPE_GEQUAL /* >= */
4100 , TYPE_SMALLER /* < */
4101 , TYPE_SEQUAL /* <= */
4102 , TYPE_MATCH /* =~ */
4103 , TYPE_NOMATCH /* !~ */
4104} exptype_T;
4105
4106/*
4107 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4110 */
4111
4112/*
4113 * Handle zero level expression.
4114 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004116 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * Return OK or FAIL.
4118 */
4119 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 char_u **nextcmd;
4124 int evaluate;
4125{
4126 int ret;
4127 char_u *p;
4128
4129 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if (ret == FAIL || !ends_excmd(*p))
4132 {
4133 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 /*
4136 * Report the invalid expression unless the expression evaluation has
4137 * been cancelled due to an aborting error, an interrupt, or an
4138 * exception.
4139 */
4140 if (!aborting())
4141 EMSG2(_(e_invexpr2), arg);
4142 ret = FAIL;
4143 }
4144 if (nextcmd != NULL)
4145 *nextcmd = check_nextcmd(p);
4146
4147 return ret;
4148}
4149
4150/*
4151 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004152 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 *
4154 * "arg" must point to the first non-white of the expression.
4155 * "arg" is advanced to the next non-white after the recognized expression.
4156 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004157 * Note: "rettv.v_lock" is not set.
4158 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 * Return OK or FAIL.
4160 */
4161 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 int evaluate;
4166{
4167 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 if ((*arg)[0] == '?')
4177 {
4178 result = FALSE;
4179 if (evaluate)
4180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 int error = FALSE;
4182
4183 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (error)
4187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189
4190 /*
4191 * Get the second variable.
4192 */
4193 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 return FAIL;
4196
4197 /*
4198 * Check for the ":".
4199 */
4200 if ((*arg)[0] != ':')
4201 {
4202 EMSG(_("E109: Missing ':' after '?'"));
4203 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206 }
4207
4208 /*
4209 * Get the third variable.
4210 */
4211 *arg = skipwhite(*arg + 1);
4212 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4213 {
4214 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FAIL;
4217 }
4218 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221
4222 return OK;
4223}
4224
4225/*
4226 * Handle first level expression:
4227 * expr2 || expr2 || expr2 logical OR
4228 *
4229 * "arg" must point to the first non-white of the expression.
4230 * "arg" is advanced to the next non-white after the recognized expression.
4231 *
4232 * Return OK or FAIL.
4233 */
4234 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 int evaluate;
4239{
Bram Moolenaar33570922005-01-25 22:26:29 +00004240 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 long result;
4242 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * Get the first variable.
4247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
4250
4251 /*
4252 * Repeat until there is no following "||".
4253 */
4254 first = TRUE;
4255 result = FALSE;
4256 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4257 {
4258 if (evaluate && first)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (error)
4264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 first = FALSE;
4266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 2);
4272 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4273 return FAIL;
4274
4275 /*
4276 * Compute the result.
4277 */
4278 if (evaluate && !result)
4279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 if (evaluate)
4287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 rettv->v_type = VAR_NUMBER;
4289 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
4297 * Handle second level expression:
4298 * expr3 && expr3 && expr3 logical AND
4299 *
4300 * "arg" must point to the first non-white of the expression.
4301 * "arg" is advanced to the next non-white after the recognized expression.
4302 *
4303 * Return OK or FAIL.
4304 */
4305 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004306eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 int evaluate;
4310{
Bram Moolenaar33570922005-01-25 22:26:29 +00004311 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 long result;
4313 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 /*
4317 * Get the first variable.
4318 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 return FAIL;
4321
4322 /*
4323 * Repeat until there is no following "&&".
4324 */
4325 first = TRUE;
4326 result = TRUE;
4327 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4328 {
4329 if (evaluate && first)
4330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004333 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (error)
4335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 first = FALSE;
4337 }
4338
4339 /*
4340 * Get the second variable.
4341 */
4342 *arg = skipwhite(*arg + 2);
4343 if (eval4(arg, &var2, evaluate && result) == FAIL)
4344 return FAIL;
4345
4346 /*
4347 * Compute the result.
4348 */
4349 if (evaluate && result)
4350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004353 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (error)
4355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 if (evaluate)
4358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 rettv->v_type = VAR_NUMBER;
4360 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 }
4363
4364 return OK;
4365}
4366
4367/*
4368 * Handle third level expression:
4369 * var1 == var2
4370 * var1 =~ var2
4371 * var1 != var2
4372 * var1 !~ var2
4373 * var1 > var2
4374 * var1 >= var2
4375 * var1 < var2
4376 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004377 * var1 is var2
4378 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 *
4380 * "arg" must point to the first non-white of the expression.
4381 * "arg" is advanced to the next non-white after the recognized expression.
4382 *
4383 * Return OK or FAIL.
4384 */
4385 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 int evaluate;
4390{
Bram Moolenaar33570922005-01-25 22:26:29 +00004391 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 char_u *p;
4393 int i;
4394 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 int len = 2;
4397 long n1, n2;
4398 char_u *s1, *s2;
4399 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4400 regmatch_T regmatch;
4401 int ic;
4402 char_u *save_cpo;
4403
4404 /*
4405 * Get the first variable.
4406 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409
4410 p = *arg;
4411 switch (p[0])
4412 {
4413 case '=': if (p[1] == '=')
4414 type = TYPE_EQUAL;
4415 else if (p[1] == '~')
4416 type = TYPE_MATCH;
4417 break;
4418 case '!': if (p[1] == '=')
4419 type = TYPE_NEQUAL;
4420 else if (p[1] == '~')
4421 type = TYPE_NOMATCH;
4422 break;
4423 case '>': if (p[1] != '=')
4424 {
4425 type = TYPE_GREATER;
4426 len = 1;
4427 }
4428 else
4429 type = TYPE_GEQUAL;
4430 break;
4431 case '<': if (p[1] != '=')
4432 {
4433 type = TYPE_SMALLER;
4434 len = 1;
4435 }
4436 else
4437 type = TYPE_SEQUAL;
4438 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 case 'i': if (p[1] == 's')
4440 {
4441 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4442 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004443 i = p[len];
4444 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 {
4446 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4447 type_is = TRUE;
4448 }
4449 }
4450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452
4453 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004454 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
4456 if (type != TYPE_UNKNOWN)
4457 {
4458 /* extra question mark appended: ignore case */
4459 if (p[len] == '?')
4460 {
4461 ic = TRUE;
4462 ++len;
4463 }
4464 /* extra '#' appended: match case */
4465 else if (p[len] == '#')
4466 {
4467 ic = FALSE;
4468 ++len;
4469 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004470 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 else
4472 ic = p_ic;
4473
4474 /*
4475 * Get the second variable.
4476 */
4477 *arg = skipwhite(p + len);
4478 if (eval5(arg, &var2, evaluate) == FAIL)
4479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 return FAIL;
4482 }
4483
4484 if (evaluate)
4485 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 if (type_is && rettv->v_type != var2.v_type)
4487 {
4488 /* For "is" a different type always means FALSE, for "notis"
4489 * it means TRUE. */
4490 n1 = (type == TYPE_NEQUAL);
4491 }
4492 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4493 {
4494 if (type_is)
4495 {
4496 n1 = (rettv->v_type == var2.v_type
4497 && rettv->vval.v_list == var2.vval.v_list);
4498 if (type == TYPE_NEQUAL)
4499 n1 = !n1;
4500 }
4501 else if (rettv->v_type != var2.v_type
4502 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4503 {
4504 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004505 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004507 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 clear_tv(rettv);
4509 clear_tv(&var2);
4510 return FAIL;
4511 }
4512 else
4513 {
4514 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004515 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4516 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004517 if (type == TYPE_NEQUAL)
4518 n1 = !n1;
4519 }
4520 }
4521
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004522 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4523 {
4524 if (type_is)
4525 {
4526 n1 = (rettv->v_type == var2.v_type
4527 && rettv->vval.v_dict == var2.vval.v_dict);
4528 if (type == TYPE_NEQUAL)
4529 n1 = !n1;
4530 }
4531 else if (rettv->v_type != var2.v_type
4532 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4533 {
4534 if (rettv->v_type != var2.v_type)
4535 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4536 else
4537 EMSG(_("E736: Invalid operation for Dictionary"));
4538 clear_tv(rettv);
4539 clear_tv(&var2);
4540 return FAIL;
4541 }
4542 else
4543 {
4544 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004545 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4546 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004547 if (type == TYPE_NEQUAL)
4548 n1 = !n1;
4549 }
4550 }
4551
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4553 {
4554 if (rettv->v_type != var2.v_type
4555 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4556 {
4557 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004558 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004560 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
4565 else
4566 {
4567 /* Compare two Funcrefs for being equal or unequal. */
4568 if (rettv->vval.v_string == NULL
4569 || var2.vval.v_string == NULL)
4570 n1 = FALSE;
4571 else
4572 n1 = STRCMP(rettv->vval.v_string,
4573 var2.vval.v_string) == 0;
4574 if (type == TYPE_NEQUAL)
4575 n1 = !n1;
4576 }
4577 }
4578
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004579#ifdef FEAT_FLOAT
4580 /*
4581 * If one of the two variables is a float, compare as a float.
4582 * When using "=~" or "!~", always compare as string.
4583 */
4584 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4585 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4586 {
4587 float_T f1, f2;
4588
4589 if (rettv->v_type == VAR_FLOAT)
4590 f1 = rettv->vval.v_float;
4591 else
4592 f1 = get_tv_number(rettv);
4593 if (var2.v_type == VAR_FLOAT)
4594 f2 = var2.vval.v_float;
4595 else
4596 f2 = get_tv_number(&var2);
4597 n1 = FALSE;
4598 switch (type)
4599 {
4600 case TYPE_EQUAL: n1 = (f1 == f2); break;
4601 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4602 case TYPE_GREATER: n1 = (f1 > f2); break;
4603 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4604 case TYPE_SMALLER: n1 = (f1 < f2); break;
4605 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4606 case TYPE_UNKNOWN:
4607 case TYPE_MATCH:
4608 case TYPE_NOMATCH: break; /* avoid gcc warning */
4609 }
4610 }
4611#endif
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
4614 * If one of the two variables is a number, compare as a number.
4615 * When using "=~" or "!~", always compare as string.
4616 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004617 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 n1 = get_tv_number(rettv);
4621 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 switch (type)
4623 {
4624 case TYPE_EQUAL: n1 = (n1 == n2); break;
4625 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4626 case TYPE_GREATER: n1 = (n1 > n2); break;
4627 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4628 case TYPE_SMALLER: n1 = (n1 < n2); break;
4629 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4630 case TYPE_UNKNOWN:
4631 case TYPE_MATCH:
4632 case TYPE_NOMATCH: break; /* avoid gcc warning */
4633 }
4634 }
4635 else
4636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 s1 = get_tv_string_buf(rettv, buf1);
4638 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4640 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4641 else
4642 i = 0;
4643 n1 = FALSE;
4644 switch (type)
4645 {
4646 case TYPE_EQUAL: n1 = (i == 0); break;
4647 case TYPE_NEQUAL: n1 = (i != 0); break;
4648 case TYPE_GREATER: n1 = (i > 0); break;
4649 case TYPE_GEQUAL: n1 = (i >= 0); break;
4650 case TYPE_SMALLER: n1 = (i < 0); break;
4651 case TYPE_SEQUAL: n1 = (i <= 0); break;
4652
4653 case TYPE_MATCH:
4654 case TYPE_NOMATCH:
4655 /* avoid 'l' flag in 'cpoptions' */
4656 save_cpo = p_cpo;
4657 p_cpo = (char_u *)"";
4658 regmatch.regprog = vim_regcomp(s2,
4659 RE_MAGIC + RE_STRING);
4660 regmatch.rm_ic = ic;
4661 if (regmatch.regprog != NULL)
4662 {
4663 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004664 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (type == TYPE_NOMATCH)
4666 n1 = !n1;
4667 }
4668 p_cpo = save_cpo;
4669 break;
4670
4671 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4672 }
4673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 rettv->v_type = VAR_NUMBER;
4677 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 }
4680
4681 return OK;
4682}
4683
4684/*
4685 * Handle fourth level expression:
4686 * + number addition
4687 * - number subtraction
4688 * . string concatenation
4689 *
4690 * "arg" must point to the first non-white of the expression.
4691 * "arg" is advanced to the next non-white after the recognized expression.
4692 *
4693 * Return OK or FAIL.
4694 */
4695 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 int evaluate;
4700{
Bram Moolenaar33570922005-01-25 22:26:29 +00004701 typval_T var2;
4702 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 int op;
4704 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705#ifdef FEAT_FLOAT
4706 float_T f1 = 0, f2 = 0;
4707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 char_u *s1, *s2;
4709 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4710 char_u *p;
4711
4712 /*
4713 * Get the first variable.
4714 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004715 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return FAIL;
4717
4718 /*
4719 * Repeat computing, until no '+', '-' or '.' is following.
4720 */
4721 for (;;)
4722 {
4723 op = **arg;
4724 if (op != '+' && op != '-' && op != '.')
4725 break;
4726
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004727 if ((op != '+' || rettv->v_type != VAR_LIST)
4728#ifdef FEAT_FLOAT
4729 && (op == '.' || rettv->v_type != VAR_FLOAT)
4730#endif
4731 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 {
4733 /* For "list + ...", an illegal use of the first operand as
4734 * a number cannot be determined before evaluating the 2nd
4735 * operand: if this is also a list, all is ok.
4736 * For "something . ...", "something - ..." or "non-list + ...",
4737 * we know that the first operand needs to be a string or number
4738 * without evaluating the 2nd operand. So check before to avoid
4739 * side effects after an error. */
4740 if (evaluate && get_tv_string_chk(rettv) == NULL)
4741 {
4742 clear_tv(rettv);
4743 return FAIL;
4744 }
4745 }
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 /*
4748 * Get the second variable.
4749 */
4750 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004751 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004753 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 return FAIL;
4755 }
4756
4757 if (evaluate)
4758 {
4759 /*
4760 * Compute the result.
4761 */
4762 if (op == '.')
4763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4765 s2 = get_tv_string_buf_chk(&var2, buf2);
4766 if (s2 == NULL) /* type error ? */
4767 {
4768 clear_tv(rettv);
4769 clear_tv(&var2);
4770 return FAIL;
4771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004772 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 clear_tv(rettv);
4774 rettv->v_type = VAR_STRING;
4775 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004777 else if (op == '+' && rettv->v_type == VAR_LIST
4778 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004779 {
4780 /* concatenate Lists */
4781 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4782 &var3) == FAIL)
4783 {
4784 clear_tv(rettv);
4785 clear_tv(&var2);
4786 return FAIL;
4787 }
4788 clear_tv(rettv);
4789 *rettv = var3;
4790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else
4792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 int error = FALSE;
4794
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795#ifdef FEAT_FLOAT
4796 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798 f1 = rettv->vval.v_float;
4799 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 else
4802#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004803 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804 n1 = get_tv_number_chk(rettv, &error);
4805 if (error)
4806 {
4807 /* This can only happen for "list + non-list". For
4808 * "non-list + ..." or "something - ...", we returned
4809 * before evaluating the 2nd operand. */
4810 clear_tv(rettv);
4811 return FAIL;
4812 }
4813#ifdef FEAT_FLOAT
4814 if (var2.v_type == VAR_FLOAT)
4815 f1 = n1;
4816#endif
4817 }
4818#ifdef FEAT_FLOAT
4819 if (var2.v_type == VAR_FLOAT)
4820 {
4821 f2 = var2.vval.v_float;
4822 n2 = 0;
4823 }
4824 else
4825#endif
4826 {
4827 n2 = get_tv_number_chk(&var2, &error);
4828 if (error)
4829 {
4830 clear_tv(rettv);
4831 clear_tv(&var2);
4832 return FAIL;
4833 }
4834#ifdef FEAT_FLOAT
4835 if (rettv->v_type == VAR_FLOAT)
4836 f2 = n2;
4837#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840
4841#ifdef FEAT_FLOAT
4842 /* If there is a float on either side the result is a float. */
4843 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4844 {
4845 if (op == '+')
4846 f1 = f1 + f2;
4847 else
4848 f1 = f1 - f2;
4849 rettv->v_type = VAR_FLOAT;
4850 rettv->vval.v_float = f1;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004853#endif
4854 {
4855 if (op == '+')
4856 n1 = n1 + n2;
4857 else
4858 n1 = n1 - n2;
4859 rettv->v_type = VAR_NUMBER;
4860 rettv->vval.v_number = n1;
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 }
4866 return OK;
4867}
4868
4869/*
4870 * Handle fifth level expression:
4871 * * number multiplication
4872 * / number division
4873 * % number modulo
4874 *
4875 * "arg" must point to the first non-white of the expression.
4876 * "arg" is advanced to the next non-white after the recognized expression.
4877 *
4878 * Return OK or FAIL.
4879 */
4880 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004881eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004885 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
Bram Moolenaar33570922005-01-25 22:26:29 +00004887 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 int op;
4889 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 int use_float = FALSE;
4892 float_T f1 = 0, f2;
4893#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004894 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 /*
4897 * Get the first variable.
4898 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004899 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 return FAIL;
4901
4902 /*
4903 * Repeat computing, until no '*', '/' or '%' is following.
4904 */
4905 for (;;)
4906 {
4907 op = **arg;
4908 if (op != '*' && op != '/' && op != '%')
4909 break;
4910
4911 if (evaluate)
4912 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913#ifdef FEAT_FLOAT
4914 if (rettv->v_type == VAR_FLOAT)
4915 {
4916 f1 = rettv->vval.v_float;
4917 use_float = TRUE;
4918 n1 = 0;
4919 }
4920 else
4921#endif
4922 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004924 if (error)
4925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927 else
4928 n1 = 0;
4929
4930 /*
4931 * Get the second variable.
4932 */
4933 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004934 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 return FAIL;
4936
4937 if (evaluate)
4938 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939#ifdef FEAT_FLOAT
4940 if (var2.v_type == VAR_FLOAT)
4941 {
4942 if (!use_float)
4943 {
4944 f1 = n1;
4945 use_float = TRUE;
4946 }
4947 f2 = var2.vval.v_float;
4948 n2 = 0;
4949 }
4950 else
4951#endif
4952 {
4953 n2 = get_tv_number_chk(&var2, &error);
4954 clear_tv(&var2);
4955 if (error)
4956 return FAIL;
4957#ifdef FEAT_FLOAT
4958 if (use_float)
4959 f2 = n2;
4960#endif
4961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963 /*
4964 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967#ifdef FEAT_FLOAT
4968 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 if (op == '*')
4971 f1 = f1 * f2;
4972 else if (op == '/')
4973 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974# ifdef VMS
4975 /* VMS crashes on divide by zero, work around it */
4976 if (f2 == 0.0)
4977 {
4978 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004979 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004980 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004981 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004983 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004984 }
4985 else
4986 f1 = f1 / f2;
4987# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 /* We rely on the floating point library to handle divide
4989 * by zero to result in "inf" and not a crash. */
4990 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004995 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 return FAIL;
4997 }
4998 rettv->v_type = VAR_FLOAT;
4999 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
5001 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 if (op == '*')
5005 n1 = n1 * n2;
5006 else if (op == '/')
5007 {
5008 if (n2 == 0) /* give an error message? */
5009 {
5010 if (n1 == 0)
5011 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5012 else if (n1 < 0)
5013 n1 = -0x7fffffffL;
5014 else
5015 n1 = 0x7fffffffL;
5016 }
5017 else
5018 n1 = n1 / n2;
5019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 {
5022 if (n2 == 0) /* give an error message? */
5023 n1 = 0;
5024 else
5025 n1 = n1 % n2;
5026 }
5027 rettv->v_type = VAR_NUMBER;
5028 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 }
5032
5033 return OK;
5034}
5035
5036/*
5037 * Handle sixth level expression:
5038 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005039 * "string" string constant
5040 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 * &option-name option value
5042 * @r register contents
5043 * identifier variable value
5044 * function() function call
5045 * $VAR environment variable
5046 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005047 * [expr, expr] List
5048 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 *
5050 * Also handle:
5051 * ! in front logical NOT
5052 * - in front unary minus
5053 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 * trailing [] subscript in String or List
5055 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *
5057 * "arg" must point to the first non-white of the expression.
5058 * "arg" is advanced to the next non-white after the recognized expression.
5059 *
5060 * Return OK or FAIL.
5061 */
5062 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005063eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005067 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 long n;
5070 int len;
5071 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 char_u *start_leader, *end_leader;
5073 int ret = OK;
5074 char_u *alias;
5075
5076 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
5082 /*
5083 * Skip '!' and '-' characters. They are handled later.
5084 */
5085 start_leader = *arg;
5086 while (**arg == '!' || **arg == '-' || **arg == '+')
5087 *arg = skipwhite(*arg + 1);
5088 end_leader = *arg;
5089
5090 switch (**arg)
5091 {
5092 /*
5093 * Number constant.
5094 */
5095 case '0':
5096 case '1':
5097 case '2':
5098 case '3':
5099 case '4':
5100 case '5':
5101 case '6':
5102 case '7':
5103 case '8':
5104 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 {
5106#ifdef FEAT_FLOAT
5107 char_u *p = skipdigits(*arg + 1);
5108 int get_float = FALSE;
5109
5110 /* We accept a float when the format matches
5111 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005112 * strict to avoid backwards compatibility problems.
5113 * Don't look for a float after the "." operator, so that
5114 * ":let vers = 1.2.3" doesn't fail. */
5115 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005117 get_float = TRUE;
5118 p = skipdigits(p + 2);
5119 if (*p == 'e' || *p == 'E')
5120 {
5121 ++p;
5122 if (*p == '-' || *p == '+')
5123 ++p;
5124 if (!vim_isdigit(*p))
5125 get_float = FALSE;
5126 else
5127 p = skipdigits(p + 1);
5128 }
5129 if (ASCII_ISALPHA(*p) || *p == '.')
5130 get_float = FALSE;
5131 }
5132 if (get_float)
5133 {
5134 float_T f;
5135
5136 *arg += string2float(*arg, &f);
5137 if (evaluate)
5138 {
5139 rettv->v_type = VAR_FLOAT;
5140 rettv->vval.v_float = f;
5141 }
5142 }
5143 else
5144#endif
5145 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005146 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 *arg += len;
5148 if (evaluate)
5149 {
5150 rettv->v_type = VAR_NUMBER;
5151 rettv->vval.v_number = n;
5152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 /*
5158 * String constant: "string".
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005166 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167 break;
5168
5169 /*
5170 * List: [expr, expr]
5171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 * Dictionary: {key: val, key: val}
5177 */
5178 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5179 break;
5180
5181 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005182 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005184 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 break;
5186
5187 /*
5188 * Environment variable: $VAR.
5189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 break;
5192
5193 /*
5194 * Register contents: @r.
5195 */
5196 case '@': ++*arg;
5197 if (evaluate)
5198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005200 rettv->vval.v_string = get_reg_contents(**arg,
5201 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203 if (**arg != NUL)
5204 ++*arg;
5205 break;
5206
5207 /*
5208 * nested expression: (expression).
5209 */
5210 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005211 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (**arg == ')')
5213 ++*arg;
5214 else if (ret == OK)
5215 {
5216 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 ret = FAIL;
5219 }
5220 break;
5221
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225
5226 if (ret == NOTDONE)
5227 {
5228 /*
5229 * Must be a variable or function name.
5230 * Can also be a curly-braces kind of name: {expr}.
5231 */
5232 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 if (alias != NULL)
5235 s = alias;
5236
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005237 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 ret = FAIL;
5239 else
5240 {
5241 if (**arg == '(') /* recursive! */
5242 {
5243 /* If "s" is the name of a variable of type VAR_FUNC
5244 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005245 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246
5247 /* Invoke the function. */
5248 ret = get_func_tv(s, len, rettv, arg,
5249 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005250 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005251
5252 /* If evaluate is FALSE rettv->v_type was not set in
5253 * get_func_tv, but it's needed in handle_subscript() to parse
5254 * what follows. So set it here. */
5255 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5256 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005257 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005258 rettv->v_type = VAR_FUNC;
5259 }
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 /* Stop the expression evaluation when immediately
5262 * aborting on error, or when an interrupt occurred or
5263 * an exception was thrown but not caught. */
5264 if (aborting())
5265 {
5266 if (ret == OK)
5267 clear_tv(rettv);
5268 ret = FAIL;
5269 }
5270 }
5271 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005272 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005273 else
5274 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005276 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 }
5278
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 *arg = skipwhite(*arg);
5280
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005281 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5282 * expr(expr). */
5283 if (ret == OK)
5284 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /*
5287 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5288 */
5289 if (ret == OK && evaluate && end_leader > start_leader)
5290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005292 int val = 0;
5293#ifdef FEAT_FLOAT
5294 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005296 if (rettv->v_type == VAR_FLOAT)
5297 f = rettv->vval.v_float;
5298 else
5299#endif
5300 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005301 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 clear_tv(rettv);
5304 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else
5307 {
5308 while (end_leader > start_leader)
5309 {
5310 --end_leader;
5311 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = !f;
5316 else
5317#endif
5318 val = !val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321 {
5322#ifdef FEAT_FLOAT
5323 if (rettv->v_type == VAR_FLOAT)
5324 f = -f;
5325 else
5326#endif
5327 val = -val;
5328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005330#ifdef FEAT_FLOAT
5331 if (rettv->v_type == VAR_FLOAT)
5332 {
5333 clear_tv(rettv);
5334 rettv->vval.v_float = f;
5335 }
5336 else
5337#endif
5338 {
5339 clear_tv(rettv);
5340 rettv->v_type = VAR_NUMBER;
5341 rettv->vval.v_number = val;
5342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345
5346 return ret;
5347}
5348
5349/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005350 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5351 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5353 */
5354 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005357 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005359 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360{
5361 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005362 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 long len = -1;
5365 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005369 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 if (verbose)
5372 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 return FAIL;
5374 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005375#ifdef FEAT_FLOAT
5376 else if (rettv->v_type == VAR_FLOAT)
5377 {
5378 if (verbose)
5379 EMSG(_(e_float_as_string));
5380 return FAIL;
5381 }
5382#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005384 init_tv(&var1);
5385 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 /*
5389 * dict.name
5390 */
5391 key = *arg + 1;
5392 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5393 ;
5394 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 }
5398 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 /*
5401 * something[idx]
5402 *
5403 * Get the (first) variable from inside the [].
5404 */
5405 *arg = skipwhite(*arg + 1);
5406 if (**arg == ':')
5407 empty1 = TRUE;
5408 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5409 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005410 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5411 {
5412 /* not a number or string */
5413 clear_tv(&var1);
5414 return FAIL;
5415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416
5417 /*
5418 * Get the second variable from inside the [:].
5419 */
5420 if (**arg == ':')
5421 {
5422 range = TRUE;
5423 *arg = skipwhite(*arg + 1);
5424 if (**arg == ']')
5425 empty2 = TRUE;
5426 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005428 if (!empty1)
5429 clear_tv(&var1);
5430 return FAIL;
5431 }
5432 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5433 {
5434 /* not a number or string */
5435 if (!empty1)
5436 clear_tv(&var1);
5437 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 return FAIL;
5439 }
5440 }
5441
5442 /* Check for the ']'. */
5443 if (**arg != ']')
5444 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005445 if (verbose)
5446 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 clear_tv(&var1);
5448 if (range)
5449 clear_tv(&var2);
5450 return FAIL;
5451 }
5452 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 }
5454
5455 if (evaluate)
5456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 n1 = 0;
5458 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 n1 = get_tv_number(&var1);
5461 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463 if (range)
5464 {
5465 if (empty2)
5466 n2 = -1;
5467 else
5468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 n2 = get_tv_number(&var2);
5470 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 }
5472 }
5473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 {
5476 case VAR_NUMBER:
5477 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 len = (long)STRLEN(s);
5480 if (range)
5481 {
5482 /* The resulting variable is a substring. If the indexes
5483 * are out of range the result is empty. */
5484 if (n1 < 0)
5485 {
5486 n1 = len + n1;
5487 if (n1 < 0)
5488 n1 = 0;
5489 }
5490 if (n2 < 0)
5491 n2 = len + n2;
5492 else if (n2 >= len)
5493 n2 = len;
5494 if (n1 >= len || n2 < 0 || n1 > n2)
5495 s = NULL;
5496 else
5497 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5498 }
5499 else
5500 {
5501 /* The resulting variable is a string of a single
5502 * character. If the index is too big or negative the
5503 * result is empty. */
5504 if (n1 >= len || n1 < 0)
5505 s = NULL;
5506 else
5507 s = vim_strnsave(s + n1, 1);
5508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005509 clear_tv(rettv);
5510 rettv->v_type = VAR_STRING;
5511 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 break;
5513
5514 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 if (n1 < 0)
5517 n1 = len + n1;
5518 if (!empty1 && (n1 < 0 || n1 >= len))
5519 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005520 /* For a range we allow invalid values and return an empty
5521 * list. A list index out of range is an error. */
5522 if (!range)
5523 {
5524 if (verbose)
5525 EMSGN(_(e_listidx), n1);
5526 return FAIL;
5527 }
5528 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 }
5530 if (range)
5531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 list_T *l;
5533 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534
5535 if (n2 < 0)
5536 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005537 else if (n2 >= len)
5538 n2 = len - 1;
5539 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005540 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 l = list_alloc();
5542 if (l == NULL)
5543 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 n1 <= n2; ++n1)
5546 {
5547 if (list_append_tv(l, &item->li_tv) == FAIL)
5548 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005549 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 return FAIL;
5551 }
5552 item = item->li_next;
5553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 clear_tv(rettv);
5555 rettv->v_type = VAR_LIST;
5556 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005557 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 }
5559 else
5560 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005561 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 clear_tv(rettv);
5563 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566
5567 case VAR_DICT:
5568 if (range)
5569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005570 if (verbose)
5571 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572 if (len == -1)
5573 clear_tv(&var1);
5574 return FAIL;
5575 }
5576 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005577 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578
5579 if (len == -1)
5580 {
5581 key = get_tv_string(&var1);
5582 if (*key == NUL)
5583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (verbose)
5585 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 clear_tv(&var1);
5587 return FAIL;
5588 }
5589 }
5590
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005591 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005593 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005594 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 if (len == -1)
5596 clear_tv(&var1);
5597 if (item == NULL)
5598 return FAIL;
5599
5600 copy_tv(&item->di_tv, &var1);
5601 clear_tv(rettv);
5602 *rettv = var1;
5603 }
5604 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 }
5606 }
5607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 return OK;
5609}
5610
5611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 * Get an option value.
5613 * "arg" points to the '&' or '+' before the option name.
5614 * "arg" is advanced to character after the option name.
5615 * Return OK or FAIL.
5616 */
5617 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 int evaluate;
5622{
5623 char_u *option_end;
5624 long numval;
5625 char_u *stringval;
5626 int opt_type;
5627 int c;
5628 int working = (**arg == '+'); /* has("+option") */
5629 int ret = OK;
5630 int opt_flags;
5631
5632 /*
5633 * Isolate the option name and find its value.
5634 */
5635 option_end = find_option_end(arg, &opt_flags);
5636 if (option_end == NULL)
5637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 EMSG2(_("E112: Option name missing: %s"), *arg);
5640 return FAIL;
5641 }
5642
5643 if (!evaluate)
5644 {
5645 *arg = option_end;
5646 return OK;
5647 }
5648
5649 c = *option_end;
5650 *option_end = NUL;
5651 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 if (opt_type == -3) /* invalid name */
5655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 EMSG2(_("E113: Unknown option: %s"), *arg);
5658 ret = FAIL;
5659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
5662 if (opt_type == -2) /* hidden string option */
5663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 rettv->v_type = VAR_STRING;
5665 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
5667 else if (opt_type == -1) /* hidden number option */
5668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 rettv->v_type = VAR_NUMBER;
5670 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672 else if (opt_type == 1) /* number option */
5673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 rettv->v_type = VAR_NUMBER;
5675 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 else /* string option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_STRING;
5680 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 }
5683 else if (working && (opt_type == -2 || opt_type == -1))
5684 ret = FAIL;
5685
5686 *option_end = c; /* put back for error messages */
5687 *arg = option_end;
5688
5689 return ret;
5690}
5691
5692/*
5693 * Allocate a variable for a string constant.
5694 * Return OK or FAIL.
5695 */
5696 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 int evaluate;
5701{
5702 char_u *p;
5703 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 int extra = 0;
5705
5706 /*
5707 * Find the end of the string, skipping backslashed characters.
5708 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
5711 if (*p == '\\' && p[1] != NUL)
5712 {
5713 ++p;
5714 /* A "\<x>" form occupies at least 4 characters, and produces up
5715 * to 6 characters: reserve space for 2 extra */
5716 if (*p == '<')
5717 extra += 2;
5718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720
5721 if (*p != '"')
5722 {
5723 EMSG2(_("E114: Missing quote: %s"), *arg);
5724 return FAIL;
5725 }
5726
5727 /* If only parsing, set *arg and return here */
5728 if (!evaluate)
5729 {
5730 *arg = p + 1;
5731 return OK;
5732 }
5733
5734 /*
5735 * Copy the string into allocated memory, handling backslashed
5736 * characters.
5737 */
5738 name = alloc((unsigned)(p - *arg + extra));
5739 if (name == NULL)
5740 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 rettv->v_type = VAR_STRING;
5742 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
5746 if (*p == '\\')
5747 {
5748 switch (*++p)
5749 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 case 'b': *name++ = BS; ++p; break;
5751 case 'e': *name++ = ESC; ++p; break;
5752 case 'f': *name++ = FF; ++p; break;
5753 case 'n': *name++ = NL; ++p; break;
5754 case 'r': *name++ = CAR; ++p; break;
5755 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
5757 case 'X': /* hex: "\x1", "\x12" */
5758 case 'x':
5759 case 'u': /* Unicode: "\u0023" */
5760 case 'U':
5761 if (vim_isxdigit(p[1]))
5762 {
5763 int n, nr;
5764 int c = toupper(*p);
5765
5766 if (c == 'X')
5767 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005768 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005770 else
5771 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 nr = 0;
5773 while (--n >= 0 && vim_isxdigit(p[1]))
5774 {
5775 ++p;
5776 nr = (nr << 4) + hex2nr(*p);
5777 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779#ifdef FEAT_MBYTE
5780 /* For "\u" store the number according to
5781 * 'encoding'. */
5782 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 else
5785#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 break;
5789
5790 /* octal: "\1", "\12", "\123" */
5791 case '0':
5792 case '1':
5793 case '2':
5794 case '3':
5795 case '4':
5796 case '5':
5797 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 case '7': *name = *p++ - '0';
5799 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 *name = (*name << 3) + *p++ - '0';
5802 if (*p >= '0' && *p <= '7')
5803 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 break;
5807
5808 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 if (extra != 0)
5811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814 }
5815 /* FALLTHROUGH */
5816
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819 }
5820 }
5821 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 *arg = p + 1;
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 return OK;
5829}
5830
5831/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 * Return OK or FAIL.
5834 */
5835 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005836get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 int evaluate;
5840{
5841 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 int reduce = 0;
5844
5845 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 */
5848 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 break;
5854 ++reduce;
5855 ++p;
5856 }
5857 }
5858
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 return FAIL;
5863 }
5864
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866 if (!evaluate)
5867 {
5868 *arg = p + 1;
5869 return OK;
5870 }
5871
5872 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 */
5875 str = alloc((unsigned)((p - *arg) - reduce));
5876 if (str == NULL)
5877 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 rettv->v_type = VAR_STRING;
5879 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 break;
5887 ++p;
5888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 *arg = p + 1;
5893
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 return OK;
5895}
5896
5897/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 * Allocate a variable for a List and fill it from "*arg".
5899 * Return OK or FAIL.
5900 */
5901 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005902get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 int evaluate;
5906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l = NULL;
5908 typval_T tv;
5909 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910
5911 if (evaluate)
5912 {
5913 l = list_alloc();
5914 if (l == NULL)
5915 return FAIL;
5916 }
5917
5918 *arg = skipwhite(*arg + 1);
5919 while (**arg != ']' && **arg != NUL)
5920 {
5921 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5922 goto failret;
5923 if (evaluate)
5924 {
5925 item = listitem_alloc();
5926 if (item != NULL)
5927 {
5928 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005929 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 list_append(l, item);
5931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005932 else
5933 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 }
5935
5936 if (**arg == ']')
5937 break;
5938 if (**arg != ',')
5939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 goto failret;
5942 }
5943 *arg = skipwhite(*arg + 1);
5944 }
5945
5946 if (**arg != ']')
5947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949failret:
5950 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005951 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 return FAIL;
5953 }
5954
5955 *arg = skipwhite(*arg + 1);
5956 if (evaluate)
5957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 rettv->v_type = VAR_LIST;
5959 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 ++l->lv_refcount;
5961 }
5962
5963 return OK;
5964}
5965
5966/*
5967 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005968 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005970 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971list_alloc()
5972{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005973 list_T *l;
5974
5975 l = (list_T *)alloc_clear(sizeof(list_T));
5976 if (l != NULL)
5977 {
5978 /* Prepend the list to the list of lists for garbage collection. */
5979 if (first_list != NULL)
5980 first_list->lv_used_prev = l;
5981 l->lv_used_prev = NULL;
5982 l->lv_used_next = first_list;
5983 first_list = l;
5984 }
5985 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005989 * Allocate an empty list for a return value.
5990 * Returns OK or FAIL.
5991 */
5992 static int
5993rettv_list_alloc(rettv)
5994 typval_T *rettv;
5995{
5996 list_T *l = list_alloc();
5997
5998 if (l == NULL)
5999 return FAIL;
6000
6001 rettv->vval.v_list = l;
6002 rettv->v_type = VAR_LIST;
6003 ++l->lv_refcount;
6004 return OK;
6005}
6006
6007/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008 * Unreference a list: decrement the reference count and free it when it
6009 * becomes zero.
6010 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006011 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006013 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015 if (l != NULL && --l->lv_refcount <= 0)
6016 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017}
6018
6019/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006020 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 * Ignores the reference count.
6022 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006023 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006024list_free(l, recurse)
6025 list_T *l;
6026 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006030 /* Remove the list from the list of lists for garbage collection. */
6031 if (l->lv_used_prev == NULL)
6032 first_list = l->lv_used_next;
6033 else
6034 l->lv_used_prev->lv_used_next = l->lv_used_next;
6035 if (l->lv_used_next != NULL)
6036 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6037
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040 /* Remove the item before deleting it. */
6041 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006042 if (recurse || (item->li_tv.v_type != VAR_LIST
6043 && item->li_tv.v_type != VAR_DICT))
6044 clear_tv(&item->li_tv);
6045 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 }
6047 vim_free(l);
6048}
6049
6050/*
6051 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006052 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006054 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055listitem_alloc()
6056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058}
6059
6060/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006061 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006063 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006067 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 vim_free(item);
6069}
6070
6071/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006072 * Remove a list item from a List and free it. Also clears the value.
6073 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006074 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 list_T *l;
6077 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006079 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 listitem_free(item);
6081}
6082
6083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 * Get the number of items in a list.
6085 */
6086 static long
6087list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 if (l == NULL)
6091 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006092 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093}
6094
6095/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 * Return TRUE when two lists have exactly the same values.
6097 */
6098 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 list_T *l1;
6101 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104{
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006107 if (l1 == NULL || l2 == NULL)
6108 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (l1 == l2)
6110 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006111 if (list_len(l1) != list_len(l2))
6112 return FALSE;
6113
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 for (item1 = l1->lv_first, item2 = l2->lv_first;
6115 item1 != NULL && item2 != NULL;
6116 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118 return FALSE;
6119 return item1 == NULL && item2 == NULL;
6120}
6121
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006122#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6123 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124/*
6125 * Return the dictitem that an entry in a hashtable points to.
6126 */
6127 dictitem_T *
6128dict_lookup(hi)
6129 hashitem_T *hi;
6130{
6131 return HI2DI(hi);
6132}
6133#endif
6134
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136 * Return TRUE when two dictionaries have exactly the same key/values.
6137 */
6138 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 dict_T *d1;
6141 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006142 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006143 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144{
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 hashitem_T *hi;
6146 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006149 if (d1 == NULL || d2 == NULL)
6150 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006151 if (d1 == d2)
6152 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006153 if (dict_len(d1) != dict_len(d2))
6154 return FALSE;
6155
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006156 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006157 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006158 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006159 if (!HASHITEM_EMPTY(hi))
6160 {
6161 item2 = dict_find(d2, hi->hi_key, -1);
6162 if (item2 == NULL)
6163 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006164 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 return FALSE;
6166 --todo;
6167 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006168 }
6169 return TRUE;
6170}
6171
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006172static int tv_equal_recurse_limit;
6173
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006174/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006176 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006177 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178 */
6179 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 typval_T *tv1;
6182 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 int ic; /* ignore case */
6184 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185{
6186 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006187 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006189 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006190
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006191 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006192 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006193
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006194 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 * recursiveness to a limit. We guess they are equal then.
6196 * A fixed limit has the problem of still taking an awful long time.
6197 * Reduce the limit every time running into it. That should work fine for
6198 * deeply linked structures that are not recursively linked and catch
6199 * recursiveness quickly. */
6200 if (!recursive)
6201 tv_equal_recurse_limit = 1000;
6202 if (recursive_cnt >= tv_equal_recurse_limit)
6203 {
6204 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006205 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207
6208 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006209 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006211 ++recursive_cnt;
6212 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6213 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006214 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006215
6216 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006217 ++recursive_cnt;
6218 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6219 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006220 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006221
6222 case VAR_FUNC:
6223 return (tv1->vval.v_string != NULL
6224 && tv2->vval.v_string != NULL
6225 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6226
6227 case VAR_NUMBER:
6228 return tv1->vval.v_number == tv2->vval.v_number;
6229
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006230#ifdef FEAT_FLOAT
6231 case VAR_FLOAT:
6232 return tv1->vval.v_float == tv2->vval.v_float;
6233#endif
6234
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006235 case VAR_STRING:
6236 s1 = get_tv_string_buf(tv1, buf1);
6237 s2 = get_tv_string_buf(tv2, buf2);
6238 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006240
6241 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 return TRUE;
6243}
6244
6245/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 * Locate item with index "n" in list "l" and return it.
6247 * A negative index is counted from the end; -1 is the last item.
6248 * Returns NULL when "n" is out of range.
6249 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006250 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 long n;
6254{
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 long idx;
6257
6258 if (l == NULL)
6259 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260
6261 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263 n = l->lv_len + n;
6264
6265 /* Check for index out of range. */
6266 if (n < 0 || n >= l->lv_len)
6267 return NULL;
6268
6269 /* When there is a cached index may start search from there. */
6270 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006272 if (n < l->lv_idx / 2)
6273 {
6274 /* closest to the start of the list */
6275 item = l->lv_first;
6276 idx = 0;
6277 }
6278 else if (n > (l->lv_idx + l->lv_len) / 2)
6279 {
6280 /* closest to the end of the list */
6281 item = l->lv_last;
6282 idx = l->lv_len - 1;
6283 }
6284 else
6285 {
6286 /* closest to the cached index */
6287 item = l->lv_idx_item;
6288 idx = l->lv_idx;
6289 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 }
6291 else
6292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 if (n < l->lv_len / 2)
6294 {
6295 /* closest to the start of the list */
6296 item = l->lv_first;
6297 idx = 0;
6298 }
6299 else
6300 {
6301 /* closest to the end of the list */
6302 item = l->lv_last;
6303 idx = l->lv_len - 1;
6304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306
6307 while (n > idx)
6308 {
6309 /* search forward */
6310 item = item->li_next;
6311 ++idx;
6312 }
6313 while (n < idx)
6314 {
6315 /* search backward */
6316 item = item->li_prev;
6317 --idx;
6318 }
6319
6320 /* cache the used index */
6321 l->lv_idx = idx;
6322 l->lv_idx_item = item;
6323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 return item;
6325}
6326
6327/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006328 * Get list item "l[idx]" as a number.
6329 */
6330 static long
6331list_find_nr(l, idx, errorp)
6332 list_T *l;
6333 long idx;
6334 int *errorp; /* set to TRUE when something wrong */
6335{
6336 listitem_T *li;
6337
6338 li = list_find(l, idx);
6339 if (li == NULL)
6340 {
6341 if (errorp != NULL)
6342 *errorp = TRUE;
6343 return -1L;
6344 }
6345 return get_tv_number_chk(&li->li_tv, errorp);
6346}
6347
6348/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006349 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6350 */
6351 char_u *
6352list_find_str(l, idx)
6353 list_T *l;
6354 long idx;
6355{
6356 listitem_T *li;
6357
6358 li = list_find(l, idx - 1);
6359 if (li == NULL)
6360 {
6361 EMSGN(_(e_listidx), idx);
6362 return NULL;
6363 }
6364 return get_tv_string(&li->li_tv);
6365}
6366
6367/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006368 * Locate "item" list "l" and return its index.
6369 * Returns -1 when "item" is not in the list.
6370 */
6371 static long
6372list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 list_T *l;
6374 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375{
6376 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378
6379 if (l == NULL)
6380 return -1;
6381 idx = 0;
6382 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6383 ++idx;
6384 if (li == NULL)
6385 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006386 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387}
6388
6389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 * Append item "item" to the end of list "l".
6391 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006392 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 list_T *l;
6395 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396{
6397 if (l->lv_last == NULL)
6398 {
6399 /* empty list */
6400 l->lv_first = item;
6401 l->lv_last = item;
6402 item->li_prev = NULL;
6403 }
6404 else
6405 {
6406 l->lv_last->li_next = item;
6407 item->li_prev = l->lv_last;
6408 l->lv_last = item;
6409 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006410 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 item->li_next = NULL;
6412}
6413
6414/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006416 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006418 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 list_T *l;
6421 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006427 copy_tv(tv, &li->li_tv);
6428 list_append(l, li);
6429 return OK;
6430}
6431
6432/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006433 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 * Return FAIL when out of memory.
6435 */
6436 int
6437list_append_dict(list, dict)
6438 list_T *list;
6439 dict_T *dict;
6440{
6441 listitem_T *li = listitem_alloc();
6442
6443 if (li == NULL)
6444 return FAIL;
6445 li->li_tv.v_type = VAR_DICT;
6446 li->li_tv.v_lock = 0;
6447 li->li_tv.vval.v_dict = dict;
6448 list_append(list, li);
6449 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 return OK;
6451}
6452
6453/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006455 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006456 * Returns FAIL when out of memory.
6457 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006458 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006459list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006460 list_T *l;
6461 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006462 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463{
6464 listitem_T *li = listitem_alloc();
6465
6466 if (li == NULL)
6467 return FAIL;
6468 list_append(l, li);
6469 li->li_tv.v_type = VAR_STRING;
6470 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006471 if (str == NULL)
6472 li->li_tv.vval.v_string = NULL;
6473 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006474 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006475 return FAIL;
6476 return OK;
6477}
6478
6479/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006480 * Append "n" to list "l".
6481 * Returns FAIL when out of memory.
6482 */
6483 static int
6484list_append_number(l, n)
6485 list_T *l;
6486 varnumber_T n;
6487{
6488 listitem_T *li;
6489
6490 li = listitem_alloc();
6491 if (li == NULL)
6492 return FAIL;
6493 li->li_tv.v_type = VAR_NUMBER;
6494 li->li_tv.v_lock = 0;
6495 li->li_tv.vval.v_number = n;
6496 list_append(l, li);
6497 return OK;
6498}
6499
6500/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 * If "item" is NULL append at the end.
6503 * Return FAIL when out of memory.
6504 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006505 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 list_T *l;
6508 typval_T *tv;
6509 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510{
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512
6513 if (ni == NULL)
6514 return FAIL;
6515 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006516 list_insert(l, ni, item);
6517 return OK;
6518}
6519
6520 void
6521list_insert(l, ni, item)
6522 list_T *l;
6523 listitem_T *ni;
6524 listitem_T *item;
6525{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 if (item == NULL)
6527 /* Append new item at end of list. */
6528 list_append(l, ni);
6529 else
6530 {
6531 /* Insert new item before existing item. */
6532 ni->li_prev = item->li_prev;
6533 ni->li_next = item;
6534 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006535 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 ++l->lv_idx;
6538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006540 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 l->lv_idx_item = NULL;
6543 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547}
6548
6549/*
6550 * Extend "l1" with "l2".
6551 * If "bef" is NULL append at the end, otherwise insert before this item.
6552 * Returns FAIL when out of memory.
6553 */
6554 static int
6555list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 list_T *l1;
6557 list_T *l2;
6558 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559{
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006561 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006563 /* We also quit the loop when we have inserted the original item count of
6564 * the list, avoid a hang when we extend a list with itself. */
6565 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6567 return FAIL;
6568 return OK;
6569}
6570
6571/*
6572 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6573 * Return FAIL when out of memory.
6574 */
6575 static int
6576list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 list_T *l1;
6578 list_T *l2;
6579 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006583 if (l1 == NULL || l2 == NULL)
6584 return FAIL;
6585
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588 if (l == NULL)
6589 return FAIL;
6590 tv->v_type = VAR_LIST;
6591 tv->vval.v_list = l;
6592
6593 /* append all items from the second list */
6594 return list_extend(l, l2, NULL);
6595}
6596
6597/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006598 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 * Returns NULL when out of memory.
6602 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006603 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608{
Bram Moolenaar33570922005-01-25 22:26:29 +00006609 list_T *copy;
6610 listitem_T *item;
6611 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612
6613 if (orig == NULL)
6614 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615
6616 copy = list_alloc();
6617 if (copy != NULL)
6618 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 if (copyID != 0)
6620 {
6621 /* Do this before adding the items, because one of the items may
6622 * refer back to this list. */
6623 orig->lv_copyID = copyID;
6624 orig->lv_copylist = copy;
6625 }
6626 for (item = orig->lv_first; item != NULL && !got_int;
6627 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 {
6629 ni = listitem_alloc();
6630 if (ni == NULL)
6631 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006632 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006633 {
6634 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6635 {
6636 vim_free(ni);
6637 break;
6638 }
6639 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006641 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642 list_append(copy, ni);
6643 }
6644 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006645 if (item != NULL)
6646 {
6647 list_unref(copy);
6648 copy = NULL;
6649 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 }
6651
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006652 return copy;
6653}
6654
6655/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006656 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006657 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006658 * This used to be called list_remove, but that conflicts with a Sun header
6659 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006661 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006662vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 list_T *l;
6664 listitem_T *item;
6665 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006666{
Bram Moolenaar33570922005-01-25 22:26:29 +00006667 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 /* notify watchers */
6670 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006672 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673 list_fix_watch(l, ip);
6674 if (ip == item2)
6675 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006677
6678 if (item2->li_next == NULL)
6679 l->lv_last = item->li_prev;
6680 else
6681 item2->li_next->li_prev = item->li_prev;
6682 if (item->li_prev == NULL)
6683 l->lv_first = item2->li_next;
6684 else
6685 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006686 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687}
6688
6689/*
6690 * Return an allocated string with the string representation of a list.
6691 * May return NULL.
6692 */
6693 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006694list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006695 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006696 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697{
6698 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699
6700 if (tv->vval.v_list == NULL)
6701 return NULL;
6702 ga_init2(&ga, (int)sizeof(char), 80);
6703 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006704 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006705 {
6706 vim_free(ga.ga_data);
6707 return NULL;
6708 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709 ga_append(&ga, ']');
6710 ga_append(&ga, NUL);
6711 return (char_u *)ga.ga_data;
6712}
6713
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006714typedef struct join_S {
6715 char_u *s;
6716 char_u *tofree;
6717} join_T;
6718
6719 static int
6720list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6721 garray_T *gap; /* to store the result in */
6722 list_T *l;
6723 char_u *sep;
6724 int echo_style;
6725 int copyID;
6726 garray_T *join_gap; /* to keep each list item string */
6727{
6728 int i;
6729 join_T *p;
6730 int len;
6731 int sumlen = 0;
6732 int first = TRUE;
6733 char_u *tofree;
6734 char_u numbuf[NUMBUFLEN];
6735 listitem_T *item;
6736 char_u *s;
6737
6738 /* Stringify each item in the list. */
6739 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6740 {
6741 if (echo_style)
6742 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6743 else
6744 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6745 if (s == NULL)
6746 return FAIL;
6747
6748 len = (int)STRLEN(s);
6749 sumlen += len;
6750
Bram Moolenaarcde88542015-08-11 19:14:00 +02006751 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006752 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6753 if (tofree != NULL || s != numbuf)
6754 {
6755 p->s = s;
6756 p->tofree = tofree;
6757 }
6758 else
6759 {
6760 p->s = vim_strnsave(s, len);
6761 p->tofree = p->s;
6762 }
6763
6764 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006765 if (did_echo_string_emsg) /* recursion error, bail out */
6766 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006767 }
6768
6769 /* Allocate result buffer with its total size, avoid re-allocation and
6770 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6771 if (join_gap->ga_len >= 2)
6772 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6773 if (ga_grow(gap, sumlen + 2) == FAIL)
6774 return FAIL;
6775
6776 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6777 {
6778 if (first)
6779 first = FALSE;
6780 else
6781 ga_concat(gap, sep);
6782 p = ((join_T *)join_gap->ga_data) + i;
6783
6784 if (p->s != NULL)
6785 ga_concat(gap, p->s);
6786 line_breakcheck();
6787 }
6788
6789 return OK;
6790}
6791
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006794 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006795 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006797 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006798list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006800 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006802 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006803 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006804{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006805 garray_T join_ga;
6806 int retval;
6807 join_T *p;
6808 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006809
Bram Moolenaard39a7512015-04-16 22:51:22 +02006810 if (l->lv_len < 1)
6811 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6813 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6814
6815 /* Dispose each item in join_ga. */
6816 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006817 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006818 p = (join_T *)join_ga.ga_data;
6819 for (i = 0; i < join_ga.ga_len; ++i)
6820 {
6821 vim_free(p->tofree);
6822 ++p;
6823 }
6824 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006826
6827 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006828}
6829
6830/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 * Garbage collection for lists and dictionaries.
6832 *
6833 * We use reference counts to be able to free most items right away when they
6834 * are no longer used. But for composite items it's possible that it becomes
6835 * unused while the reference count is > 0: When there is a recursive
6836 * reference. Example:
6837 * :let l = [1, 2, 3]
6838 * :let d = {9: l}
6839 * :let l[1] = d
6840 *
6841 * Since this is quite unusual we handle this with garbage collection: every
6842 * once in a while find out which lists and dicts are not referenced from any
6843 * variable.
6844 *
6845 * Here is a good reference text about garbage collection (refers to Python
6846 * but it applies to all reference-counting mechanisms):
6847 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006848 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849
6850/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 * Do garbage collection for lists and dicts.
6852 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 int
6855garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006858 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 buf_T *buf;
6860 win_T *wp;
6861 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006862 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006863 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006865#ifdef FEAT_WINDOWS
6866 tabpage_T *tp;
6867#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006869 /* Only do this once. */
6870 want_garbage_collect = FALSE;
6871 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006872 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006873
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 /* We advance by two because we add one for items referenced through
6875 * previous_funccal. */
6876 current_copyID += COPYID_INC;
6877 copyID = current_copyID;
6878
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 /*
6880 * 1. Go through all accessible variables and mark all lists and dicts
6881 * with copyID.
6882 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883
6884 /* Don't free variables in the previous_funccal list unless they are only
6885 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006886 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006887 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6888 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6890 NULL);
6891 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6892 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893 }
6894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 /* script-local variables */
6896 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898
6899 /* buffer-local variables */
6900 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6902 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903
6904 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6907 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006908#ifdef FEAT_AUTOCMD
6909 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6911 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006912#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006914#ifdef FEAT_WINDOWS
6915 /* tabpage-local variables */
6916 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6918 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006919#endif
6920
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923
6924 /* function-local variables */
6925 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6926 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6928 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 }
6930
Bram Moolenaard812df62008-11-09 12:46:09 +00006931 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006933
Bram Moolenaar1dced572012-04-05 16:54:08 +02006934#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006936#endif
6937
Bram Moolenaardb913952012-06-29 12:54:53 +02006938#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006940#endif
6941
6942#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006944#endif
6945
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 /*
6949 * 2. Free lists and dictionaries that are not referenced.
6950 */
6951 did_free = free_unref_items(copyID);
6952
6953 /*
6954 * 3. Check if any funccal can be freed now.
6955 */
6956 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 if (can_free_funccal(*pfc, copyID))
6959 {
6960 fc = *pfc;
6961 *pfc = fc->caller;
6962 free_funccal(fc, TRUE);
6963 did_free = TRUE;
6964 did_free_funccal = TRUE;
6965 }
6966 else
6967 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 if (did_free_funccal)
6970 /* When a funccal was freed some more items might be garbage
6971 * collected, so run again. */
6972 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 else if (p_verbose > 0)
6975 {
6976 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6977 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978
6979 return did_free;
6980}
6981
6982/*
6983 * Free lists and dictionaries that are no longer referenced.
6984 */
6985 static int
6986free_unref_items(copyID)
6987 int copyID;
6988{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006989 dict_T *dd, *dd_next;
6990 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 int did_free = FALSE;
6992
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 */
6996 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006997 {
6998 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006999 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007001 /* Free the Dictionary and ordinary items it contains, but don't
7002 * recurse into Lists and Dictionaries, they will be in the list
7003 * of dicts or list of lists. */
7004 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 dd = dd_next;
7008 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009
7010 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007011 * Go through the list of lists and free items without the copyID.
7012 * But don't free a list that has a watcher (used in a for loop), these
7013 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 */
7015 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007016 {
7017 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7019 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007021 /* Free the List and ordinary items it contains, but don't recurse
7022 * into Lists and Dictionaries, they will be in the list of dicts
7023 * or list of lists. */
7024 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007027 ll = ll_next;
7028 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 return did_free;
7030}
7031
7032/*
7033 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 * "list_stack" is used to add lists to be marked. Can be NULL.
7035 *
7036 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 int
7039set_ref_in_ht(ht, copyID, list_stack)
7040 hashtab_T *ht;
7041 int copyID;
7042 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043{
7044 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 hashtab_T *cur_ht;
7048 ht_stack_T *ht_stack = NULL;
7049 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051 cur_ht = ht;
7052 for (;;)
7053 {
7054 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 /* Mark each item in the hashtab. If the item contains a hashtab
7057 * it is added to ht_stack, if it contains a list it is added to
7058 * list_stack. */
7059 todo = (int)cur_ht->ht_used;
7060 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7061 if (!HASHITEM_EMPTY(hi))
7062 {
7063 --todo;
7064 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7065 &ht_stack, list_stack);
7066 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068
7069 if (ht_stack == NULL)
7070 break;
7071
7072 /* take an item from the stack */
7073 cur_ht = ht_stack->ht;
7074 tempitem = ht_stack;
7075 ht_stack = ht_stack->prev;
7076 free(tempitem);
7077 }
7078
7079 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080}
7081
7082/*
7083 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7085 *
7086 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 int
7089set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 list_T *l;
7091 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 listitem_T *li;
7095 int abort = FALSE;
7096 list_T *cur_l;
7097 list_stack_T *list_stack = NULL;
7098 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 cur_l = l;
7101 for (;;)
7102 {
7103 if (!abort)
7104 /* Mark each item in the list. If the item contains a hashtab
7105 * it is added to ht_stack, if it contains a list it is added to
7106 * list_stack. */
7107 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7108 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7109 ht_stack, &list_stack);
7110 if (list_stack == NULL)
7111 break;
7112
7113 /* take an item from the stack */
7114 cur_l = list_stack->list;
7115 tempitem = list_stack;
7116 list_stack = list_stack->prev;
7117 free(tempitem);
7118 }
7119
7120 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007121}
7122
7123/*
7124 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 * "list_stack" is used to add lists to be marked. Can be NULL.
7126 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7127 *
7128 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007129 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 int
7131set_ref_in_item(tv, copyID, ht_stack, list_stack)
7132 typval_T *tv;
7133 int copyID;
7134 ht_stack_T **ht_stack;
7135 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007136{
7137 dict_T *dd;
7138 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007140
7141 switch (tv->v_type)
7142 {
7143 case VAR_DICT:
7144 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007145 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007147 /* Didn't see this dict yet. */
7148 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007149 if (ht_stack == NULL)
7150 {
7151 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7152 }
7153 else
7154 {
7155 ht_stack_T *newitem = (ht_stack_T*)malloc(
7156 sizeof(ht_stack_T));
7157 if (newitem == NULL)
7158 abort = TRUE;
7159 else
7160 {
7161 newitem->ht = &dd->dv_hashtab;
7162 newitem->prev = *ht_stack;
7163 *ht_stack = newitem;
7164 }
7165 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007167 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168
7169 case VAR_LIST:
7170 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007171 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173 /* Didn't see this list yet. */
7174 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007175 if (list_stack == NULL)
7176 {
7177 abort = set_ref_in_list(ll, copyID, ht_stack);
7178 }
7179 else
7180 {
7181 list_stack_T *newitem = (list_stack_T*)malloc(
7182 sizeof(list_stack_T));
7183 if (newitem == NULL)
7184 abort = TRUE;
7185 else
7186 {
7187 newitem->list = ll;
7188 newitem->prev = *list_stack;
7189 *list_stack = newitem;
7190 }
7191 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007194 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007195 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007196}
7197
7198/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199 * Allocate an empty header for a dictionary.
7200 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007201 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202dict_alloc()
7203{
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205
Bram Moolenaar33570922005-01-25 22:26:29 +00007206 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007208 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007209 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210 if (first_dict != NULL)
7211 first_dict->dv_used_prev = d;
7212 d->dv_used_next = first_dict;
7213 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007214 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007215
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007217 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007218 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007219 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223}
7224
7225/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007226 * Allocate an empty dict for a return value.
7227 * Returns OK or FAIL.
7228 */
7229 static int
7230rettv_dict_alloc(rettv)
7231 typval_T *rettv;
7232{
7233 dict_T *d = dict_alloc();
7234
7235 if (d == NULL)
7236 return FAIL;
7237
7238 rettv->vval.v_dict = d;
7239 rettv->v_type = VAR_DICT;
7240 ++d->dv_refcount;
7241 return OK;
7242}
7243
7244
7245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 * Unreference a Dictionary: decrement the reference count and free it when it
7247 * becomes zero.
7248 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007249 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007253 if (d != NULL && --d->dv_refcount <= 0)
7254 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255}
7256
7257/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007258 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 * Ignores the reference count.
7260 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007261 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007262dict_free(d, recurse)
7263 dict_T *d;
7264 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007268 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007270 /* Remove the dict from the list of dicts for garbage collection. */
7271 if (d->dv_used_prev == NULL)
7272 first_dict = d->dv_used_next;
7273 else
7274 d->dv_used_prev->dv_used_next = d->dv_used_next;
7275 if (d->dv_used_next != NULL)
7276 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7277
7278 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007279 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007280 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 if (!HASHITEM_EMPTY(hi))
7284 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007285 /* Remove the item before deleting it, just in case there is
7286 * something recursive causing trouble. */
7287 di = HI2DI(hi);
7288 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007289 if (recurse || (di->di_tv.v_type != VAR_LIST
7290 && di->di_tv.v_type != VAR_DICT))
7291 clear_tv(&di->di_tv);
7292 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 --todo;
7294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297 vim_free(d);
7298}
7299
7300/*
7301 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 * The "key" is copied to the new item.
7303 * Note that the value of the item "di_tv" still needs to be initialized!
7304 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007306 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307dictitem_alloc(key)
7308 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309{
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007312 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007314 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007316 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007317 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319}
7320
7321/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 * Make a copy of a Dictionary item.
7323 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327{
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007330 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7331 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 if (di != NULL)
7333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007335 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336 copy_tv(&org->di_tv, &di->di_tv);
7337 }
7338 return di;
7339}
7340
7341/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 * Remove item "item" from Dictionary "dict" and free it.
7343 */
7344 static void
7345dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 dict_T *dict;
7347 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348{
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 if (HASHITEM_EMPTY(hi))
7353 EMSG2(_(e_intern2), "dictitem_remove()");
7354 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356 dictitem_free(item);
7357}
7358
7359/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360 * Free a dict item. Also clears the value.
7361 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007362 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007367 if (item->di_flags & DI_FLAGS_ALLOC)
7368 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369}
7370
7371/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7373 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 * Returns NULL when out of memory.
7376 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382{
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 dict_T *copy;
7384 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387
7388 if (orig == NULL)
7389 return NULL;
7390
7391 copy = dict_alloc();
7392 if (copy != NULL)
7393 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007394 if (copyID != 0)
7395 {
7396 orig->dv_copyID = copyID;
7397 orig->dv_copydict = copy;
7398 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007399 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007400 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 --todo;
7405
7406 di = dictitem_alloc(hi->hi_key);
7407 if (di == NULL)
7408 break;
7409 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007410 {
7411 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7412 copyID) == FAIL)
7413 {
7414 vim_free(di);
7415 break;
7416 }
7417 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418 else
7419 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7420 if (dict_add(copy, di) == FAIL)
7421 {
7422 dictitem_free(di);
7423 break;
7424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007429 if (todo > 0)
7430 {
7431 dict_unref(copy);
7432 copy = NULL;
7433 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 }
7435
7436 return copy;
7437}
7438
7439/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007441 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007443 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 dict_T *d;
7446 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447{
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449}
7450
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007452 * Add a number or string entry to dictionary "d".
7453 * When "str" is NULL use number "nr", otherwise use "str".
7454 * Returns FAIL when out of memory and when key already exists.
7455 */
7456 int
7457dict_add_nr_str(d, key, nr, str)
7458 dict_T *d;
7459 char *key;
7460 long nr;
7461 char_u *str;
7462{
7463 dictitem_T *item;
7464
7465 item = dictitem_alloc((char_u *)key);
7466 if (item == NULL)
7467 return FAIL;
7468 item->di_tv.v_lock = 0;
7469 if (str == NULL)
7470 {
7471 item->di_tv.v_type = VAR_NUMBER;
7472 item->di_tv.vval.v_number = nr;
7473 }
7474 else
7475 {
7476 item->di_tv.v_type = VAR_STRING;
7477 item->di_tv.vval.v_string = vim_strsave(str);
7478 }
7479 if (dict_add(d, item) == FAIL)
7480 {
7481 dictitem_free(item);
7482 return FAIL;
7483 }
7484 return OK;
7485}
7486
7487/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007488 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007489 * Returns FAIL when out of memory and when key already exists.
7490 */
7491 int
7492dict_add_list(d, key, list)
7493 dict_T *d;
7494 char *key;
7495 list_T *list;
7496{
7497 dictitem_T *item;
7498
7499 item = dictitem_alloc((char_u *)key);
7500 if (item == NULL)
7501 return FAIL;
7502 item->di_tv.v_lock = 0;
7503 item->di_tv.v_type = VAR_LIST;
7504 item->di_tv.vval.v_list = list;
7505 if (dict_add(d, item) == FAIL)
7506 {
7507 dictitem_free(item);
7508 return FAIL;
7509 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007510 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007511 return OK;
7512}
7513
7514/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515 * Get the number of items in a Dictionary.
7516 */
7517 static long
7518dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007520{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007521 if (d == NULL)
7522 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007523 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007524}
7525
7526/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 * Find item "key[len]" in Dictionary "d".
7528 * If "len" is negative use strlen(key).
7529 * Returns NULL when not found.
7530 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007531 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007533 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 char_u *key;
7535 int len;
7536{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537#define AKEYLEN 200
7538 char_u buf[AKEYLEN];
7539 char_u *akey;
7540 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007541 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 if (len < 0)
7544 akey = key;
7545 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007546 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 tofree = akey = vim_strnsave(key, len);
7548 if (akey == NULL)
7549 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007550 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 else
7552 {
7553 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007554 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 akey = buf;
7556 }
7557
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 vim_free(tofree);
7560 if (HASHITEM_EMPTY(hi))
7561 return NULL;
7562 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563}
7564
7565/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007566 * Get a string item from a dictionary.
7567 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007568 * Returns NULL if the entry doesn't exist or out of memory.
7569 */
7570 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007571get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007572 dict_T *d;
7573 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007574 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007575{
7576 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007577 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007578
7579 di = dict_find(d, key, -1);
7580 if (di == NULL)
7581 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007582 s = get_tv_string(&di->di_tv);
7583 if (save && s != NULL)
7584 s = vim_strsave(s);
7585 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007586}
7587
7588/*
7589 * Get a number item from a dictionary.
7590 * Returns 0 if the entry doesn't exist or out of memory.
7591 */
7592 long
7593get_dict_number(d, key)
7594 dict_T *d;
7595 char_u *key;
7596{
7597 dictitem_T *di;
7598
7599 di = dict_find(d, key, -1);
7600 if (di == NULL)
7601 return 0;
7602 return get_tv_number(&di->di_tv);
7603}
7604
7605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 * Return an allocated string with the string representation of a Dictionary.
7607 * May return NULL.
7608 */
7609 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007610dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007612 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613{
7614 garray_T ga;
7615 int first = TRUE;
7616 char_u *tofree;
7617 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007620 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 return NULL;
7625 ga_init2(&ga, (int)sizeof(char), 80);
7626 ga_append(&ga, '{');
7627
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007628 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007629 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 --todo;
7634
7635 if (first)
7636 first = FALSE;
7637 else
7638 ga_concat(&ga, (char_u *)", ");
7639
7640 tofree = string_quote(hi->hi_key, FALSE);
7641 if (tofree != NULL)
7642 {
7643 ga_concat(&ga, tofree);
7644 vim_free(tofree);
7645 }
7646 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007647 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007648 if (s != NULL)
7649 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007651 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007652 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007653 line_breakcheck();
7654
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007657 if (todo > 0)
7658 {
7659 vim_free(ga.ga_data);
7660 return NULL;
7661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662
7663 ga_append(&ga, '}');
7664 ga_append(&ga, NUL);
7665 return (char_u *)ga.ga_data;
7666}
7667
7668/*
7669 * Allocate a variable for a Dictionary and fill it from "*arg".
7670 * Return OK or FAIL. Returns NOTDONE for {expr}.
7671 */
7672 static int
7673get_dict_tv(arg, rettv, evaluate)
7674 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 int evaluate;
7677{
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 dict_T *d = NULL;
7679 typval_T tvkey;
7680 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007681 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685
7686 /*
7687 * First check if it's not a curly-braces thing: {expr}.
7688 * Must do this without evaluating, otherwise a function may be called
7689 * twice. Unfortunately this means we need to call eval1() twice for the
7690 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007691 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007693 if (*start != '}')
7694 {
7695 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7696 return FAIL;
7697 if (*start == '}')
7698 return NOTDONE;
7699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700
7701 if (evaluate)
7702 {
7703 d = dict_alloc();
7704 if (d == NULL)
7705 return FAIL;
7706 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007707 tvkey.v_type = VAR_UNKNOWN;
7708 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709
7710 *arg = skipwhite(*arg + 1);
7711 while (**arg != '}' && **arg != NUL)
7712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 goto failret;
7715 if (**arg != ':')
7716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007717 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007718 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 goto failret;
7720 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007721 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007723 key = get_tv_string_buf_chk(&tvkey, buf);
7724 if (key == NULL || *key == NUL)
7725 {
7726 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7727 if (key != NULL)
7728 EMSG(_(e_emptykey));
7729 clear_tv(&tvkey);
7730 goto failret;
7731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733
7734 *arg = skipwhite(*arg + 1);
7735 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7736 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007737 if (evaluate)
7738 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 goto failret;
7740 }
7741 if (evaluate)
7742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 if (item != NULL)
7745 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007746 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748 clear_tv(&tv);
7749 goto failret;
7750 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751 item = dictitem_alloc(key);
7752 clear_tv(&tvkey);
7753 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007756 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 if (dict_add(d, item) == FAIL)
7758 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 }
7760 }
7761
7762 if (**arg == '}')
7763 break;
7764 if (**arg != ',')
7765 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007766 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767 goto failret;
7768 }
7769 *arg = skipwhite(*arg + 1);
7770 }
7771
7772 if (**arg != '}')
7773 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007774 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775failret:
7776 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007777 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778 return FAIL;
7779 }
7780
7781 *arg = skipwhite(*arg + 1);
7782 if (evaluate)
7783 {
7784 rettv->v_type = VAR_DICT;
7785 rettv->vval.v_dict = d;
7786 ++d->dv_refcount;
7787 }
7788
7789 return OK;
7790}
7791
Bram Moolenaar8c711452005-01-14 21:53:12 +00007792/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 * Return a string with the string representation of a variable.
7794 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007796 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007797 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007798 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 */
7800 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007801echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007804 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007805 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007807 static int recurse = 0;
7808 char_u *r = NULL;
7809
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007812 if (!did_echo_string_emsg)
7813 {
7814 /* Only give this message once for a recursive call to avoid
7815 * flooding the user with errors. And stop iterating over lists
7816 * and dicts. */
7817 did_echo_string_emsg = TRUE;
7818 EMSG(_("E724: variable nested too deep for displaying"));
7819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007821 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 }
7823 ++recurse;
7824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007825 switch (tv->v_type)
7826 {
7827 case VAR_FUNC:
7828 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 r = tv->vval.v_string;
7830 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007833 if (tv->vval.v_list == NULL)
7834 {
7835 *tofree = NULL;
7836 r = NULL;
7837 }
7838 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7839 {
7840 *tofree = NULL;
7841 r = (char_u *)"[...]";
7842 }
7843 else
7844 {
7845 tv->vval.v_list->lv_copyID = copyID;
7846 *tofree = list2string(tv, copyID);
7847 r = *tofree;
7848 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007850
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852 if (tv->vval.v_dict == NULL)
7853 {
7854 *tofree = NULL;
7855 r = NULL;
7856 }
7857 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7858 {
7859 *tofree = NULL;
7860 r = (char_u *)"{...}";
7861 }
7862 else
7863 {
7864 tv->vval.v_dict->dv_copyID = copyID;
7865 *tofree = dict2string(tv, copyID);
7866 r = *tofree;
7867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 case VAR_STRING:
7871 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007872 *tofree = NULL;
7873 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007874 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007875
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#ifdef FEAT_FLOAT
7877 case VAR_FLOAT:
7878 *tofree = NULL;
7879 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7880 r = numbuf;
7881 break;
7882#endif
7883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007885 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007886 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888
Bram Moolenaar8502c702014-06-17 12:51:16 +02007889 if (--recurse == 0)
7890 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892}
7893
7894/*
7895 * Return a string with the string representation of a variable.
7896 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7897 * "numbuf" is used for a number.
7898 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007899 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007900 */
7901 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007902tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904 char_u **tofree;
7905 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007906 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907{
7908 switch (tv->v_type)
7909 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 case VAR_FUNC:
7911 *tofree = string_quote(tv->vval.v_string, TRUE);
7912 return *tofree;
7913 case VAR_STRING:
7914 *tofree = string_quote(tv->vval.v_string, FALSE);
7915 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007916#ifdef FEAT_FLOAT
7917 case VAR_FLOAT:
7918 *tofree = NULL;
7919 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7920 return numbuf;
7921#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007922 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007924 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007925 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007929 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007930}
7931
7932/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 * Return string "str" in ' quotes, doubling ' characters.
7934 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007936 */
7937 static char_u *
7938string_quote(str, function)
7939 char_u *str;
7940 int function;
7941{
Bram Moolenaar33570922005-01-25 22:26:29 +00007942 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007943 char_u *p, *r, *s;
7944
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 len = (function ? 13 : 3);
7946 if (str != NULL)
7947 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007948 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 for (p = str; *p != NUL; mb_ptr_adv(p))
7950 if (*p == '\'')
7951 ++len;
7952 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007953 s = r = alloc(len);
7954 if (r != NULL)
7955 {
7956 if (function)
7957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 r += 10;
7960 }
7961 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007962 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 if (str != NULL)
7964 for (p = str; *p != NUL; )
7965 {
7966 if (*p == '\'')
7967 *r++ = '\'';
7968 MB_COPY_CHAR(p, r);
7969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971 if (function)
7972 *r++ = ')';
7973 *r++ = NUL;
7974 }
7975 return s;
7976}
7977
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007978#ifdef FEAT_FLOAT
7979/*
7980 * Convert the string "text" to a floating point number.
7981 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7982 * this always uses a decimal point.
7983 * Returns the length of the text that was consumed.
7984 */
7985 static int
7986string2float(text, value)
7987 char_u *text;
7988 float_T *value; /* result stored here */
7989{
7990 char *s = (char *)text;
7991 float_T f;
7992
7993 f = strtod(s, &s);
7994 *value = f;
7995 return (int)((char_u *)s - text);
7996}
7997#endif
7998
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 * Get the value of an environment variable.
8001 * "arg" is pointing to the '$'. It is advanced to after the name.
8002 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008003 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 */
8005 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 int evaluate;
8010{
8011 char_u *string = NULL;
8012 int len;
8013 int cc;
8014 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008015 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 ++*arg;
8018 name = *arg;
8019 len = get_env_len(arg);
8020 if (evaluate)
8021 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008023 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008024
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008025 cc = name[len];
8026 name[len] = NUL;
8027 /* first try vim_getenv(), fast for normal environment vars */
8028 string = vim_getenv(name, &mustfree);
8029 if (string != NULL && *string != NUL)
8030 {
8031 if (!mustfree)
8032 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008034 else
8035 {
8036 if (mustfree)
8037 vim_free(string);
8038
8039 /* next try expanding things like $VIM and ${HOME} */
8040 string = expand_env_save(name - 1);
8041 if (string != NULL && *string == '$')
8042 {
8043 vim_free(string);
8044 string = NULL;
8045 }
8046 }
8047 name[len] = cc;
8048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 rettv->v_type = VAR_STRING;
8050 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052
8053 return OK;
8054}
8055
8056/*
8057 * Array with names and number of arguments of all internal functions
8058 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8059 */
8060static struct fst
8061{
8062 char *f_name; /* function name */
8063 char f_min_argc; /* minimal number of arguments */
8064 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008065 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008066 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067} functions[] =
8068{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008069#ifdef FEAT_FLOAT
8070 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008071 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008073 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008074 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008075 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"append", 2, 2, f_append},
8077 {"argc", 0, 0, f_argc},
8078 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008079 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008080 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008081#ifdef FEAT_FLOAT
8082 {"asin", 1, 1, f_asin}, /* WJMc */
8083#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008084 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008085 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008086 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008087 {"assert_false", 1, 2, f_assert_false},
8088 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008089#ifdef FEAT_FLOAT
8090 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008091 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008094 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"bufexists", 1, 1, f_bufexists},
8096 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8097 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8098 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8099 {"buflisted", 1, 1, f_buflisted},
8100 {"bufloaded", 1, 1, f_bufloaded},
8101 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008102 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"bufwinnr", 1, 1, f_bufwinnr},
8104 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008105 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008106 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008107 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#ifdef FEAT_FLOAT
8109 {"ceil", 1, 1, f_ceil},
8110#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008111 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008112 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008114 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008116#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008117 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008118 {"complete_add", 1, 1, f_complete_add},
8119 {"complete_check", 0, 0, f_complete_check},
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008122 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008123#ifdef FEAT_FLOAT
8124 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008125 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008126#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008127 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008129 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008130 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"delete", 1, 1, f_delete},
8132 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008133 {"diff_filler", 1, 1, f_diff_filler},
8134 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008135 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008137 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"eventhandler", 0, 0, f_eventhandler},
8139 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008140 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008142#ifdef FEAT_FLOAT
8143 {"exp", 1, 1, f_exp},
8144#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008145 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008146 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008147 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8149 {"filereadable", 1, 1, f_filereadable},
8150 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008151 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008152 {"finddir", 1, 3, f_finddir},
8153 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008154#ifdef FEAT_FLOAT
8155 {"float2nr", 1, 1, f_float2nr},
8156 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008157 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008158#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008159 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"fnamemodify", 2, 2, f_fnamemodify},
8161 {"foldclosed", 1, 1, f_foldclosed},
8162 {"foldclosedend", 1, 1, f_foldclosedend},
8163 {"foldlevel", 1, 1, f_foldlevel},
8164 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008165 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008168 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008170 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008171 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"getchar", 0, 1, f_getchar},
8173 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008174 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"getcmdline", 0, 0, f_getcmdline},
8176 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008177 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008178 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008179 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008181 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008182 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"getfsize", 1, 1, f_getfsize},
8184 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008185 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008186 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008187 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008188 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008189 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008190 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008191 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008192 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008194 {"gettabvar", 2, 3, f_gettabvar},
8195 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"getwinposx", 0, 0, f_getwinposx},
8197 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008198 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008199 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008200 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008201 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008204 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008205 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8207 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8208 {"histadd", 2, 2, f_histadd},
8209 {"histdel", 1, 2, f_histdel},
8210 {"histget", 1, 2, f_histget},
8211 {"histnr", 1, 1, f_histnr},
8212 {"hlID", 1, 1, f_hlID},
8213 {"hlexists", 1, 1, f_hlexists},
8214 {"hostname", 0, 0, f_hostname},
8215 {"iconv", 3, 3, f_iconv},
8216 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008217 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008218 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008220 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"inputrestore", 0, 0, f_inputrestore},
8222 {"inputsave", 0, 0, f_inputsave},
8223 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008224 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008225 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008227 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008228 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008230 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008232 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"libcall", 3, 3, f_libcall},
8234 {"libcallnr", 3, 3, f_libcallnr},
8235 {"line", 1, 1, f_line},
8236 {"line2byte", 1, 1, f_line2byte},
8237 {"lispindent", 1, 1, f_lispindent},
8238 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008240 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008241 {"log10", 1, 1, f_log10},
8242#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008243#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008244 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008245#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008246 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008247 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008248 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008249 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008250 {"matchadd", 2, 5, f_matchadd},
8251 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008252 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008253 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008254 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008255 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008256 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008257 {"max", 1, 1, f_max},
8258 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008259#ifdef vim_mkdir
8260 {"mkdir", 1, 3, f_mkdir},
8261#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008262 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008263#ifdef FEAT_MZSCHEME
8264 {"mzeval", 1, 1, f_mzeval},
8265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008267 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008268 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008269 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008270#ifdef FEAT_FLOAT
8271 {"pow", 2, 2, f_pow},
8272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008274 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008275 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008276#ifdef FEAT_PYTHON3
8277 {"py3eval", 1, 1, f_py3eval},
8278#endif
8279#ifdef FEAT_PYTHON
8280 {"pyeval", 1, 1, f_pyeval},
8281#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008282 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008283 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008284 {"reltime", 0, 2, f_reltime},
8285 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"remote_expr", 2, 3, f_remote_expr},
8287 {"remote_foreground", 1, 1, f_remote_foreground},
8288 {"remote_peek", 1, 2, f_remote_peek},
8289 {"remote_read", 1, 1, f_remote_read},
8290 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008291 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008293 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008295 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008296#ifdef FEAT_FLOAT
8297 {"round", 1, 1, f_round},
8298#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008299 {"screenattr", 2, 2, f_screenattr},
8300 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008301 {"screencol", 0, 0, f_screencol},
8302 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008303 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008304 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008305 {"searchpair", 3, 7, f_searchpair},
8306 {"searchpairpos", 3, 7, f_searchpairpos},
8307 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"server2client", 2, 2, f_server2client},
8309 {"serverlist", 0, 0, f_serverlist},
8310 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008311 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"setcmdpos", 1, 1, f_setcmdpos},
8313 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008314 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008315 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008316 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008317 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008319 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008320 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008322#ifdef FEAT_CRYPT
8323 {"sha256", 1, 1, f_sha256},
8324#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008325 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008326 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008328#ifdef FEAT_FLOAT
8329 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008330 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008331#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008332 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008333 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008334 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008335 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008336 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008337#ifdef FEAT_FLOAT
8338 {"sqrt", 1, 1, f_sqrt},
8339 {"str2float", 1, 1, f_str2float},
8340#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008341 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008342 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008343 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344#ifdef HAVE_STRFTIME
8345 {"strftime", 1, 2, f_strftime},
8346#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"strlen", 1, 1, f_strlen},
8350 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008351 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008353 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008354 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"substitute", 4, 4, f_substitute},
8356 {"synID", 3, 3, f_synID},
8357 {"synIDattr", 2, 3, f_synIDattr},
8358 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008359 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008360 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008361 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008362 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008363 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008364 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008365 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008366 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008367 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008368#ifdef FEAT_FLOAT
8369 {"tan", 1, 1, f_tan},
8370 {"tanh", 1, 1, f_tanh},
8371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008373 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {"tolower", 1, 1, f_tolower},
8375 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008376 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008377#ifdef FEAT_FLOAT
8378 {"trunc", 1, 1, f_trunc},
8379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008381 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008382 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008383 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008384 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"virtcol", 1, 1, f_virtcol},
8386 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008387 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"winbufnr", 1, 1, f_winbufnr},
8389 {"wincol", 0, 0, f_wincol},
8390 {"winheight", 1, 1, f_winheight},
8391 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008392 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008394 {"winrestview", 1, 1, f_winrestview},
8395 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008397 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008398 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008399 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400};
8401
8402#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8403
8404/*
8405 * Function given to ExpandGeneric() to obtain the list of internal
8406 * or user defined function names.
8407 */
8408 char_u *
8409get_function_name(xp, idx)
8410 expand_T *xp;
8411 int idx;
8412{
8413 static int intidx = -1;
8414 char_u *name;
8415
8416 if (idx == 0)
8417 intidx = -1;
8418 if (intidx < 0)
8419 {
8420 name = get_user_func_name(xp, idx);
8421 if (name != NULL)
8422 return name;
8423 }
8424 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8425 {
8426 STRCPY(IObuff, functions[intidx].f_name);
8427 STRCAT(IObuff, "(");
8428 if (functions[intidx].f_max_argc == 0)
8429 STRCAT(IObuff, ")");
8430 return IObuff;
8431 }
8432
8433 return NULL;
8434}
8435
8436/*
8437 * Function given to ExpandGeneric() to obtain the list of internal or
8438 * user defined variable or function names.
8439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 char_u *
8441get_expr_name(xp, idx)
8442 expand_T *xp;
8443 int idx;
8444{
8445 static int intidx = -1;
8446 char_u *name;
8447
8448 if (idx == 0)
8449 intidx = -1;
8450 if (intidx < 0)
8451 {
8452 name = get_function_name(xp, idx);
8453 if (name != NULL)
8454 return name;
8455 }
8456 return get_user_var_name(xp, ++intidx);
8457}
8458
8459#endif /* FEAT_CMDL_COMPL */
8460
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008461#if defined(EBCDIC) || defined(PROTO)
8462/*
8463 * Compare struct fst by function name.
8464 */
8465 static int
8466compare_func_name(s1, s2)
8467 const void *s1;
8468 const void *s2;
8469{
8470 struct fst *p1 = (struct fst *)s1;
8471 struct fst *p2 = (struct fst *)s2;
8472
8473 return STRCMP(p1->f_name, p2->f_name);
8474}
8475
8476/*
8477 * Sort the function table by function name.
8478 * The sorting of the table above is ASCII dependant.
8479 * On machines using EBCDIC we have to sort it.
8480 */
8481 static void
8482sortFunctions()
8483{
8484 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8485
8486 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8487}
8488#endif
8489
8490
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491/*
8492 * Find internal function in table above.
8493 * Return index, or -1 if not found
8494 */
8495 static int
8496find_internal_func(name)
8497 char_u *name; /* name of the function */
8498{
8499 int first = 0;
8500 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8501 int cmp;
8502 int x;
8503
8504 /*
8505 * Find the function name in the table. Binary search.
8506 */
8507 while (first <= last)
8508 {
8509 x = first + ((unsigned)(last - first) >> 1);
8510 cmp = STRCMP(name, functions[x].f_name);
8511 if (cmp < 0)
8512 last = x - 1;
8513 else if (cmp > 0)
8514 first = x + 1;
8515 else
8516 return x;
8517 }
8518 return -1;
8519}
8520
8521/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8523 * name it contains, otherwise return "name".
8524 */
8525 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008526deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 char_u *name;
8528 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008529 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530{
Bram Moolenaar33570922005-01-25 22:26:29 +00008531 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008532 int cc;
8533
8534 cc = name[*lenp];
8535 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008536 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008537 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008539 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008541 {
8542 *lenp = 0;
8543 return (char_u *)""; /* just in case */
8544 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008545 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008547 }
8548
8549 return name;
8550}
8551
8552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 * Allocate a variable for the result of a function.
8554 * Return OK or FAIL.
8555 */
8556 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8558 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 char_u *name; /* name of the function */
8560 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 char_u **arg; /* argument, pointing to the '(' */
8563 linenr_T firstline; /* first line of range */
8564 linenr_T lastline; /* last line of range */
8565 int *doesrange; /* return: function handled range */
8566 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008567 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568{
8569 char_u *argp;
8570 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008571 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 int argcount = 0; /* number of arguments found */
8573
8574 /*
8575 * Get the arguments.
8576 */
8577 argp = *arg;
8578 while (argcount < MAX_FUNC_ARGS)
8579 {
8580 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8581 if (*argp == ')' || *argp == ',' || *argp == NUL)
8582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8584 {
8585 ret = FAIL;
8586 break;
8587 }
8588 ++argcount;
8589 if (*argp != ',')
8590 break;
8591 }
8592 if (*argp == ')')
8593 ++argp;
8594 else
8595 ret = FAIL;
8596
8597 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008599 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 {
8602 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008603 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008605 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
8608 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
8611 *arg = skipwhite(argp);
8612 return ret;
8613}
8614
8615
8616/*
8617 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008618 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008619 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 */
8621 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008622call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008624 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008628 typval_T *argvars; /* vars for arguments, must have "argcount"
8629 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 linenr_T firstline; /* first line of range */
8631 linenr_T lastline; /* last line of range */
8632 int *doesrange; /* return: function handled range */
8633 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
8636 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637#define ERROR_UNKNOWN 0
8638#define ERROR_TOOMANY 1
8639#define ERROR_TOOFEW 2
8640#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008641#define ERROR_DICT 4
8642#define ERROR_NONE 5
8643#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 int error = ERROR_NONE;
8645 int i;
8646 int llen;
8647 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648#define FLEN_FIXED 40
8649 char_u fname_buf[FLEN_FIXED + 1];
8650 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008651 char_u *name;
8652
8653 /* Make a copy of the name, if it comes from a funcref variable it could
8654 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008655 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008656 if (name == NULL)
8657 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
8659 /*
8660 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8661 * Change <SNR>123_name() to K_SNR 123_name().
8662 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 llen = eval_fname_script(name);
8665 if (llen > 0)
8666 {
8667 fname_buf[0] = K_SPECIAL;
8668 fname_buf[1] = KS_EXTRA;
8669 fname_buf[2] = (int)KE_SNR;
8670 i = 3;
8671 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8672 {
8673 if (current_SID <= 0)
8674 error = ERROR_SCRIPT;
8675 else
8676 {
8677 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8678 i = (int)STRLEN(fname_buf);
8679 }
8680 }
8681 if (i + STRLEN(name + llen) < FLEN_FIXED)
8682 {
8683 STRCPY(fname_buf + i, name + llen);
8684 fname = fname_buf;
8685 }
8686 else
8687 {
8688 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8689 if (fname == NULL)
8690 error = ERROR_OTHER;
8691 else
8692 {
8693 mch_memmove(fname, fname_buf, (size_t)i);
8694 STRCPY(fname + i, name + llen);
8695 }
8696 }
8697 }
8698 else
8699 fname = name;
8700
8701 *doesrange = FALSE;
8702
8703
8704 /* execute the function if no errors detected and executing */
8705 if (evaluate && error == ERROR_NONE)
8706 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008707 char_u *rfname = fname;
8708
8709 /* Ignore "g:" before a function name. */
8710 if (fname[0] == 'g' && fname[1] == ':')
8711 rfname = fname + 2;
8712
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008713 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8714 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 error = ERROR_UNKNOWN;
8716
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008717 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {
8719 /*
8720 * User defined function.
8721 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008722 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008725 /* Trigger FuncUndefined event, may load the function. */
8726 if (fp == NULL
8727 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008728 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008729 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008731 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008732 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 }
8734#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008735 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008736 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008737 {
8738 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008739 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008740 }
8741
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 if (fp != NULL)
8743 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008744 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008746 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008748 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008750 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008751 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 else
8753 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008754 int did_save_redo = FALSE;
8755
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 /*
8757 * Call the user function.
8758 * Save and restore search patterns, script variables and
8759 * redo buffer.
8760 */
8761 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008762#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008763 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008764#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008765 {
8766 saveRedobuff();
8767 did_save_redo = TRUE;
8768 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008769 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008771 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008772 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8773 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8774 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008775 /* Function was unreferenced while being used, free it
8776 * now. */
8777 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008778 if (did_save_redo)
8779 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 restore_search_patterns();
8781 error = ERROR_NONE;
8782 }
8783 }
8784 }
8785 else
8786 {
8787 /*
8788 * Find the function name in the table, call its implementation.
8789 */
8790 i = find_internal_func(fname);
8791 if (i >= 0)
8792 {
8793 if (argcount < functions[i].f_min_argc)
8794 error = ERROR_TOOFEW;
8795 else if (argcount > functions[i].f_max_argc)
8796 error = ERROR_TOOMANY;
8797 else
8798 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008799 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 error = ERROR_NONE;
8802 }
8803 }
8804 }
8805 /*
8806 * The function call (or "FuncUndefined" autocommand sequence) might
8807 * have been aborted by an error, an interrupt, or an explicitly thrown
8808 * exception that has not been caught so far. This situation can be
8809 * tested for by calling aborting(). For an error in an internal
8810 * function or for the "E132" error in call_user_func(), however, the
8811 * throw point at which the "force_abort" flag (temporarily reset by
8812 * emsg()) is normally updated has not been reached yet. We need to
8813 * update that flag first to make aborting() reliable.
8814 */
8815 update_force_abort();
8816 }
8817 if (error == ERROR_NONE)
8818 ret = OK;
8819
8820 /*
8821 * Report an error unless the argument evaluation or function call has been
8822 * cancelled due to an aborting error, an interrupt, or an exception.
8823 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008824 if (!aborting())
8825 {
8826 switch (error)
8827 {
8828 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008829 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008830 break;
8831 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008832 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008833 break;
8834 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008835 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008836 name);
8837 break;
8838 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008839 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008840 name);
8841 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008842 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008843 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008844 name);
8845 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008846 }
8847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (fname != name && fname != fname_buf)
8850 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008851 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
8853 return ret;
8854}
8855
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008856/*
8857 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008858 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008859 */
8860 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008861emsg_funcname(ermsg, name)
8862 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008863 char_u *name;
8864{
8865 char_u *p;
8866
8867 if (*name == K_SPECIAL)
8868 p = concat_str((char_u *)"<SNR>", name + 3);
8869 else
8870 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008871 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008872 if (p != name)
8873 vim_free(p);
8874}
8875
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008876/*
8877 * Return TRUE for a non-zero Number and a non-empty String.
8878 */
8879 static int
8880non_zero_arg(argvars)
8881 typval_T *argvars;
8882{
8883 return ((argvars[0].v_type == VAR_NUMBER
8884 && argvars[0].vval.v_number != 0)
8885 || (argvars[0].v_type == VAR_STRING
8886 && argvars[0].vval.v_string != NULL
8887 && *argvars[0].vval.v_string != NUL));
8888}
8889
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890/*********************************************
8891 * Implementation of the built-in functions
8892 */
8893
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008894#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008895static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8896
8897/*
8898 * Get the float value of "argvars[0]" into "f".
8899 * Returns FAIL when the argument is not a Number or Float.
8900 */
8901 static int
8902get_float_arg(argvars, f)
8903 typval_T *argvars;
8904 float_T *f;
8905{
8906 if (argvars[0].v_type == VAR_FLOAT)
8907 {
8908 *f = argvars[0].vval.v_float;
8909 return OK;
8910 }
8911 if (argvars[0].v_type == VAR_NUMBER)
8912 {
8913 *f = (float_T)argvars[0].vval.v_number;
8914 return OK;
8915 }
8916 EMSG(_("E808: Number or Float required"));
8917 return FAIL;
8918}
8919
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008920/*
8921 * "abs(expr)" function
8922 */
8923 static void
8924f_abs(argvars, rettv)
8925 typval_T *argvars;
8926 typval_T *rettv;
8927{
8928 if (argvars[0].v_type == VAR_FLOAT)
8929 {
8930 rettv->v_type = VAR_FLOAT;
8931 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8932 }
8933 else
8934 {
8935 varnumber_T n;
8936 int error = FALSE;
8937
8938 n = get_tv_number_chk(&argvars[0], &error);
8939 if (error)
8940 rettv->vval.v_number = -1;
8941 else if (n > 0)
8942 rettv->vval.v_number = n;
8943 else
8944 rettv->vval.v_number = -n;
8945 }
8946}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008947
8948/*
8949 * "acos()" function
8950 */
8951 static void
8952f_acos(argvars, rettv)
8953 typval_T *argvars;
8954 typval_T *rettv;
8955{
8956 float_T f;
8957
8958 rettv->v_type = VAR_FLOAT;
8959 if (get_float_arg(argvars, &f) == OK)
8960 rettv->vval.v_float = acos(f);
8961 else
8962 rettv->vval.v_float = 0.0;
8963}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008964#endif
8965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008967 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 */
8969 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008970f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 typval_T *argvars;
8972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
Bram Moolenaar33570922005-01-25 22:26:29 +00008974 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008977 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008979 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008980 && !tv_check_lock(l->lv_lock,
8981 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008982 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008984 }
8985 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008986 EMSG(_(e_listreq));
8987}
8988
8989/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008990 * "alloc_fail(id, countdown, repeat)" function
8991 */
8992 static void
8993f_alloc_fail(argvars, rettv)
8994 typval_T *argvars;
8995 typval_T *rettv UNUSED;
8996{
8997 if (argvars[0].v_type != VAR_NUMBER
8998 || argvars[0].vval.v_number <= 0
8999 || argvars[1].v_type != VAR_NUMBER
9000 || argvars[1].vval.v_number < 0
9001 || argvars[2].v_type != VAR_NUMBER)
9002 EMSG(_(e_invarg));
9003 else
9004 {
9005 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009006 if (alloc_fail_id >= aid_last)
9007 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009008 alloc_fail_countdown = argvars[1].vval.v_number;
9009 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009010 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009011 }
9012}
9013
9014/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009015 * "and(expr, expr)" function
9016 */
9017 static void
9018f_and(argvars, rettv)
9019 typval_T *argvars;
9020 typval_T *rettv;
9021{
9022 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9023 & get_tv_number_chk(&argvars[1], NULL);
9024}
9025
9026/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009027 * "append(lnum, string/list)" function
9028 */
9029 static void
9030f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T *argvars;
9032 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009033{
9034 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009035 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009036 list_T *l = NULL;
9037 listitem_T *li = NULL;
9038 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009039 long added = 0;
9040
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009041 /* When coming here from Insert mode, sync undo, so that this can be
9042 * undone separately from what was previously inserted. */
9043 if (u_sync_once == 2)
9044 {
9045 u_sync_once = 1; /* notify that u_sync() was called */
9046 u_sync(TRUE);
9047 }
9048
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049 lnum = get_tv_lnum(argvars);
9050 if (lnum >= 0
9051 && lnum <= curbuf->b_ml.ml_line_count
9052 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009053 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009054 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009055 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009056 l = argvars[1].vval.v_list;
9057 if (l == NULL)
9058 return;
9059 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009060 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009061 for (;;)
9062 {
9063 if (l == NULL)
9064 tv = &argvars[1]; /* append a string */
9065 else if (li == NULL)
9066 break; /* end of list */
9067 else
9068 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 line = get_tv_string_chk(tv);
9070 if (line == NULL) /* type error */
9071 {
9072 rettv->vval.v_number = 1; /* Failed */
9073 break;
9074 }
9075 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009076 ++added;
9077 if (l == NULL)
9078 break;
9079 li = li->li_next;
9080 }
9081
9082 appended_lines_mark(lnum, added);
9083 if (curwin->w_cursor.lnum > lnum)
9084 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009086 else
9087 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088}
9089
9090/*
9091 * "argc()" function
9092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009095 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099}
9100
9101/*
9102 * "argidx()" function
9103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009106 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110}
9111
9112/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009113 * "arglistid()" function
9114 */
9115 static void
9116f_arglistid(argvars, rettv)
9117 typval_T *argvars UNUSED;
9118 typval_T *rettv;
9119{
9120 win_T *wp;
9121 tabpage_T *tp = NULL;
9122 long n;
9123
9124 rettv->vval.v_number = -1;
9125 if (argvars[0].v_type != VAR_UNKNOWN)
9126 {
9127 if (argvars[1].v_type != VAR_UNKNOWN)
9128 {
9129 n = get_tv_number(&argvars[1]);
9130 if (n >= 0)
9131 tp = find_tabpage(n);
9132 }
9133 else
9134 tp = curtab;
9135
9136 if (tp != NULL)
9137 {
9138 wp = find_win_by_nr(&argvars[0], tp);
9139 if (wp != NULL)
9140 rettv->vval.v_number = wp->w_alist->id;
9141 }
9142 }
9143 else
9144 rettv->vval.v_number = curwin->w_alist->id;
9145}
9146
9147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 * "argv(nr)" function
9149 */
9150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009152 typval_T *argvars;
9153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155 int idx;
9156
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009157 if (argvars[0].v_type != VAR_UNKNOWN)
9158 {
9159 idx = get_tv_number_chk(&argvars[0], NULL);
9160 if (idx >= 0 && idx < ARGCOUNT)
9161 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9162 else
9163 rettv->vval.v_string = NULL;
9164 rettv->v_type = VAR_STRING;
9165 }
9166 else if (rettv_list_alloc(rettv) == OK)
9167 for (idx = 0; idx < ARGCOUNT; ++idx)
9168 list_append_string(rettv->vval.v_list,
9169 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170}
9171
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009172static void prepare_assert_error __ARGS((garray_T*gap));
9173static 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));
9174static void assert_error __ARGS((garray_T *gap));
9175static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9176
9177/*
9178 * Prepare "gap" for an assert error and add the sourcing position.
9179 */
9180 static void
9181prepare_assert_error(gap)
9182 garray_T *gap;
9183{
9184 char buf[NUMBUFLEN];
9185
9186 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009187 if (sourcing_name != NULL)
9188 {
9189 ga_concat(gap, sourcing_name);
9190 if (sourcing_lnum > 0)
9191 ga_concat(gap, (char_u *)" ");
9192 }
9193 if (sourcing_lnum > 0)
9194 {
9195 sprintf(buf, "line %ld", (long)sourcing_lnum);
9196 ga_concat(gap, (char_u *)buf);
9197 }
9198 if (sourcing_name != NULL || sourcing_lnum > 0)
9199 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009200}
9201
9202/*
9203 * Fill "gap" with information about an assert error.
9204 */
9205 static void
9206fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9207 garray_T *gap;
9208 typval_T *opt_msg_tv;
9209 char_u *exp_str;
9210 typval_T *exp_tv;
9211 typval_T *got_tv;
9212{
9213 char_u numbuf[NUMBUFLEN];
9214 char_u *tofree;
9215
9216 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9217 {
9218 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9219 vim_free(tofree);
9220 }
9221 else
9222 {
9223 ga_concat(gap, (char_u *)"Expected ");
9224 if (exp_str == NULL)
9225 {
9226 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9227 vim_free(tofree);
9228 }
9229 else
9230 ga_concat(gap, exp_str);
9231 ga_concat(gap, (char_u *)" but got ");
9232 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9233 vim_free(tofree);
9234 }
9235}
Bram Moolenaar43345542015-11-29 17:35:35 +01009236
9237/*
9238 * Add an assert error to v:errors.
9239 */
9240 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009241assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009242 garray_T *gap;
9243{
9244 struct vimvar *vp = &vimvars[VV_ERRORS];
9245
9246 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9247 /* Make sure v:errors is a list. */
9248 set_vim_var_list(VV_ERRORS, list_alloc());
9249 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9250}
9251
Bram Moolenaar43345542015-11-29 17:35:35 +01009252/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009253 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009254 */
9255 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009257 typval_T *argvars;
9258 typval_T *rettv UNUSED;
9259{
9260 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009261
9262 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9263 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009264 prepare_assert_error(&ga);
9265 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9266 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009267 ga_clear(&ga);
9268 }
9269}
9270
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009271/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009272 * "assert_exception(string[, msg])" function
9273 */
9274 static void
9275f_assert_exception(argvars, rettv)
9276 typval_T *argvars;
9277 typval_T *rettv UNUSED;
9278{
9279 garray_T ga;
9280 char *error;
9281
9282 error = (char *)get_tv_string_chk(&argvars[0]);
9283 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9284 {
9285 prepare_assert_error(&ga);
9286 ga_concat(&ga, (char_u *)"v:exception is not set");
9287 assert_error(&ga);
9288 ga_clear(&ga);
9289 }
9290 else if (strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
9291 {
9292 prepare_assert_error(&ga);
9293 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9294 &vimvars[VV_EXCEPTION].vv_tv);
9295 assert_error(&ga);
9296 ga_clear(&ga);
9297 }
9298}
9299
9300/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009301 * "assert_fails(cmd [, error])" function
9302 */
9303 static void
9304f_assert_fails(argvars, rettv)
9305 typval_T *argvars;
9306 typval_T *rettv UNUSED;
9307{
9308 char_u *cmd = get_tv_string_chk(&argvars[0]);
9309 garray_T ga;
9310
9311 called_emsg = FALSE;
9312 suppress_errthrow = TRUE;
9313 emsg_silent = TRUE;
9314 do_cmdline_cmd(cmd);
9315 if (!called_emsg)
9316 {
9317 prepare_assert_error(&ga);
9318 ga_concat(&ga, (char_u *)"command did not fail: ");
9319 ga_concat(&ga, cmd);
9320 assert_error(&ga);
9321 ga_clear(&ga);
9322 }
9323 else if (argvars[1].v_type != VAR_UNKNOWN)
9324 {
9325 char_u buf[NUMBUFLEN];
9326 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9327
9328 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9329 {
9330 prepare_assert_error(&ga);
9331 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9332 &vimvars[VV_ERRMSG].vv_tv);
9333 assert_error(&ga);
9334 ga_clear(&ga);
9335 }
9336 }
9337
9338 called_emsg = FALSE;
9339 suppress_errthrow = FALSE;
9340 emsg_silent = FALSE;
9341 emsg_on_display = FALSE;
9342 set_vim_var_string(VV_ERRMSG, NULL, 0);
9343}
9344
9345/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009346 * Common for assert_true() and assert_false().
9347 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009348 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009349assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009350 typval_T *argvars;
9351 int isTrue;
9352{
9353 int error = FALSE;
9354 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009355
9356 if (argvars[0].v_type != VAR_NUMBER
9357 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9358 || error)
9359 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009360 prepare_assert_error(&ga);
9361 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009362 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009363 NULL, &argvars[0]);
9364 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009365 ga_clear(&ga);
9366 }
9367}
9368
9369/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009370 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009371 */
9372 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009373f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009374 typval_T *argvars;
9375 typval_T *rettv UNUSED;
9376{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009377 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009378}
9379
9380/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009381 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009382 */
9383 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009384f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009385 typval_T *argvars;
9386 typval_T *rettv UNUSED;
9387{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009388 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009389}
9390
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009391#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009392/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009393 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009394 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009395 static void
9396f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009397 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009398 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009399{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009400 float_T f;
9401
9402 rettv->v_type = VAR_FLOAT;
9403 if (get_float_arg(argvars, &f) == OK)
9404 rettv->vval.v_float = asin(f);
9405 else
9406 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009407}
9408
9409/*
9410 * "atan()" function
9411 */
9412 static void
9413f_atan(argvars, rettv)
9414 typval_T *argvars;
9415 typval_T *rettv;
9416{
9417 float_T f;
9418
9419 rettv->v_type = VAR_FLOAT;
9420 if (get_float_arg(argvars, &f) == OK)
9421 rettv->vval.v_float = atan(f);
9422 else
9423 rettv->vval.v_float = 0.0;
9424}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009425
9426/*
9427 * "atan2()" function
9428 */
9429 static void
9430f_atan2(argvars, rettv)
9431 typval_T *argvars;
9432 typval_T *rettv;
9433{
9434 float_T fx, fy;
9435
9436 rettv->v_type = VAR_FLOAT;
9437 if (get_float_arg(argvars, &fx) == OK
9438 && get_float_arg(&argvars[1], &fy) == OK)
9439 rettv->vval.v_float = atan2(fx, fy);
9440 else
9441 rettv->vval.v_float = 0.0;
9442}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009443#endif
9444
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445/*
9446 * "browse(save, title, initdir, default)" function
9447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009450 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453#ifdef FEAT_BROWSE
9454 int save;
9455 char_u *title;
9456 char_u *initdir;
9457 char_u *defname;
9458 char_u buf[NUMBUFLEN];
9459 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009460 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009462 save = get_tv_number_chk(&argvars[0], &error);
9463 title = get_tv_string_chk(&argvars[1]);
9464 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9465 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 if (error || title == NULL || initdir == NULL || defname == NULL)
9468 rettv->vval.v_string = NULL;
9469 else
9470 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009471 do_browse(save ? BROWSE_SAVE : 0,
9472 title, defname, NULL, initdir, NULL, curbuf);
9473#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009475#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009477}
9478
9479/*
9480 * "browsedir(title, initdir)" function
9481 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009484 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009485 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009486{
9487#ifdef FEAT_BROWSE
9488 char_u *title;
9489 char_u *initdir;
9490 char_u buf[NUMBUFLEN];
9491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009492 title = get_tv_string_chk(&argvars[0]);
9493 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009495 if (title == NULL || initdir == NULL)
9496 rettv->vval.v_string = NULL;
9497 else
9498 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009499 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504}
9505
Bram Moolenaar33570922005-01-25 22:26:29 +00009506static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009507
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508/*
9509 * Find a buffer by number or exact name.
9510 */
9511 static buf_T *
9512find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009517 if (avar->v_type == VAR_NUMBER)
9518 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009519 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009521 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009522 if (buf == NULL)
9523 {
9524 /* No full path name match, try a match with a URL or a "nofile"
9525 * buffer, these don't use the full path. */
9526 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9527 if (buf->b_fname != NULL
9528 && (path_with_url(buf->b_fname)
9529#ifdef FEAT_QUICKFIX
9530 || bt_nofile(buf)
9531#endif
9532 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009533 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009534 break;
9535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 }
9537 return buf;
9538}
9539
9540/*
9541 * "bufexists(expr)" function
9542 */
9543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009545 typval_T *argvars;
9546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549}
9550
9551/*
9552 * "buflisted(expr)" function
9553 */
9554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009556 typval_T *argvars;
9557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559 buf_T *buf;
9560
9561 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563}
9564
9565/*
9566 * "bufloaded(expr)" function
9567 */
9568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009570 typval_T *argvars;
9571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 buf_T *buf;
9574
9575 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577}
9578
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009579static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009580
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581/*
9582 * Get buffer by number or pattern.
9583 */
9584 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009585get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009587 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 int save_magic;
9591 char_u *save_cpo;
9592 buf_T *buf;
9593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 if (tv->v_type == VAR_NUMBER)
9595 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009596 if (tv->v_type != VAR_STRING)
9597 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 if (name == NULL || *name == NUL)
9599 return curbuf;
9600 if (name[0] == '$' && name[1] == NUL)
9601 return lastbuf;
9602
9603 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9604 save_magic = p_magic;
9605 p_magic = TRUE;
9606 save_cpo = p_cpo;
9607 p_cpo = (char_u *)"";
9608
9609 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009610 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611
9612 p_magic = save_magic;
9613 p_cpo = save_cpo;
9614
9615 /* If not found, try expanding the name, like done for bufexists(). */
9616 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618
9619 return buf;
9620}
9621
9622/*
9623 * "bufname(expr)" function
9624 */
9625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 typval_T *argvars;
9628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
9630 buf_T *buf;
9631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009632 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009634 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 --emsg_off;
9641}
9642
9643/*
9644 * "bufnr(expr)" function
9645 */
9646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009648 typval_T *argvars;
9649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
9651 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009652 int error = FALSE;
9653 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009657 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009658 --emsg_off;
9659
9660 /* If the buffer isn't found and the second argument is not zero create a
9661 * new buffer. */
9662 if (buf == NULL
9663 && argvars[1].v_type != VAR_UNKNOWN
9664 && get_tv_number_chk(&argvars[1], &error) != 0
9665 && !error
9666 && (name = get_tv_string_chk(&argvars[0])) != NULL
9667 && !error)
9668 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9669
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674}
9675
9676/*
9677 * "bufwinnr(nr)" function
9678 */
9679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009681 typval_T *argvars;
9682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684#ifdef FEAT_WINDOWS
9685 win_T *wp;
9686 int winnr = 0;
9687#endif
9688 buf_T *buf;
9689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009692 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693#ifdef FEAT_WINDOWS
9694 for (wp = firstwin; wp; wp = wp->w_next)
9695 {
9696 ++winnr;
9697 if (wp->w_buffer == buf)
9698 break;
9699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703#endif
9704 --emsg_off;
9705}
9706
9707/*
9708 * "byte2line(byte)" function
9709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009712 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714{
9715#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717#else
9718 long boff = 0;
9719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009720 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 (linenr_T)0, &boff);
9726#endif
9727}
9728
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009729 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009730byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009731 typval_T *argvars;
9732 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009733 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009734{
9735#ifdef FEAT_MBYTE
9736 char_u *t;
9737#endif
9738 char_u *str;
9739 long idx;
9740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009741 str = get_tv_string_chk(&argvars[0]);
9742 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009745 return;
9746
9747#ifdef FEAT_MBYTE
9748 t = str;
9749 for ( ; idx > 0; idx--)
9750 {
9751 if (*t == NUL) /* EOL reached */
9752 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009753 if (enc_utf8 && comp)
9754 t += utf_ptr2len(t);
9755 else
9756 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009757 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009758 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009759#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009760 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009762#endif
9763}
9764
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009765/*
9766 * "byteidx()" function
9767 */
9768 static void
9769f_byteidx(argvars, rettv)
9770 typval_T *argvars;
9771 typval_T *rettv;
9772{
9773 byteidx(argvars, rettv, FALSE);
9774}
9775
9776/*
9777 * "byteidxcomp()" function
9778 */
9779 static void
9780f_byteidxcomp(argvars, rettv)
9781 typval_T *argvars;
9782 typval_T *rettv;
9783{
9784 byteidx(argvars, rettv, TRUE);
9785}
9786
Bram Moolenaardb913952012-06-29 12:54:53 +02009787 int
9788func_call(name, args, selfdict, rettv)
9789 char_u *name;
9790 typval_T *args;
9791 dict_T *selfdict;
9792 typval_T *rettv;
9793{
9794 listitem_T *item;
9795 typval_T argv[MAX_FUNC_ARGS + 1];
9796 int argc = 0;
9797 int dummy;
9798 int r = 0;
9799
9800 for (item = args->vval.v_list->lv_first; item != NULL;
9801 item = item->li_next)
9802 {
9803 if (argc == MAX_FUNC_ARGS)
9804 {
9805 EMSG(_("E699: Too many arguments"));
9806 break;
9807 }
9808 /* Make a copy of each argument. This is needed to be able to set
9809 * v_lock to VAR_FIXED in the copy without changing the original list.
9810 */
9811 copy_tv(&item->li_tv, &argv[argc++]);
9812 }
9813
9814 if (item == NULL)
9815 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9816 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9817 &dummy, TRUE, selfdict);
9818
9819 /* Free the arguments. */
9820 while (argc > 0)
9821 clear_tv(&argv[--argc]);
9822
9823 return r;
9824}
9825
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009826/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009827 * "call(func, arglist)" function
9828 */
9829 static void
9830f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 typval_T *argvars;
9832 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009833{
9834 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009835 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009836
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009837 if (argvars[1].v_type != VAR_LIST)
9838 {
9839 EMSG(_(e_listreq));
9840 return;
9841 }
9842 if (argvars[1].vval.v_list == NULL)
9843 return;
9844
9845 if (argvars[0].v_type == VAR_FUNC)
9846 func = argvars[0].vval.v_string;
9847 else
9848 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009849 if (*func == NUL)
9850 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009851
Bram Moolenaare9a41262005-01-15 22:18:47 +00009852 if (argvars[2].v_type != VAR_UNKNOWN)
9853 {
9854 if (argvars[2].v_type != VAR_DICT)
9855 {
9856 EMSG(_(e_dictreq));
9857 return;
9858 }
9859 selfdict = argvars[2].vval.v_dict;
9860 }
9861
Bram Moolenaardb913952012-06-29 12:54:53 +02009862 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009863}
9864
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009865#ifdef FEAT_FLOAT
9866/*
9867 * "ceil({float})" function
9868 */
9869 static void
9870f_ceil(argvars, rettv)
9871 typval_T *argvars;
9872 typval_T *rettv;
9873{
9874 float_T f;
9875
9876 rettv->v_type = VAR_FLOAT;
9877 if (get_float_arg(argvars, &f) == OK)
9878 rettv->vval.v_float = ceil(f);
9879 else
9880 rettv->vval.v_float = 0.0;
9881}
9882#endif
9883
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009884/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009885 * "changenr()" function
9886 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009887 static void
9888f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009889 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009890 typval_T *rettv;
9891{
9892 rettv->vval.v_number = curbuf->b_u_seq_cur;
9893}
9894
9895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 * "char2nr(string)" function
9897 */
9898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009900 typval_T *argvars;
9901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902{
9903#ifdef FEAT_MBYTE
9904 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009905 {
9906 int utf8 = 0;
9907
9908 if (argvars[1].v_type != VAR_UNKNOWN)
9909 utf8 = get_tv_number_chk(&argvars[1], NULL);
9910
9911 if (utf8)
9912 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9913 else
9914 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 else
9917#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919}
9920
9921/*
9922 * "cindent(lnum)" function
9923 */
9924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
9929#ifdef FEAT_CINDENT
9930 pos_T pos;
9931 linenr_T lnum;
9932
9933 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9936 {
9937 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 curwin->w_cursor = pos;
9940 }
9941 else
9942#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944}
9945
9946/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009947 * "clearmatches()" function
9948 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009949 static void
9950f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009951 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009952 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009953{
9954#ifdef FEAT_SEARCH_EXTRA
9955 clear_matches(curwin);
9956#endif
9957}
9958
9959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 * "col(string)" function
9961 */
9962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009964 typval_T *argvars;
9965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
9967 colnr_T col = 0;
9968 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009969 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009971 fp = var2fpos(&argvars[0], FALSE, &fnum);
9972 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 {
9974 if (fp->col == MAXCOL)
9975 {
9976 /* '> can be MAXCOL, get the length of the line then */
9977 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009978 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 else
9980 col = MAXCOL;
9981 }
9982 else
9983 {
9984 col = fp->col + 1;
9985#ifdef FEAT_VIRTUALEDIT
9986 /* col(".") when the cursor is on the NUL at the end of the line
9987 * because of "coladd" can be seen as an extra column. */
9988 if (virtual_active() && fp == &curwin->w_cursor)
9989 {
9990 char_u *p = ml_get_cursor();
9991
9992 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9993 curwin->w_virtcol - curwin->w_cursor.coladd))
9994 {
9995# ifdef FEAT_MBYTE
9996 int l;
9997
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009998 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 col += l;
10000# else
10001 if (*p != NUL && p[1] == NUL)
10002 ++col;
10003# endif
10004 }
10005 }
10006#endif
10007 }
10008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010}
10011
Bram Moolenaar572cb562005-08-05 21:35:02 +000010012#if defined(FEAT_INS_EXPAND)
10013/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010014 * "complete()" function
10015 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010016 static void
10017f_complete(argvars, rettv)
10018 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010019 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010020{
10021 int startcol;
10022
10023 if ((State & INSERT) == 0)
10024 {
10025 EMSG(_("E785: complete() can only be used in Insert mode"));
10026 return;
10027 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010028
10029 /* Check for undo allowed here, because if something was already inserted
10030 * the line was already saved for undo and this check isn't done. */
10031 if (!undo_allowed())
10032 return;
10033
Bram Moolenaarade00832006-03-10 21:46:58 +000010034 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10035 {
10036 EMSG(_(e_invarg));
10037 return;
10038 }
10039
10040 startcol = get_tv_number_chk(&argvars[0], NULL);
10041 if (startcol <= 0)
10042 return;
10043
10044 set_completion(startcol - 1, argvars[1].vval.v_list);
10045}
10046
10047/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010048 * "complete_add()" function
10049 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010050 static void
10051f_complete_add(argvars, rettv)
10052 typval_T *argvars;
10053 typval_T *rettv;
10054{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010055 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010056}
10057
10058/*
10059 * "complete_check()" function
10060 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010061 static void
10062f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010063 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010064 typval_T *rettv;
10065{
10066 int saved = RedrawingDisabled;
10067
10068 RedrawingDisabled = 0;
10069 ins_compl_check_keys(0);
10070 rettv->vval.v_number = compl_interrupted;
10071 RedrawingDisabled = saved;
10072}
10073#endif
10074
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075/*
10076 * "confirm(message, buttons[, default [, type]])" function
10077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010080 typval_T *argvars UNUSED;
10081 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10084 char_u *message;
10085 char_u *buttons = NULL;
10086 char_u buf[NUMBUFLEN];
10087 char_u buf2[NUMBUFLEN];
10088 int def = 1;
10089 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 char_u *typestr;
10091 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 message = get_tv_string_chk(&argvars[0]);
10094 if (message == NULL)
10095 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010096 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10099 if (buttons == NULL)
10100 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010101 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010104 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10107 if (typestr == NULL)
10108 error = TRUE;
10109 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 switch (TOUPPER_ASC(*typestr))
10112 {
10113 case 'E': type = VIM_ERROR; break;
10114 case 'Q': type = VIM_QUESTION; break;
10115 case 'I': type = VIM_INFO; break;
10116 case 'W': type = VIM_WARNING; break;
10117 case 'G': type = VIM_GENERIC; break;
10118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 }
10120 }
10121 }
10122 }
10123
10124 if (buttons == NULL || *buttons == NUL)
10125 buttons = (char_u *)_("&Ok");
10126
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010127 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010129 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130#endif
10131}
10132
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010133/*
10134 * "copy()" function
10135 */
10136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010138 typval_T *argvars;
10139 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010140{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010141 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010142}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010144#ifdef FEAT_FLOAT
10145/*
10146 * "cos()" function
10147 */
10148 static void
10149f_cos(argvars, rettv)
10150 typval_T *argvars;
10151 typval_T *rettv;
10152{
10153 float_T f;
10154
10155 rettv->v_type = VAR_FLOAT;
10156 if (get_float_arg(argvars, &f) == OK)
10157 rettv->vval.v_float = cos(f);
10158 else
10159 rettv->vval.v_float = 0.0;
10160}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010161
10162/*
10163 * "cosh()" function
10164 */
10165 static void
10166f_cosh(argvars, rettv)
10167 typval_T *argvars;
10168 typval_T *rettv;
10169{
10170 float_T f;
10171
10172 rettv->v_type = VAR_FLOAT;
10173 if (get_float_arg(argvars, &f) == OK)
10174 rettv->vval.v_float = cosh(f);
10175 else
10176 rettv->vval.v_float = 0.0;
10177}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010178#endif
10179
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010181 * "count()" function
10182 */
10183 static void
10184f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010185 typval_T *argvars;
10186 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010187{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010188 long n = 0;
10189 int ic = FALSE;
10190
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010192 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010193 listitem_T *li;
10194 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010195 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010196
Bram Moolenaare9a41262005-01-15 22:18:47 +000010197 if ((l = argvars[0].vval.v_list) != NULL)
10198 {
10199 li = l->lv_first;
10200 if (argvars[2].v_type != VAR_UNKNOWN)
10201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 int error = FALSE;
10203
10204 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 if (argvars[3].v_type != VAR_UNKNOWN)
10206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 idx = get_tv_number_chk(&argvars[3], &error);
10208 if (!error)
10209 {
10210 li = list_find(l, idx);
10211 if (li == NULL)
10212 EMSGN(_(e_listidx), idx);
10213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 if (error)
10216 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010217 }
10218
10219 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010220 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221 ++n;
10222 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010224 else if (argvars[0].v_type == VAR_DICT)
10225 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010226 int todo;
10227 dict_T *d;
10228 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010230 if ((d = argvars[0].vval.v_dict) != NULL)
10231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 int error = FALSE;
10233
Bram Moolenaare9a41262005-01-15 22:18:47 +000010234 if (argvars[2].v_type != VAR_UNKNOWN)
10235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 if (argvars[3].v_type != VAR_UNKNOWN)
10238 EMSG(_(e_invarg));
10239 }
10240
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010241 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010243 {
10244 if (!HASHITEM_EMPTY(hi))
10245 {
10246 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010247 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010248 ++n;
10249 }
10250 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251 }
10252 }
10253 else
10254 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010255 rettv->vval.v_number = n;
10256}
10257
10258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10260 *
10261 * Checks the existence of a cscope connection.
10262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010265 typval_T *argvars UNUSED;
10266 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267{
10268#ifdef FEAT_CSCOPE
10269 int num = 0;
10270 char_u *dbpath = NULL;
10271 char_u *prepend = NULL;
10272 char_u buf[NUMBUFLEN];
10273
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010274 if (argvars[0].v_type != VAR_UNKNOWN
10275 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 num = (int)get_tv_number(&argvars[0]);
10278 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010279 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 }
10282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010283 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284#endif
10285}
10286
10287/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010288 * "cursor(lnum, col)" function, or
10289 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010291 * Moves the cursor to the specified line and column.
10292 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010296 typval_T *argvars;
10297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
10299 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010300#ifdef FEAT_VIRTUALEDIT
10301 long coladd = 0;
10302#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010303 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010305 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010306 if (argvars[1].v_type == VAR_UNKNOWN)
10307 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010308 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010309 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010310
Bram Moolenaar493c1782014-05-28 14:34:46 +020010311 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010312 {
10313 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010314 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010315 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010316 line = pos.lnum;
10317 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010318#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010319 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010320#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010321 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010322 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010323 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010324 set_curswant = FALSE;
10325 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010326 }
10327 else
10328 {
10329 line = get_tv_lnum(argvars);
10330 col = get_tv_number_chk(&argvars[1], NULL);
10331#ifdef FEAT_VIRTUALEDIT
10332 if (argvars[2].v_type != VAR_UNKNOWN)
10333 coladd = get_tv_number_chk(&argvars[2], NULL);
10334#endif
10335 }
10336 if (line < 0 || col < 0
10337#ifdef FEAT_VIRTUALEDIT
10338 || coladd < 0
10339#endif
10340 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010341 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 if (line > 0)
10343 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 if (col > 0)
10345 curwin->w_cursor.col = col - 1;
10346#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010347 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348#endif
10349
10350 /* Make sure the cursor is in a valid position. */
10351 check_cursor();
10352#ifdef FEAT_MBYTE
10353 /* Correct cursor for multi-byte character. */
10354 if (has_mbyte)
10355 mb_adjust_cursor();
10356#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010357
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010358 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010359 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360}
10361
10362/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010363 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 */
10365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010367 typval_T *argvars;
10368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010370 int noref = 0;
10371
10372 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010373 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010374 if (noref < 0 || noref > 1)
10375 EMSG(_(e_invarg));
10376 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010377 {
10378 current_copyID += COPYID_INC;
10379 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381}
10382
10383/*
10384 * "delete()" function
10385 */
10386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010387f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010388 typval_T *argvars;
10389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390{
10391 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010392 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010394 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395}
10396
10397/*
10398 * "did_filetype()" function
10399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010402 typval_T *argvars UNUSED;
10403 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404{
10405#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407#endif
10408}
10409
10410/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010411 * "diff_filler()" function
10412 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010414f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010415 typval_T *argvars UNUSED;
10416 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010417{
10418#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010420#endif
10421}
10422
10423/*
10424 * "diff_hlID()" function
10425 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010428 typval_T *argvars UNUSED;
10429 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010430{
10431#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010433 static linenr_T prev_lnum = 0;
10434 static int changedtick = 0;
10435 static int fnum = 0;
10436 static int change_start = 0;
10437 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010438 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010439 int filler_lines;
10440 int col;
10441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 if (lnum < 0) /* ignore type error in {lnum} arg */
10443 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010444 if (lnum != prev_lnum
10445 || changedtick != curbuf->b_changedtick
10446 || fnum != curbuf->b_fnum)
10447 {
10448 /* New line, buffer, change: need to get the values. */
10449 filler_lines = diff_check(curwin, lnum);
10450 if (filler_lines < 0)
10451 {
10452 if (filler_lines == -1)
10453 {
10454 change_start = MAXCOL;
10455 change_end = -1;
10456 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10457 hlID = HLF_ADD; /* added line */
10458 else
10459 hlID = HLF_CHD; /* changed line */
10460 }
10461 else
10462 hlID = HLF_ADD; /* added line */
10463 }
10464 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010465 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010466 prev_lnum = lnum;
10467 changedtick = curbuf->b_changedtick;
10468 fnum = curbuf->b_fnum;
10469 }
10470
10471 if (hlID == HLF_CHD || hlID == HLF_TXD)
10472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010474 if (col >= change_start && col <= change_end)
10475 hlID = HLF_TXD; /* changed text */
10476 else
10477 hlID = HLF_CHD; /* changed line */
10478 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010479 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010480#endif
10481}
10482
10483/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010484 * "empty({expr})" function
10485 */
10486 static void
10487f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010488 typval_T *argvars;
10489 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010490{
10491 int n;
10492
10493 switch (argvars[0].v_type)
10494 {
10495 case VAR_STRING:
10496 case VAR_FUNC:
10497 n = argvars[0].vval.v_string == NULL
10498 || *argvars[0].vval.v_string == NUL;
10499 break;
10500 case VAR_NUMBER:
10501 n = argvars[0].vval.v_number == 0;
10502 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010503#ifdef FEAT_FLOAT
10504 case VAR_FLOAT:
10505 n = argvars[0].vval.v_float == 0.0;
10506 break;
10507#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010508 case VAR_LIST:
10509 n = argvars[0].vval.v_list == NULL
10510 || argvars[0].vval.v_list->lv_first == NULL;
10511 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010512 case VAR_DICT:
10513 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010514 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010516 default:
10517 EMSG2(_(e_intern2), "f_empty()");
10518 n = 0;
10519 }
10520
10521 rettv->vval.v_number = n;
10522}
10523
10524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 * "escape({string}, {chars})" function
10526 */
10527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010529 typval_T *argvars;
10530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532 char_u buf[NUMBUFLEN];
10533
Bram Moolenaar758711c2005-02-02 23:11:38 +000010534 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10535 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537}
10538
10539/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010540 * "eval()" function
10541 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010542 static void
10543f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010544 typval_T *argvars;
10545 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010546{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010547 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010549 s = get_tv_string_chk(&argvars[0]);
10550 if (s != NULL)
10551 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010552
Bram Moolenaar615b9972015-01-14 17:15:05 +010010553 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10555 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010556 if (p != NULL && !aborting())
10557 EMSG2(_(e_invexpr2), p);
10558 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010560 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010562 else if (*s != NUL)
10563 EMSG(_(e_trailing));
10564}
10565
10566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 * "eventhandler()" function
10568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010570f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575}
10576
10577/*
10578 * "executable()" function
10579 */
10580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010582 typval_T *argvars;
10583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010585 char_u *name = get_tv_string(&argvars[0]);
10586
10587 /* Check in $PATH and also check directly if there is a directory name. */
10588 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10589 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010590}
10591
10592/*
10593 * "exepath()" function
10594 */
10595 static void
10596f_exepath(argvars, rettv)
10597 typval_T *argvars;
10598 typval_T *rettv;
10599{
10600 char_u *p = NULL;
10601
Bram Moolenaarb5971142015-03-21 17:32:19 +010010602 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010603 rettv->v_type = VAR_STRING;
10604 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605}
10606
10607/*
10608 * "exists()" function
10609 */
10610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010612 typval_T *argvars;
10613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614{
10615 char_u *p;
10616 char_u *name;
10617 int n = FALSE;
10618 int len = 0;
10619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 if (*p == '$') /* environment variable */
10622 {
10623 /* first try "normal" environment variables (fast) */
10624 if (mch_getenv(p + 1) != NULL)
10625 n = TRUE;
10626 else
10627 {
10628 /* try expanding things like $VIM and ${HOME} */
10629 p = expand_env_save(p);
10630 if (p != NULL && *p != '$')
10631 n = TRUE;
10632 vim_free(p);
10633 }
10634 }
10635 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010638 if (*skipwhite(p) != NUL)
10639 n = FALSE; /* trailing garbage */
10640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 else if (*p == '*') /* internal or user defined function */
10642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010643 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 }
10645 else if (*p == ':')
10646 {
10647 n = cmd_exists(p + 1);
10648 }
10649 else if (*p == '#')
10650 {
10651#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010652 if (p[1] == '#')
10653 n = autocmd_supported(p + 2);
10654 else
10655 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656#endif
10657 }
10658 else /* internal variable */
10659 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010660 char_u *tofree;
10661 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010663 /* get_name_len() takes care of expanding curly braces */
10664 name = p;
10665 len = get_name_len(&p, &tofree, TRUE, FALSE);
10666 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010668 if (tofree != NULL)
10669 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010670 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010671 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010673 /* handle d.key, l[idx], f(expr) */
10674 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10675 if (n)
10676 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 }
10678 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010679 if (*p != NUL)
10680 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010682 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 }
10684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686}
10687
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010688#ifdef FEAT_FLOAT
10689/*
10690 * "exp()" function
10691 */
10692 static void
10693f_exp(argvars, rettv)
10694 typval_T *argvars;
10695 typval_T *rettv;
10696{
10697 float_T f;
10698
10699 rettv->v_type = VAR_FLOAT;
10700 if (get_float_arg(argvars, &f) == OK)
10701 rettv->vval.v_float = exp(f);
10702 else
10703 rettv->vval.v_float = 0.0;
10704}
10705#endif
10706
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707/*
10708 * "expand()" function
10709 */
10710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010712 typval_T *argvars;
10713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715 char_u *s;
10716 int len;
10717 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010718 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010720 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010721 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010724 if (argvars[1].v_type != VAR_UNKNOWN
10725 && argvars[2].v_type != VAR_UNKNOWN
10726 && get_tv_number_chk(&argvars[2], &error)
10727 && !error)
10728 {
10729 rettv->v_type = VAR_LIST;
10730 rettv->vval.v_list = NULL;
10731 }
10732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 if (*s == '%' || *s == '#' || *s == '<')
10735 {
10736 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010737 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010739 if (rettv->v_type == VAR_LIST)
10740 {
10741 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10742 list_append_string(rettv->vval.v_list, result, -1);
10743 else
10744 vim_free(result);
10745 }
10746 else
10747 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 }
10749 else
10750 {
10751 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010752 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 if (argvars[1].v_type != VAR_UNKNOWN
10754 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010755 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756 if (!error)
10757 {
10758 ExpandInit(&xpc);
10759 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010760 if (p_wic)
10761 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010762 if (rettv->v_type == VAR_STRING)
10763 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10764 options, WILD_ALL);
10765 else if (rettv_list_alloc(rettv) != FAIL)
10766 {
10767 int i;
10768
10769 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10770 for (i = 0; i < xpc.xp_numfiles; i++)
10771 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10772 ExpandCleanup(&xpc);
10773 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010774 }
10775 else
10776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 }
10778}
10779
10780/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010781 * Go over all entries in "d2" and add them to "d1".
10782 * When "action" is "error" then a duplicate key is an error.
10783 * When "action" is "force" then a duplicate key is overwritten.
10784 * Otherwise duplicate keys are ignored ("action" is "keep").
10785 */
10786 void
10787dict_extend(d1, d2, action)
10788 dict_T *d1;
10789 dict_T *d2;
10790 char_u *action;
10791{
10792 dictitem_T *di1;
10793 hashitem_T *hi2;
10794 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010795 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010796
10797 todo = (int)d2->dv_hashtab.ht_used;
10798 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10799 {
10800 if (!HASHITEM_EMPTY(hi2))
10801 {
10802 --todo;
10803 di1 = dict_find(d1, hi2->hi_key, -1);
10804 if (d1->dv_scope != 0)
10805 {
10806 /* Disallow replacing a builtin function in l: and g:.
10807 * Check the key to be valid when adding to any
10808 * scope. */
10809 if (d1->dv_scope == VAR_DEF_SCOPE
10810 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10811 && var_check_func_name(hi2->hi_key,
10812 di1 == NULL))
10813 break;
10814 if (!valid_varname(hi2->hi_key))
10815 break;
10816 }
10817 if (di1 == NULL)
10818 {
10819 di1 = dictitem_copy(HI2DI(hi2));
10820 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10821 dictitem_free(di1);
10822 }
10823 else if (*action == 'e')
10824 {
10825 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10826 break;
10827 }
10828 else if (*action == 'f' && HI2DI(hi2) != di1)
10829 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010830 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10831 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010832 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010833 clear_tv(&di1->di_tv);
10834 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10835 }
10836 }
10837 }
10838}
10839
10840/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010841 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010843 */
10844 static void
10845f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010846 typval_T *argvars;
10847 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010848{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010849 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010850
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010852 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010853 list_T *l1, *l2;
10854 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010857
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 l1 = argvars[0].vval.v_list;
10859 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010860 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010861 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010862 {
10863 if (argvars[2].v_type != VAR_UNKNOWN)
10864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 before = get_tv_number_chk(&argvars[2], &error);
10866 if (error)
10867 return; /* type error; errmsg already given */
10868
Bram Moolenaar758711c2005-02-02 23:11:38 +000010869 if (before == l1->lv_len)
10870 item = NULL;
10871 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010872 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010873 item = list_find(l1, before);
10874 if (item == NULL)
10875 {
10876 EMSGN(_(e_listidx), before);
10877 return;
10878 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010879 }
10880 }
10881 else
10882 item = NULL;
10883 list_extend(l1, l2, item);
10884
Bram Moolenaare9a41262005-01-15 22:18:47 +000010885 copy_tv(&argvars[0], rettv);
10886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10889 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010890 dict_T *d1, *d2;
10891 char_u *action;
10892 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010893
10894 d1 = argvars[0].vval.v_dict;
10895 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010896 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010897 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010898 {
10899 /* Check the third argument. */
10900 if (argvars[2].v_type != VAR_UNKNOWN)
10901 {
10902 static char *(av[]) = {"keep", "force", "error"};
10903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 action = get_tv_string_chk(&argvars[2]);
10905 if (action == NULL)
10906 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 for (i = 0; i < 3; ++i)
10908 if (STRCMP(action, av[i]) == 0)
10909 break;
10910 if (i == 3)
10911 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010912 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010913 return;
10914 }
10915 }
10916 else
10917 action = (char_u *)"force";
10918
Bram Moolenaara9922d62013-05-30 13:01:18 +020010919 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920
Bram Moolenaare9a41262005-01-15 22:18:47 +000010921 copy_tv(&argvars[0], rettv);
10922 }
10923 }
10924 else
10925 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010926}
10927
10928/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010929 * "feedkeys()" function
10930 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010931 static void
10932f_feedkeys(argvars, rettv)
10933 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010934 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010935{
10936 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010937 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010938 char_u *keys, *flags;
10939 char_u nbuf[NUMBUFLEN];
10940 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010941 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010942
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010943 /* This is not allowed in the sandbox. If the commands would still be
10944 * executed in the sandbox it would be OK, but it probably happens later,
10945 * when "sandbox" is no longer set. */
10946 if (check_secure())
10947 return;
10948
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010949 keys = get_tv_string(&argvars[0]);
10950 if (*keys != NUL)
10951 {
10952 if (argvars[1].v_type != VAR_UNKNOWN)
10953 {
10954 flags = get_tv_string_buf(&argvars[1], nbuf);
10955 for ( ; *flags != NUL; ++flags)
10956 {
10957 switch (*flags)
10958 {
10959 case 'n': remap = FALSE; break;
10960 case 'm': remap = TRUE; break;
10961 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010962 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010963 }
10964 }
10965 }
10966
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010967 /* Need to escape K_SPECIAL and CSI before putting the string in the
10968 * typeahead buffer. */
10969 keys_esc = vim_strsave_escape_csi(keys);
10970 if (keys_esc != NULL)
10971 {
10972 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010973 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010974 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010975 if (vgetc_busy)
10976 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010977 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010978 }
10979}
10980
10981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 * "filereadable()" function
10983 */
10984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010986 typval_T *argvars;
10987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010989 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 char_u *p;
10991 int n;
10992
Bram Moolenaarc236c162008-07-13 17:41:49 +000010993#ifndef O_NONBLOCK
10994# define O_NONBLOCK 0
10995#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010997 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10998 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 {
11000 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011001 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 }
11003 else
11004 n = FALSE;
11005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007}
11008
11009/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011010 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 * rights to write into.
11012 */
11013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011015 typval_T *argvars;
11016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011018 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019}
11020
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011021static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011022
11023 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011024findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011025 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011027 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011028{
11029#ifdef FEAT_SEARCHPATH
11030 char_u *fname;
11031 char_u *fresult = NULL;
11032 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11033 char_u *p;
11034 char_u pathbuf[NUMBUFLEN];
11035 int count = 1;
11036 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011037 int error = FALSE;
11038#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011039
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011040 rettv->vval.v_string = NULL;
11041 rettv->v_type = VAR_STRING;
11042
11043#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011045
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011046 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11049 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011050 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 else
11052 {
11053 if (*p != NUL)
11054 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011057 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011059 }
11060
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011061 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11062 error = TRUE;
11063
11064 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 do
11067 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011068 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011069 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 fresult = find_file_in_path_option(first ? fname : NULL,
11071 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011072 0, first, path,
11073 find_what,
11074 curbuf->b_ffname,
11075 find_what == FINDFILE_DIR
11076 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011078
11079 if (fresult != NULL && rettv->v_type == VAR_LIST)
11080 list_append_string(rettv->vval.v_list, fresult, -1);
11081
11082 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011084
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011085 if (rettv->v_type == VAR_STRING)
11086 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011087#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011088}
11089
Bram Moolenaar33570922005-01-25 22:26:29 +000011090static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11091static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011092
11093/*
11094 * Implementation of map() and filter().
11095 */
11096 static void
11097filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011098 typval_T *argvars;
11099 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011100 int map;
11101{
11102 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 listitem_T *li, *nli;
11105 list_T *l = NULL;
11106 dictitem_T *di;
11107 hashtab_T *ht;
11108 hashitem_T *hi;
11109 dict_T *d = NULL;
11110 typval_T save_val;
11111 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011113 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011114 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011115 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011116 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011117 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011118 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011119
Bram Moolenaare9a41262005-01-15 22:18:47 +000011120 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011121 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011122 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011123 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 return;
11125 }
11126 else if (argvars[0].v_type == VAR_DICT)
11127 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011128 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011129 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 return;
11131 }
11132 else
11133 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011134 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135 return;
11136 }
11137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011138 expr = get_tv_string_buf_chk(&argvars[1], buf);
11139 /* On type errors, the preceding call has already displayed an error
11140 * message. Avoid a misleading error message for an empty string that
11141 * was not passed as argument. */
11142 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011144 prepare_vimvar(VV_VAL, &save_val);
11145 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011146
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011147 /* We reset "did_emsg" to be able to detect whether an error
11148 * occurred during evaluation of the expression. */
11149 save_did_emsg = did_emsg;
11150 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011151
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011152 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011153 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 vimvars[VV_KEY].vv_type = VAR_STRING;
11156
11157 ht = &d->dv_hashtab;
11158 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011159 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 if (!HASHITEM_EMPTY(hi))
11163 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011164 int r;
11165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 --todo;
11167 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011168 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011169 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11170 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 break;
11172 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011173 r = filter_map_one(&di->di_tv, expr, map, &rem);
11174 clear_tv(&vimvars[VV_KEY].vv_tv);
11175 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011176 break;
11177 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011178 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011179 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11180 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011181 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011182 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011183 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 }
11185 }
11186 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 }
11188 else
11189 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011190 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 for (li = l->lv_first; li != NULL; li = nli)
11193 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011194 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011195 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011197 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011198 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011199 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011200 break;
11201 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011203 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011204 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011205 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011206
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011207 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011209
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011210 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011211 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011212
11213 copy_tv(&argvars[0], rettv);
11214}
11215
11216 static int
11217filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011218 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011219 char_u *expr;
11220 int map;
11221 int *remp;
11222{
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011224 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011225 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011226
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 s = expr;
11229 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011230 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231 if (*s != NUL) /* check for trailing chars after expr */
11232 {
11233 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011234 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011235 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 }
11237 if (map)
11238 {
11239 /* map(): replace the list item value */
11240 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011241 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011242 *tv = rettv;
11243 }
11244 else
11245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011246 int error = FALSE;
11247
Bram Moolenaare9a41262005-01-15 22:18:47 +000011248 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011249 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 /* On type error, nothing has been removed; return FAIL to stop the
11252 * loop. The error message was given by get_tv_number_chk(). */
11253 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011254 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011255 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011256 retval = OK;
11257theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011258 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011259 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011260}
11261
11262/*
11263 * "filter()" function
11264 */
11265 static void
11266f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011267 typval_T *argvars;
11268 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011269{
11270 filter_map(argvars, rettv, FALSE);
11271}
11272
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011273/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011274 * "finddir({fname}[, {path}[, {count}]])" function
11275 */
11276 static void
11277f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011278 typval_T *argvars;
11279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011280{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011281 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011282}
11283
11284/*
11285 * "findfile({fname}[, {path}[, {count}]])" function
11286 */
11287 static void
11288f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011289 typval_T *argvars;
11290 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011291{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011292 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011293}
11294
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011295#ifdef FEAT_FLOAT
11296/*
11297 * "float2nr({float})" function
11298 */
11299 static void
11300f_float2nr(argvars, rettv)
11301 typval_T *argvars;
11302 typval_T *rettv;
11303{
11304 float_T f;
11305
11306 if (get_float_arg(argvars, &f) == OK)
11307 {
11308 if (f < -0x7fffffff)
11309 rettv->vval.v_number = -0x7fffffff;
11310 else if (f > 0x7fffffff)
11311 rettv->vval.v_number = 0x7fffffff;
11312 else
11313 rettv->vval.v_number = (varnumber_T)f;
11314 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011315}
11316
11317/*
11318 * "floor({float})" function
11319 */
11320 static void
11321f_floor(argvars, rettv)
11322 typval_T *argvars;
11323 typval_T *rettv;
11324{
11325 float_T f;
11326
11327 rettv->v_type = VAR_FLOAT;
11328 if (get_float_arg(argvars, &f) == OK)
11329 rettv->vval.v_float = floor(f);
11330 else
11331 rettv->vval.v_float = 0.0;
11332}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011333
11334/*
11335 * "fmod()" function
11336 */
11337 static void
11338f_fmod(argvars, rettv)
11339 typval_T *argvars;
11340 typval_T *rettv;
11341{
11342 float_T fx, fy;
11343
11344 rettv->v_type = VAR_FLOAT;
11345 if (get_float_arg(argvars, &fx) == OK
11346 && get_float_arg(&argvars[1], &fy) == OK)
11347 rettv->vval.v_float = fmod(fx, fy);
11348 else
11349 rettv->vval.v_float = 0.0;
11350}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011351#endif
11352
Bram Moolenaar0d660222005-01-07 21:51:51 +000011353/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011354 * "fnameescape({string})" function
11355 */
11356 static void
11357f_fnameescape(argvars, rettv)
11358 typval_T *argvars;
11359 typval_T *rettv;
11360{
11361 rettv->vval.v_string = vim_strsave_fnameescape(
11362 get_tv_string(&argvars[0]), FALSE);
11363 rettv->v_type = VAR_STRING;
11364}
11365
11366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 * "fnamemodify({fname}, {mods})" function
11368 */
11369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011371 typval_T *argvars;
11372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373{
11374 char_u *fname;
11375 char_u *mods;
11376 int usedlen = 0;
11377 int len;
11378 char_u *fbuf = NULL;
11379 char_u buf[NUMBUFLEN];
11380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 fname = get_tv_string_chk(&argvars[0]);
11382 mods = get_tv_string_buf_chk(&argvars[1], buf);
11383 if (fname == NULL || mods == NULL)
11384 fname = NULL;
11385 else
11386 {
11387 len = (int)STRLEN(fname);
11388 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 vim_free(fbuf);
11397}
11398
Bram Moolenaar33570922005-01-25 22:26:29 +000011399static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400
11401/*
11402 * "foldclosed()" function
11403 */
11404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011405foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011406 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011407 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011408 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409{
11410#ifdef FEAT_FOLDING
11411 linenr_T lnum;
11412 linenr_T first, last;
11413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11416 {
11417 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11418 {
11419 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 return;
11424 }
11425 }
11426#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428}
11429
11430/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011431 * "foldclosed()" function
11432 */
11433 static void
11434f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011435 typval_T *argvars;
11436 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011437{
11438 foldclosed_both(argvars, rettv, FALSE);
11439}
11440
11441/*
11442 * "foldclosedend()" function
11443 */
11444 static void
11445f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011446 typval_T *argvars;
11447 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011448{
11449 foldclosed_both(argvars, rettv, TRUE);
11450}
11451
11452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453 * "foldlevel()" function
11454 */
11455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011457 typval_T *argvars UNUSED;
11458 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
11460#ifdef FEAT_FOLDING
11461 linenr_T lnum;
11462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467}
11468
11469/*
11470 * "foldtext()" function
11471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011474 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476{
11477#ifdef FEAT_FOLDING
11478 linenr_T lnum;
11479 char_u *s;
11480 char_u *r;
11481 int len;
11482 char *txt;
11483#endif
11484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->v_type = VAR_STRING;
11486 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011488 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11489 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11490 <= curbuf->b_ml.ml_line_count
11491 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 {
11493 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11495 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 {
11497 if (!linewhite(lnum))
11498 break;
11499 ++lnum;
11500 }
11501
11502 /* Find interesting text in this line. */
11503 s = skipwhite(ml_get(lnum));
11504 /* skip C comment-start */
11505 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011508 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011509 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011510 {
11511 s = skipwhite(ml_get(lnum + 1));
11512 if (*s == '*')
11513 s = skipwhite(s + 1);
11514 }
11515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 txt = _("+-%s%3ld lines: ");
11517 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 + 20 /* for %3ld */
11520 + STRLEN(s))); /* concatenated */
11521 if (r != NULL)
11522 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011523 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11524 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11525 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 len = (int)STRLEN(r);
11527 STRCAT(r, s);
11528 /* remove 'foldmarker' and 'commentstring' */
11529 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531 }
11532 }
11533#endif
11534}
11535
11536/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011537 * "foldtextresult(lnum)" function
11538 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011543{
11544#ifdef FEAT_FOLDING
11545 linenr_T lnum;
11546 char_u *text;
11547 char_u buf[51];
11548 foldinfo_T foldinfo;
11549 int fold_count;
11550#endif
11551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->v_type = VAR_STRING;
11553 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011554#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011556 /* treat illegal types and illegal string values for {lnum} the same */
11557 if (lnum < 0)
11558 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011559 fold_count = foldedCount(curwin, lnum, &foldinfo);
11560 if (fold_count > 0)
11561 {
11562 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11563 &foldinfo, buf);
11564 if (text == buf)
11565 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011567 }
11568#endif
11569}
11570
11571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 * "foreground()" function
11573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011576 typval_T *argvars UNUSED;
11577 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579#ifdef FEAT_GUI
11580 if (gui.in_use)
11581 gui_mch_set_foreground();
11582#else
11583# ifdef WIN32
11584 win32_set_foreground();
11585# endif
11586#endif
11587}
11588
11589/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011590 * "function()" function
11591 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011594 typval_T *argvars;
11595 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011596{
11597 char_u *s;
11598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011600 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011601 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011602 /* Don't check an autoload name for existence here. */
11603 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011604 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011605 else
11606 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011607 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011608 {
11609 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011610 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011611
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011612 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11613 * also be called from another script. Using trans_function_name()
11614 * would also work, but some plugins depend on the name being
11615 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011616 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011617 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011618 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011619 if (rettv->vval.v_string != NULL)
11620 {
11621 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011622 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011623 }
11624 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011625 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011626 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011628 }
11629}
11630
11631/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011632 * "garbagecollect()" function
11633 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011634 static void
11635f_garbagecollect(argvars, rettv)
11636 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011637 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011638{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011639 /* This is postponed until we are back at the toplevel, because we may be
11640 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11641 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011642
11643 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11644 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011645}
11646
11647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011648 * "get()" function
11649 */
11650 static void
11651f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 typval_T *argvars;
11653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011654{
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 listitem_T *li;
11656 list_T *l;
11657 dictitem_T *di;
11658 dict_T *d;
11659 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660
Bram Moolenaare9a41262005-01-15 22:18:47 +000011661 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011663 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011665 int error = FALSE;
11666
11667 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11668 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011669 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011672 else if (argvars[0].v_type == VAR_DICT)
11673 {
11674 if ((d = argvars[0].vval.v_dict) != NULL)
11675 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011676 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677 if (di != NULL)
11678 tv = &di->di_tv;
11679 }
11680 }
11681 else
11682 EMSG2(_(e_listdictarg), "get()");
11683
11684 if (tv == NULL)
11685 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011686 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011687 copy_tv(&argvars[2], rettv);
11688 }
11689 else
11690 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011691}
11692
Bram Moolenaar342337a2005-07-21 21:11:17 +000011693static 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 +000011694
11695/*
11696 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011697 * Return a range (from start to end) of lines in rettv from the specified
11698 * buffer.
11699 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011700 */
11701 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011702get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011703 buf_T *buf;
11704 linenr_T start;
11705 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011706 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011707 typval_T *rettv;
11708{
11709 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011710
Bram Moolenaar959a1432013-12-14 12:17:38 +010011711 rettv->v_type = VAR_STRING;
11712 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011713 if (retlist && rettv_list_alloc(rettv) == FAIL)
11714 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011715
11716 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11717 return;
11718
11719 if (!retlist)
11720 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011721 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11722 p = ml_get_buf(buf, start, FALSE);
11723 else
11724 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011725 rettv->vval.v_string = vim_strsave(p);
11726 }
11727 else
11728 {
11729 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011730 return;
11731
11732 if (start < 1)
11733 start = 1;
11734 if (end > buf->b_ml.ml_line_count)
11735 end = buf->b_ml.ml_line_count;
11736 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011737 if (list_append_string(rettv->vval.v_list,
11738 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011739 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011740 }
11741}
11742
11743/*
11744 * "getbufline()" function
11745 */
11746 static void
11747f_getbufline(argvars, rettv)
11748 typval_T *argvars;
11749 typval_T *rettv;
11750{
11751 linenr_T lnum;
11752 linenr_T end;
11753 buf_T *buf;
11754
11755 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11756 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011757 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011758 --emsg_off;
11759
Bram Moolenaar661b1822005-07-28 22:36:45 +000011760 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011761 if (argvars[2].v_type == VAR_UNKNOWN)
11762 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011763 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011764 end = get_tv_lnum_buf(&argvars[2], buf);
11765
Bram Moolenaar342337a2005-07-21 21:11:17 +000011766 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011767}
11768
Bram Moolenaar0d660222005-01-07 21:51:51 +000011769/*
11770 * "getbufvar()" function
11771 */
11772 static void
11773f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011774 typval_T *argvars;
11775 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011776{
11777 buf_T *buf;
11778 buf_T *save_curbuf;
11779 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011780 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011781 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11784 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011785 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011786 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011787
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011788 rettv->v_type = VAR_STRING;
11789 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011790
11791 if (buf != NULL && varname != NULL)
11792 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011793 /* set curbuf to be our buf, temporarily */
11794 save_curbuf = curbuf;
11795 curbuf = buf;
11796
Bram Moolenaar0d660222005-01-07 21:51:51 +000011797 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011798 {
11799 if (get_option_tv(&varname, rettv, TRUE) == OK)
11800 done = TRUE;
11801 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011802 else if (STRCMP(varname, "changedtick") == 0)
11803 {
11804 rettv->v_type = VAR_NUMBER;
11805 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011806 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011807 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011808 else
11809 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011810 /* Look up the variable. */
11811 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11812 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11813 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011814 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011815 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011817 done = TRUE;
11818 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011819 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011820
11821 /* restore previous notion of curbuf */
11822 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011823 }
11824
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011825 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11826 /* use the default value */
11827 copy_tv(&argvars[2], rettv);
11828
Bram Moolenaar0d660222005-01-07 21:51:51 +000011829 --emsg_off;
11830}
11831
11832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 * "getchar()" function
11834 */
11835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011837 typval_T *argvars;
11838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839{
11840 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011841 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011843 /* Position the cursor. Needed after a message that ends in a space. */
11844 windgoto(msg_row, msg_col);
11845
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 ++no_mapping;
11847 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011848 for (;;)
11849 {
11850 if (argvars[0].v_type == VAR_UNKNOWN)
11851 /* getchar(): blocking wait. */
11852 n = safe_vgetc();
11853 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11854 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011855 n = vpeekc_any();
11856 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011857 /* illegal argument or getchar(0) and no char avail: return zero */
11858 n = 0;
11859 else
11860 /* getchar(0) and char avail: return char */
11861 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011862
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011863 if (n == K_IGNORE)
11864 continue;
11865 break;
11866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 --no_mapping;
11868 --allow_keys;
11869
Bram Moolenaar219b8702006-11-01 14:32:36 +000011870 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11871 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11872 vimvars[VV_MOUSE_COL].vv_nr = 0;
11873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011874 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 if (IS_SPECIAL(n) || mod_mask != 0)
11876 {
11877 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11878 int i = 0;
11879
11880 /* Turn a special key into three bytes, plus modifier. */
11881 if (mod_mask != 0)
11882 {
11883 temp[i++] = K_SPECIAL;
11884 temp[i++] = KS_MODIFIER;
11885 temp[i++] = mod_mask;
11886 }
11887 if (IS_SPECIAL(n))
11888 {
11889 temp[i++] = K_SPECIAL;
11890 temp[i++] = K_SECOND(n);
11891 temp[i++] = K_THIRD(n);
11892 }
11893#ifdef FEAT_MBYTE
11894 else if (has_mbyte)
11895 i += (*mb_char2bytes)(n, temp + i);
11896#endif
11897 else
11898 temp[i++] = n;
11899 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->v_type = VAR_STRING;
11901 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011902
11903#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011904 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011905 {
11906 int row = mouse_row;
11907 int col = mouse_col;
11908 win_T *win;
11909 linenr_T lnum;
11910# ifdef FEAT_WINDOWS
11911 win_T *wp;
11912# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011913 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011914
11915 if (row >= 0 && col >= 0)
11916 {
11917 /* Find the window at the mouse coordinates and compute the
11918 * text position. */
11919 win = mouse_find_win(&row, &col);
11920 (void)mouse_comp_pos(win, &row, &col, &lnum);
11921# ifdef FEAT_WINDOWS
11922 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011923 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011924# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011925 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011926 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11927 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11928 }
11929 }
11930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 }
11932}
11933
11934/*
11935 * "getcharmod()" function
11936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011938f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011939 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943}
11944
11945/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011946 * "getcharsearch()" function
11947 */
11948 static void
11949f_getcharsearch(argvars, rettv)
11950 typval_T *argvars UNUSED;
11951 typval_T *rettv;
11952{
11953 if (rettv_dict_alloc(rettv) != FAIL)
11954 {
11955 dict_T *dict = rettv->vval.v_dict;
11956
11957 dict_add_nr_str(dict, "char", 0L, last_csearch());
11958 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11959 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11960 }
11961}
11962
11963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 * "getcmdline()" function
11965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011968 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011971 rettv->v_type = VAR_STRING;
11972 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973}
11974
11975/*
11976 * "getcmdpos()" function
11977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011980 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011983 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984}
11985
11986/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011987 * "getcmdtype()" function
11988 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011989 static void
11990f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011991 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011992 typval_T *rettv;
11993{
11994 rettv->v_type = VAR_STRING;
11995 rettv->vval.v_string = alloc(2);
11996 if (rettv->vval.v_string != NULL)
11997 {
11998 rettv->vval.v_string[0] = get_cmdline_type();
11999 rettv->vval.v_string[1] = NUL;
12000 }
12001}
12002
12003/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012004 * "getcmdwintype()" function
12005 */
12006 static void
12007f_getcmdwintype(argvars, rettv)
12008 typval_T *argvars UNUSED;
12009 typval_T *rettv;
12010{
12011 rettv->v_type = VAR_STRING;
12012 rettv->vval.v_string = NULL;
12013#ifdef FEAT_CMDWIN
12014 rettv->vval.v_string = alloc(2);
12015 if (rettv->vval.v_string != NULL)
12016 {
12017 rettv->vval.v_string[0] = cmdwin_type;
12018 rettv->vval.v_string[1] = NUL;
12019 }
12020#endif
12021}
12022
12023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 * "getcwd()" function
12025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012028 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030{
Bram Moolenaard9462e32011-04-11 21:35:11 +020012031 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012034 rettv->vval.v_string = NULL;
12035 cwd = alloc(MAXPATHL);
12036 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020012038 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12039 {
12040 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020012042 if (rettv->vval.v_string != NULL)
12043 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020012045 }
12046 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 }
12048}
12049
12050/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012051 * "getfontname()" function
12052 */
12053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012054f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012055 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012056 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012057{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->v_type = VAR_STRING;
12059 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012060#ifdef FEAT_GUI
12061 if (gui.in_use)
12062 {
12063 GuiFont font;
12064 char_u *name = NULL;
12065
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012066 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012067 {
12068 /* Get the "Normal" font. Either the name saved by
12069 * hl_set_font_name() or from the font ID. */
12070 font = gui.norm_font;
12071 name = hl_get_font_name();
12072 }
12073 else
12074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012075 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012076 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12077 return;
12078 font = gui_mch_get_font(name, FALSE);
12079 if (font == NOFONT)
12080 return; /* Invalid font name, return empty string. */
12081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012083 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012084 gui_mch_free_font(font);
12085 }
12086#endif
12087}
12088
12089/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012090 * "getfperm({fname})" function
12091 */
12092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012096{
12097 char_u *fname;
12098 struct stat st;
12099 char_u *perm = NULL;
12100 char_u flags[] = "rwx";
12101 int i;
12102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012106 if (mch_stat((char *)fname, &st) >= 0)
12107 {
12108 perm = vim_strsave((char_u *)"---------");
12109 if (perm != NULL)
12110 {
12111 for (i = 0; i < 9; i++)
12112 {
12113 if (st.st_mode & (1 << (8 - i)))
12114 perm[i] = flags[i % 3];
12115 }
12116 }
12117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012119}
12120
12121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 * "getfsize({fname})" function
12123 */
12124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012126 typval_T *argvars;
12127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128{
12129 char_u *fname;
12130 struct stat st;
12131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135
12136 if (mch_stat((char *)fname, &st) >= 0)
12137 {
12138 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012143
12144 /* non-perfect check for overflow */
12145 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12146 rettv->vval.v_number = -2;
12147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 }
12149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151}
12152
12153/*
12154 * "getftime({fname})" function
12155 */
12156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012158 typval_T *argvars;
12159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160{
12161 char_u *fname;
12162 struct stat st;
12163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165
12166 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170}
12171
12172/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012173 * "getftype({fname})" function
12174 */
12175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012177 typval_T *argvars;
12178 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012179{
12180 char_u *fname;
12181 struct stat st;
12182 char_u *type = NULL;
12183 char *t;
12184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012185 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012188 if (mch_lstat((char *)fname, &st) >= 0)
12189 {
12190#ifdef S_ISREG
12191 if (S_ISREG(st.st_mode))
12192 t = "file";
12193 else if (S_ISDIR(st.st_mode))
12194 t = "dir";
12195# ifdef S_ISLNK
12196 else if (S_ISLNK(st.st_mode))
12197 t = "link";
12198# endif
12199# ifdef S_ISBLK
12200 else if (S_ISBLK(st.st_mode))
12201 t = "bdev";
12202# endif
12203# ifdef S_ISCHR
12204 else if (S_ISCHR(st.st_mode))
12205 t = "cdev";
12206# endif
12207# ifdef S_ISFIFO
12208 else if (S_ISFIFO(st.st_mode))
12209 t = "fifo";
12210# endif
12211# ifdef S_ISSOCK
12212 else if (S_ISSOCK(st.st_mode))
12213 t = "fifo";
12214# endif
12215 else
12216 t = "other";
12217#else
12218# ifdef S_IFMT
12219 switch (st.st_mode & S_IFMT)
12220 {
12221 case S_IFREG: t = "file"; break;
12222 case S_IFDIR: t = "dir"; break;
12223# ifdef S_IFLNK
12224 case S_IFLNK: t = "link"; break;
12225# endif
12226# ifdef S_IFBLK
12227 case S_IFBLK: t = "bdev"; break;
12228# endif
12229# ifdef S_IFCHR
12230 case S_IFCHR: t = "cdev"; break;
12231# endif
12232# ifdef S_IFIFO
12233 case S_IFIFO: t = "fifo"; break;
12234# endif
12235# ifdef S_IFSOCK
12236 case S_IFSOCK: t = "socket"; break;
12237# endif
12238 default: t = "other";
12239 }
12240# else
12241 if (mch_isdir(fname))
12242 t = "dir";
12243 else
12244 t = "file";
12245# endif
12246#endif
12247 type = vim_strsave((char_u *)t);
12248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012250}
12251
12252/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012253 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012254 */
12255 static void
12256f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012257 typval_T *argvars;
12258 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012259{
12260 linenr_T lnum;
12261 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012262 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012263
12264 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012265 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012266 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012267 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012268 retlist = FALSE;
12269 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012270 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012271 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012272 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012273 retlist = TRUE;
12274 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012275
Bram Moolenaar342337a2005-07-21 21:11:17 +000012276 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012277}
12278
12279/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012280 * "getmatches()" function
12281 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012282 static void
12283f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012284 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012285 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012286{
12287#ifdef FEAT_SEARCH_EXTRA
12288 dict_T *dict;
12289 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012290 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012291
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012292 if (rettv_list_alloc(rettv) == OK)
12293 {
12294 while (cur != NULL)
12295 {
12296 dict = dict_alloc();
12297 if (dict == NULL)
12298 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012299 if (cur->match.regprog == NULL)
12300 {
12301 /* match added with matchaddpos() */
12302 for (i = 0; i < MAXPOSMATCH; ++i)
12303 {
12304 llpos_T *llpos;
12305 char buf[6];
12306 list_T *l;
12307
12308 llpos = &cur->pos.pos[i];
12309 if (llpos->lnum == 0)
12310 break;
12311 l = list_alloc();
12312 if (l == NULL)
12313 break;
12314 list_append_number(l, (varnumber_T)llpos->lnum);
12315 if (llpos->col > 0)
12316 {
12317 list_append_number(l, (varnumber_T)llpos->col);
12318 list_append_number(l, (varnumber_T)llpos->len);
12319 }
12320 sprintf(buf, "pos%d", i + 1);
12321 dict_add_list(dict, buf, l);
12322 }
12323 }
12324 else
12325 {
12326 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12327 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012328 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012329 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12330 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012331# ifdef FEAT_CONCEAL
12332 if (cur->conceal_char)
12333 {
12334 char_u buf[MB_MAXBYTES + 1];
12335
12336 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12337 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12338 }
12339# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012340 list_append_dict(rettv->vval.v_list, dict);
12341 cur = cur->next;
12342 }
12343 }
12344#endif
12345}
12346
12347/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012348 * "getpid()" function
12349 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012350 static void
12351f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012352 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012353 typval_T *rettv;
12354{
12355 rettv->vval.v_number = mch_get_pid();
12356}
12357
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012358static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12359
12360/*
12361 * "getcurpos()" function
12362 */
12363 static void
12364f_getcurpos(argvars, rettv)
12365 typval_T *argvars;
12366 typval_T *rettv;
12367{
12368 getpos_both(argvars, rettv, TRUE);
12369}
12370
Bram Moolenaar18081e32008-02-20 19:11:07 +000012371/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012372 * "getpos(string)" function
12373 */
12374 static void
12375f_getpos(argvars, rettv)
12376 typval_T *argvars;
12377 typval_T *rettv;
12378{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012379 getpos_both(argvars, rettv, FALSE);
12380}
12381
12382 static void
12383getpos_both(argvars, rettv, getcurpos)
12384 typval_T *argvars;
12385 typval_T *rettv;
12386 int getcurpos;
12387{
Bram Moolenaara5525202006-03-02 22:52:09 +000012388 pos_T *fp;
12389 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012390 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012391
12392 if (rettv_list_alloc(rettv) == OK)
12393 {
12394 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012395 if (getcurpos)
12396 fp = &curwin->w_cursor;
12397 else
12398 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012399 if (fnum != -1)
12400 list_append_number(l, (varnumber_T)fnum);
12401 else
12402 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012403 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12404 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012405 list_append_number(l, (fp != NULL)
12406 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012407 : (varnumber_T)0);
12408 list_append_number(l,
12409#ifdef FEAT_VIRTUALEDIT
12410 (fp != NULL) ? (varnumber_T)fp->coladd :
12411#endif
12412 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012413 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012414 list_append_number(l, curwin->w_curswant == MAXCOL ?
12415 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012416 }
12417 else
12418 rettv->vval.v_number = FALSE;
12419}
12420
12421/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012422 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012423 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012424 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012425f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012426 typval_T *argvars UNUSED;
12427 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012428{
12429#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012430 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012431#endif
12432
Bram Moolenaar2641f772005-03-25 21:58:17 +000012433#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012434 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012435 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012436 wp = NULL;
12437 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12438 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012439 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012440 if (wp == NULL)
12441 return;
12442 }
12443
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012444 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012445 }
12446#endif
12447}
12448
12449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 * "getreg()" function
12451 */
12452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012454 typval_T *argvars;
12455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456{
12457 char_u *strregname;
12458 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012459 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012460 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012461 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012463 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012465 strregname = get_tv_string_chk(&argvars[0]);
12466 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012467 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012469 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012470 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12471 return_list = get_tv_number_chk(&argvars[2], &error);
12472 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012475 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012476
12477 if (error)
12478 return;
12479
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 regname = (strregname == NULL ? '"' : *strregname);
12481 if (regname == 0)
12482 regname = '"';
12483
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012484 if (return_list)
12485 {
12486 rettv->v_type = VAR_LIST;
12487 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12488 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012489 if (rettv->vval.v_list != NULL)
12490 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012491 }
12492 else
12493 {
12494 rettv->v_type = VAR_STRING;
12495 rettv->vval.v_string = get_reg_contents(regname,
12496 arg2 ? GREG_EXPR_SRC : 0);
12497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498}
12499
12500/*
12501 * "getregtype()" function
12502 */
12503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012505 typval_T *argvars;
12506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507{
12508 char_u *strregname;
12509 int regname;
12510 char_u buf[NUMBUFLEN + 2];
12511 long reglen = 0;
12512
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012513 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012514 {
12515 strregname = get_tv_string_chk(&argvars[0]);
12516 if (strregname == NULL) /* type error; errmsg already given */
12517 {
12518 rettv->v_type = VAR_STRING;
12519 rettv->vval.v_string = NULL;
12520 return;
12521 }
12522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 else
12524 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012525 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526
12527 regname = (strregname == NULL ? '"' : *strregname);
12528 if (regname == 0)
12529 regname = '"';
12530
12531 buf[0] = NUL;
12532 buf[1] = NUL;
12533 switch (get_reg_type(regname, &reglen))
12534 {
12535 case MLINE: buf[0] = 'V'; break;
12536 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 case MBLOCK:
12538 buf[0] = Ctrl_V;
12539 sprintf((char *)buf + 1, "%ld", reglen + 1);
12540 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012542 rettv->v_type = VAR_STRING;
12543 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012544}
12545
12546/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012547 * "gettabvar()" function
12548 */
12549 static void
12550f_gettabvar(argvars, rettv)
12551 typval_T *argvars;
12552 typval_T *rettv;
12553{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012554 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012555 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012556 dictitem_T *v;
12557 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012558 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012559
12560 rettv->v_type = VAR_STRING;
12561 rettv->vval.v_string = NULL;
12562
12563 varname = get_tv_string_chk(&argvars[1]);
12564 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12565 if (tp != NULL && varname != NULL)
12566 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012567 /* Set tp to be our tabpage, temporarily. Also set the window to the
12568 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012569 if (switch_win(&oldcurwin, &oldtabpage,
12570 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012571 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012572 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012573 /* look up the variable */
12574 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12575 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12576 if (v != NULL)
12577 {
12578 copy_tv(&v->di_tv, rettv);
12579 done = TRUE;
12580 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012581 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012582
12583 /* restore previous notion of curwin */
12584 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012585 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012586
12587 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012588 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012589}
12590
12591/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012592 * "gettabwinvar()" function
12593 */
12594 static void
12595f_gettabwinvar(argvars, rettv)
12596 typval_T *argvars;
12597 typval_T *rettv;
12598{
12599 getwinvar(argvars, rettv, 1);
12600}
12601
12602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603 * "getwinposx()" function
12604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012606f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611#ifdef FEAT_GUI
12612 if (gui.in_use)
12613 {
12614 int x, y;
12615
12616 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012617 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618 }
12619#endif
12620}
12621
12622/*
12623 * "getwinposy()" function
12624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012626f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012627 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631#ifdef FEAT_GUI
12632 if (gui.in_use)
12633 {
12634 int x, y;
12635
12636 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 }
12639#endif
12640}
12641
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012642/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012643 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012644 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012645 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012646find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012647 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012648 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012649{
12650#ifdef FEAT_WINDOWS
12651 win_T *wp;
12652#endif
12653 int nr;
12654
12655 nr = get_tv_number_chk(vp, NULL);
12656
12657#ifdef FEAT_WINDOWS
12658 if (nr < 0)
12659 return NULL;
12660 if (nr == 0)
12661 return curwin;
12662
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012663 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12664 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012665 if (--nr <= 0)
12666 break;
12667 return wp;
12668#else
12669 if (nr == 0 || nr == 1)
12670 return curwin;
12671 return NULL;
12672#endif
12673}
12674
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675/*
12676 * "getwinvar()" function
12677 */
12678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012680 typval_T *argvars;
12681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012683 getwinvar(argvars, rettv, 0);
12684}
12685
12686/*
12687 * getwinvar() and gettabwinvar()
12688 */
12689 static void
12690getwinvar(argvars, rettv, off)
12691 typval_T *argvars;
12692 typval_T *rettv;
12693 int off; /* 1 for gettabwinvar() */
12694{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012695 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012698 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012699 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012700#ifdef FEAT_WINDOWS
12701 win_T *oldcurwin;
12702 tabpage_T *oldtabpage;
12703 int need_switch_win;
12704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012706#ifdef FEAT_WINDOWS
12707 if (off == 1)
12708 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12709 else
12710 tp = curtab;
12711#endif
12712 win = find_win_by_nr(&argvars[off], tp);
12713 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012716 rettv->v_type = VAR_STRING;
12717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718
12719 if (win != NULL && varname != NULL)
12720 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012721#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012722 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012723 * otherwise the window is not valid. Only do this when needed,
12724 * autocommands get blocked. */
12725 need_switch_win = !(tp == curtab && win == curwin);
12726 if (!need_switch_win
12727 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12728#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012729 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012730 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012731 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012732 if (get_option_tv(&varname, rettv, 1) == OK)
12733 done = TRUE;
12734 }
12735 else
12736 {
12737 /* Look up the variable. */
12738 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12739 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12740 varname, FALSE);
12741 if (v != NULL)
12742 {
12743 copy_tv(&v->di_tv, rettv);
12744 done = TRUE;
12745 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012748
Bram Moolenaarba117c22015-09-29 16:53:22 +020012749#ifdef FEAT_WINDOWS
12750 if (need_switch_win)
12751 /* restore previous notion of curwin */
12752 restore_win(oldcurwin, oldtabpage, TRUE);
12753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 }
12755
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012756 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12757 /* use the default return value */
12758 copy_tv(&argvars[off + 2], rettv);
12759
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 --emsg_off;
12761}
12762
12763/*
12764 * "glob()" function
12765 */
12766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012768 typval_T *argvars;
12769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012771 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012773 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012775 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012776 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012778 if (argvars[1].v_type != VAR_UNKNOWN)
12779 {
12780 if (get_tv_number_chk(&argvars[1], &error))
12781 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012782 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012783 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012784 if (get_tv_number_chk(&argvars[2], &error))
12785 {
12786 rettv->v_type = VAR_LIST;
12787 rettv->vval.v_list = NULL;
12788 }
12789 if (argvars[3].v_type != VAR_UNKNOWN
12790 && get_tv_number_chk(&argvars[3], &error))
12791 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012792 }
12793 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012794 if (!error)
12795 {
12796 ExpandInit(&xpc);
12797 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012798 if (p_wic)
12799 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012800 if (rettv->v_type == VAR_STRING)
12801 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012802 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012803 else if (rettv_list_alloc(rettv) != FAIL)
12804 {
12805 int i;
12806
12807 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12808 NULL, options, WILD_ALL_KEEP);
12809 for (i = 0; i < xpc.xp_numfiles; i++)
12810 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12811
12812 ExpandCleanup(&xpc);
12813 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012814 }
12815 else
12816 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817}
12818
12819/*
12820 * "globpath()" function
12821 */
12822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *argvars;
12825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012827 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012829 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012830 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012831 garray_T ga;
12832 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012834 /* When the optional second argument is non-zero, don't remove matches
12835 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012837 if (argvars[2].v_type != VAR_UNKNOWN)
12838 {
12839 if (get_tv_number_chk(&argvars[2], &error))
12840 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012841 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012842 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012843 if (get_tv_number_chk(&argvars[3], &error))
12844 {
12845 rettv->v_type = VAR_LIST;
12846 rettv->vval.v_list = NULL;
12847 }
12848 if (argvars[4].v_type != VAR_UNKNOWN
12849 && get_tv_number_chk(&argvars[4], &error))
12850 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012851 }
12852 }
12853 if (file != NULL && !error)
12854 {
12855 ga_init2(&ga, (int)sizeof(char_u *), 10);
12856 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12857 if (rettv->v_type == VAR_STRING)
12858 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12859 else if (rettv_list_alloc(rettv) != FAIL)
12860 for (i = 0; i < ga.ga_len; ++i)
12861 list_append_string(rettv->vval.v_list,
12862 ((char_u **)(ga.ga_data))[i], -1);
12863 ga_clear_strings(&ga);
12864 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012866 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867}
12868
12869/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012870 * "glob2regpat()" function
12871 */
12872 static void
12873f_glob2regpat(argvars, rettv)
12874 typval_T *argvars;
12875 typval_T *rettv;
12876{
12877 char_u *pat = get_tv_string_chk(&argvars[0]);
12878
12879 rettv->v_type = VAR_STRING;
12880 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12881}
12882
12883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 * "has()" function
12885 */
12886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012888 typval_T *argvars;
12889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890{
12891 int i;
12892 char_u *name;
12893 int n = FALSE;
12894 static char *(has_list[]) =
12895 {
12896#ifdef AMIGA
12897 "amiga",
12898# ifdef FEAT_ARP
12899 "arp",
12900# endif
12901#endif
12902#ifdef __BEOS__
12903 "beos",
12904#endif
12905#ifdef MSDOS
12906# ifdef DJGPP
12907 "dos32",
12908# else
12909 "dos16",
12910# endif
12911#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012912#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 "mac",
12914#endif
12915#if defined(MACOS_X_UNIX)
12916 "macunix",
12917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918#ifdef __QNX__
12919 "qnx",
12920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921#ifdef UNIX
12922 "unix",
12923#endif
12924#ifdef VMS
12925 "vms",
12926#endif
12927#ifdef WIN16
12928 "win16",
12929#endif
12930#ifdef WIN32
12931 "win32",
12932#endif
12933#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12934 "win32unix",
12935#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012936#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 "win64",
12938#endif
12939#ifdef EBCDIC
12940 "ebcdic",
12941#endif
12942#ifndef CASE_INSENSITIVE_FILENAME
12943 "fname_case",
12944#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012945#ifdef HAVE_ACL
12946 "acl",
12947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948#ifdef FEAT_ARABIC
12949 "arabic",
12950#endif
12951#ifdef FEAT_AUTOCMD
12952 "autocmd",
12953#endif
12954#ifdef FEAT_BEVAL
12955 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012956# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12957 "balloon_multiline",
12958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959#endif
12960#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12961 "builtin_terms",
12962# ifdef ALL_BUILTIN_TCAPS
12963 "all_builtin_terms",
12964# endif
12965#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012966#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12967 || defined(FEAT_GUI_W32) \
12968 || defined(FEAT_GUI_MOTIF))
12969 "browsefilter",
12970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971#ifdef FEAT_BYTEOFF
12972 "byte_offset",
12973#endif
12974#ifdef FEAT_CINDENT
12975 "cindent",
12976#endif
12977#ifdef FEAT_CLIENTSERVER
12978 "clientserver",
12979#endif
12980#ifdef FEAT_CLIPBOARD
12981 "clipboard",
12982#endif
12983#ifdef FEAT_CMDL_COMPL
12984 "cmdline_compl",
12985#endif
12986#ifdef FEAT_CMDHIST
12987 "cmdline_hist",
12988#endif
12989#ifdef FEAT_COMMENTS
12990 "comments",
12991#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012992#ifdef FEAT_CONCEAL
12993 "conceal",
12994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995#ifdef FEAT_CRYPT
12996 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012997 "crypt-blowfish",
12998 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999#endif
13000#ifdef FEAT_CSCOPE
13001 "cscope",
13002#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013003#ifdef FEAT_CURSORBIND
13004 "cursorbind",
13005#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013006#ifdef CURSOR_SHAPE
13007 "cursorshape",
13008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009#ifdef DEBUG
13010 "debug",
13011#endif
13012#ifdef FEAT_CON_DIALOG
13013 "dialog_con",
13014#endif
13015#ifdef FEAT_GUI_DIALOG
13016 "dialog_gui",
13017#endif
13018#ifdef FEAT_DIFF
13019 "diff",
13020#endif
13021#ifdef FEAT_DIGRAPHS
13022 "digraphs",
13023#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013024#ifdef FEAT_DIRECTX
13025 "directx",
13026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027#ifdef FEAT_DND
13028 "dnd",
13029#endif
13030#ifdef FEAT_EMACS_TAGS
13031 "emacs_tags",
13032#endif
13033 "eval", /* always present, of course! */
13034#ifdef FEAT_EX_EXTRA
13035 "ex_extra",
13036#endif
13037#ifdef FEAT_SEARCH_EXTRA
13038 "extra_search",
13039#endif
13040#ifdef FEAT_FKMAP
13041 "farsi",
13042#endif
13043#ifdef FEAT_SEARCHPATH
13044 "file_in_path",
13045#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013046#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013047 "filterpipe",
13048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049#ifdef FEAT_FIND_ID
13050 "find_in_path",
13051#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013052#ifdef FEAT_FLOAT
13053 "float",
13054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055#ifdef FEAT_FOLDING
13056 "folding",
13057#endif
13058#ifdef FEAT_FOOTER
13059 "footer",
13060#endif
13061#if !defined(USE_SYSTEM) && defined(UNIX)
13062 "fork",
13063#endif
13064#ifdef FEAT_GETTEXT
13065 "gettext",
13066#endif
13067#ifdef FEAT_GUI
13068 "gui",
13069#endif
13070#ifdef FEAT_GUI_ATHENA
13071# ifdef FEAT_GUI_NEXTAW
13072 "gui_neXtaw",
13073# else
13074 "gui_athena",
13075# endif
13076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077#ifdef FEAT_GUI_GTK
13078 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013081#ifdef FEAT_GUI_GNOME
13082 "gui_gnome",
13083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084#ifdef FEAT_GUI_MAC
13085 "gui_mac",
13086#endif
13087#ifdef FEAT_GUI_MOTIF
13088 "gui_motif",
13089#endif
13090#ifdef FEAT_GUI_PHOTON
13091 "gui_photon",
13092#endif
13093#ifdef FEAT_GUI_W16
13094 "gui_win16",
13095#endif
13096#ifdef FEAT_GUI_W32
13097 "gui_win32",
13098#endif
13099#ifdef FEAT_HANGULIN
13100 "hangul_input",
13101#endif
13102#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13103 "iconv",
13104#endif
13105#ifdef FEAT_INS_EXPAND
13106 "insert_expand",
13107#endif
13108#ifdef FEAT_JUMPLIST
13109 "jumplist",
13110#endif
13111#ifdef FEAT_KEYMAP
13112 "keymap",
13113#endif
13114#ifdef FEAT_LANGMAP
13115 "langmap",
13116#endif
13117#ifdef FEAT_LIBCALL
13118 "libcall",
13119#endif
13120#ifdef FEAT_LINEBREAK
13121 "linebreak",
13122#endif
13123#ifdef FEAT_LISP
13124 "lispindent",
13125#endif
13126#ifdef FEAT_LISTCMDS
13127 "listcmds",
13128#endif
13129#ifdef FEAT_LOCALMAP
13130 "localmap",
13131#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013132#ifdef FEAT_LUA
13133# ifndef DYNAMIC_LUA
13134 "lua",
13135# endif
13136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137#ifdef FEAT_MENU
13138 "menu",
13139#endif
13140#ifdef FEAT_SESSION
13141 "mksession",
13142#endif
13143#ifdef FEAT_MODIFY_FNAME
13144 "modify_fname",
13145#endif
13146#ifdef FEAT_MOUSE
13147 "mouse",
13148#endif
13149#ifdef FEAT_MOUSESHAPE
13150 "mouseshape",
13151#endif
13152#if defined(UNIX) || defined(VMS)
13153# ifdef FEAT_MOUSE_DEC
13154 "mouse_dec",
13155# endif
13156# ifdef FEAT_MOUSE_GPM
13157 "mouse_gpm",
13158# endif
13159# ifdef FEAT_MOUSE_JSB
13160 "mouse_jsbterm",
13161# endif
13162# ifdef FEAT_MOUSE_NET
13163 "mouse_netterm",
13164# endif
13165# ifdef FEAT_MOUSE_PTERM
13166 "mouse_pterm",
13167# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013168# ifdef FEAT_MOUSE_SGR
13169 "mouse_sgr",
13170# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013171# ifdef FEAT_SYSMOUSE
13172 "mouse_sysmouse",
13173# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013174# ifdef FEAT_MOUSE_URXVT
13175 "mouse_urxvt",
13176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177# ifdef FEAT_MOUSE_XTERM
13178 "mouse_xterm",
13179# endif
13180#endif
13181#ifdef FEAT_MBYTE
13182 "multi_byte",
13183#endif
13184#ifdef FEAT_MBYTE_IME
13185 "multi_byte_ime",
13186#endif
13187#ifdef FEAT_MULTI_LANG
13188 "multi_lang",
13189#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013190#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013191#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013192 "mzscheme",
13193#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195#ifdef FEAT_OLE
13196 "ole",
13197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198#ifdef FEAT_PATH_EXTRA
13199 "path_extra",
13200#endif
13201#ifdef FEAT_PERL
13202#ifndef DYNAMIC_PERL
13203 "perl",
13204#endif
13205#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013206#ifdef FEAT_PERSISTENT_UNDO
13207 "persistent_undo",
13208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209#ifdef FEAT_PYTHON
13210#ifndef DYNAMIC_PYTHON
13211 "python",
13212#endif
13213#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013214#ifdef FEAT_PYTHON3
13215#ifndef DYNAMIC_PYTHON3
13216 "python3",
13217#endif
13218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219#ifdef FEAT_POSTSCRIPT
13220 "postscript",
13221#endif
13222#ifdef FEAT_PRINTER
13223 "printer",
13224#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013225#ifdef FEAT_PROFILE
13226 "profile",
13227#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013228#ifdef FEAT_RELTIME
13229 "reltime",
13230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231#ifdef FEAT_QUICKFIX
13232 "quickfix",
13233#endif
13234#ifdef FEAT_RIGHTLEFT
13235 "rightleft",
13236#endif
13237#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13238 "ruby",
13239#endif
13240#ifdef FEAT_SCROLLBIND
13241 "scrollbind",
13242#endif
13243#ifdef FEAT_CMDL_INFO
13244 "showcmd",
13245 "cmdline_info",
13246#endif
13247#ifdef FEAT_SIGNS
13248 "signs",
13249#endif
13250#ifdef FEAT_SMARTINDENT
13251 "smartindent",
13252#endif
13253#ifdef FEAT_SNIFF
13254 "sniff",
13255#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013256#ifdef STARTUPTIME
13257 "startuptime",
13258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259#ifdef FEAT_STL_OPT
13260 "statusline",
13261#endif
13262#ifdef FEAT_SUN_WORKSHOP
13263 "sun_workshop",
13264#endif
13265#ifdef FEAT_NETBEANS_INTG
13266 "netbeans_intg",
13267#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013268#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013269 "spell",
13270#endif
13271#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 "syntax",
13273#endif
13274#if defined(USE_SYSTEM) || !defined(UNIX)
13275 "system",
13276#endif
13277#ifdef FEAT_TAG_BINS
13278 "tag_binary",
13279#endif
13280#ifdef FEAT_TAG_OLDSTATIC
13281 "tag_old_static",
13282#endif
13283#ifdef FEAT_TAG_ANYWHITE
13284 "tag_any_white",
13285#endif
13286#ifdef FEAT_TCL
13287# ifndef DYNAMIC_TCL
13288 "tcl",
13289# endif
13290#endif
13291#ifdef TERMINFO
13292 "terminfo",
13293#endif
13294#ifdef FEAT_TERMRESPONSE
13295 "termresponse",
13296#endif
13297#ifdef FEAT_TEXTOBJ
13298 "textobjects",
13299#endif
13300#ifdef HAVE_TGETENT
13301 "tgetent",
13302#endif
13303#ifdef FEAT_TITLE
13304 "title",
13305#endif
13306#ifdef FEAT_TOOLBAR
13307 "toolbar",
13308#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013309#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13310 "unnamedplus",
13311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312#ifdef FEAT_USR_CMDS
13313 "user-commands", /* was accidentally included in 5.4 */
13314 "user_commands",
13315#endif
13316#ifdef FEAT_VIMINFO
13317 "viminfo",
13318#endif
13319#ifdef FEAT_VERTSPLIT
13320 "vertsplit",
13321#endif
13322#ifdef FEAT_VIRTUALEDIT
13323 "virtualedit",
13324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326#ifdef FEAT_VISUALEXTRA
13327 "visualextra",
13328#endif
13329#ifdef FEAT_VREPLACE
13330 "vreplace",
13331#endif
13332#ifdef FEAT_WILDIGN
13333 "wildignore",
13334#endif
13335#ifdef FEAT_WILDMENU
13336 "wildmenu",
13337#endif
13338#ifdef FEAT_WINDOWS
13339 "windows",
13340#endif
13341#ifdef FEAT_WAK
13342 "winaltkeys",
13343#endif
13344#ifdef FEAT_WRITEBACKUP
13345 "writebackup",
13346#endif
13347#ifdef FEAT_XIM
13348 "xim",
13349#endif
13350#ifdef FEAT_XFONTSET
13351 "xfontset",
13352#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013353#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013354 "xpm",
13355 "xpm_w32", /* for backward compatibility */
13356#else
13357# if defined(HAVE_XPM)
13358 "xpm",
13359# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361#ifdef USE_XSMP
13362 "xsmp",
13363#endif
13364#ifdef USE_XSMP_INTERACT
13365 "xsmp_interact",
13366#endif
13367#ifdef FEAT_XCLIPBOARD
13368 "xterm_clipboard",
13369#endif
13370#ifdef FEAT_XTERM_SAVE
13371 "xterm_save",
13372#endif
13373#if defined(UNIX) && defined(FEAT_X11)
13374 "X11",
13375#endif
13376 NULL
13377 };
13378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013379 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 for (i = 0; has_list[i] != NULL; ++i)
13381 if (STRICMP(name, has_list[i]) == 0)
13382 {
13383 n = TRUE;
13384 break;
13385 }
13386
13387 if (n == FALSE)
13388 {
13389 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013390 {
13391 if (name[5] == '-'
13392 && STRLEN(name) > 11
13393 && vim_isdigit(name[6])
13394 && vim_isdigit(name[8])
13395 && vim_isdigit(name[10]))
13396 {
13397 int major = atoi((char *)name + 6);
13398 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013399
13400 /* Expect "patch-9.9.01234". */
13401 n = (major < VIM_VERSION_MAJOR
13402 || (major == VIM_VERSION_MAJOR
13403 && (minor < VIM_VERSION_MINOR
13404 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013405 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013406 }
13407 else
13408 n = has_patch(atoi((char *)name + 5));
13409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 else if (STRICMP(name, "vim_starting") == 0)
13411 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013412#ifdef FEAT_MBYTE
13413 else if (STRICMP(name, "multi_byte_encoding") == 0)
13414 n = has_mbyte;
13415#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013416#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13417 else if (STRICMP(name, "balloon_multiline") == 0)
13418 n = multiline_balloon_available();
13419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420#ifdef DYNAMIC_TCL
13421 else if (STRICMP(name, "tcl") == 0)
13422 n = tcl_enabled(FALSE);
13423#endif
13424#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13425 else if (STRICMP(name, "iconv") == 0)
13426 n = iconv_enabled(FALSE);
13427#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013428#ifdef DYNAMIC_LUA
13429 else if (STRICMP(name, "lua") == 0)
13430 n = lua_enabled(FALSE);
13431#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013432#ifdef DYNAMIC_MZSCHEME
13433 else if (STRICMP(name, "mzscheme") == 0)
13434 n = mzscheme_enabled(FALSE);
13435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436#ifdef DYNAMIC_RUBY
13437 else if (STRICMP(name, "ruby") == 0)
13438 n = ruby_enabled(FALSE);
13439#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013440#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441#ifdef DYNAMIC_PYTHON
13442 else if (STRICMP(name, "python") == 0)
13443 n = python_enabled(FALSE);
13444#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013445#endif
13446#ifdef FEAT_PYTHON3
13447#ifdef DYNAMIC_PYTHON3
13448 else if (STRICMP(name, "python3") == 0)
13449 n = python3_enabled(FALSE);
13450#endif
13451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452#ifdef DYNAMIC_PERL
13453 else if (STRICMP(name, "perl") == 0)
13454 n = perl_enabled(FALSE);
13455#endif
13456#ifdef FEAT_GUI
13457 else if (STRICMP(name, "gui_running") == 0)
13458 n = (gui.in_use || gui.starting);
13459# ifdef FEAT_GUI_W32
13460 else if (STRICMP(name, "gui_win32s") == 0)
13461 n = gui_is_win32s();
13462# endif
13463# ifdef FEAT_BROWSE
13464 else if (STRICMP(name, "browse") == 0)
13465 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13466# endif
13467#endif
13468#ifdef FEAT_SYN_HL
13469 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013470 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471#endif
13472#if defined(WIN3264)
13473 else if (STRICMP(name, "win95") == 0)
13474 n = mch_windows95();
13475#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013476#ifdef FEAT_NETBEANS_INTG
13477 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013478 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480 }
13481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483}
13484
13485/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013486 * "has_key()" function
13487 */
13488 static void
13489f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013490 typval_T *argvars;
13491 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013492{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013493 if (argvars[0].v_type != VAR_DICT)
13494 {
13495 EMSG(_(e_dictreq));
13496 return;
13497 }
13498 if (argvars[0].vval.v_dict == NULL)
13499 return;
13500
13501 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013502 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013503}
13504
13505/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013506 * "haslocaldir()" function
13507 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013508 static void
13509f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013510 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013511 typval_T *rettv;
13512{
13513 rettv->vval.v_number = (curwin->w_localdir != NULL);
13514}
13515
13516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 * "hasmapto()" function
13518 */
13519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013521 typval_T *argvars;
13522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523{
13524 char_u *name;
13525 char_u *mode;
13526 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013527 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013530 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 mode = (char_u *)"nvo";
13532 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013534 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013535 if (argvars[2].v_type != VAR_UNKNOWN)
13536 abbr = get_tv_number(&argvars[2]);
13537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaar2c932302006-03-18 21:42:09 +000013539 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543}
13544
13545/*
13546 * "histadd()" function
13547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013550 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552{
13553#ifdef FEAT_CMDHIST
13554 int histype;
13555 char_u *str;
13556 char_u buf[NUMBUFLEN];
13557#endif
13558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 if (check_restricted() || check_secure())
13561 return;
13562#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13564 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 if (histype >= 0)
13566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568 if (*str != NUL)
13569 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013570 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573 return;
13574 }
13575 }
13576#endif
13577}
13578
13579/*
13580 * "histdel()" function
13581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013584 typval_T *argvars UNUSED;
13585 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586{
13587#ifdef FEAT_CMDHIST
13588 int n;
13589 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013592 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13593 if (str == NULL)
13594 n = 0;
13595 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013598 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013600 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 else
13603 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013604 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 get_tv_string_buf(&argvars[1], buf));
13606 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607#endif
13608}
13609
13610/*
13611 * "histget()" function
13612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013615 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618#ifdef FEAT_CMDHIST
13619 int type;
13620 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13624 if (str == NULL)
13625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 {
13628 type = get_histtype(str);
13629 if (argvars[1].v_type == VAR_UNKNOWN)
13630 idx = get_history_idx(type);
13631 else
13632 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13633 /* -1 on type error */
13634 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640}
13641
13642/*
13643 * "histnr()" function
13644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013647 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649{
13650 int i;
13651
13652#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013653 char_u *history = get_tv_string_chk(&argvars[0]);
13654
13655 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 if (i >= HIST_CMD && i < HIST_COUNT)
13657 i = get_history_idx(i);
13658 else
13659#endif
13660 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013661 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662}
13663
13664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 * "highlightID(name)" function
13666 */
13667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013669 typval_T *argvars;
13670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673}
13674
13675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676 * "highlight_exists()" function
13677 */
13678 static void
13679f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *argvars;
13681 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682{
13683 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13684}
13685
13686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 * "hostname()" function
13688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013691 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693{
13694 char_u hostname[256];
13695
13696 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697 rettv->v_type = VAR_STRING;
13698 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699}
13700
13701/*
13702 * iconv() function
13703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013706 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708{
13709#ifdef FEAT_MBYTE
13710 char_u buf1[NUMBUFLEN];
13711 char_u buf2[NUMBUFLEN];
13712 char_u *from, *to, *str;
13713 vimconv_T vimconv;
13714#endif
13715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716 rettv->v_type = VAR_STRING;
13717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718
13719#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720 str = get_tv_string(&argvars[0]);
13721 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13722 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 vimconv.vc_type = CONV_NONE;
13724 convert_setup(&vimconv, from, to);
13725
13726 /* If the encodings are equal, no conversion needed. */
13727 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013730 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731
13732 convert_setup(&vimconv, NULL, NULL);
13733 vim_free(from);
13734 vim_free(to);
13735#endif
13736}
13737
13738/*
13739 * "indent()" function
13740 */
13741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013743 typval_T *argvars;
13744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745{
13746 linenr_T lnum;
13747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753}
13754
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013755/*
13756 * "index()" function
13757 */
13758 static void
13759f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013760 typval_T *argvars;
13761 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013762{
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 list_T *l;
13764 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013765 long idx = 0;
13766 int ic = FALSE;
13767
13768 rettv->vval.v_number = -1;
13769 if (argvars[0].v_type != VAR_LIST)
13770 {
13771 EMSG(_(e_listreq));
13772 return;
13773 }
13774 l = argvars[0].vval.v_list;
13775 if (l != NULL)
13776 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013777 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013778 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 int error = FALSE;
13781
Bram Moolenaar758711c2005-02-02 23:11:38 +000013782 /* Start at specified item. Use the cached index that list_find()
13783 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013785 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013786 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013787 ic = get_tv_number_chk(&argvars[3], &error);
13788 if (error)
13789 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013790 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013791
Bram Moolenaar758711c2005-02-02 23:11:38 +000013792 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013793 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013794 {
13795 rettv->vval.v_number = idx;
13796 break;
13797 }
13798 }
13799}
13800
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801static int inputsecret_flag = 0;
13802
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013803static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13804
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013806 * This function is used by f_input() and f_inputdialog() functions. The third
13807 * argument to f_input() specifies the type of completion to use at the
13808 * prompt. The third argument to f_inputdialog() specifies the value to return
13809 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810 */
13811 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013812get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 typval_T *argvars;
13814 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013815 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013817 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 char_u *p = NULL;
13819 int c;
13820 char_u buf[NUMBUFLEN];
13821 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013823 int xp_type = EXPAND_NOTHING;
13824 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013827 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828
13829#ifdef NO_CONSOLE_INPUT
13830 /* While starting up, there is no place to enter text. */
13831 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833#endif
13834
13835 cmd_silent = FALSE; /* Want to see the prompt. */
13836 if (prompt != NULL)
13837 {
13838 /* Only the part of the message after the last NL is considered as
13839 * prompt for the command line */
13840 p = vim_strrchr(prompt, '\n');
13841 if (p == NULL)
13842 p = prompt;
13843 else
13844 {
13845 ++p;
13846 c = *p;
13847 *p = NUL;
13848 msg_start();
13849 msg_clr_eos();
13850 msg_puts_attr(prompt, echo_attr);
13851 msg_didout = FALSE;
13852 msg_starthere();
13853 *p = c;
13854 }
13855 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 if (argvars[1].v_type != VAR_UNKNOWN)
13858 {
13859 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13860 if (defstr != NULL)
13861 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013863 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013864 {
13865 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013866 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013867 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013868
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013869 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013870 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013871
Bram Moolenaar4463f292005-09-25 22:20:24 +000013872 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13873 if (xp_name == NULL)
13874 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013875
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013876 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013877
Bram Moolenaar4463f292005-09-25 22:20:24 +000013878 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13879 &xp_arg) == FAIL)
13880 return;
13881 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013882 }
13883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013884 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013885 {
13886# ifdef FEAT_EX_EXTRA
13887 int save_ex_normal_busy = ex_normal_busy;
13888 ex_normal_busy = 0;
13889# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013890 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013891 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13892 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013893# ifdef FEAT_EX_EXTRA
13894 ex_normal_busy = save_ex_normal_busy;
13895# endif
13896 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013897 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013898 && argvars[1].v_type != VAR_UNKNOWN
13899 && argvars[2].v_type != VAR_UNKNOWN)
13900 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13901 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013902
13903 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 /* since the user typed this, no need to wait for return */
13906 need_wait_return = FALSE;
13907 msg_didout = FALSE;
13908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 cmd_silent = cmd_silent_save;
13910}
13911
13912/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013913 * "input()" function
13914 * Also handles inputsecret() when inputsecret is set.
13915 */
13916 static void
13917f_input(argvars, rettv)
13918 typval_T *argvars;
13919 typval_T *rettv;
13920{
13921 get_user_input(argvars, rettv, FALSE);
13922}
13923
13924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 * "inputdialog()" function
13926 */
13927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013929 typval_T *argvars;
13930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931{
13932#if defined(FEAT_GUI_TEXTDIALOG)
13933 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13934 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13935 {
13936 char_u *message;
13937 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013938 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 message = get_tv_string_chk(&argvars[0]);
13941 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013942 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013943 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 else
13945 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 if (message != NULL && defstr != NULL
13947 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013948 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 else
13951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 if (message != NULL && defstr != NULL
13953 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013954 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 rettv->vval.v_string = vim_strsave(
13956 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 }
13962 else
13963#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013964 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965}
13966
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013967/*
13968 * "inputlist()" function
13969 */
13970 static void
13971f_inputlist(argvars, rettv)
13972 typval_T *argvars;
13973 typval_T *rettv;
13974{
13975 listitem_T *li;
13976 int selected;
13977 int mouse_used;
13978
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013979#ifdef NO_CONSOLE_INPUT
13980 /* While starting up, there is no place to enter text. */
13981 if (no_console_input())
13982 return;
13983#endif
13984 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13985 {
13986 EMSG2(_(e_listarg), "inputlist()");
13987 return;
13988 }
13989
13990 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013991 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013992 lines_left = Rows; /* avoid more prompt */
13993 msg_scroll = TRUE;
13994 msg_clr_eos();
13995
13996 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13997 {
13998 msg_puts(get_tv_string(&li->li_tv));
13999 msg_putchar('\n');
14000 }
14001
14002 /* Ask for choice. */
14003 selected = prompt_for_number(&mouse_used);
14004 if (mouse_used)
14005 selected -= lines_left;
14006
14007 rettv->vval.v_number = selected;
14008}
14009
14010
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14012
14013/*
14014 * "inputrestore()" function
14015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014018 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020{
14021 if (ga_userinput.ga_len > 0)
14022 {
14023 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14025 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014026 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 }
14028 else if (p_verbose > 1)
14029 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014030 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014031 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 }
14033}
14034
14035/*
14036 * "inputsave()" function
14037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014040 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014043 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 if (ga_grow(&ga_userinput, 1) == OK)
14045 {
14046 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14047 + ga_userinput.ga_len);
14048 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014049 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 }
14051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014052 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053}
14054
14055/*
14056 * "inputsecret()" function
14057 */
14058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014059f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014060 typval_T *argvars;
14061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062{
14063 ++cmdline_star;
14064 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 --cmdline_star;
14067 --inputsecret_flag;
14068}
14069
14070/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014071 * "insert()" function
14072 */
14073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014074f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014075 typval_T *argvars;
14076 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014077{
14078 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014079 listitem_T *item;
14080 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014081 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014082
14083 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014084 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014085 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014086 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014087 {
14088 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 before = get_tv_number_chk(&argvars[2], &error);
14090 if (error)
14091 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014092
Bram Moolenaar758711c2005-02-02 23:11:38 +000014093 if (before == l->lv_len)
14094 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014095 else
14096 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014097 item = list_find(l, before);
14098 if (item == NULL)
14099 {
14100 EMSGN(_(e_listidx), before);
14101 l = NULL;
14102 }
14103 }
14104 if (l != NULL)
14105 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014106 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014107 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014108 }
14109 }
14110}
14111
14112/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014113 * "invert(expr)" function
14114 */
14115 static void
14116f_invert(argvars, rettv)
14117 typval_T *argvars;
14118 typval_T *rettv;
14119{
14120 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14121}
14122
14123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 * "isdirectory()" function
14125 */
14126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014128 typval_T *argvars;
14129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132}
14133
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014134/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014135 * "islocked()" function
14136 */
14137 static void
14138f_islocked(argvars, rettv)
14139 typval_T *argvars;
14140 typval_T *rettv;
14141{
14142 lval_T lv;
14143 char_u *end;
14144 dictitem_T *di;
14145
14146 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014147 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14148 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014149 if (end != NULL && lv.ll_name != NULL)
14150 {
14151 if (*end != NUL)
14152 EMSG(_(e_trailing));
14153 else
14154 {
14155 if (lv.ll_tv == NULL)
14156 {
14157 if (check_changedtick(lv.ll_name))
14158 rettv->vval.v_number = 1; /* always locked */
14159 else
14160 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014161 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014162 if (di != NULL)
14163 {
14164 /* Consider a variable locked when:
14165 * 1. the variable itself is locked
14166 * 2. the value of the variable is locked.
14167 * 3. the List or Dict value is locked.
14168 */
14169 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14170 || tv_islocked(&di->di_tv));
14171 }
14172 }
14173 }
14174 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014175 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014176 else if (lv.ll_newkey != NULL)
14177 EMSG2(_(e_dictkey), lv.ll_newkey);
14178 else if (lv.ll_list != NULL)
14179 /* List item. */
14180 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14181 else
14182 /* Dictionary item. */
14183 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14184 }
14185 }
14186
14187 clear_lval(&lv);
14188}
14189
Bram Moolenaar33570922005-01-25 22:26:29 +000014190static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014191
14192/*
14193 * Turn a dict into a list:
14194 * "what" == 0: list of keys
14195 * "what" == 1: list of values
14196 * "what" == 2: list of items
14197 */
14198 static void
14199dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 typval_T *argvars;
14201 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014202 int what;
14203{
Bram Moolenaar33570922005-01-25 22:26:29 +000014204 list_T *l2;
14205 dictitem_T *di;
14206 hashitem_T *hi;
14207 listitem_T *li;
14208 listitem_T *li2;
14209 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014210 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014211
Bram Moolenaar8c711452005-01-14 21:53:12 +000014212 if (argvars[0].v_type != VAR_DICT)
14213 {
14214 EMSG(_(e_dictreq));
14215 return;
14216 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014217 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014218 return;
14219
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014220 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014221 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014222
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014223 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014224 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014225 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014226 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014227 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014228 --todo;
14229 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014230
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014231 li = listitem_alloc();
14232 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014233 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014234 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014235
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014236 if (what == 0)
14237 {
14238 /* keys() */
14239 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014240 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014241 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14242 }
14243 else if (what == 1)
14244 {
14245 /* values() */
14246 copy_tv(&di->di_tv, &li->li_tv);
14247 }
14248 else
14249 {
14250 /* items() */
14251 l2 = list_alloc();
14252 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014253 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014254 li->li_tv.vval.v_list = l2;
14255 if (l2 == NULL)
14256 break;
14257 ++l2->lv_refcount;
14258
14259 li2 = listitem_alloc();
14260 if (li2 == NULL)
14261 break;
14262 list_append(l2, li2);
14263 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014264 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014265 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14266
14267 li2 = listitem_alloc();
14268 if (li2 == NULL)
14269 break;
14270 list_append(l2, li2);
14271 copy_tv(&di->di_tv, &li2->li_tv);
14272 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014273 }
14274 }
14275}
14276
14277/*
14278 * "items(dict)" function
14279 */
14280 static void
14281f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014282 typval_T *argvars;
14283 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014284{
14285 dict_list(argvars, rettv, 2);
14286}
14287
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014289 * "join()" function
14290 */
14291 static void
14292f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 typval_T *argvars;
14294 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014295{
14296 garray_T ga;
14297 char_u *sep;
14298
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014299 if (argvars[0].v_type != VAR_LIST)
14300 {
14301 EMSG(_(e_listreq));
14302 return;
14303 }
14304 if (argvars[0].vval.v_list == NULL)
14305 return;
14306 if (argvars[1].v_type == VAR_UNKNOWN)
14307 sep = (char_u *)" ";
14308 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014310
14311 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312
14313 if (sep != NULL)
14314 {
14315 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014316 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014317 ga_append(&ga, NUL);
14318 rettv->vval.v_string = (char_u *)ga.ga_data;
14319 }
14320 else
14321 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014322}
14323
14324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014325 * "keys()" function
14326 */
14327 static void
14328f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014331{
14332 dict_list(argvars, rettv, 0);
14333}
14334
14335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 * "last_buffer_nr()" function.
14337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014340 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342{
14343 int n = 0;
14344 buf_T *buf;
14345
14346 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14347 if (n < buf->b_fnum)
14348 n = buf->b_fnum;
14349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014351}
14352
14353/*
14354 * "len()" function
14355 */
14356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014358 typval_T *argvars;
14359 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014360{
14361 switch (argvars[0].v_type)
14362 {
14363 case VAR_STRING:
14364 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 rettv->vval.v_number = (varnumber_T)STRLEN(
14366 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014367 break;
14368 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014370 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014371 case VAR_DICT:
14372 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14373 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014374 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014375 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014376 break;
14377 }
14378}
14379
Bram Moolenaar33570922005-01-25 22:26:29 +000014380static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014381
14382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014383libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014384 typval_T *argvars;
14385 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014386 int type;
14387{
14388#ifdef FEAT_LIBCALL
14389 char_u *string_in;
14390 char_u **string_result;
14391 int nr_result;
14392#endif
14393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014395 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014396 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014397
14398 if (check_restricted() || check_secure())
14399 return;
14400
14401#ifdef FEAT_LIBCALL
14402 /* The first two args must be strings, otherwise its meaningless */
14403 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14404 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014405 string_in = NULL;
14406 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014407 string_in = argvars[2].vval.v_string;
14408 if (type == VAR_NUMBER)
14409 string_result = NULL;
14410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014412 if (mch_libcall(argvars[0].vval.v_string,
14413 argvars[1].vval.v_string,
14414 string_in,
14415 argvars[2].vval.v_number,
14416 string_result,
14417 &nr_result) == OK
14418 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014420 }
14421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422}
14423
14424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 * "libcall()" function
14426 */
14427 static void
14428f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014429 typval_T *argvars;
14430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431{
14432 libcall_common(argvars, rettv, VAR_STRING);
14433}
14434
14435/*
14436 * "libcallnr()" function
14437 */
14438 static void
14439f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014440 typval_T *argvars;
14441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014442{
14443 libcall_common(argvars, rettv, VAR_NUMBER);
14444}
14445
14446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 * "line(string)" function
14448 */
14449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014451 typval_T *argvars;
14452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453{
14454 linenr_T lnum = 0;
14455 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014456 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014458 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 if (fp != NULL)
14460 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462}
14463
14464/*
14465 * "line2byte(lnum)" function
14466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014469 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471{
14472#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474#else
14475 linenr_T lnum;
14476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14482 if (rettv->vval.v_number >= 0)
14483 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484#endif
14485}
14486
14487/*
14488 * "lispindent(lnum)" function
14489 */
14490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014492 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494{
14495#ifdef FEAT_LISP
14496 pos_T pos;
14497 linenr_T lnum;
14498
14499 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14502 {
14503 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 curwin->w_cursor = pos;
14506 }
14507 else
14508#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510}
14511
14512/*
14513 * "localtime()" function
14514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014516f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014517 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521}
14522
Bram Moolenaar33570922005-01-25 22:26:29 +000014523static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524
14525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014527 typval_T *argvars;
14528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 int exact;
14530{
14531 char_u *keys;
14532 char_u *which;
14533 char_u buf[NUMBUFLEN];
14534 char_u *keys_buf = NULL;
14535 char_u *rhs;
14536 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014537 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014538 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014539 mapblock_T *mp;
14540 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541
14542 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014543 rettv->v_type = VAR_STRING;
14544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 if (*keys == NUL)
14548 return;
14549
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014550 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014553 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014554 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014555 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014556 if (argvars[3].v_type != VAR_UNKNOWN)
14557 get_dict = get_tv_number(&argvars[3]);
14558 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 else
14561 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 if (which == NULL)
14563 return;
14564
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 mode = get_map_mode(&which, 0);
14566
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014567 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014568 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014570
14571 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014573 /* Return a string. */
14574 if (rhs != NULL)
14575 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576
Bram Moolenaarbd743252010-10-20 21:23:33 +020014577 }
14578 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14579 {
14580 /* Return a dictionary. */
14581 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14582 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14583 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584
Bram Moolenaarbd743252010-10-20 21:23:33 +020014585 dict_add_nr_str(dict, "lhs", 0L, lhs);
14586 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14587 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14588 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14589 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14590 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14591 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014592 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014593 dict_add_nr_str(dict, "mode", 0L, mapmode);
14594
14595 vim_free(lhs);
14596 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597 }
14598}
14599
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014600#ifdef FEAT_FLOAT
14601/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014602 * "log()" function
14603 */
14604 static void
14605f_log(argvars, rettv)
14606 typval_T *argvars;
14607 typval_T *rettv;
14608{
14609 float_T f;
14610
14611 rettv->v_type = VAR_FLOAT;
14612 if (get_float_arg(argvars, &f) == OK)
14613 rettv->vval.v_float = log(f);
14614 else
14615 rettv->vval.v_float = 0.0;
14616}
14617
14618/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014619 * "log10()" function
14620 */
14621 static void
14622f_log10(argvars, rettv)
14623 typval_T *argvars;
14624 typval_T *rettv;
14625{
14626 float_T f;
14627
14628 rettv->v_type = VAR_FLOAT;
14629 if (get_float_arg(argvars, &f) == OK)
14630 rettv->vval.v_float = log10(f);
14631 else
14632 rettv->vval.v_float = 0.0;
14633}
14634#endif
14635
Bram Moolenaar1dced572012-04-05 16:54:08 +020014636#ifdef FEAT_LUA
14637/*
14638 * "luaeval()" function
14639 */
14640 static void
14641f_luaeval(argvars, rettv)
14642 typval_T *argvars;
14643 typval_T *rettv;
14644{
14645 char_u *str;
14646 char_u buf[NUMBUFLEN];
14647
14648 str = get_tv_string_buf(&argvars[0], buf);
14649 do_luaeval(str, argvars + 1, rettv);
14650}
14651#endif
14652
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014654 * "map()" function
14655 */
14656 static void
14657f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014658 typval_T *argvars;
14659 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014660{
14661 filter_map(argvars, rettv, TRUE);
14662}
14663
14664/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014665 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 */
14667 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014669 typval_T *argvars;
14670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673}
14674
14675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014676 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 */
14678 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 typval_T *argvars;
14681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684}
14685
Bram Moolenaar33570922005-01-25 22:26:29 +000014686static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687
14688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 typval_T *argvars;
14691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692 int type;
14693{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014694 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014695 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014696 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697 char_u *pat;
14698 regmatch_T regmatch;
14699 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014700 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 char_u *save_cpo;
14702 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014703 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014704 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014705 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014706 list_T *l = NULL;
14707 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014708 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014709 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710
14711 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14712 save_cpo = p_cpo;
14713 p_cpo = (char_u *)"";
14714
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014715 rettv->vval.v_number = -1;
14716 if (type == 3)
14717 {
14718 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014719 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014720 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014721 }
14722 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014724 rettv->v_type = VAR_STRING;
14725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014728 if (argvars[0].v_type == VAR_LIST)
14729 {
14730 if ((l = argvars[0].vval.v_list) == NULL)
14731 goto theend;
14732 li = l->lv_first;
14733 }
14734 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014735 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014736 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014737 len = (long)STRLEN(str);
14738 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014740 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14741 if (pat == NULL)
14742 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014743
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014744 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 int error = FALSE;
14747
14748 start = get_tv_number_chk(&argvars[2], &error);
14749 if (error)
14750 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014751 if (l != NULL)
14752 {
14753 li = list_find(l, start);
14754 if (li == NULL)
14755 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014756 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014757 }
14758 else
14759 {
14760 if (start < 0)
14761 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014762 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014763 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014764 /* When "count" argument is there ignore matches before "start",
14765 * otherwise skip part of the string. Differs when pattern is "^"
14766 * or "\<". */
14767 if (argvars[3].v_type != VAR_UNKNOWN)
14768 startcol = start;
14769 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014770 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014771 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014772 len -= start;
14773 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014774 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014775
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014776 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014777 nth = get_tv_number_chk(&argvars[3], &error);
14778 if (error)
14779 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780 }
14781
14782 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14783 if (regmatch.regprog != NULL)
14784 {
14785 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014786
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014787 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014788 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014789 if (l != NULL)
14790 {
14791 if (li == NULL)
14792 {
14793 match = FALSE;
14794 break;
14795 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014796 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014797 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014798 if (str == NULL)
14799 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014800 }
14801
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014802 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014803
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014804 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014805 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014806 if (l == NULL && !match)
14807 break;
14808
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014809 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014810 if (l != NULL)
14811 {
14812 li = li->li_next;
14813 ++idx;
14814 }
14815 else
14816 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014817#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014818 startcol = (colnr_T)(regmatch.startp[0]
14819 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014820#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014821 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014822#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014823 if (startcol > (colnr_T)len
14824 || str + startcol <= regmatch.startp[0])
14825 {
14826 match = FALSE;
14827 break;
14828 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014829 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014830 }
14831
14832 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014834 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014835 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014836 int i;
14837
14838 /* return list with matched string and submatches */
14839 for (i = 0; i < NSUBEXP; ++i)
14840 {
14841 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014842 {
14843 if (list_append_string(rettv->vval.v_list,
14844 (char_u *)"", 0) == FAIL)
14845 break;
14846 }
14847 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014848 regmatch.startp[i],
14849 (int)(regmatch.endp[i] - regmatch.startp[i]))
14850 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014851 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014852 }
14853 }
14854 else if (type == 2)
14855 {
14856 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014857 if (l != NULL)
14858 copy_tv(&li->li_tv, rettv);
14859 else
14860 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014862 }
14863 else if (l != NULL)
14864 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865 else
14866 {
14867 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 (varnumber_T)(regmatch.startp[0] - str);
14870 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014871 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014873 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 }
14875 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014876 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877 }
14878
14879theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014880 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881 p_cpo = save_cpo;
14882}
14883
14884/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014885 * "match()" function
14886 */
14887 static void
14888f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014889 typval_T *argvars;
14890 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014891{
14892 find_some_match(argvars, rettv, 1);
14893}
14894
14895/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014896 * "matchadd()" function
14897 */
14898 static void
14899f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014900 typval_T *argvars UNUSED;
14901 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014902{
14903#ifdef FEAT_SEARCH_EXTRA
14904 char_u buf[NUMBUFLEN];
14905 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14906 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14907 int prio = 10; /* default priority */
14908 int id = -1;
14909 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014910 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014911
14912 rettv->vval.v_number = -1;
14913
14914 if (grp == NULL || pat == NULL)
14915 return;
14916 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014917 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014918 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014919 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014920 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014921 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014922 if (argvars[4].v_type != VAR_UNKNOWN)
14923 {
14924 if (argvars[4].v_type != VAR_DICT)
14925 {
14926 EMSG(_(e_dictreq));
14927 return;
14928 }
14929 if (dict_find(argvars[4].vval.v_dict,
14930 (char_u *)"conceal", -1) != NULL)
14931 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14932 (char_u *)"conceal", FALSE);
14933 }
14934 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014935 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014936 if (error == TRUE)
14937 return;
14938 if (id >= 1 && id <= 3)
14939 {
14940 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14941 return;
14942 }
14943
Bram Moolenaar6561d522015-07-21 15:48:27 +020014944 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14945 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014946#endif
14947}
14948
14949/*
14950 * "matchaddpos()" function
14951 */
14952 static void
14953f_matchaddpos(argvars, rettv)
14954 typval_T *argvars UNUSED;
14955 typval_T *rettv UNUSED;
14956{
14957#ifdef FEAT_SEARCH_EXTRA
14958 char_u buf[NUMBUFLEN];
14959 char_u *group;
14960 int prio = 10;
14961 int id = -1;
14962 int error = FALSE;
14963 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014964 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014965
14966 rettv->vval.v_number = -1;
14967
14968 group = get_tv_string_buf_chk(&argvars[0], buf);
14969 if (group == NULL)
14970 return;
14971
14972 if (argvars[1].v_type != VAR_LIST)
14973 {
14974 EMSG2(_(e_listarg), "matchaddpos()");
14975 return;
14976 }
14977 l = argvars[1].vval.v_list;
14978 if (l == NULL)
14979 return;
14980
14981 if (argvars[2].v_type != VAR_UNKNOWN)
14982 {
14983 prio = get_tv_number_chk(&argvars[2], &error);
14984 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014985 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014986 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014987 if (argvars[4].v_type != VAR_UNKNOWN)
14988 {
14989 if (argvars[4].v_type != VAR_DICT)
14990 {
14991 EMSG(_(e_dictreq));
14992 return;
14993 }
14994 if (dict_find(argvars[4].vval.v_dict,
14995 (char_u *)"conceal", -1) != NULL)
14996 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14997 (char_u *)"conceal", FALSE);
14998 }
14999 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015000 }
15001 if (error == TRUE)
15002 return;
15003
15004 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15005 if (id == 1 || id == 2)
15006 {
15007 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15008 return;
15009 }
15010
Bram Moolenaar6561d522015-07-21 15:48:27 +020015011 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15012 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015013#endif
15014}
15015
15016/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015017 * "matcharg()" function
15018 */
15019 static void
15020f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015021 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015022 typval_T *rettv;
15023{
15024 if (rettv_list_alloc(rettv) == OK)
15025 {
15026#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015027 int id = get_tv_number(&argvars[0]);
15028 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015029
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015030 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015031 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015032 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15033 {
15034 list_append_string(rettv->vval.v_list,
15035 syn_id2name(m->hlg_id), -1);
15036 list_append_string(rettv->vval.v_list, m->pattern, -1);
15037 }
15038 else
15039 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015040 list_append_string(rettv->vval.v_list, NULL, -1);
15041 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015042 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015043 }
15044#endif
15045 }
15046}
15047
15048/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015049 * "matchdelete()" function
15050 */
15051 static void
15052f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015053 typval_T *argvars UNUSED;
15054 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015055{
15056#ifdef FEAT_SEARCH_EXTRA
15057 rettv->vval.v_number = match_delete(curwin,
15058 (int)get_tv_number(&argvars[0]), TRUE);
15059#endif
15060}
15061
15062/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015063 * "matchend()" function
15064 */
15065 static void
15066f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015067 typval_T *argvars;
15068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069{
15070 find_some_match(argvars, rettv, 0);
15071}
15072
15073/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015074 * "matchlist()" function
15075 */
15076 static void
15077f_matchlist(argvars, rettv)
15078 typval_T *argvars;
15079 typval_T *rettv;
15080{
15081 find_some_match(argvars, rettv, 3);
15082}
15083
15084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 * "matchstr()" function
15086 */
15087 static void
15088f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015089 typval_T *argvars;
15090 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091{
15092 find_some_match(argvars, rettv, 2);
15093}
15094
Bram Moolenaar33570922005-01-25 22:26:29 +000015095static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015096
15097 static void
15098max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015099 typval_T *argvars;
15100 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015101 int domax;
15102{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015103 long n = 0;
15104 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015105 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015106
15107 if (argvars[0].v_type == VAR_LIST)
15108 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015109 list_T *l;
15110 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015111
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015112 l = argvars[0].vval.v_list;
15113 if (l != NULL)
15114 {
15115 li = l->lv_first;
15116 if (li != NULL)
15117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015118 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015119 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015120 {
15121 li = li->li_next;
15122 if (li == NULL)
15123 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015124 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015125 if (domax ? i > n : i < n)
15126 n = i;
15127 }
15128 }
15129 }
15130 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015131 else if (argvars[0].v_type == VAR_DICT)
15132 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015133 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015134 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015136 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015137
15138 d = argvars[0].vval.v_dict;
15139 if (d != NULL)
15140 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015141 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015143 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015144 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015146 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015147 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015148 if (first)
15149 {
15150 n = i;
15151 first = FALSE;
15152 }
15153 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015154 n = i;
15155 }
15156 }
15157 }
15158 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015159 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015160 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015161 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015162}
15163
15164/*
15165 * "max()" function
15166 */
15167 static void
15168f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015169 typval_T *argvars;
15170 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015171{
15172 max_min(argvars, rettv, TRUE);
15173}
15174
15175/*
15176 * "min()" function
15177 */
15178 static void
15179f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015180 typval_T *argvars;
15181 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015182{
15183 max_min(argvars, rettv, FALSE);
15184}
15185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015186static int mkdir_recurse __ARGS((char_u *dir, int prot));
15187
15188/*
15189 * Create the directory in which "dir" is located, and higher levels when
15190 * needed.
15191 */
15192 static int
15193mkdir_recurse(dir, prot)
15194 char_u *dir;
15195 int prot;
15196{
15197 char_u *p;
15198 char_u *updir;
15199 int r = FAIL;
15200
15201 /* Get end of directory name in "dir".
15202 * We're done when it's "/" or "c:/". */
15203 p = gettail_sep(dir);
15204 if (p <= get_past_head(dir))
15205 return OK;
15206
15207 /* If the directory exists we're done. Otherwise: create it.*/
15208 updir = vim_strnsave(dir, (int)(p - dir));
15209 if (updir == NULL)
15210 return FAIL;
15211 if (mch_isdir(updir))
15212 r = OK;
15213 else if (mkdir_recurse(updir, prot) == OK)
15214 r = vim_mkdir_emsg(updir, prot);
15215 vim_free(updir);
15216 return r;
15217}
15218
15219#ifdef vim_mkdir
15220/*
15221 * "mkdir()" function
15222 */
15223 static void
15224f_mkdir(argvars, rettv)
15225 typval_T *argvars;
15226 typval_T *rettv;
15227{
15228 char_u *dir;
15229 char_u buf[NUMBUFLEN];
15230 int prot = 0755;
15231
15232 rettv->vval.v_number = FAIL;
15233 if (check_restricted() || check_secure())
15234 return;
15235
15236 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015237 if (*dir == NUL)
15238 rettv->vval.v_number = FAIL;
15239 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015240 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015241 if (*gettail(dir) == NUL)
15242 /* remove trailing slashes */
15243 *gettail_sep(dir) = NUL;
15244
15245 if (argvars[1].v_type != VAR_UNKNOWN)
15246 {
15247 if (argvars[2].v_type != VAR_UNKNOWN)
15248 prot = get_tv_number_chk(&argvars[2], NULL);
15249 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15250 mkdir_recurse(dir, prot);
15251 }
15252 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015253 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015254}
15255#endif
15256
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 * "mode()" function
15259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015261f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015262 typval_T *argvars;
15263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015265 char_u buf[3];
15266
15267 buf[1] = NUL;
15268 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 if (VIsual_active)
15271 {
15272 if (VIsual_select)
15273 buf[0] = VIsual_mode + 's' - 'v';
15274 else
15275 buf[0] = VIsual_mode;
15276 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015277 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015278 || State == CONFIRM)
15279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015281 if (State == ASKMORE)
15282 buf[1] = 'm';
15283 else if (State == CONFIRM)
15284 buf[1] = '?';
15285 }
15286 else if (State == EXTERNCMD)
15287 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288 else if (State & INSERT)
15289 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015290#ifdef FEAT_VREPLACE
15291 if (State & VREPLACE_FLAG)
15292 {
15293 buf[0] = 'R';
15294 buf[1] = 'v';
15295 }
15296 else
15297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 if (State & REPLACE_FLAG)
15299 buf[0] = 'R';
15300 else
15301 buf[0] = 'i';
15302 }
15303 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015306 if (exmode_active)
15307 buf[1] = 'v';
15308 }
15309 else if (exmode_active)
15310 {
15311 buf[0] = 'c';
15312 buf[1] = 'e';
15313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015317 if (finish_op)
15318 buf[1] = 'o';
15319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015321 /* Clear out the minor mode when the argument is not a non-zero number or
15322 * non-empty string. */
15323 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015324 buf[1] = NUL;
15325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015326 rettv->vval.v_string = vim_strsave(buf);
15327 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328}
15329
Bram Moolenaar429fa852013-04-15 12:27:36 +020015330#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015331/*
15332 * "mzeval()" function
15333 */
15334 static void
15335f_mzeval(argvars, rettv)
15336 typval_T *argvars;
15337 typval_T *rettv;
15338{
15339 char_u *str;
15340 char_u buf[NUMBUFLEN];
15341
15342 str = get_tv_string_buf(&argvars[0], buf);
15343 do_mzeval(str, rettv);
15344}
Bram Moolenaar75676462013-01-30 14:55:42 +010015345
15346 void
15347mzscheme_call_vim(name, args, rettv)
15348 char_u *name;
15349 typval_T *args;
15350 typval_T *rettv;
15351{
15352 typval_T argvars[3];
15353
15354 argvars[0].v_type = VAR_STRING;
15355 argvars[0].vval.v_string = name;
15356 copy_tv(args, &argvars[1]);
15357 argvars[2].v_type = VAR_UNKNOWN;
15358 f_call(argvars, rettv);
15359 clear_tv(&argvars[1]);
15360}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015361#endif
15362
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015364 * "nextnonblank()" function
15365 */
15366 static void
15367f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015368 typval_T *argvars;
15369 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015370{
15371 linenr_T lnum;
15372
15373 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015375 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015376 {
15377 lnum = 0;
15378 break;
15379 }
15380 if (*skipwhite(ml_get(lnum)) != NUL)
15381 break;
15382 }
15383 rettv->vval.v_number = lnum;
15384}
15385
15386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 * "nr2char()" function
15388 */
15389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015391 typval_T *argvars;
15392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393{
15394 char_u buf[NUMBUFLEN];
15395
15396#ifdef FEAT_MBYTE
15397 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015398 {
15399 int utf8 = 0;
15400
15401 if (argvars[1].v_type != VAR_UNKNOWN)
15402 utf8 = get_tv_number_chk(&argvars[1], NULL);
15403 if (utf8)
15404 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15405 else
15406 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 else
15409#endif
15410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015411 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412 buf[1] = NUL;
15413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 rettv->v_type = VAR_STRING;
15415 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015416}
15417
15418/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015419 * "or(expr, expr)" function
15420 */
15421 static void
15422f_or(argvars, rettv)
15423 typval_T *argvars;
15424 typval_T *rettv;
15425{
15426 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15427 | get_tv_number_chk(&argvars[1], NULL);
15428}
15429
15430/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015431 * "pathshorten()" function
15432 */
15433 static void
15434f_pathshorten(argvars, rettv)
15435 typval_T *argvars;
15436 typval_T *rettv;
15437{
15438 char_u *p;
15439
15440 rettv->v_type = VAR_STRING;
15441 p = get_tv_string_chk(&argvars[0]);
15442 if (p == NULL)
15443 rettv->vval.v_string = NULL;
15444 else
15445 {
15446 p = vim_strsave(p);
15447 rettv->vval.v_string = p;
15448 if (p != NULL)
15449 shorten_dir(p);
15450 }
15451}
15452
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015453#ifdef FEAT_FLOAT
15454/*
15455 * "pow()" function
15456 */
15457 static void
15458f_pow(argvars, rettv)
15459 typval_T *argvars;
15460 typval_T *rettv;
15461{
15462 float_T fx, fy;
15463
15464 rettv->v_type = VAR_FLOAT;
15465 if (get_float_arg(argvars, &fx) == OK
15466 && get_float_arg(&argvars[1], &fy) == OK)
15467 rettv->vval.v_float = pow(fx, fy);
15468 else
15469 rettv->vval.v_float = 0.0;
15470}
15471#endif
15472
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015474 * "prevnonblank()" function
15475 */
15476 static void
15477f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015478 typval_T *argvars;
15479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015480{
15481 linenr_T lnum;
15482
15483 lnum = get_tv_lnum(argvars);
15484 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15485 lnum = 0;
15486 else
15487 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15488 --lnum;
15489 rettv->vval.v_number = lnum;
15490}
15491
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015492#ifdef HAVE_STDARG_H
15493/* This dummy va_list is here because:
15494 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15495 * - locally in the function results in a "used before set" warning
15496 * - using va_start() to initialize it gives "function with fixed args" error */
15497static va_list ap;
15498#endif
15499
Bram Moolenaar8c711452005-01-14 21:53:12 +000015500/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015501 * "printf()" function
15502 */
15503 static void
15504f_printf(argvars, rettv)
15505 typval_T *argvars;
15506 typval_T *rettv;
15507{
15508 rettv->v_type = VAR_STRING;
15509 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015510#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015511 {
15512 char_u buf[NUMBUFLEN];
15513 int len;
15514 char_u *s;
15515 int saved_did_emsg = did_emsg;
15516 char *fmt;
15517
15518 /* Get the required length, allocate the buffer and do it for real. */
15519 did_emsg = FALSE;
15520 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015521 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015522 if (!did_emsg)
15523 {
15524 s = alloc(len + 1);
15525 if (s != NULL)
15526 {
15527 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015528 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015529 }
15530 }
15531 did_emsg |= saved_did_emsg;
15532 }
15533#endif
15534}
15535
15536/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015537 * "pumvisible()" function
15538 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015539 static void
15540f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015541 typval_T *argvars UNUSED;
15542 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015543{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015544#ifdef FEAT_INS_EXPAND
15545 if (pum_visible())
15546 rettv->vval.v_number = 1;
15547#endif
15548}
15549
Bram Moolenaardb913952012-06-29 12:54:53 +020015550#ifdef FEAT_PYTHON3
15551/*
15552 * "py3eval()" function
15553 */
15554 static void
15555f_py3eval(argvars, rettv)
15556 typval_T *argvars;
15557 typval_T *rettv;
15558{
15559 char_u *str;
15560 char_u buf[NUMBUFLEN];
15561
15562 str = get_tv_string_buf(&argvars[0], buf);
15563 do_py3eval(str, rettv);
15564}
15565#endif
15566
15567#ifdef FEAT_PYTHON
15568/*
15569 * "pyeval()" function
15570 */
15571 static void
15572f_pyeval(argvars, rettv)
15573 typval_T *argvars;
15574 typval_T *rettv;
15575{
15576 char_u *str;
15577 char_u buf[NUMBUFLEN];
15578
15579 str = get_tv_string_buf(&argvars[0], buf);
15580 do_pyeval(str, rettv);
15581}
15582#endif
15583
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015584/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015585 * "range()" function
15586 */
15587 static void
15588f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015589 typval_T *argvars;
15590 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015591{
15592 long start;
15593 long end;
15594 long stride = 1;
15595 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015596 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015599 if (argvars[1].v_type == VAR_UNKNOWN)
15600 {
15601 end = start - 1;
15602 start = 0;
15603 }
15604 else
15605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015606 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015607 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015608 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015609 }
15610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015611 if (error)
15612 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015613 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015614 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015615 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015616 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015617 else
15618 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015619 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015620 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015621 if (list_append_number(rettv->vval.v_list,
15622 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015623 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015624 }
15625}
15626
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015627/*
15628 * "readfile()" function
15629 */
15630 static void
15631f_readfile(argvars, rettv)
15632 typval_T *argvars;
15633 typval_T *rettv;
15634{
15635 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015636 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015637 char_u *fname;
15638 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015639 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15640 int io_size = sizeof(buf);
15641 int readlen; /* size of last fread() */
15642 char_u *prev = NULL; /* previously read bytes, if any */
15643 long prevlen = 0; /* length of data in prev */
15644 long prevsize = 0; /* size of prev buffer */
15645 long maxline = MAXLNUM;
15646 long cnt = 0;
15647 char_u *p; /* position in buf */
15648 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015649
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015650 if (argvars[1].v_type != VAR_UNKNOWN)
15651 {
15652 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15653 binary = TRUE;
15654 if (argvars[2].v_type != VAR_UNKNOWN)
15655 maxline = get_tv_number(&argvars[2]);
15656 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015657
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015658 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015659 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015660
15661 /* Always open the file in binary mode, library functions have a mind of
15662 * their own about CR-LF conversion. */
15663 fname = get_tv_string(&argvars[0]);
15664 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15665 {
15666 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15667 return;
15668 }
15669
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015670 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015671 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015672 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015673
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015674 /* This for loop processes what was read, but is also entered at end
15675 * of file so that either:
15676 * - an incomplete line gets written
15677 * - a "binary" file gets an empty line at the end if it ends in a
15678 * newline. */
15679 for (p = buf, start = buf;
15680 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15681 ++p)
15682 {
15683 if (*p == '\n' || readlen <= 0)
15684 {
15685 listitem_T *li;
15686 char_u *s = NULL;
15687 long_u len = p - start;
15688
15689 /* Finished a line. Remove CRs before NL. */
15690 if (readlen > 0 && !binary)
15691 {
15692 while (len > 0 && start[len - 1] == '\r')
15693 --len;
15694 /* removal may cross back to the "prev" string */
15695 if (len == 0)
15696 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15697 --prevlen;
15698 }
15699 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015700 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015701 else
15702 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015703 /* Change "prev" buffer to be the right size. This way
15704 * the bytes are only copied once, and very long lines are
15705 * allocated only once. */
15706 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015707 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015708 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015709 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015710 prev = NULL; /* the list will own the string */
15711 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015712 }
15713 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015714 if (s == NULL)
15715 {
15716 do_outofmem_msg((long_u) prevlen + len + 1);
15717 failed = TRUE;
15718 break;
15719 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015720
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015721 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015722 {
15723 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015724 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015725 break;
15726 }
15727 li->li_tv.v_type = VAR_STRING;
15728 li->li_tv.v_lock = 0;
15729 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015730 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015731
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015732 start = p + 1; /* step over newline */
15733 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734 break;
15735 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015736 else if (*p == NUL)
15737 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015738#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015739 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15740 * when finding the BF and check the previous two bytes. */
15741 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015742 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015743 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15744 * + 1, these may be in the "prev" string. */
15745 char_u back1 = p >= buf + 1 ? p[-1]
15746 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15747 char_u back2 = p >= buf + 2 ? p[-2]
15748 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15749 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015750
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015751 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015752 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015753 char_u *dest = p - 2;
15754
15755 /* Usually a BOM is at the beginning of a file, and so at
15756 * the beginning of a line; then we can just step over it.
15757 */
15758 if (start == dest)
15759 start = p + 1;
15760 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015761 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015762 /* have to shuffle buf to close gap */
15763 int adjust_prevlen = 0;
15764
15765 if (dest < buf)
15766 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015767 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015768 dest = buf;
15769 }
15770 if (readlen > p - buf + 1)
15771 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15772 readlen -= 3 - adjust_prevlen;
15773 prevlen -= adjust_prevlen;
15774 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015775 }
15776 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015777 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015778#endif
15779 } /* for */
15780
15781 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15782 break;
15783 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015784 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015785 /* There's part of a line in buf, store it in "prev". */
15786 if (p - start + prevlen >= prevsize)
15787 {
15788 /* need bigger "prev" buffer */
15789 char_u *newprev;
15790
15791 /* A common use case is ordinary text files and "prev" gets a
15792 * fragment of a line, so the first allocation is made
15793 * small, to avoid repeatedly 'allocing' large and
15794 * 'reallocing' small. */
15795 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015796 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015797 else
15798 {
15799 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015800 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015801 prevsize = grow50pc > growmin ? grow50pc : growmin;
15802 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015803 newprev = prev == NULL ? alloc(prevsize)
15804 : vim_realloc(prev, prevsize);
15805 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015806 {
15807 do_outofmem_msg((long_u)prevsize);
15808 failed = TRUE;
15809 break;
15810 }
15811 prev = newprev;
15812 }
15813 /* Add the line part to end of "prev". */
15814 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015815 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015816 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015817 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015818
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015819 /*
15820 * For a negative line count use only the lines at the end of the file,
15821 * free the rest.
15822 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015823 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015824 while (cnt > -maxline)
15825 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015826 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015827 --cnt;
15828 }
15829
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015830 if (failed)
15831 {
15832 list_free(rettv->vval.v_list, TRUE);
15833 /* readfile doc says an empty list is returned on error */
15834 rettv->vval.v_list = list_alloc();
15835 }
15836
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015837 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015838 fclose(fd);
15839}
15840
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015841#if defined(FEAT_RELTIME)
15842static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15843
15844/*
15845 * Convert a List to proftime_T.
15846 * Return FAIL when there is something wrong.
15847 */
15848 static int
15849list2proftime(arg, tm)
15850 typval_T *arg;
15851 proftime_T *tm;
15852{
15853 long n1, n2;
15854 int error = FALSE;
15855
15856 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15857 || arg->vval.v_list->lv_len != 2)
15858 return FAIL;
15859 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15860 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15861# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015862 tm->HighPart = n1;
15863 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015864# else
15865 tm->tv_sec = n1;
15866 tm->tv_usec = n2;
15867# endif
15868 return error ? FAIL : OK;
15869}
15870#endif /* FEAT_RELTIME */
15871
15872/*
15873 * "reltime()" function
15874 */
15875 static void
15876f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015877 typval_T *argvars UNUSED;
15878 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015879{
15880#ifdef FEAT_RELTIME
15881 proftime_T res;
15882 proftime_T start;
15883
15884 if (argvars[0].v_type == VAR_UNKNOWN)
15885 {
15886 /* No arguments: get current time. */
15887 profile_start(&res);
15888 }
15889 else if (argvars[1].v_type == VAR_UNKNOWN)
15890 {
15891 if (list2proftime(&argvars[0], &res) == FAIL)
15892 return;
15893 profile_end(&res);
15894 }
15895 else
15896 {
15897 /* Two arguments: compute the difference. */
15898 if (list2proftime(&argvars[0], &start) == FAIL
15899 || list2proftime(&argvars[1], &res) == FAIL)
15900 return;
15901 profile_sub(&res, &start);
15902 }
15903
15904 if (rettv_list_alloc(rettv) == OK)
15905 {
15906 long n1, n2;
15907
15908# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015909 n1 = res.HighPart;
15910 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015911# else
15912 n1 = res.tv_sec;
15913 n2 = res.tv_usec;
15914# endif
15915 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15916 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15917 }
15918#endif
15919}
15920
15921/*
15922 * "reltimestr()" function
15923 */
15924 static void
15925f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015926 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015927 typval_T *rettv;
15928{
15929#ifdef FEAT_RELTIME
15930 proftime_T tm;
15931#endif
15932
15933 rettv->v_type = VAR_STRING;
15934 rettv->vval.v_string = NULL;
15935#ifdef FEAT_RELTIME
15936 if (list2proftime(&argvars[0], &tm) == OK)
15937 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15938#endif
15939}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015940
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15942static void make_connection __ARGS((void));
15943static int check_connection __ARGS((void));
15944
15945 static void
15946make_connection()
15947{
15948 if (X_DISPLAY == NULL
15949# ifdef FEAT_GUI
15950 && !gui.in_use
15951# endif
15952 )
15953 {
15954 x_force_connect = TRUE;
15955 setup_term_clip();
15956 x_force_connect = FALSE;
15957 }
15958}
15959
15960 static int
15961check_connection()
15962{
15963 make_connection();
15964 if (X_DISPLAY == NULL)
15965 {
15966 EMSG(_("E240: No connection to Vim server"));
15967 return FAIL;
15968 }
15969 return OK;
15970}
15971#endif
15972
15973#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015974static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015975
15976 static void
15977remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 typval_T *argvars;
15979 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015980 int expr;
15981{
15982 char_u *server_name;
15983 char_u *keys;
15984 char_u *r = NULL;
15985 char_u buf[NUMBUFLEN];
15986# ifdef WIN32
15987 HWND w;
15988# else
15989 Window w;
15990# endif
15991
15992 if (check_restricted() || check_secure())
15993 return;
15994
15995# ifdef FEAT_X11
15996 if (check_connection() == FAIL)
15997 return;
15998# endif
15999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016000 server_name = get_tv_string_chk(&argvars[0]);
16001 if (server_name == NULL)
16002 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016003 keys = get_tv_string_buf(&argvars[1], buf);
16004# ifdef WIN32
16005 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16006# else
16007 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16008 < 0)
16009# endif
16010 {
16011 if (r != NULL)
16012 EMSG(r); /* sending worked but evaluation failed */
16013 else
16014 EMSG2(_("E241: Unable to send to %s"), server_name);
16015 return;
16016 }
16017
16018 rettv->vval.v_string = r;
16019
16020 if (argvars[2].v_type != VAR_UNKNOWN)
16021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016022 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016023 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016024 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016025
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016026 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016027 v.di_tv.v_type = VAR_STRING;
16028 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016029 idvar = get_tv_string_chk(&argvars[2]);
16030 if (idvar != NULL)
16031 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016032 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016033 }
16034}
16035#endif
16036
16037/*
16038 * "remote_expr()" function
16039 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016040 static void
16041f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016042 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016043 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016044{
16045 rettv->v_type = VAR_STRING;
16046 rettv->vval.v_string = NULL;
16047#ifdef FEAT_CLIENTSERVER
16048 remote_common(argvars, rettv, TRUE);
16049#endif
16050}
16051
16052/*
16053 * "remote_foreground()" function
16054 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016055 static void
16056f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016057 typval_T *argvars UNUSED;
16058 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016059{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016060#ifdef FEAT_CLIENTSERVER
16061# ifdef WIN32
16062 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016063 {
16064 char_u *server_name = get_tv_string_chk(&argvars[0]);
16065
16066 if (server_name != NULL)
16067 serverForeground(server_name);
16068 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016069# else
16070 /* Send a foreground() expression to the server. */
16071 argvars[1].v_type = VAR_STRING;
16072 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16073 argvars[2].v_type = VAR_UNKNOWN;
16074 remote_common(argvars, rettv, TRUE);
16075 vim_free(argvars[1].vval.v_string);
16076# endif
16077#endif
16078}
16079
Bram Moolenaar0d660222005-01-07 21:51:51 +000016080 static void
16081f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016082 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016083 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016084{
16085#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016087 char_u *s = NULL;
16088# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016089 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016091 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016092
16093 if (check_restricted() || check_secure())
16094 {
16095 rettv->vval.v_number = -1;
16096 return;
16097 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098 serverid = get_tv_string_chk(&argvars[0]);
16099 if (serverid == NULL)
16100 {
16101 rettv->vval.v_number = -1;
16102 return; /* type error; errmsg already given */
16103 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016105 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016106 if (n == 0)
16107 rettv->vval.v_number = -1;
16108 else
16109 {
16110 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16111 rettv->vval.v_number = (s != NULL);
16112 }
16113# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016114 if (check_connection() == FAIL)
16115 return;
16116
16117 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016119# endif
16120
16121 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016123 char_u *retvar;
16124
Bram Moolenaar33570922005-01-25 22:26:29 +000016125 v.di_tv.v_type = VAR_STRING;
16126 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016127 retvar = get_tv_string_chk(&argvars[1]);
16128 if (retvar != NULL)
16129 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016130 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131 }
16132#else
16133 rettv->vval.v_number = -1;
16134#endif
16135}
16136
Bram Moolenaar0d660222005-01-07 21:51:51 +000016137 static void
16138f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016139 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016141{
16142 char_u *r = NULL;
16143
16144#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016145 char_u *serverid = get_tv_string_chk(&argvars[0]);
16146
16147 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016148 {
16149# ifdef WIN32
16150 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016151 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016153 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016154 if (n != 0)
16155 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16156 if (r == NULL)
16157# else
16158 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016159 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160# endif
16161 EMSG(_("E277: Unable to read a server reply"));
16162 }
16163#endif
16164 rettv->v_type = VAR_STRING;
16165 rettv->vval.v_string = r;
16166}
16167
16168/*
16169 * "remote_send()" function
16170 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171 static void
16172f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016173 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016174 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175{
16176 rettv->v_type = VAR_STRING;
16177 rettv->vval.v_string = NULL;
16178#ifdef FEAT_CLIENTSERVER
16179 remote_common(argvars, rettv, FALSE);
16180#endif
16181}
16182
16183/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016184 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016185 */
16186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016187f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016188 typval_T *argvars;
16189 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016190{
Bram Moolenaar33570922005-01-25 22:26:29 +000016191 list_T *l;
16192 listitem_T *item, *item2;
16193 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016194 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016195 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016196 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016197 dict_T *d;
16198 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016199 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016200
Bram Moolenaar8c711452005-01-14 21:53:12 +000016201 if (argvars[0].v_type == VAR_DICT)
16202 {
16203 if (argvars[2].v_type != VAR_UNKNOWN)
16204 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016205 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016206 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016208 key = get_tv_string_chk(&argvars[1]);
16209 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016211 di = dict_find(d, key, -1);
16212 if (di == NULL)
16213 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016214 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16215 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016216 {
16217 *rettv = di->di_tv;
16218 init_tv(&di->di_tv);
16219 dictitem_remove(d, di);
16220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016221 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016222 }
16223 }
16224 else if (argvars[0].v_type != VAR_LIST)
16225 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016226 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016227 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016229 int error = FALSE;
16230
16231 idx = get_tv_number_chk(&argvars[1], &error);
16232 if (error)
16233 ; /* type error: do nothing, errmsg already given */
16234 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016235 EMSGN(_(e_listidx), idx);
16236 else
16237 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016238 if (argvars[2].v_type == VAR_UNKNOWN)
16239 {
16240 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016241 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016242 *rettv = item->li_tv;
16243 vim_free(item);
16244 }
16245 else
16246 {
16247 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016248 end = get_tv_number_chk(&argvars[2], &error);
16249 if (error)
16250 ; /* type error: do nothing */
16251 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016252 EMSGN(_(e_listidx), end);
16253 else
16254 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016255 int cnt = 0;
16256
16257 for (li = item; li != NULL; li = li->li_next)
16258 {
16259 ++cnt;
16260 if (li == item2)
16261 break;
16262 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016263 if (li == NULL) /* didn't find "item2" after "item" */
16264 EMSG(_(e_invrange));
16265 else
16266 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016267 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016268 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016269 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016270 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016271 l->lv_first = item;
16272 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016273 item->li_prev = NULL;
16274 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016275 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016276 }
16277 }
16278 }
16279 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016280 }
16281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282}
16283
16284/*
16285 * "rename({from}, {to})" function
16286 */
16287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016289 typval_T *argvars;
16290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291{
16292 char_u buf[NUMBUFLEN];
16293
16294 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016295 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016297 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16298 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299}
16300
16301/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016302 * "repeat()" function
16303 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016304 static void
16305f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016306 typval_T *argvars;
16307 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016308{
16309 char_u *p;
16310 int n;
16311 int slen;
16312 int len;
16313 char_u *r;
16314 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016315
16316 n = get_tv_number(&argvars[1]);
16317 if (argvars[0].v_type == VAR_LIST)
16318 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016319 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016320 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016321 if (list_extend(rettv->vval.v_list,
16322 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016323 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016324 }
16325 else
16326 {
16327 p = get_tv_string(&argvars[0]);
16328 rettv->v_type = VAR_STRING;
16329 rettv->vval.v_string = NULL;
16330
16331 slen = (int)STRLEN(p);
16332 len = slen * n;
16333 if (len <= 0)
16334 return;
16335
16336 r = alloc(len + 1);
16337 if (r != NULL)
16338 {
16339 for (i = 0; i < n; i++)
16340 mch_memmove(r + i * slen, p, (size_t)slen);
16341 r[len] = NUL;
16342 }
16343
16344 rettv->vval.v_string = r;
16345 }
16346}
16347
16348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349 * "resolve()" function
16350 */
16351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016353 typval_T *argvars;
16354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355{
16356 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016357#ifdef HAVE_READLINK
16358 char_u *buf = NULL;
16359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362#ifdef FEAT_SHORTCUT
16363 {
16364 char_u *v = NULL;
16365
16366 v = mch_resolve_shortcut(p);
16367 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016368 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 }
16372#else
16373# ifdef HAVE_READLINK
16374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 char_u *cpy;
16376 int len;
16377 char_u *remain = NULL;
16378 char_u *q;
16379 int is_relative_to_current = FALSE;
16380 int has_trailing_pathsep = FALSE;
16381 int limit = 100;
16382
16383 p = vim_strsave(p);
16384
16385 if (p[0] == '.' && (vim_ispathsep(p[1])
16386 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16387 is_relative_to_current = TRUE;
16388
16389 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016390 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016393 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395
16396 q = getnextcomp(p);
16397 if (*q != NUL)
16398 {
16399 /* Separate the first path component in "p", and keep the
16400 * remainder (beginning with the path separator). */
16401 remain = vim_strsave(q - 1);
16402 q[-1] = NUL;
16403 }
16404
Bram Moolenaard9462e32011-04-11 21:35:11 +020016405 buf = alloc(MAXPATHL + 1);
16406 if (buf == NULL)
16407 goto fail;
16408
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 for (;;)
16410 {
16411 for (;;)
16412 {
16413 len = readlink((char *)p, (char *)buf, MAXPATHL);
16414 if (len <= 0)
16415 break;
16416 buf[len] = NUL;
16417
16418 if (limit-- == 0)
16419 {
16420 vim_free(p);
16421 vim_free(remain);
16422 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016423 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 goto fail;
16425 }
16426
16427 /* Ensure that the result will have a trailing path separator
16428 * if the argument has one. */
16429 if (remain == NULL && has_trailing_pathsep)
16430 add_pathsep(buf);
16431
16432 /* Separate the first path component in the link value and
16433 * concatenate the remainders. */
16434 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16435 if (*q != NUL)
16436 {
16437 if (remain == NULL)
16438 remain = vim_strsave(q - 1);
16439 else
16440 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016441 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442 if (cpy != NULL)
16443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 vim_free(remain);
16445 remain = cpy;
16446 }
16447 }
16448 q[-1] = NUL;
16449 }
16450
16451 q = gettail(p);
16452 if (q > p && *q == NUL)
16453 {
16454 /* Ignore trailing path separator. */
16455 q[-1] = NUL;
16456 q = gettail(p);
16457 }
16458 if (q > p && !mch_isFullName(buf))
16459 {
16460 /* symlink is relative to directory of argument */
16461 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16462 if (cpy != NULL)
16463 {
16464 STRCPY(cpy, p);
16465 STRCPY(gettail(cpy), buf);
16466 vim_free(p);
16467 p = cpy;
16468 }
16469 }
16470 else
16471 {
16472 vim_free(p);
16473 p = vim_strsave(buf);
16474 }
16475 }
16476
16477 if (remain == NULL)
16478 break;
16479
16480 /* Append the first path component of "remain" to "p". */
16481 q = getnextcomp(remain + 1);
16482 len = q - remain - (*q != NUL);
16483 cpy = vim_strnsave(p, STRLEN(p) + len);
16484 if (cpy != NULL)
16485 {
16486 STRNCAT(cpy, remain, len);
16487 vim_free(p);
16488 p = cpy;
16489 }
16490 /* Shorten "remain". */
16491 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016492 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 else
16494 {
16495 vim_free(remain);
16496 remain = NULL;
16497 }
16498 }
16499
16500 /* If the result is a relative path name, make it explicitly relative to
16501 * the current directory if and only if the argument had this form. */
16502 if (!vim_ispathsep(*p))
16503 {
16504 if (is_relative_to_current
16505 && *p != NUL
16506 && !(p[0] == '.'
16507 && (p[1] == NUL
16508 || vim_ispathsep(p[1])
16509 || (p[1] == '.'
16510 && (p[2] == NUL
16511 || vim_ispathsep(p[2]))))))
16512 {
16513 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016514 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515 if (cpy != NULL)
16516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 vim_free(p);
16518 p = cpy;
16519 }
16520 }
16521 else if (!is_relative_to_current)
16522 {
16523 /* Strip leading "./". */
16524 q = p;
16525 while (q[0] == '.' && vim_ispathsep(q[1]))
16526 q += 2;
16527 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016528 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 }
16530 }
16531
16532 /* Ensure that the result will have no trailing path separator
16533 * if the argument had none. But keep "/" or "//". */
16534 if (!has_trailing_pathsep)
16535 {
16536 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016537 if (after_pathsep(p, q))
16538 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 }
16540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016541 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 }
16543# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016544 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545# endif
16546#endif
16547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016548 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549
16550#ifdef HAVE_READLINK
16551fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016552 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016554 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555}
16556
16557/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016558 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 */
16560 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016562 typval_T *argvars;
16563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564{
Bram Moolenaar33570922005-01-25 22:26:29 +000016565 list_T *l;
16566 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568 if (argvars[0].v_type != VAR_LIST)
16569 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016570 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016571 && !tv_check_lock(l->lv_lock,
16572 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016573 {
16574 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016575 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016576 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577 while (li != NULL)
16578 {
16579 ni = li->li_prev;
16580 list_append(l, li);
16581 li = ni;
16582 }
16583 rettv->vval.v_list = l;
16584 rettv->v_type = VAR_LIST;
16585 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016586 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588}
16589
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016590#define SP_NOMOVE 0x01 /* don't move cursor */
16591#define SP_REPEAT 0x02 /* repeat to find outer pair */
16592#define SP_RETCOUNT 0x04 /* return matchcount */
16593#define SP_SETPCMARK 0x08 /* set previous context mark */
16594#define SP_START 0x10 /* accept match at start position */
16595#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16596#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016597#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016598
Bram Moolenaar33570922005-01-25 22:26:29 +000016599static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016600
16601/*
16602 * Get flags for a search function.
16603 * Possibly sets "p_ws".
16604 * Returns BACKWARD, FORWARD or zero (for an error).
16605 */
16606 static int
16607get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016608 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016609 int *flagsp;
16610{
16611 int dir = FORWARD;
16612 char_u *flags;
16613 char_u nbuf[NUMBUFLEN];
16614 int mask;
16615
16616 if (varp->v_type != VAR_UNKNOWN)
16617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016618 flags = get_tv_string_buf_chk(varp, nbuf);
16619 if (flags == NULL)
16620 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621 while (*flags != NUL)
16622 {
16623 switch (*flags)
16624 {
16625 case 'b': dir = BACKWARD; break;
16626 case 'w': p_ws = TRUE; break;
16627 case 'W': p_ws = FALSE; break;
16628 default: mask = 0;
16629 if (flagsp != NULL)
16630 switch (*flags)
16631 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016632 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016633 case 'e': mask = SP_END; break;
16634 case 'm': mask = SP_RETCOUNT; break;
16635 case 'n': mask = SP_NOMOVE; break;
16636 case 'p': mask = SP_SUBPAT; break;
16637 case 'r': mask = SP_REPEAT; break;
16638 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016639 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016640 }
16641 if (mask == 0)
16642 {
16643 EMSG2(_(e_invarg2), flags);
16644 dir = 0;
16645 }
16646 else
16647 *flagsp |= mask;
16648 }
16649 if (dir == 0)
16650 break;
16651 ++flags;
16652 }
16653 }
16654 return dir;
16655}
16656
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016658 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016660 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016661search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016663 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016664 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016666 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 char_u *pat;
16668 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016669 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 int save_p_ws = p_ws;
16671 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016672 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016673 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016674 proftime_T tm;
16675#ifdef FEAT_RELTIME
16676 long time_limit = 0;
16677#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016678 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016679 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016681 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016682 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016683 if (dir == 0)
16684 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016685 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016686 if (flags & SP_START)
16687 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016688 if (flags & SP_END)
16689 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016690 if (flags & SP_COLUMN)
16691 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016692
Bram Moolenaar76929292008-01-06 19:07:36 +000016693 /* Optional arguments: line number to stop searching and timeout. */
16694 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016695 {
16696 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16697 if (lnum_stop < 0)
16698 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016699#ifdef FEAT_RELTIME
16700 if (argvars[3].v_type != VAR_UNKNOWN)
16701 {
16702 time_limit = get_tv_number_chk(&argvars[3], NULL);
16703 if (time_limit < 0)
16704 goto theend;
16705 }
16706#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016707 }
16708
Bram Moolenaar76929292008-01-06 19:07:36 +000016709#ifdef FEAT_RELTIME
16710 /* Set the time limit, if there is one. */
16711 profile_setlimit(time_limit, &tm);
16712#endif
16713
Bram Moolenaar231334e2005-07-25 20:46:57 +000016714 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016715 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016716 * Check to make sure only those flags are set.
16717 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16718 * flags cannot be set. Check for that condition also.
16719 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016720 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016721 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016723 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016724 goto theend;
16725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016727 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016728 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016729 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016730 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016732 if (flags & SP_SUBPAT)
16733 retval = subpatnum;
16734 else
16735 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016736 if (flags & SP_SETPCMARK)
16737 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016739 if (match_pos != NULL)
16740 {
16741 /* Store the match cursor position */
16742 match_pos->lnum = pos.lnum;
16743 match_pos->col = pos.col + 1;
16744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 /* "/$" will put the cursor after the end of the line, may need to
16746 * correct that here */
16747 check_cursor();
16748 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016749
16750 /* If 'n' flag is used: restore cursor position. */
16751 if (flags & SP_NOMOVE)
16752 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016753 else
16754 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016755theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016757
16758 return retval;
16759}
16760
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016761#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016762
16763/*
16764 * round() is not in C90, use ceil() or floor() instead.
16765 */
16766 float_T
16767vim_round(f)
16768 float_T f;
16769{
16770 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16771}
16772
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016773/*
16774 * "round({float})" function
16775 */
16776 static void
16777f_round(argvars, rettv)
16778 typval_T *argvars;
16779 typval_T *rettv;
16780{
16781 float_T f;
16782
16783 rettv->v_type = VAR_FLOAT;
16784 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016785 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016786 else
16787 rettv->vval.v_float = 0.0;
16788}
16789#endif
16790
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016791/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016792 * "screenattr()" function
16793 */
16794 static void
16795f_screenattr(argvars, rettv)
16796 typval_T *argvars UNUSED;
16797 typval_T *rettv;
16798{
16799 int row;
16800 int col;
16801 int c;
16802
16803 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16804 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16805 if (row < 0 || row >= screen_Rows
16806 || col < 0 || col >= screen_Columns)
16807 c = -1;
16808 else
16809 c = ScreenAttrs[LineOffset[row] + col];
16810 rettv->vval.v_number = c;
16811}
16812
16813/*
16814 * "screenchar()" function
16815 */
16816 static void
16817f_screenchar(argvars, rettv)
16818 typval_T *argvars UNUSED;
16819 typval_T *rettv;
16820{
16821 int row;
16822 int col;
16823 int off;
16824 int c;
16825
16826 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16827 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16828 if (row < 0 || row >= screen_Rows
16829 || col < 0 || col >= screen_Columns)
16830 c = -1;
16831 else
16832 {
16833 off = LineOffset[row] + col;
16834#ifdef FEAT_MBYTE
16835 if (enc_utf8 && ScreenLinesUC[off] != 0)
16836 c = ScreenLinesUC[off];
16837 else
16838#endif
16839 c = ScreenLines[off];
16840 }
16841 rettv->vval.v_number = c;
16842}
16843
16844/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016845 * "screencol()" function
16846 *
16847 * First column is 1 to be consistent with virtcol().
16848 */
16849 static void
16850f_screencol(argvars, rettv)
16851 typval_T *argvars UNUSED;
16852 typval_T *rettv;
16853{
16854 rettv->vval.v_number = screen_screencol() + 1;
16855}
16856
16857/*
16858 * "screenrow()" function
16859 */
16860 static void
16861f_screenrow(argvars, rettv)
16862 typval_T *argvars UNUSED;
16863 typval_T *rettv;
16864{
16865 rettv->vval.v_number = screen_screenrow() + 1;
16866}
16867
16868/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016869 * "search()" function
16870 */
16871 static void
16872f_search(argvars, rettv)
16873 typval_T *argvars;
16874 typval_T *rettv;
16875{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016876 int flags = 0;
16877
16878 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879}
16880
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016882 * "searchdecl()" function
16883 */
16884 static void
16885f_searchdecl(argvars, rettv)
16886 typval_T *argvars;
16887 typval_T *rettv;
16888{
16889 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016890 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016891 int error = FALSE;
16892 char_u *name;
16893
16894 rettv->vval.v_number = 1; /* default: FAIL */
16895
16896 name = get_tv_string_chk(&argvars[0]);
16897 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016898 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016899 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016900 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16901 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16902 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016903 if (!error && name != NULL)
16904 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016905 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016906}
16907
16908/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016909 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016911 static int
16912searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016914 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915{
16916 char_u *spat, *mpat, *epat;
16917 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 int dir;
16920 int flags = 0;
16921 char_u nbuf1[NUMBUFLEN];
16922 char_u nbuf2[NUMBUFLEN];
16923 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016924 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016925 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016926 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016929 spat = get_tv_string_chk(&argvars[0]);
16930 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16931 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16932 if (spat == NULL || mpat == NULL || epat == NULL)
16933 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 /* Handle the optional fourth argument: flags */
16936 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016937 if (dir == 0)
16938 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016939
16940 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016941 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16942 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016943 if ((flags & (SP_END | SP_SUBPAT)) != 0
16944 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016945 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016946 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016947 goto theend;
16948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016950 /* Using 'r' implies 'W', otherwise it doesn't work. */
16951 if (flags & SP_REPEAT)
16952 p_ws = FALSE;
16953
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016954 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016955 if (argvars[3].v_type == VAR_UNKNOWN
16956 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 skip = (char_u *)"";
16958 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016960 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016961 if (argvars[5].v_type != VAR_UNKNOWN)
16962 {
16963 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16964 if (lnum_stop < 0)
16965 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016966#ifdef FEAT_RELTIME
16967 if (argvars[6].v_type != VAR_UNKNOWN)
16968 {
16969 time_limit = get_tv_number_chk(&argvars[6], NULL);
16970 if (time_limit < 0)
16971 goto theend;
16972 }
16973#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016974 }
16975 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016976 if (skip == NULL)
16977 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016979 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016980 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016981
16982theend:
16983 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016984
16985 return retval;
16986}
16987
16988/*
16989 * "searchpair()" function
16990 */
16991 static void
16992f_searchpair(argvars, rettv)
16993 typval_T *argvars;
16994 typval_T *rettv;
16995{
16996 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16997}
16998
16999/*
17000 * "searchpairpos()" function
17001 */
17002 static void
17003f_searchpairpos(argvars, rettv)
17004 typval_T *argvars;
17005 typval_T *rettv;
17006{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017007 pos_T match_pos;
17008 int lnum = 0;
17009 int col = 0;
17010
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017011 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017012 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017013
17014 if (searchpair_cmn(argvars, &match_pos) > 0)
17015 {
17016 lnum = match_pos.lnum;
17017 col = match_pos.col;
17018 }
17019
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017020 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17021 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017022}
17023
17024/*
17025 * Search for a start/middle/end thing.
17026 * Used by searchpair(), see its documentation for the details.
17027 * Returns 0 or -1 for no match,
17028 */
17029 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017030do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17031 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017032 char_u *spat; /* start pattern */
17033 char_u *mpat; /* middle pattern */
17034 char_u *epat; /* end pattern */
17035 int dir; /* BACKWARD or FORWARD */
17036 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017037 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017038 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017039 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017040 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017041{
17042 char_u *save_cpo;
17043 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17044 long retval = 0;
17045 pos_T pos;
17046 pos_T firstpos;
17047 pos_T foundpos;
17048 pos_T save_cursor;
17049 pos_T save_pos;
17050 int n;
17051 int r;
17052 int nest = 1;
17053 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017054 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017055 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017056
17057 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17058 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017059 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017060
Bram Moolenaar76929292008-01-06 19:07:36 +000017061#ifdef FEAT_RELTIME
17062 /* Set the time limit, if there is one. */
17063 profile_setlimit(time_limit, &tm);
17064#endif
17065
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017066 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17067 * start/middle/end (pat3, for the top pair). */
17068 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17069 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17070 if (pat2 == NULL || pat3 == NULL)
17071 goto theend;
17072 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17073 if (*mpat == NUL)
17074 STRCPY(pat3, pat2);
17075 else
17076 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17077 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017078 if (flags & SP_START)
17079 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017080
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 save_cursor = curwin->w_cursor;
17082 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017083 clearpos(&firstpos);
17084 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 pat = pat3;
17086 for (;;)
17087 {
17088 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017089 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17091 /* didn't find it or found the first match again: FAIL */
17092 break;
17093
17094 if (firstpos.lnum == 0)
17095 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017096 if (equalpos(pos, foundpos))
17097 {
17098 /* Found the same position again. Can happen with a pattern that
17099 * has "\zs" at the end and searching backwards. Advance one
17100 * character and try again. */
17101 if (dir == BACKWARD)
17102 decl(&pos);
17103 else
17104 incl(&pos);
17105 }
17106 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017108 /* clear the start flag to avoid getting stuck here */
17109 options &= ~SEARCH_START;
17110
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 /* If the skip pattern matches, ignore this match. */
17112 if (*skip != NUL)
17113 {
17114 save_pos = curwin->w_cursor;
17115 curwin->w_cursor = pos;
17116 r = eval_to_bool(skip, &err, NULL, FALSE);
17117 curwin->w_cursor = save_pos;
17118 if (err)
17119 {
17120 /* Evaluating {skip} caused an error, break here. */
17121 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017122 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 break;
17124 }
17125 if (r)
17126 continue;
17127 }
17128
17129 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17130 {
17131 /* Found end when searching backwards or start when searching
17132 * forward: nested pair. */
17133 ++nest;
17134 pat = pat2; /* nested, don't search for middle */
17135 }
17136 else
17137 {
17138 /* Found end when searching forward or start when searching
17139 * backward: end of (nested) pair; or found middle in outer pair. */
17140 if (--nest == 1)
17141 pat = pat3; /* outer level, search for middle */
17142 }
17143
17144 if (nest == 0)
17145 {
17146 /* Found the match: return matchcount or line number. */
17147 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017148 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017150 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017151 if (flags & SP_SETPCMARK)
17152 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 curwin->w_cursor = pos;
17154 if (!(flags & SP_REPEAT))
17155 break;
17156 nest = 1; /* search for next unmatched */
17157 }
17158 }
17159
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017160 if (match_pos != NULL)
17161 {
17162 /* Store the match cursor position */
17163 match_pos->lnum = curwin->w_cursor.lnum;
17164 match_pos->col = curwin->w_cursor.col + 1;
17165 }
17166
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017168 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 curwin->w_cursor = save_cursor;
17170
17171theend:
17172 vim_free(pat2);
17173 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017174 if (p_cpo == empty_option)
17175 p_cpo = save_cpo;
17176 else
17177 /* Darn, evaluating the {skip} expression changed the value. */
17178 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017179
17180 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181}
17182
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017183/*
17184 * "searchpos()" function
17185 */
17186 static void
17187f_searchpos(argvars, rettv)
17188 typval_T *argvars;
17189 typval_T *rettv;
17190{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017191 pos_T match_pos;
17192 int lnum = 0;
17193 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017194 int n;
17195 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017196
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017197 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017198 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017199
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017200 n = search_cmn(argvars, &match_pos, &flags);
17201 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017202 {
17203 lnum = match_pos.lnum;
17204 col = match_pos.col;
17205 }
17206
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017207 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17208 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017209 if (flags & SP_SUBPAT)
17210 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017211}
17212
17213
Bram Moolenaar0d660222005-01-07 21:51:51 +000017214 static void
17215f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017216 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017219#ifdef FEAT_CLIENTSERVER
17220 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017221 char_u *server = get_tv_string_chk(&argvars[0]);
17222 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017225 if (server == NULL || reply == NULL)
17226 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227 if (check_restricted() || check_secure())
17228 return;
17229# ifdef FEAT_X11
17230 if (check_connection() == FAIL)
17231 return;
17232# endif
17233
17234 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017236 EMSG(_("E258: Unable to send to client"));
17237 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017239 rettv->vval.v_number = 0;
17240#else
17241 rettv->vval.v_number = -1;
17242#endif
17243}
17244
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245 static void
17246f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017247 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017248 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249{
17250 char_u *r = NULL;
17251
17252#ifdef FEAT_CLIENTSERVER
17253# ifdef WIN32
17254 r = serverGetVimNames();
17255# else
17256 make_connection();
17257 if (X_DISPLAY != NULL)
17258 r = serverGetVimNames(X_DISPLAY);
17259# endif
17260#endif
17261 rettv->v_type = VAR_STRING;
17262 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263}
17264
17265/*
17266 * "setbufvar()" function
17267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017269f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017270 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017271 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272{
17273 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 char_u nbuf[NUMBUFLEN];
17278
17279 if (check_restricted() || check_secure())
17280 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017281 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17282 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017283 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 varp = &argvars[2];
17285
17286 if (buf != NULL && varname != NULL && varp != NULL)
17287 {
17288 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290
17291 if (*varname == '&')
17292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017293 long numval;
17294 char_u *strval;
17295 int error = FALSE;
17296
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017298 numval = get_tv_number_chk(varp, &error);
17299 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 if (!error && strval != NULL)
17301 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302 }
17303 else
17304 {
17305 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17306 if (bufvarname != NULL)
17307 {
17308 STRCPY(bufvarname, "b:");
17309 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017310 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 vim_free(bufvarname);
17312 }
17313 }
17314
17315 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318}
17319
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017320 static void
17321f_setcharsearch(argvars, rettv)
17322 typval_T *argvars;
17323 typval_T *rettv UNUSED;
17324{
17325 dict_T *d;
17326 dictitem_T *di;
17327 char_u *csearch;
17328
17329 if (argvars[0].v_type != VAR_DICT)
17330 {
17331 EMSG(_(e_dictreq));
17332 return;
17333 }
17334
17335 if ((d = argvars[0].vval.v_dict) != NULL)
17336 {
17337 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17338 if (csearch != NULL)
17339 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017340#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017341 if (enc_utf8)
17342 {
17343 int pcc[MAX_MCO];
17344 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017345
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017346 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17347 }
17348 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017349#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017350 set_last_csearch(PTR2CHAR(csearch),
17351 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017352 }
17353
17354 di = dict_find(d, (char_u *)"forward", -1);
17355 if (di != NULL)
17356 set_csearch_direction(get_tv_number(&di->di_tv)
17357 ? FORWARD : BACKWARD);
17358
17359 di = dict_find(d, (char_u *)"until", -1);
17360 if (di != NULL)
17361 set_csearch_until(!!get_tv_number(&di->di_tv));
17362 }
17363}
17364
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365/*
17366 * "setcmdpos()" function
17367 */
17368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 typval_T *argvars;
17371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373 int pos = (int)get_tv_number(&argvars[0]) - 1;
17374
17375 if (pos >= 0)
17376 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377}
17378
17379/*
17380 * "setline()" function
17381 */
17382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017384 typval_T *argvars;
17385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386{
17387 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017388 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017389 list_T *l = NULL;
17390 listitem_T *li = NULL;
17391 long added = 0;
17392 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017394 lnum = get_tv_lnum(&argvars[0]);
17395 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017397 l = argvars[1].vval.v_list;
17398 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017400 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017401 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017402
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017403 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017404 for (;;)
17405 {
17406 if (l != NULL)
17407 {
17408 /* list argument, get next string */
17409 if (li == NULL)
17410 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017411 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017412 li = li->li_next;
17413 }
17414
17415 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017416 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017417 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017418
17419 /* When coming here from Insert mode, sync undo, so that this can be
17420 * undone separately from what was previously inserted. */
17421 if (u_sync_once == 2)
17422 {
17423 u_sync_once = 1; /* notify that u_sync() was called */
17424 u_sync(TRUE);
17425 }
17426
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017427 if (lnum <= curbuf->b_ml.ml_line_count)
17428 {
17429 /* existing line, replace it */
17430 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17431 {
17432 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017433 if (lnum == curwin->w_cursor.lnum)
17434 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017435 rettv->vval.v_number = 0; /* OK */
17436 }
17437 }
17438 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17439 {
17440 /* lnum is one past the last line, append the line */
17441 ++added;
17442 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17443 rettv->vval.v_number = 0; /* OK */
17444 }
17445
17446 if (l == NULL) /* only one string argument */
17447 break;
17448 ++lnum;
17449 }
17450
17451 if (added > 0)
17452 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453}
17454
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017455static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17456
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017458 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017459 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017460 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017461set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017462 win_T *wp UNUSED;
17463 typval_T *list_arg UNUSED;
17464 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017465 typval_T *rettv;
17466{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017467#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017468 char_u *act;
17469 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017470#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017471
Bram Moolenaar2641f772005-03-25 21:58:17 +000017472 rettv->vval.v_number = -1;
17473
17474#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017475 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017476 EMSG(_(e_listreq));
17477 else
17478 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017479 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017480
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017481 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017482 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017483 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017484 if (act == NULL)
17485 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017486 if (*act == 'a' || *act == 'r')
17487 action = *act;
17488 }
17489
Bram Moolenaar81484f42012-12-05 15:16:47 +010017490 if (l != NULL && set_errorlist(wp, l, action,
17491 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017492 rettv->vval.v_number = 0;
17493 }
17494#endif
17495}
17496
17497/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017498 * "setloclist()" function
17499 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017500 static void
17501f_setloclist(argvars, rettv)
17502 typval_T *argvars;
17503 typval_T *rettv;
17504{
17505 win_T *win;
17506
17507 rettv->vval.v_number = -1;
17508
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017509 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017510 if (win != NULL)
17511 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17512}
17513
17514/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017515 * "setmatches()" function
17516 */
17517 static void
17518f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017519 typval_T *argvars UNUSED;
17520 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017521{
17522#ifdef FEAT_SEARCH_EXTRA
17523 list_T *l;
17524 listitem_T *li;
17525 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017526 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017527
17528 rettv->vval.v_number = -1;
17529 if (argvars[0].v_type != VAR_LIST)
17530 {
17531 EMSG(_(e_listreq));
17532 return;
17533 }
17534 if ((l = argvars[0].vval.v_list) != NULL)
17535 {
17536
17537 /* To some extent make sure that we are dealing with a list from
17538 * "getmatches()". */
17539 li = l->lv_first;
17540 while (li != NULL)
17541 {
17542 if (li->li_tv.v_type != VAR_DICT
17543 || (d = li->li_tv.vval.v_dict) == NULL)
17544 {
17545 EMSG(_(e_invarg));
17546 return;
17547 }
17548 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017549 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17550 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017551 && dict_find(d, (char_u *)"priority", -1) != NULL
17552 && dict_find(d, (char_u *)"id", -1) != NULL))
17553 {
17554 EMSG(_(e_invarg));
17555 return;
17556 }
17557 li = li->li_next;
17558 }
17559
17560 clear_matches(curwin);
17561 li = l->lv_first;
17562 while (li != NULL)
17563 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017564 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017565 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017566 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017567 char_u *group;
17568 int priority;
17569 int id;
17570 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017571
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017572 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017573 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17574 {
17575 if (s == NULL)
17576 {
17577 s = list_alloc();
17578 if (s == NULL)
17579 return;
17580 }
17581
17582 /* match from matchaddpos() */
17583 for (i = 1; i < 9; i++)
17584 {
17585 sprintf((char *)buf, (char *)"pos%d", i);
17586 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17587 {
17588 if (di->di_tv.v_type != VAR_LIST)
17589 return;
17590
17591 list_append_tv(s, &di->di_tv);
17592 s->lv_refcount++;
17593 }
17594 else
17595 break;
17596 }
17597 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017598
17599 group = get_dict_string(d, (char_u *)"group", FALSE);
17600 priority = (int)get_dict_number(d, (char_u *)"priority");
17601 id = (int)get_dict_number(d, (char_u *)"id");
17602 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17603 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17604 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017605 if (i == 0)
17606 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017607 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017608 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017609 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017610 }
17611 else
17612 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017613 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017614 list_unref(s);
17615 s = NULL;
17616 }
17617
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017618 li = li->li_next;
17619 }
17620 rettv->vval.v_number = 0;
17621 }
17622#endif
17623}
17624
17625/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017626 * "setpos()" function
17627 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017628 static void
17629f_setpos(argvars, rettv)
17630 typval_T *argvars;
17631 typval_T *rettv;
17632{
17633 pos_T pos;
17634 int fnum;
17635 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017636 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017637
Bram Moolenaar08250432008-02-13 11:42:46 +000017638 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017639 name = get_tv_string_chk(argvars);
17640 if (name != NULL)
17641 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017642 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017643 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017644 if (--pos.col < 0)
17645 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017646 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017647 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017648 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017649 if (fnum == curbuf->b_fnum)
17650 {
17651 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017652 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017653 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017654 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017655 curwin->w_set_curswant = FALSE;
17656 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017657 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017658 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017659 }
17660 else
17661 EMSG(_(e_invarg));
17662 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017663 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17664 {
17665 /* set mark */
17666 if (setmark_pos(name[1], &pos, fnum) == OK)
17667 rettv->vval.v_number = 0;
17668 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017669 else
17670 EMSG(_(e_invarg));
17671 }
17672 }
17673}
17674
17675/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017676 * "setqflist()" function
17677 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017678 static void
17679f_setqflist(argvars, rettv)
17680 typval_T *argvars;
17681 typval_T *rettv;
17682{
17683 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17684}
17685
17686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 * "setreg()" function
17688 */
17689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017690f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017691 typval_T *argvars;
17692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693{
17694 int regname;
17695 char_u *strregname;
17696 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017697 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 int append;
17699 char_u yank_type;
17700 long block_len;
17701
17702 block_len = -1;
17703 yank_type = MAUTO;
17704 append = FALSE;
17705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017706 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017707 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017709 if (strregname == NULL)
17710 return; /* type error; errmsg already given */
17711 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 if (regname == 0 || regname == '@')
17713 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017715 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017717 stropt = get_tv_string_chk(&argvars[2]);
17718 if (stropt == NULL)
17719 return; /* type error */
17720 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 switch (*stropt)
17722 {
17723 case 'a': case 'A': /* append */
17724 append = TRUE;
17725 break;
17726 case 'v': case 'c': /* character-wise selection */
17727 yank_type = MCHAR;
17728 break;
17729 case 'V': case 'l': /* line-wise selection */
17730 yank_type = MLINE;
17731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 case 'b': case Ctrl_V: /* block-wise selection */
17733 yank_type = MBLOCK;
17734 if (VIM_ISDIGIT(stropt[1]))
17735 {
17736 ++stropt;
17737 block_len = getdigits(&stropt) - 1;
17738 --stropt;
17739 }
17740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 }
17742 }
17743
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017744 if (argvars[1].v_type == VAR_LIST)
17745 {
17746 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017747 char_u **allocval;
17748 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017749 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017750 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017751 int len = argvars[1].vval.v_list->lv_len;
17752 listitem_T *li;
17753
Bram Moolenaar7d647822014-04-05 21:28:56 +020017754 /* First half: use for pointers to result lines; second half: use for
17755 * pointers to allocated copies. */
17756 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017757 if (lstval == NULL)
17758 return;
17759 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017760 allocval = lstval + len + 2;
17761 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017762
17763 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17764 li = li->li_next)
17765 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017766 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017767 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017768 goto free_lstval;
17769 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017770 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017771 /* Need to make a copy, next get_tv_string_buf_chk() will
17772 * overwrite the string. */
17773 strval = vim_strsave(buf);
17774 if (strval == NULL)
17775 goto free_lstval;
17776 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017777 }
17778 *curval++ = strval;
17779 }
17780 *curval++ = NULL;
17781
17782 write_reg_contents_lst(regname, lstval, -1,
17783 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017784free_lstval:
17785 while (curallocval > allocval)
17786 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017787 vim_free(lstval);
17788 }
17789 else
17790 {
17791 strval = get_tv_string_chk(&argvars[1]);
17792 if (strval == NULL)
17793 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017794 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017797 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798}
17799
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017800/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017801 * "settabvar()" function
17802 */
17803 static void
17804f_settabvar(argvars, rettv)
17805 typval_T *argvars;
17806 typval_T *rettv;
17807{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017808#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017809 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017810 tabpage_T *tp;
17811#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017812 char_u *varname, *tabvarname;
17813 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017814
17815 rettv->vval.v_number = 0;
17816
17817 if (check_restricted() || check_secure())
17818 return;
17819
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017820#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017821 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017822#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017823 varname = get_tv_string_chk(&argvars[1]);
17824 varp = &argvars[2];
17825
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017826 if (varname != NULL && varp != NULL
17827#ifdef FEAT_WINDOWS
17828 && tp != NULL
17829#endif
17830 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017831 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017832#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017833 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017834 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017835#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017836
17837 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17838 if (tabvarname != NULL)
17839 {
17840 STRCPY(tabvarname, "t:");
17841 STRCPY(tabvarname + 2, varname);
17842 set_var(tabvarname, varp, TRUE);
17843 vim_free(tabvarname);
17844 }
17845
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017846#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017847 /* Restore current tabpage */
17848 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017849 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017850#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017851 }
17852}
17853
17854/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017855 * "settabwinvar()" function
17856 */
17857 static void
17858f_settabwinvar(argvars, rettv)
17859 typval_T *argvars;
17860 typval_T *rettv;
17861{
17862 setwinvar(argvars, rettv, 1);
17863}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864
17865/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017866 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017870 typval_T *argvars;
17871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017873 setwinvar(argvars, rettv, 0);
17874}
17875
17876/*
17877 * "setwinvar()" and "settabwinvar()" functions
17878 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017879
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017880 static void
17881setwinvar(argvars, rettv, off)
17882 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017883 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017884 int off;
17885{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 win_T *win;
17887#ifdef FEAT_WINDOWS
17888 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017889 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017890 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891#endif
17892 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017893 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017895 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896
17897 if (check_restricted() || check_secure())
17898 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017899
17900#ifdef FEAT_WINDOWS
17901 if (off == 1)
17902 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17903 else
17904 tp = curtab;
17905#endif
17906 win = find_win_by_nr(&argvars[off], tp);
17907 varname = get_tv_string_chk(&argvars[off + 1]);
17908 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909
17910 if (win != NULL && varname != NULL && varp != NULL)
17911 {
17912#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017913 need_switch_win = !(tp == curtab && win == curwin);
17914 if (!need_switch_win
17915 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017918 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017920 long numval;
17921 char_u *strval;
17922 int error = FALSE;
17923
17924 ++varname;
17925 numval = get_tv_number_chk(varp, &error);
17926 strval = get_tv_string_buf_chk(varp, nbuf);
17927 if (!error && strval != NULL)
17928 set_option_value(varname, numval, strval, OPT_LOCAL);
17929 }
17930 else
17931 {
17932 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17933 if (winvarname != NULL)
17934 {
17935 STRCPY(winvarname, "w:");
17936 STRCPY(winvarname + 2, varname);
17937 set_var(winvarname, varp, TRUE);
17938 vim_free(winvarname);
17939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 }
17941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017943 if (need_switch_win)
17944 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945#endif
17946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947}
17948
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017949#ifdef FEAT_CRYPT
17950/*
17951 * "sha256({string})" function
17952 */
17953 static void
17954f_sha256(argvars, rettv)
17955 typval_T *argvars;
17956 typval_T *rettv;
17957{
17958 char_u *p;
17959
17960 p = get_tv_string(&argvars[0]);
17961 rettv->vval.v_string = vim_strsave(
17962 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17963 rettv->v_type = VAR_STRING;
17964}
17965#endif /* FEAT_CRYPT */
17966
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017968 * "shellescape({string})" function
17969 */
17970 static void
17971f_shellescape(argvars, rettv)
17972 typval_T *argvars;
17973 typval_T *rettv;
17974{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017975 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017976 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017977 rettv->v_type = VAR_STRING;
17978}
17979
17980/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017981 * shiftwidth() function
17982 */
17983 static void
17984f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017985 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017986 typval_T *rettv;
17987{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017988 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017989}
17990
17991/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017992 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 */
17994 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017996 typval_T *argvars;
17997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017999 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000
Bram Moolenaar0d660222005-01-07 21:51:51 +000018001 p = get_tv_string(&argvars[0]);
18002 rettv->vval.v_string = vim_strsave(p);
18003 simplify_filename(rettv->vval.v_string); /* simplify in place */
18004 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005}
18006
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018007#ifdef FEAT_FLOAT
18008/*
18009 * "sin()" function
18010 */
18011 static void
18012f_sin(argvars, rettv)
18013 typval_T *argvars;
18014 typval_T *rettv;
18015{
18016 float_T f;
18017
18018 rettv->v_type = VAR_FLOAT;
18019 if (get_float_arg(argvars, &f) == OK)
18020 rettv->vval.v_float = sin(f);
18021 else
18022 rettv->vval.v_float = 0.0;
18023}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018024
18025/*
18026 * "sinh()" function
18027 */
18028 static void
18029f_sinh(argvars, rettv)
18030 typval_T *argvars;
18031 typval_T *rettv;
18032{
18033 float_T f;
18034
18035 rettv->v_type = VAR_FLOAT;
18036 if (get_float_arg(argvars, &f) == OK)
18037 rettv->vval.v_float = sinh(f);
18038 else
18039 rettv->vval.v_float = 0.0;
18040}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018041#endif
18042
Bram Moolenaar0d660222005-01-07 21:51:51 +000018043static int
18044#ifdef __BORLANDC__
18045 _RTLENTRYF
18046#endif
18047 item_compare __ARGS((const void *s1, const void *s2));
18048static int
18049#ifdef __BORLANDC__
18050 _RTLENTRYF
18051#endif
18052 item_compare2 __ARGS((const void *s1, const void *s2));
18053
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018054/* struct used in the array that's given to qsort() */
18055typedef struct
18056{
18057 listitem_T *item;
18058 int idx;
18059} sortItem_T;
18060
Bram Moolenaar0d660222005-01-07 21:51:51 +000018061static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018062static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018063static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018064static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018065static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018066static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018067static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018068static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018069#define ITEM_COMPARE_FAIL 999
18070
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018072 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018074 static int
18075#ifdef __BORLANDC__
18076_RTLENTRYF
18077#endif
18078item_compare(s1, s2)
18079 const void *s1;
18080 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018082 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018083 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018085 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018086 int res;
18087 char_u numbuf1[NUMBUFLEN];
18088 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018090 si1 = (sortItem_T *)s1;
18091 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018092 tv1 = &si1->item->li_tv;
18093 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018094
18095 if (item_compare_numbers)
18096 {
18097 long v1 = get_tv_number(tv1);
18098 long v2 = get_tv_number(tv2);
18099
18100 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18101 }
18102
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018103 /* tv2string() puts quotes around a string and allocates memory. Don't do
18104 * that for string variables. Use a single quote when comparing with a
18105 * non-string to do what the docs promise. */
18106 if (tv1->v_type == VAR_STRING)
18107 {
18108 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18109 p1 = (char_u *)"'";
18110 else
18111 p1 = tv1->vval.v_string;
18112 }
18113 else
18114 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18115 if (tv2->v_type == VAR_STRING)
18116 {
18117 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18118 p2 = (char_u *)"'";
18119 else
18120 p2 = tv2->vval.v_string;
18121 }
18122 else
18123 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018124 if (p1 == NULL)
18125 p1 = (char_u *)"";
18126 if (p2 == NULL)
18127 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018128 if (!item_compare_numeric)
18129 {
18130 if (item_compare_ic)
18131 res = STRICMP(p1, p2);
18132 else
18133 res = STRCMP(p1, p2);
18134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018136 {
18137 double n1, n2;
18138 n1 = strtod((char *)p1, (char **)&p1);
18139 n2 = strtod((char *)p2, (char **)&p2);
18140 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18141 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018142
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018143 /* When the result would be zero, compare the item indexes. Makes the
18144 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018145 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018146 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018147
Bram Moolenaar0d660222005-01-07 21:51:51 +000018148 vim_free(tofree1);
18149 vim_free(tofree2);
18150 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151}
18152
18153 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018154#ifdef __BORLANDC__
18155_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018157item_compare2(s1, s2)
18158 const void *s1;
18159 const void *s2;
18160{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018161 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018162 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018163 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018164 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018165 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018167 /* shortcut after failure in previous call; compare all items equal */
18168 if (item_compare_func_err)
18169 return 0;
18170
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018171 si1 = (sortItem_T *)s1;
18172 si2 = (sortItem_T *)s2;
18173
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018174 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018175 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018176 copy_tv(&si1->item->li_tv, &argv[0]);
18177 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018178
18179 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018180 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018181 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18182 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018183 clear_tv(&argv[0]);
18184 clear_tv(&argv[1]);
18185
18186 if (res == FAIL)
18187 res = ITEM_COMPARE_FAIL;
18188 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018189 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18190 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018191 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018192 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018193
18194 /* When the result would be zero, compare the pointers themselves. Makes
18195 * the sort stable. */
18196 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018197 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018198
Bram Moolenaar0d660222005-01-07 21:51:51 +000018199 return res;
18200}
18201
18202/*
18203 * "sort({list})" function
18204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018206do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018207 typval_T *argvars;
18208 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018209 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210{
Bram Moolenaar33570922005-01-25 22:26:29 +000018211 list_T *l;
18212 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018213 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214 long len;
18215 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216
Bram Moolenaar0d660222005-01-07 21:51:51 +000018217 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018218 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 else
18220 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018222 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018223 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18224 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018225 return;
18226 rettv->vval.v_list = l;
18227 rettv->v_type = VAR_LIST;
18228 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229
Bram Moolenaar0d660222005-01-07 21:51:51 +000018230 len = list_len(l);
18231 if (len <= 1)
18232 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233
Bram Moolenaar0d660222005-01-07 21:51:51 +000018234 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018235 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018236 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018238 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018239 if (argvars[1].v_type != VAR_UNKNOWN)
18240 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018241 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018242 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018243 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018244 else
18245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018246 int error = FALSE;
18247
18248 i = get_tv_number_chk(&argvars[1], &error);
18249 if (error)
18250 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251 if (i == 1)
18252 item_compare_ic = TRUE;
18253 else
18254 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018255 if (item_compare_func != NULL)
18256 {
18257 if (STRCMP(item_compare_func, "n") == 0)
18258 {
18259 item_compare_func = NULL;
18260 item_compare_numeric = TRUE;
18261 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018262 else if (STRCMP(item_compare_func, "N") == 0)
18263 {
18264 item_compare_func = NULL;
18265 item_compare_numbers = TRUE;
18266 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018267 else if (STRCMP(item_compare_func, "i") == 0)
18268 {
18269 item_compare_func = NULL;
18270 item_compare_ic = TRUE;
18271 }
18272 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018273 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018274
18275 if (argvars[2].v_type != VAR_UNKNOWN)
18276 {
18277 /* optional third argument: {dict} */
18278 if (argvars[2].v_type != VAR_DICT)
18279 {
18280 EMSG(_(e_dictreq));
18281 return;
18282 }
18283 item_compare_selfdict = argvars[2].vval.v_dict;
18284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018288 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289 if (ptrs == NULL)
18290 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291
Bram Moolenaar327aa022014-03-25 18:24:23 +010018292 i = 0;
18293 if (sort)
18294 {
18295 /* sort(): ptrs will be the list to sort */
18296 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018297 {
18298 ptrs[i].item = li;
18299 ptrs[i].idx = i;
18300 ++i;
18301 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018302
18303 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018304 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018305 /* test the compare function */
18306 if (item_compare_func != NULL
18307 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018308 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018309 EMSG(_("E702: Sort compare function failed"));
18310 else
18311 {
18312 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018313 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018314 item_compare_func == NULL ? item_compare : item_compare2);
18315
18316 if (!item_compare_func_err)
18317 {
18318 /* Clear the List and append the items in sorted order. */
18319 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18320 l->lv_len = 0;
18321 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018322 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018323 }
18324 }
18325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018327 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018328 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18329
18330 /* f_uniq(): ptrs will be a stack of items to remove */
18331 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018332 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018333 item_compare_func_ptr = item_compare_func
18334 ? item_compare2 : item_compare;
18335
18336 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18337 li = li->li_next)
18338 {
18339 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18340 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018341 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018342 if (item_compare_func_err)
18343 {
18344 EMSG(_("E882: Uniq compare function failed"));
18345 break;
18346 }
18347 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018349 if (!item_compare_func_err)
18350 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018351 while (--i >= 0)
18352 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018353 li = ptrs[i].item->li_next;
18354 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018355 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018356 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018357 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018358 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018359 list_fix_watch(l, li);
18360 listitem_free(li);
18361 l->lv_len--;
18362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018363 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018364 }
18365
18366 vim_free(ptrs);
18367 }
18368}
18369
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018370/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018371 * "sort({list})" function
18372 */
18373 static void
18374f_sort(argvars, rettv)
18375 typval_T *argvars;
18376 typval_T *rettv;
18377{
18378 do_sort_uniq(argvars, rettv, TRUE);
18379}
18380
18381/*
18382 * "uniq({list})" function
18383 */
18384 static void
18385f_uniq(argvars, rettv)
18386 typval_T *argvars;
18387 typval_T *rettv;
18388{
18389 do_sort_uniq(argvars, rettv, FALSE);
18390}
18391
18392/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018393 * "soundfold({word})" function
18394 */
18395 static void
18396f_soundfold(argvars, rettv)
18397 typval_T *argvars;
18398 typval_T *rettv;
18399{
18400 char_u *s;
18401
18402 rettv->v_type = VAR_STRING;
18403 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018404#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018405 rettv->vval.v_string = eval_soundfold(s);
18406#else
18407 rettv->vval.v_string = vim_strsave(s);
18408#endif
18409}
18410
18411/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018412 * "spellbadword()" function
18413 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018414 static void
18415f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018416 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018417 typval_T *rettv;
18418{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018419 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018420 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018421 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018422
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018423 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018424 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018425
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018426#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018427 if (argvars[0].v_type == VAR_UNKNOWN)
18428 {
18429 /* Find the start and length of the badly spelled word. */
18430 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18431 if (len != 0)
18432 word = ml_get_cursor();
18433 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018434 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018435 {
18436 char_u *str = get_tv_string_chk(&argvars[0]);
18437 int capcol = -1;
18438
18439 if (str != NULL)
18440 {
18441 /* Check the argument for spelling. */
18442 while (*str != NUL)
18443 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018444 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018445 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018446 {
18447 word = str;
18448 break;
18449 }
18450 str += len;
18451 }
18452 }
18453 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018454#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018455
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018456 list_append_string(rettv->vval.v_list, word, len);
18457 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018458 attr == HLF_SPB ? "bad" :
18459 attr == HLF_SPR ? "rare" :
18460 attr == HLF_SPL ? "local" :
18461 attr == HLF_SPC ? "caps" :
18462 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018463}
18464
18465/*
18466 * "spellsuggest()" function
18467 */
18468 static void
18469f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018470 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018471 typval_T *rettv;
18472{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018473#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018474 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018475 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018476 int maxcount;
18477 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018478 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018479 listitem_T *li;
18480 int need_capital = FALSE;
18481#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018482
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018483 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018484 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018485
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018486#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018487 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018488 {
18489 str = get_tv_string(&argvars[0]);
18490 if (argvars[1].v_type != VAR_UNKNOWN)
18491 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018492 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018493 if (maxcount <= 0)
18494 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018495 if (argvars[2].v_type != VAR_UNKNOWN)
18496 {
18497 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18498 if (typeerr)
18499 return;
18500 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018501 }
18502 else
18503 maxcount = 25;
18504
Bram Moolenaar4770d092006-01-12 23:22:24 +000018505 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018506
18507 for (i = 0; i < ga.ga_len; ++i)
18508 {
18509 str = ((char_u **)ga.ga_data)[i];
18510
18511 li = listitem_alloc();
18512 if (li == NULL)
18513 vim_free(str);
18514 else
18515 {
18516 li->li_tv.v_type = VAR_STRING;
18517 li->li_tv.v_lock = 0;
18518 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018519 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018520 }
18521 }
18522 ga_clear(&ga);
18523 }
18524#endif
18525}
18526
Bram Moolenaar0d660222005-01-07 21:51:51 +000018527 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018528f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018529 typval_T *argvars;
18530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018531{
18532 char_u *str;
18533 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018534 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018535 regmatch_T regmatch;
18536 char_u patbuf[NUMBUFLEN];
18537 char_u *save_cpo;
18538 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018539 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018540 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018541 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018542
18543 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18544 save_cpo = p_cpo;
18545 p_cpo = (char_u *)"";
18546
18547 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018548 if (argvars[1].v_type != VAR_UNKNOWN)
18549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018550 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18551 if (pat == NULL)
18552 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018553 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018554 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018555 }
18556 if (pat == NULL || *pat == NUL)
18557 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018558
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018559 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018561 if (typeerr)
18562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563
Bram Moolenaar0d660222005-01-07 21:51:51 +000018564 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18565 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018567 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018568 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018569 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018570 if (*str == NUL)
18571 match = FALSE; /* empty item at the end */
18572 else
18573 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018574 if (match)
18575 end = regmatch.startp[0];
18576 else
18577 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018578 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18579 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018580 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018581 if (list_append_string(rettv->vval.v_list, str,
18582 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018583 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018584 }
18585 if (!match)
18586 break;
18587 /* Advance to just after the match. */
18588 if (regmatch.endp[0] > str)
18589 col = 0;
18590 else
18591 {
18592 /* Don't get stuck at the same match. */
18593#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018594 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018595#else
18596 col = 1;
18597#endif
18598 }
18599 str = regmatch.endp[0];
18600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601
Bram Moolenaar473de612013-06-08 18:19:48 +020018602 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604
Bram Moolenaar0d660222005-01-07 21:51:51 +000018605 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606}
18607
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018608#ifdef FEAT_FLOAT
18609/*
18610 * "sqrt()" function
18611 */
18612 static void
18613f_sqrt(argvars, rettv)
18614 typval_T *argvars;
18615 typval_T *rettv;
18616{
18617 float_T f;
18618
18619 rettv->v_type = VAR_FLOAT;
18620 if (get_float_arg(argvars, &f) == OK)
18621 rettv->vval.v_float = sqrt(f);
18622 else
18623 rettv->vval.v_float = 0.0;
18624}
18625
18626/*
18627 * "str2float()" function
18628 */
18629 static void
18630f_str2float(argvars, rettv)
18631 typval_T *argvars;
18632 typval_T *rettv;
18633{
18634 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18635
18636 if (*p == '+')
18637 p = skipwhite(p + 1);
18638 (void)string2float(p, &rettv->vval.v_float);
18639 rettv->v_type = VAR_FLOAT;
18640}
18641#endif
18642
Bram Moolenaar2c932302006-03-18 21:42:09 +000018643/*
18644 * "str2nr()" function
18645 */
18646 static void
18647f_str2nr(argvars, rettv)
18648 typval_T *argvars;
18649 typval_T *rettv;
18650{
18651 int base = 10;
18652 char_u *p;
18653 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018654 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018655
18656 if (argvars[1].v_type != VAR_UNKNOWN)
18657 {
18658 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018659 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018660 {
18661 EMSG(_(e_invarg));
18662 return;
18663 }
18664 }
18665
18666 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018667 if (*p == '+')
18668 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018669 switch (base)
18670 {
18671 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18672 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18673 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18674 default: what = 0;
18675 }
18676 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018677 rettv->vval.v_number = n;
18678}
18679
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680#ifdef HAVE_STRFTIME
18681/*
18682 * "strftime({format}[, {time}])" function
18683 */
18684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018685f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018686 typval_T *argvars;
18687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688{
18689 char_u result_buf[256];
18690 struct tm *curtime;
18691 time_t seconds;
18692 char_u *p;
18693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018696 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018697 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 seconds = time(NULL);
18699 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018700 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 curtime = localtime(&seconds);
18702 /* MSVC returns NULL for an invalid value of seconds. */
18703 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 else
18706 {
18707# ifdef FEAT_MBYTE
18708 vimconv_T conv;
18709 char_u *enc;
18710
18711 conv.vc_type = CONV_NONE;
18712 enc = enc_locale();
18713 convert_setup(&conv, p_enc, enc);
18714 if (conv.vc_type != CONV_NONE)
18715 p = string_convert(&conv, p, NULL);
18716# endif
18717 if (p != NULL)
18718 (void)strftime((char *)result_buf, sizeof(result_buf),
18719 (char *)p, curtime);
18720 else
18721 result_buf[0] = NUL;
18722
18723# ifdef FEAT_MBYTE
18724 if (conv.vc_type != CONV_NONE)
18725 vim_free(p);
18726 convert_setup(&conv, enc, p_enc);
18727 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 else
18730# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732
18733# ifdef FEAT_MBYTE
18734 /* Release conversion descriptors */
18735 convert_setup(&conv, NULL, NULL);
18736 vim_free(enc);
18737# endif
18738 }
18739}
18740#endif
18741
18742/*
18743 * "stridx()" function
18744 */
18745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018746f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018747 typval_T *argvars;
18748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749{
18750 char_u buf[NUMBUFLEN];
18751 char_u *needle;
18752 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018753 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018755 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018757 needle = get_tv_string_chk(&argvars[1]);
18758 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018759 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018760 if (needle == NULL || haystack == NULL)
18761 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762
Bram Moolenaar33570922005-01-25 22:26:29 +000018763 if (argvars[2].v_type != VAR_UNKNOWN)
18764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018765 int error = FALSE;
18766
18767 start_idx = get_tv_number_chk(&argvars[2], &error);
18768 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018769 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018770 if (start_idx >= 0)
18771 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018772 }
18773
18774 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18775 if (pos != NULL)
18776 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777}
18778
18779/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018780 * "string()" function
18781 */
18782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018783f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018784 typval_T *argvars;
18785 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018786{
18787 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018788 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018791 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018792 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018793 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018794 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795}
18796
18797/*
18798 * "strlen()" function
18799 */
18800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018802 typval_T *argvars;
18803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805 rettv->vval.v_number = (varnumber_T)(STRLEN(
18806 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807}
18808
18809/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018810 * "strchars()" function
18811 */
18812 static void
18813f_strchars(argvars, rettv)
18814 typval_T *argvars;
18815 typval_T *rettv;
18816{
18817 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018818 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018819#ifdef FEAT_MBYTE
18820 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018821 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018822#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018823
18824 if (argvars[1].v_type != VAR_UNKNOWN)
18825 skipcc = get_tv_number_chk(&argvars[1], NULL);
18826 if (skipcc < 0 || skipcc > 1)
18827 EMSG(_(e_invarg));
18828 else
18829 {
18830#ifdef FEAT_MBYTE
18831 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18832 while (*s != NUL)
18833 {
18834 func_mb_ptr2char_adv(&s);
18835 ++len;
18836 }
18837 rettv->vval.v_number = len;
18838#else
18839 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18840#endif
18841 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018842}
18843
18844/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018845 * "strdisplaywidth()" function
18846 */
18847 static void
18848f_strdisplaywidth(argvars, rettv)
18849 typval_T *argvars;
18850 typval_T *rettv;
18851{
18852 char_u *s = get_tv_string(&argvars[0]);
18853 int col = 0;
18854
18855 if (argvars[1].v_type != VAR_UNKNOWN)
18856 col = get_tv_number(&argvars[1]);
18857
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018858 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018859}
18860
18861/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018862 * "strwidth()" function
18863 */
18864 static void
18865f_strwidth(argvars, rettv)
18866 typval_T *argvars;
18867 typval_T *rettv;
18868{
18869 char_u *s = get_tv_string(&argvars[0]);
18870
18871 rettv->vval.v_number = (varnumber_T)(
18872#ifdef FEAT_MBYTE
18873 mb_string2cells(s, -1)
18874#else
18875 STRLEN(s)
18876#endif
18877 );
18878}
18879
18880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 * "strpart()" function
18882 */
18883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018884f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018885 typval_T *argvars;
18886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887{
18888 char_u *p;
18889 int n;
18890 int len;
18891 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018892 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018894 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 slen = (int)STRLEN(p);
18896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018897 n = get_tv_number_chk(&argvars[1], &error);
18898 if (error)
18899 len = 0;
18900 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 else
18903 len = slen - n; /* default len: all bytes that are available. */
18904
18905 /*
18906 * Only return the overlap between the specified part and the actual
18907 * string.
18908 */
18909 if (n < 0)
18910 {
18911 len += n;
18912 n = 0;
18913 }
18914 else if (n > slen)
18915 n = slen;
18916 if (len < 0)
18917 len = 0;
18918 else if (n + len > slen)
18919 len = slen - n;
18920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018921 rettv->v_type = VAR_STRING;
18922 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923}
18924
18925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018926 * "strridx()" function
18927 */
18928 static void
18929f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018930 typval_T *argvars;
18931 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018932{
18933 char_u buf[NUMBUFLEN];
18934 char_u *needle;
18935 char_u *haystack;
18936 char_u *rest;
18937 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018938 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018940 needle = get_tv_string_chk(&argvars[1]);
18941 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018942
18943 rettv->vval.v_number = -1;
18944 if (needle == NULL || haystack == NULL)
18945 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018946
18947 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018948 if (argvars[2].v_type != VAR_UNKNOWN)
18949 {
18950 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018951 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018952 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018953 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018954 }
18955 else
18956 end_idx = haystack_len;
18957
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018959 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018960 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018961 lastmatch = haystack + end_idx;
18962 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018964 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018965 for (rest = haystack; *rest != '\0'; ++rest)
18966 {
18967 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018968 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969 break;
18970 lastmatch = rest;
18971 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018972 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973
18974 if (lastmatch == NULL)
18975 rettv->vval.v_number = -1;
18976 else
18977 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18978}
18979
18980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 * "strtrans()" function
18982 */
18983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018985 typval_T *argvars;
18986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018988 rettv->v_type = VAR_STRING;
18989 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990}
18991
18992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018993 * "submatch()" function
18994 */
18995 static void
18996f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018997 typval_T *argvars;
18998 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999{
Bram Moolenaar41571762014-04-02 19:00:58 +020019000 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019001 int no;
19002 int retList = 0;
19003
19004 no = (int)get_tv_number_chk(&argvars[0], &error);
19005 if (error)
19006 return;
19007 error = FALSE;
19008 if (argvars[1].v_type != VAR_UNKNOWN)
19009 retList = get_tv_number_chk(&argvars[1], &error);
19010 if (error)
19011 return;
19012
19013 if (retList == 0)
19014 {
19015 rettv->v_type = VAR_STRING;
19016 rettv->vval.v_string = reg_submatch(no);
19017 }
19018 else
19019 {
19020 rettv->v_type = VAR_LIST;
19021 rettv->vval.v_list = reg_submatch_list(no);
19022 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023}
19024
19025/*
19026 * "substitute()" function
19027 */
19028 static void
19029f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019030 typval_T *argvars;
19031 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032{
19033 char_u patbuf[NUMBUFLEN];
19034 char_u subbuf[NUMBUFLEN];
19035 char_u flagsbuf[NUMBUFLEN];
19036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019037 char_u *str = get_tv_string_chk(&argvars[0]);
19038 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19039 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19040 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19041
Bram Moolenaar0d660222005-01-07 21:51:51 +000019042 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019043 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19044 rettv->vval.v_string = NULL;
19045 else
19046 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047}
19048
19049/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019050 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019053f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019054 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056{
19057 int id = 0;
19058#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019059 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 long col;
19061 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019062 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019064 lnum = get_tv_lnum(argvars); /* -1 on type error */
19065 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19066 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019068 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019069 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019070 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071#endif
19072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019073 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074}
19075
19076/*
19077 * "synIDattr(id, what [, mode])" function
19078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019080f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019081 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083{
19084 char_u *p = NULL;
19085#ifdef FEAT_SYN_HL
19086 int id;
19087 char_u *what;
19088 char_u *mode;
19089 char_u modebuf[NUMBUFLEN];
19090 int modec;
19091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092 id = get_tv_number(&argvars[0]);
19093 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019096 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019098 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 modec = 0; /* replace invalid with current */
19100 }
19101 else
19102 {
19103#ifdef FEAT_GUI
19104 if (gui.in_use)
19105 modec = 'g';
19106 else
19107#endif
19108 if (t_colors > 1)
19109 modec = 'c';
19110 else
19111 modec = 't';
19112 }
19113
19114
19115 switch (TOLOWER_ASC(what[0]))
19116 {
19117 case 'b':
19118 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19119 p = highlight_color(id, what, modec);
19120 else /* bold */
19121 p = highlight_has_attr(id, HL_BOLD, modec);
19122 break;
19123
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019124 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 p = highlight_color(id, what, modec);
19126 break;
19127
19128 case 'i':
19129 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19130 p = highlight_has_attr(id, HL_INVERSE, modec);
19131 else /* italic */
19132 p = highlight_has_attr(id, HL_ITALIC, modec);
19133 break;
19134
19135 case 'n': /* name */
19136 p = get_highlight_name(NULL, id - 1);
19137 break;
19138
19139 case 'r': /* reverse */
19140 p = highlight_has_attr(id, HL_INVERSE, modec);
19141 break;
19142
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019143 case 's':
19144 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19145 p = highlight_color(id, what, modec);
19146 else /* standout */
19147 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148 break;
19149
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019150 case 'u':
19151 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19152 /* underline */
19153 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19154 else
19155 /* undercurl */
19156 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 break;
19158 }
19159
19160 if (p != NULL)
19161 p = vim_strsave(p);
19162#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019163 rettv->v_type = VAR_STRING;
19164 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165}
19166
19167/*
19168 * "synIDtrans(id)" function
19169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019171f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019172 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174{
19175 int id;
19176
19177#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179
19180 if (id > 0)
19181 id = syn_get_final_id(id);
19182 else
19183#endif
19184 id = 0;
19185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187}
19188
19189/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019190 * "synconcealed(lnum, col)" function
19191 */
19192 static void
19193f_synconcealed(argvars, rettv)
19194 typval_T *argvars UNUSED;
19195 typval_T *rettv;
19196{
19197#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19198 long lnum;
19199 long col;
19200 int syntax_flags = 0;
19201 int cchar;
19202 int matchid = 0;
19203 char_u str[NUMBUFLEN];
19204#endif
19205
19206 rettv->v_type = VAR_LIST;
19207 rettv->vval.v_list = NULL;
19208
19209#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19210 lnum = get_tv_lnum(argvars); /* -1 on type error */
19211 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19212
19213 vim_memset(str, NUL, sizeof(str));
19214
19215 if (rettv_list_alloc(rettv) != FAIL)
19216 {
19217 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19218 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19219 && curwin->w_p_cole > 0)
19220 {
19221 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19222 syntax_flags = get_syntax_info(&matchid);
19223
19224 /* get the conceal character */
19225 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19226 {
19227 cchar = syn_get_sub_char();
19228 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19229 cchar = lcs_conceal;
19230 if (cchar != NUL)
19231 {
19232# ifdef FEAT_MBYTE
19233 if (has_mbyte)
19234 (*mb_char2bytes)(cchar, str);
19235 else
19236# endif
19237 str[0] = cchar;
19238 }
19239 }
19240 }
19241
19242 list_append_number(rettv->vval.v_list,
19243 (syntax_flags & HL_CONCEAL) != 0);
19244 /* -1 to auto-determine strlen */
19245 list_append_string(rettv->vval.v_list, str, -1);
19246 list_append_number(rettv->vval.v_list, matchid);
19247 }
19248#endif
19249}
19250
19251/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019252 * "synstack(lnum, col)" function
19253 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019254 static void
19255f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019256 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019257 typval_T *rettv;
19258{
19259#ifdef FEAT_SYN_HL
19260 long lnum;
19261 long col;
19262 int i;
19263 int id;
19264#endif
19265
19266 rettv->v_type = VAR_LIST;
19267 rettv->vval.v_list = NULL;
19268
19269#ifdef FEAT_SYN_HL
19270 lnum = get_tv_lnum(argvars); /* -1 on type error */
19271 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19272
19273 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019274 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019275 && rettv_list_alloc(rettv) != FAIL)
19276 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019277 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019278 for (i = 0; ; ++i)
19279 {
19280 id = syn_get_stack_item(i);
19281 if (id < 0)
19282 break;
19283 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19284 break;
19285 }
19286 }
19287#endif
19288}
19289
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019291get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019292 typval_T *argvars;
19293 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019294 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019296 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019298 char_u *infile = NULL;
19299 char_u buf[NUMBUFLEN];
19300 int err = FALSE;
19301 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019302 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019303 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019305 rettv->v_type = VAR_STRING;
19306 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019307 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019308 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019309
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019310 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019311 {
19312 /*
19313 * Write the string to a temp file, to be used for input of the shell
19314 * command.
19315 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019316 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019317 {
19318 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019319 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019320 }
19321
19322 fd = mch_fopen((char *)infile, WRITEBIN);
19323 if (fd == NULL)
19324 {
19325 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019326 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019327 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019328 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019329 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019330 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19331 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019332 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019333 else
19334 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019335 size_t len;
19336
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019337 p = get_tv_string_buf_chk(&argvars[1], buf);
19338 if (p == NULL)
19339 {
19340 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019341 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019342 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019343 len = STRLEN(p);
19344 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019345 err = TRUE;
19346 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019347 if (fclose(fd) != 0)
19348 err = TRUE;
19349 if (err)
19350 {
19351 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019352 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019353 }
19354 }
19355
Bram Moolenaar52a72462014-08-29 15:53:52 +020019356 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19357 * echoes typeahead, that messes up the display. */
19358 if (!msg_silent)
19359 flags += SHELL_COOKED;
19360
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019361 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019363 int len;
19364 listitem_T *li;
19365 char_u *s = NULL;
19366 char_u *start;
19367 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019368 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369
Bram Moolenaar52a72462014-08-29 15:53:52 +020019370 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019371 if (res == NULL)
19372 goto errret;
19373
19374 list = list_alloc();
19375 if (list == NULL)
19376 goto errret;
19377
19378 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019380 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019381 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019382 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019383 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019384
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019385 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019386 if (s == NULL)
19387 goto errret;
19388
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019389 for (p = s; start < end; ++p, ++start)
19390 *p = *start == NUL ? NL : *start;
19391 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019392
19393 li = listitem_alloc();
19394 if (li == NULL)
19395 {
19396 vim_free(s);
19397 goto errret;
19398 }
19399 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019400 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019401 li->li_tv.vval.v_string = s;
19402 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019404
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019405 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019406 rettv->v_type = VAR_LIST;
19407 rettv->vval.v_list = list;
19408 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019410 else
19411 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019412 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019413#ifdef USE_CR
19414 /* translate <CR> into <NL> */
19415 if (res != NULL)
19416 {
19417 char_u *s;
19418
19419 for (s = res; *s; ++s)
19420 {
19421 if (*s == CAR)
19422 *s = NL;
19423 }
19424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425#else
19426# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019427 /* translate <CR><NL> into <NL> */
19428 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019430 char_u *s, *d;
19431
19432 d = res;
19433 for (s = res; *s; ++s)
19434 {
19435 if (s[0] == CAR && s[1] == NL)
19436 ++s;
19437 *d++ = *s;
19438 }
19439 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441# endif
19442#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019443 rettv->vval.v_string = res;
19444 res = NULL;
19445 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019446
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019447errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019448 if (infile != NULL)
19449 {
19450 mch_remove(infile);
19451 vim_free(infile);
19452 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019453 if (res != NULL)
19454 vim_free(res);
19455 if (list != NULL)
19456 list_free(list, TRUE);
19457}
19458
19459/*
19460 * "system()" function
19461 */
19462 static void
19463f_system(argvars, rettv)
19464 typval_T *argvars;
19465 typval_T *rettv;
19466{
19467 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19468}
19469
19470/*
19471 * "systemlist()" function
19472 */
19473 static void
19474f_systemlist(argvars, rettv)
19475 typval_T *argvars;
19476 typval_T *rettv;
19477{
19478 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479}
19480
19481/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019482 * "tabpagebuflist()" function
19483 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019484 static void
19485f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019486 typval_T *argvars UNUSED;
19487 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019488{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019489#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019490 tabpage_T *tp;
19491 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019492
19493 if (argvars[0].v_type == VAR_UNKNOWN)
19494 wp = firstwin;
19495 else
19496 {
19497 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19498 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019499 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019500 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019501 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019502 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019503 for (; wp != NULL; wp = wp->w_next)
19504 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019505 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019506 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019507 }
19508#endif
19509}
19510
19511
19512/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019513 * "tabpagenr()" function
19514 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019515 static void
19516f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019517 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019518 typval_T *rettv;
19519{
19520 int nr = 1;
19521#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019522 char_u *arg;
19523
19524 if (argvars[0].v_type != VAR_UNKNOWN)
19525 {
19526 arg = get_tv_string_chk(&argvars[0]);
19527 nr = 0;
19528 if (arg != NULL)
19529 {
19530 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019531 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019532 else
19533 EMSG2(_(e_invexpr2), arg);
19534 }
19535 }
19536 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019537 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019538#endif
19539 rettv->vval.v_number = nr;
19540}
19541
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019542
19543#ifdef FEAT_WINDOWS
19544static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19545
19546/*
19547 * Common code for tabpagewinnr() and winnr().
19548 */
19549 static int
19550get_winnr(tp, argvar)
19551 tabpage_T *tp;
19552 typval_T *argvar;
19553{
19554 win_T *twin;
19555 int nr = 1;
19556 win_T *wp;
19557 char_u *arg;
19558
19559 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19560 if (argvar->v_type != VAR_UNKNOWN)
19561 {
19562 arg = get_tv_string_chk(argvar);
19563 if (arg == NULL)
19564 nr = 0; /* type error; errmsg already given */
19565 else if (STRCMP(arg, "$") == 0)
19566 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19567 else if (STRCMP(arg, "#") == 0)
19568 {
19569 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19570 if (twin == NULL)
19571 nr = 0;
19572 }
19573 else
19574 {
19575 EMSG2(_(e_invexpr2), arg);
19576 nr = 0;
19577 }
19578 }
19579
19580 if (nr > 0)
19581 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19582 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019583 {
19584 if (wp == NULL)
19585 {
19586 /* didn't find it in this tabpage */
19587 nr = 0;
19588 break;
19589 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019590 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019591 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019592 return nr;
19593}
19594#endif
19595
19596/*
19597 * "tabpagewinnr()" function
19598 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019599 static void
19600f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019601 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019602 typval_T *rettv;
19603{
19604 int nr = 1;
19605#ifdef FEAT_WINDOWS
19606 tabpage_T *tp;
19607
19608 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19609 if (tp == NULL)
19610 nr = 0;
19611 else
19612 nr = get_winnr(tp, &argvars[1]);
19613#endif
19614 rettv->vval.v_number = nr;
19615}
19616
19617
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019618/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019619 * "tagfiles()" function
19620 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019621 static void
19622f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019623 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019624 typval_T *rettv;
19625{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019626 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019627 tagname_T tn;
19628 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019629
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019630 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019631 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019632 fname = alloc(MAXPATHL);
19633 if (fname == NULL)
19634 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019635
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019636 for (first = TRUE; ; first = FALSE)
19637 if (get_tagfname(&tn, first, fname) == FAIL
19638 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019639 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019640 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019641 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019642}
19643
19644/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019645 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019646 */
19647 static void
19648f_taglist(argvars, rettv)
19649 typval_T *argvars;
19650 typval_T *rettv;
19651{
19652 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019653
19654 tag_pattern = get_tv_string(&argvars[0]);
19655
19656 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019657 if (*tag_pattern == NUL)
19658 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019659
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019660 if (rettv_list_alloc(rettv) == OK)
19661 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019662}
19663
19664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 * "tempname()" function
19666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019669 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671{
19672 static int x = 'A';
19673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019674 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019675 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676
19677 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19678 * names. Skip 'I' and 'O', they are used for shell redirection. */
19679 do
19680 {
19681 if (x == 'Z')
19682 x = '0';
19683 else if (x == '9')
19684 x = 'A';
19685 else
19686 {
19687#ifdef EBCDIC
19688 if (x == 'I')
19689 x = 'J';
19690 else if (x == 'R')
19691 x = 'S';
19692 else
19693#endif
19694 ++x;
19695 }
19696 } while (x == 'I' || x == 'O');
19697}
19698
19699/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019700 * "test(list)" function: Just checking the walls...
19701 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019702 static void
19703f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019704 typval_T *argvars UNUSED;
19705 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019706{
19707 /* Used for unit testing. Change the code below to your liking. */
19708#if 0
19709 listitem_T *li;
19710 list_T *l;
19711 char_u *bad, *good;
19712
19713 if (argvars[0].v_type != VAR_LIST)
19714 return;
19715 l = argvars[0].vval.v_list;
19716 if (l == NULL)
19717 return;
19718 li = l->lv_first;
19719 if (li == NULL)
19720 return;
19721 bad = get_tv_string(&li->li_tv);
19722 li = li->li_next;
19723 if (li == NULL)
19724 return;
19725 good = get_tv_string(&li->li_tv);
19726 rettv->vval.v_number = test_edit_score(bad, good);
19727#endif
19728}
19729
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019730#ifdef FEAT_FLOAT
19731/*
19732 * "tan()" function
19733 */
19734 static void
19735f_tan(argvars, rettv)
19736 typval_T *argvars;
19737 typval_T *rettv;
19738{
19739 float_T f;
19740
19741 rettv->v_type = VAR_FLOAT;
19742 if (get_float_arg(argvars, &f) == OK)
19743 rettv->vval.v_float = tan(f);
19744 else
19745 rettv->vval.v_float = 0.0;
19746}
19747
19748/*
19749 * "tanh()" function
19750 */
19751 static void
19752f_tanh(argvars, rettv)
19753 typval_T *argvars;
19754 typval_T *rettv;
19755{
19756 float_T f;
19757
19758 rettv->v_type = VAR_FLOAT;
19759 if (get_float_arg(argvars, &f) == OK)
19760 rettv->vval.v_float = tanh(f);
19761 else
19762 rettv->vval.v_float = 0.0;
19763}
19764#endif
19765
Bram Moolenaard52d9742005-08-21 22:20:28 +000019766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 * "tolower(string)" function
19768 */
19769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019770f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019771 typval_T *argvars;
19772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773{
19774 char_u *p;
19775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019776 p = vim_strsave(get_tv_string(&argvars[0]));
19777 rettv->v_type = VAR_STRING;
19778 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779
19780 if (p != NULL)
19781 while (*p != NUL)
19782 {
19783#ifdef FEAT_MBYTE
19784 int l;
19785
19786 if (enc_utf8)
19787 {
19788 int c, lc;
19789
19790 c = utf_ptr2char(p);
19791 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019792 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 /* TODO: reallocate string when byte count changes. */
19794 if (utf_char2len(lc) == l)
19795 utf_char2bytes(lc, p);
19796 p += l;
19797 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019798 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 p += l; /* skip multi-byte character */
19800 else
19801#endif
19802 {
19803 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19804 ++p;
19805 }
19806 }
19807}
19808
19809/*
19810 * "toupper(string)" function
19811 */
19812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019813f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 typval_T *argvars;
19815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019818 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819}
19820
19821/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019822 * "tr(string, fromstr, tostr)" function
19823 */
19824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019826 typval_T *argvars;
19827 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019828{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019829 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019830 char_u *fromstr;
19831 char_u *tostr;
19832 char_u *p;
19833#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019834 int inlen;
19835 int fromlen;
19836 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019837 int idx;
19838 char_u *cpstr;
19839 int cplen;
19840 int first = TRUE;
19841#endif
19842 char_u buf[NUMBUFLEN];
19843 char_u buf2[NUMBUFLEN];
19844 garray_T ga;
19845
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019846 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019847 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19848 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019849
19850 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851 rettv->v_type = VAR_STRING;
19852 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019853 if (fromstr == NULL || tostr == NULL)
19854 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019855 ga_init2(&ga, (int)sizeof(char), 80);
19856
19857#ifdef FEAT_MBYTE
19858 if (!has_mbyte)
19859#endif
19860 /* not multi-byte: fromstr and tostr must be the same length */
19861 if (STRLEN(fromstr) != STRLEN(tostr))
19862 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019863#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019864error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019865#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019866 EMSG2(_(e_invarg2), fromstr);
19867 ga_clear(&ga);
19868 return;
19869 }
19870
19871 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019872 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019873 {
19874#ifdef FEAT_MBYTE
19875 if (has_mbyte)
19876 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019877 inlen = (*mb_ptr2len)(in_str);
19878 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019879 cplen = inlen;
19880 idx = 0;
19881 for (p = fromstr; *p != NUL; p += fromlen)
19882 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019883 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019884 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019885 {
19886 for (p = tostr; *p != NUL; p += tolen)
19887 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019888 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019889 if (idx-- == 0)
19890 {
19891 cplen = tolen;
19892 cpstr = p;
19893 break;
19894 }
19895 }
19896 if (*p == NUL) /* tostr is shorter than fromstr */
19897 goto error;
19898 break;
19899 }
19900 ++idx;
19901 }
19902
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019903 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019904 {
19905 /* Check that fromstr and tostr have the same number of
19906 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019907 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019908 first = FALSE;
19909 for (p = tostr; *p != NUL; p += tolen)
19910 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019911 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019912 --idx;
19913 }
19914 if (idx != 0)
19915 goto error;
19916 }
19917
Bram Moolenaarcde88542015-08-11 19:14:00 +020019918 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019919 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019920 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019921
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019922 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019923 }
19924 else
19925#endif
19926 {
19927 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019928 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019929 if (p != NULL)
19930 ga_append(&ga, tostr[p - fromstr]);
19931 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019932 ga_append(&ga, *in_str);
19933 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019934 }
19935 }
19936
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019937 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019938 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019939 ga_append(&ga, NUL);
19940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019942}
19943
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019944#ifdef FEAT_FLOAT
19945/*
19946 * "trunc({float})" function
19947 */
19948 static void
19949f_trunc(argvars, rettv)
19950 typval_T *argvars;
19951 typval_T *rettv;
19952{
19953 float_T f;
19954
19955 rettv->v_type = VAR_FLOAT;
19956 if (get_float_arg(argvars, &f) == OK)
19957 /* trunc() is not in C90, use floor() or ceil() instead. */
19958 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19959 else
19960 rettv->vval.v_float = 0.0;
19961}
19962#endif
19963
Bram Moolenaar8299df92004-07-10 09:47:34 +000019964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 * "type(expr)" function
19966 */
19967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019969 typval_T *argvars;
19970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019972 int n;
19973
19974 switch (argvars[0].v_type)
19975 {
19976 case VAR_NUMBER: n = 0; break;
19977 case VAR_STRING: n = 1; break;
19978 case VAR_FUNC: n = 2; break;
19979 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019980 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019981#ifdef FEAT_FLOAT
19982 case VAR_FLOAT: n = 5; break;
19983#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019984 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19985 }
19986 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987}
19988
19989/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019990 * "undofile(name)" function
19991 */
19992 static void
19993f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019994 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019995 typval_T *rettv;
19996{
19997 rettv->v_type = VAR_STRING;
19998#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019999 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020000 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020001
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020002 if (*fname == NUL)
20003 {
20004 /* If there is no file name there will be no undo file. */
20005 rettv->vval.v_string = NULL;
20006 }
20007 else
20008 {
20009 char_u *ffname = FullName_save(fname, FALSE);
20010
20011 if (ffname != NULL)
20012 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20013 vim_free(ffname);
20014 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020015 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020016#else
20017 rettv->vval.v_string = NULL;
20018#endif
20019}
20020
20021/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020022 * "undotree()" function
20023 */
20024 static void
20025f_undotree(argvars, rettv)
20026 typval_T *argvars UNUSED;
20027 typval_T *rettv;
20028{
20029 if (rettv_dict_alloc(rettv) == OK)
20030 {
20031 dict_T *dict = rettv->vval.v_dict;
20032 list_T *list;
20033
Bram Moolenaar730cde92010-06-27 05:18:54 +020020034 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020035 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020036 dict_add_nr_str(dict, "save_last",
20037 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020038 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20039 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020040 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020041
20042 list = list_alloc();
20043 if (list != NULL)
20044 {
20045 u_eval_tree(curbuf->b_u_oldhead, list);
20046 dict_add_list(dict, "entries", list);
20047 }
20048 }
20049}
20050
20051/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020052 * "values(dict)" function
20053 */
20054 static void
20055f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020056 typval_T *argvars;
20057 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020058{
20059 dict_list(argvars, rettv, 1);
20060}
20061
20062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 * "virtcol(string)" function
20064 */
20065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020067 typval_T *argvars;
20068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069{
20070 colnr_T vcol = 0;
20071 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020072 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020074 fp = var2fpos(&argvars[0], FALSE, &fnum);
20075 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20076 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 {
20078 getvvcol(curwin, fp, NULL, NULL, &vcol);
20079 ++vcol;
20080 }
20081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020082 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083}
20084
20085/*
20086 * "visualmode()" function
20087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020090 typval_T *argvars UNUSED;
20091 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 char_u str[2];
20094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 str[0] = curbuf->b_visual_mode_eval;
20097 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099
20100 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020101 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103}
20104
20105/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020106 * "wildmenumode()" function
20107 */
20108 static void
20109f_wildmenumode(argvars, rettv)
20110 typval_T *argvars UNUSED;
20111 typval_T *rettv UNUSED;
20112{
20113#ifdef FEAT_WILDMENU
20114 if (wild_menu_showing)
20115 rettv->vval.v_number = 1;
20116#endif
20117}
20118
20119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 * "winbufnr(nr)" function
20121 */
20122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020123f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020124 typval_T *argvars;
20125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126{
20127 win_T *wp;
20128
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020129 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020133 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134}
20135
20136/*
20137 * "wincol()" function
20138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020140f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143{
20144 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020145 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146}
20147
20148/*
20149 * "winheight(nr)" function
20150 */
20151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020152f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 typval_T *argvars;
20154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155{
20156 win_T *wp;
20157
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020158 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020160 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020162 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163}
20164
20165/*
20166 * "winline()" function
20167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020169f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172{
20173 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020174 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175}
20176
20177/*
20178 * "winnr()" function
20179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020181f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184{
20185 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020186
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020188 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191}
20192
20193/*
20194 * "winrestcmd()" function
20195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020198 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200{
20201#ifdef FEAT_WINDOWS
20202 win_T *wp;
20203 int winnr = 1;
20204 garray_T ga;
20205 char_u buf[50];
20206
20207 ga_init2(&ga, (int)sizeof(char), 70);
20208 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20209 {
20210 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20211 ga_concat(&ga, buf);
20212# ifdef FEAT_VERTSPLIT
20213 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20214 ga_concat(&ga, buf);
20215# endif
20216 ++winnr;
20217 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020218 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020220 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020222 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020224 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225}
20226
20227/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020228 * "winrestview()" function
20229 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020230 static void
20231f_winrestview(argvars, rettv)
20232 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020233 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020234{
20235 dict_T *dict;
20236
20237 if (argvars[0].v_type != VAR_DICT
20238 || (dict = argvars[0].vval.v_dict) == NULL)
20239 EMSG(_(e_invarg));
20240 else
20241 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020242 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20243 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20244 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20245 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020246#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020247 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20248 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020249#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020250 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20251 {
20252 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20253 curwin->w_set_curswant = FALSE;
20254 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020255
Bram Moolenaar82c25852014-05-28 16:47:16 +020020256 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20257 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020258#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020259 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20260 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020261#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020262 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20263 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20264 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20265 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020266
20267 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020268 win_new_height(curwin, curwin->w_height);
20269# ifdef FEAT_VERTSPLIT
20270 win_new_width(curwin, W_WIDTH(curwin));
20271# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020272 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020273
Bram Moolenaarb851a962014-10-31 15:45:52 +010020274 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020275 curwin->w_topline = 1;
20276 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20277 curwin->w_topline = curbuf->b_ml.ml_line_count;
20278#ifdef FEAT_DIFF
20279 check_topfill(curwin, TRUE);
20280#endif
20281 }
20282}
20283
20284/*
20285 * "winsaveview()" function
20286 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020287 static void
20288f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020289 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020290 typval_T *rettv;
20291{
20292 dict_T *dict;
20293
Bram Moolenaara800b422010-06-27 01:15:55 +020020294 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020295 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020296 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020297
20298 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20299 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20300#ifdef FEAT_VIRTUALEDIT
20301 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20302#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020303 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020304 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20305
20306 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20307#ifdef FEAT_DIFF
20308 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20309#endif
20310 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20311 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20312}
20313
20314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 * "winwidth(nr)" function
20316 */
20317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020318f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 typval_T *argvars;
20320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321{
20322 win_T *wp;
20323
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020324 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020326 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 else
20328#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020329 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020331 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332#endif
20333}
20334
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020336 * "wordcount()" function
20337 */
20338 static void
20339f_wordcount(argvars, rettv)
20340 typval_T *argvars UNUSED;
20341 typval_T *rettv;
20342{
20343 if (rettv_dict_alloc(rettv) == FAIL)
20344 return;
20345 cursor_pos_info(rettv->vval.v_dict);
20346}
20347
20348/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020349 * Write list of strings to file
20350 */
20351 static int
20352write_list(fd, list, binary)
20353 FILE *fd;
20354 list_T *list;
20355 int binary;
20356{
20357 listitem_T *li;
20358 int c;
20359 int ret = OK;
20360 char_u *s;
20361
20362 for (li = list->lv_first; li != NULL; li = li->li_next)
20363 {
20364 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20365 {
20366 if (*s == '\n')
20367 c = putc(NUL, fd);
20368 else
20369 c = putc(*s, fd);
20370 if (c == EOF)
20371 {
20372 ret = FAIL;
20373 break;
20374 }
20375 }
20376 if (!binary || li->li_next != NULL)
20377 if (putc('\n', fd) == EOF)
20378 {
20379 ret = FAIL;
20380 break;
20381 }
20382 if (ret == FAIL)
20383 {
20384 EMSG(_(e_write));
20385 break;
20386 }
20387 }
20388 return ret;
20389}
20390
20391/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020392 * "writefile()" function
20393 */
20394 static void
20395f_writefile(argvars, rettv)
20396 typval_T *argvars;
20397 typval_T *rettv;
20398{
20399 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020400 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020401 char_u *fname;
20402 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020403 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020404
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020405 if (check_restricted() || check_secure())
20406 return;
20407
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020408 if (argvars[0].v_type != VAR_LIST)
20409 {
20410 EMSG2(_(e_listarg), "writefile()");
20411 return;
20412 }
20413 if (argvars[0].vval.v_list == NULL)
20414 return;
20415
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020416 if (argvars[2].v_type != VAR_UNKNOWN)
20417 {
20418 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20419 binary = TRUE;
20420 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20421 append = TRUE;
20422 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020423
20424 /* Always open the file in binary mode, library functions have a mind of
20425 * their own about CR-LF conversion. */
20426 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020427 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20428 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020429 {
20430 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20431 ret = -1;
20432 }
20433 else
20434 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020435 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20436 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020437 fclose(fd);
20438 }
20439
20440 rettv->vval.v_number = ret;
20441}
20442
20443/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020444 * "xor(expr, expr)" function
20445 */
20446 static void
20447f_xor(argvars, rettv)
20448 typval_T *argvars;
20449 typval_T *rettv;
20450{
20451 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20452 ^ get_tv_number_chk(&argvars[1], NULL);
20453}
20454
20455
20456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020458 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 */
20460 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020461var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020462 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020463 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020464 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020466 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020468 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469
Bram Moolenaara5525202006-03-02 22:52:09 +000020470 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020471 if (varp->v_type == VAR_LIST)
20472 {
20473 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020474 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020475 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020476 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020477
20478 l = varp->vval.v_list;
20479 if (l == NULL)
20480 return NULL;
20481
20482 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020483 pos.lnum = list_find_nr(l, 0L, &error);
20484 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020485 return NULL; /* invalid line number */
20486
20487 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020488 pos.col = list_find_nr(l, 1L, &error);
20489 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020490 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020491 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020492
20493 /* We accept "$" for the column number: last column. */
20494 li = list_find(l, 1L);
20495 if (li != NULL && li->li_tv.v_type == VAR_STRING
20496 && li->li_tv.vval.v_string != NULL
20497 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20498 pos.col = len + 1;
20499
Bram Moolenaara5525202006-03-02 22:52:09 +000020500 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020501 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020502 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020503 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020504
Bram Moolenaara5525202006-03-02 22:52:09 +000020505#ifdef FEAT_VIRTUALEDIT
20506 /* Get the virtual offset. Defaults to zero. */
20507 pos.coladd = list_find_nr(l, 2L, &error);
20508 if (error)
20509 pos.coladd = 0;
20510#endif
20511
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020512 return &pos;
20513 }
20514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020515 name = get_tv_string_chk(varp);
20516 if (name == NULL)
20517 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020518 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020520 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20521 {
20522 if (VIsual_active)
20523 return &VIsual;
20524 return &curwin->w_cursor;
20525 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020526 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020528 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20530 return NULL;
20531 return pp;
20532 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020533
20534#ifdef FEAT_VIRTUALEDIT
20535 pos.coladd = 0;
20536#endif
20537
Bram Moolenaar477933c2007-07-17 14:32:23 +000020538 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020539 {
20540 pos.col = 0;
20541 if (name[1] == '0') /* "w0": first visible line */
20542 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020543 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020544 pos.lnum = curwin->w_topline;
20545 return &pos;
20546 }
20547 else if (name[1] == '$') /* "w$": last visible line */
20548 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020549 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020550 pos.lnum = curwin->w_botline - 1;
20551 return &pos;
20552 }
20553 }
20554 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020556 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 {
20558 pos.lnum = curbuf->b_ml.ml_line_count;
20559 pos.col = 0;
20560 }
20561 else
20562 {
20563 pos.lnum = curwin->w_cursor.lnum;
20564 pos.col = (colnr_T)STRLEN(ml_get_curline());
20565 }
20566 return &pos;
20567 }
20568 return NULL;
20569}
20570
20571/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020572 * Convert list in "arg" into a position and optional file number.
20573 * When "fnump" is NULL there is no file number, only 3 items.
20574 * Note that the column is passed on as-is, the caller may want to decrement
20575 * it to use 1 for the first column.
20576 * Return FAIL when conversion is not possible, doesn't check the position for
20577 * validity.
20578 */
20579 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020580list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020581 typval_T *arg;
20582 pos_T *posp;
20583 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020584 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020585{
20586 list_T *l = arg->vval.v_list;
20587 long i = 0;
20588 long n;
20589
Bram Moolenaar493c1782014-05-28 14:34:46 +020020590 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20591 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020592 if (arg->v_type != VAR_LIST
20593 || l == NULL
20594 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020595 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020596 return FAIL;
20597
20598 if (fnump != NULL)
20599 {
20600 n = list_find_nr(l, i++, NULL); /* fnum */
20601 if (n < 0)
20602 return FAIL;
20603 if (n == 0)
20604 n = curbuf->b_fnum; /* current buffer */
20605 *fnump = n;
20606 }
20607
20608 n = list_find_nr(l, i++, NULL); /* lnum */
20609 if (n < 0)
20610 return FAIL;
20611 posp->lnum = n;
20612
20613 n = list_find_nr(l, i++, NULL); /* col */
20614 if (n < 0)
20615 return FAIL;
20616 posp->col = n;
20617
20618#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020619 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020620 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020621 posp->coladd = 0;
20622 else
20623 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020624#endif
20625
Bram Moolenaar493c1782014-05-28 14:34:46 +020020626 if (curswantp != NULL)
20627 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20628
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020629 return OK;
20630}
20631
20632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 * Get the length of an environment variable name.
20634 * Advance "arg" to the first character after the name.
20635 * Return 0 for error.
20636 */
20637 static int
20638get_env_len(arg)
20639 char_u **arg;
20640{
20641 char_u *p;
20642 int len;
20643
20644 for (p = *arg; vim_isIDc(*p); ++p)
20645 ;
20646 if (p == *arg) /* no name found */
20647 return 0;
20648
20649 len = (int)(p - *arg);
20650 *arg = p;
20651 return len;
20652}
20653
20654/*
20655 * Get the length of the name of a function or internal variable.
20656 * "arg" is advanced to the first non-white character after the name.
20657 * Return 0 if something is wrong.
20658 */
20659 static int
20660get_id_len(arg)
20661 char_u **arg;
20662{
20663 char_u *p;
20664 int len;
20665
20666 /* Find the end of the name. */
20667 for (p = *arg; eval_isnamec(*p); ++p)
20668 ;
20669 if (p == *arg) /* no name found */
20670 return 0;
20671
20672 len = (int)(p - *arg);
20673 *arg = skipwhite(p);
20674
20675 return len;
20676}
20677
20678/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020679 * Get the length of the name of a variable or function.
20680 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020682 * Return -1 if curly braces expansion failed.
20683 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 * If the name contains 'magic' {}'s, expand them and return the
20685 * expanded name in an allocated string via 'alias' - caller must free.
20686 */
20687 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020688get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 char_u **arg;
20690 char_u **alias;
20691 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020692 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693{
20694 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695 char_u *p;
20696 char_u *expr_start;
20697 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698
20699 *alias = NULL; /* default to no alias */
20700
20701 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20702 && (*arg)[2] == (int)KE_SNR)
20703 {
20704 /* hard coded <SNR>, already translated */
20705 *arg += 3;
20706 return get_id_len(arg) + 3;
20707 }
20708 len = eval_fname_script(*arg);
20709 if (len > 0)
20710 {
20711 /* literal "<SID>", "s:" or "<SNR>" */
20712 *arg += len;
20713 }
20714
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020716 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020718 p = find_name_end(*arg, &expr_start, &expr_end,
20719 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720 if (expr_start != NULL)
20721 {
20722 char_u *temp_string;
20723
20724 if (!evaluate)
20725 {
20726 len += (int)(p - *arg);
20727 *arg = skipwhite(p);
20728 return len;
20729 }
20730
20731 /*
20732 * Include any <SID> etc in the expanded string:
20733 * Thus the -len here.
20734 */
20735 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20736 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020737 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 *alias = temp_string;
20739 *arg = skipwhite(p);
20740 return (int)STRLEN(temp_string);
20741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742
20743 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020744 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 EMSG2(_(e_invexpr2), *arg);
20746
20747 return len;
20748}
20749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020750/*
20751 * Find the end of a variable or function name, taking care of magic braces.
20752 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20753 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020754 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020755 * Return a pointer to just after the name. Equal to "arg" if there is no
20756 * valid name.
20757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020759find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 char_u *arg;
20761 char_u **expr_start;
20762 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020763 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020765 int mb_nest = 0;
20766 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 char_u *p;
20768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020769 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020771 *expr_start = NULL;
20772 *expr_end = NULL;
20773 }
20774
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020775 /* Quick check for valid starting character. */
20776 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20777 return arg;
20778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020779 for (p = arg; *p != NUL
20780 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020781 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020782 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020783 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020784 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020785 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020786 if (*p == '\'')
20787 {
20788 /* skip over 'string' to avoid counting [ and ] inside it. */
20789 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20790 ;
20791 if (*p == NUL)
20792 break;
20793 }
20794 else if (*p == '"')
20795 {
20796 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20797 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20798 if (*p == '\\' && p[1] != NUL)
20799 ++p;
20800 if (*p == NUL)
20801 break;
20802 }
20803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020806 if (*p == '[')
20807 ++br_nest;
20808 else if (*p == ']')
20809 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020812 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020814 if (*p == '{')
20815 {
20816 mb_nest++;
20817 if (expr_start != NULL && *expr_start == NULL)
20818 *expr_start = p;
20819 }
20820 else if (*p == '}')
20821 {
20822 mb_nest--;
20823 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20824 *expr_end = p;
20825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 }
20828
20829 return p;
20830}
20831
20832/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020833 * Expands out the 'magic' {}'s in a variable/function name.
20834 * Note that this can call itself recursively, to deal with
20835 * constructs like foo{bar}{baz}{bam}
20836 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20837 * "in_start" ^
20838 * "expr_start" ^
20839 * "expr_end" ^
20840 * "in_end" ^
20841 *
20842 * Returns a new allocated string, which the caller must free.
20843 * Returns NULL for failure.
20844 */
20845 static char_u *
20846make_expanded_name(in_start, expr_start, expr_end, in_end)
20847 char_u *in_start;
20848 char_u *expr_start;
20849 char_u *expr_end;
20850 char_u *in_end;
20851{
20852 char_u c1;
20853 char_u *retval = NULL;
20854 char_u *temp_result;
20855 char_u *nextcmd = NULL;
20856
20857 if (expr_end == NULL || in_end == NULL)
20858 return NULL;
20859 *expr_start = NUL;
20860 *expr_end = NUL;
20861 c1 = *in_end;
20862 *in_end = NUL;
20863
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020864 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020865 if (temp_result != NULL && nextcmd == NULL)
20866 {
20867 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20868 + (in_end - expr_end) + 1));
20869 if (retval != NULL)
20870 {
20871 STRCPY(retval, in_start);
20872 STRCAT(retval, temp_result);
20873 STRCAT(retval, expr_end + 1);
20874 }
20875 }
20876 vim_free(temp_result);
20877
20878 *in_end = c1; /* put char back for error messages */
20879 *expr_start = '{';
20880 *expr_end = '}';
20881
20882 if (retval != NULL)
20883 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020884 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020885 if (expr_start != NULL)
20886 {
20887 /* Further expansion! */
20888 temp_result = make_expanded_name(retval, expr_start,
20889 expr_end, temp_result);
20890 vim_free(retval);
20891 retval = temp_result;
20892 }
20893 }
20894
20895 return retval;
20896}
20897
20898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020900 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 */
20902 static int
20903eval_isnamec(c)
20904 int c;
20905{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020906 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20907}
20908
20909/*
20910 * Return TRUE if character "c" can be used as the first character in a
20911 * variable or function name (excluding '{' and '}').
20912 */
20913 static int
20914eval_isnamec1(c)
20915 int c;
20916{
20917 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918}
20919
20920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 * Set number v: variable to "val".
20922 */
20923 void
20924set_vim_var_nr(idx, val)
20925 int idx;
20926 long val;
20927{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020928 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929}
20930
20931/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020932 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 */
20934 long
20935get_vim_var_nr(idx)
20936 int idx;
20937{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020938 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939}
20940
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020941/*
20942 * Get string v: variable value. Uses a static buffer, can only be used once.
20943 */
20944 char_u *
20945get_vim_var_str(idx)
20946 int idx;
20947{
20948 return get_tv_string(&vimvars[idx].vv_tv);
20949}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020950
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020952 * Get List v: variable value. Caller must take care of reference count when
20953 * needed.
20954 */
20955 list_T *
20956get_vim_var_list(idx)
20957 int idx;
20958{
20959 return vimvars[idx].vv_list;
20960}
20961
20962/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020963 * Set v:char to character "c".
20964 */
20965 void
20966set_vim_var_char(c)
20967 int c;
20968{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020969 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020970
20971#ifdef FEAT_MBYTE
20972 if (has_mbyte)
20973 buf[(*mb_char2bytes)(c, buf)] = NUL;
20974 else
20975#endif
20976 {
20977 buf[0] = c;
20978 buf[1] = NUL;
20979 }
20980 set_vim_var_string(VV_CHAR, buf, -1);
20981}
20982
20983/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020984 * Set v:count to "count" and v:count1 to "count1".
20985 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 */
20987 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020988set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 long count;
20990 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020991 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020993 if (set_prevcount)
20994 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020995 vimvars[VV_COUNT].vv_nr = count;
20996 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997}
20998
20999/*
21000 * Set string v: variable to a copy of "val".
21001 */
21002 void
21003set_vim_var_string(idx, val, len)
21004 int idx;
21005 char_u *val;
21006 int len; /* length of "val" to use or -1 (whole string) */
21007{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021008 /* Need to do this (at least) once, since we can't initialize a union.
21009 * Will always be invoked when "v:progname" is set. */
21010 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21011
Bram Moolenaare9a41262005-01-15 22:18:47 +000021012 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021014 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021016 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021018 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019}
21020
21021/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021022 * Set List v: variable to "val".
21023 */
21024 void
21025set_vim_var_list(idx, val)
21026 int idx;
21027 list_T *val;
21028{
21029 list_unref(vimvars[idx].vv_list);
21030 vimvars[idx].vv_list = val;
21031 if (val != NULL)
21032 ++val->lv_refcount;
21033}
21034
21035/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021036 * Set Dictionary v: variable to "val".
21037 */
21038 void
21039set_vim_var_dict(idx, val)
21040 int idx;
21041 dict_T *val;
21042{
21043 int todo;
21044 hashitem_T *hi;
21045
21046 dict_unref(vimvars[idx].vv_dict);
21047 vimvars[idx].vv_dict = val;
21048 if (val != NULL)
21049 {
21050 ++val->dv_refcount;
21051
21052 /* Set readonly */
21053 todo = (int)val->dv_hashtab.ht_used;
21054 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21055 {
21056 if (HASHITEM_EMPTY(hi))
21057 continue;
21058 --todo;
21059 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21060 }
21061 }
21062}
21063
21064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 * Set v:register if needed.
21066 */
21067 void
21068set_reg_var(c)
21069 int c;
21070{
21071 char_u regname;
21072
21073 if (c == 0 || c == ' ')
21074 regname = '"';
21075 else
21076 regname = c;
21077 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021078 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079 set_vim_var_string(VV_REG, &regname, 1);
21080}
21081
21082/*
21083 * Get or set v:exception. If "oldval" == NULL, return the current value.
21084 * Otherwise, restore the value to "oldval" and return NULL.
21085 * Must always be called in pairs to save and restore v:exception! Does not
21086 * take care of memory allocations.
21087 */
21088 char_u *
21089v_exception(oldval)
21090 char_u *oldval;
21091{
21092 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021093 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094
Bram Moolenaare9a41262005-01-15 22:18:47 +000021095 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 return NULL;
21097}
21098
21099/*
21100 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21101 * Otherwise, restore the value to "oldval" and return NULL.
21102 * Must always be called in pairs to save and restore v:throwpoint! Does not
21103 * take care of memory allocations.
21104 */
21105 char_u *
21106v_throwpoint(oldval)
21107 char_u *oldval;
21108{
21109 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021110 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111
Bram Moolenaare9a41262005-01-15 22:18:47 +000021112 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 return NULL;
21114}
21115
21116#if defined(FEAT_AUTOCMD) || defined(PROTO)
21117/*
21118 * Set v:cmdarg.
21119 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21120 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21121 * Must always be called in pairs!
21122 */
21123 char_u *
21124set_cmdarg(eap, oldarg)
21125 exarg_T *eap;
21126 char_u *oldarg;
21127{
21128 char_u *oldval;
21129 char_u *newval;
21130 unsigned len;
21131
Bram Moolenaare9a41262005-01-15 22:18:47 +000021132 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021133 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021135 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021136 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021137 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 }
21139
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021140 if (eap->force_bin == FORCE_BIN)
21141 len = 6;
21142 else if (eap->force_bin == FORCE_NOBIN)
21143 len = 8;
21144 else
21145 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021146
21147 if (eap->read_edit)
21148 len += 7;
21149
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021150 if (eap->force_ff != 0)
21151 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21152# ifdef FEAT_MBYTE
21153 if (eap->force_enc != 0)
21154 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021155 if (eap->bad_char != 0)
21156 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021157# endif
21158
21159 newval = alloc(len + 1);
21160 if (newval == NULL)
21161 return NULL;
21162
21163 if (eap->force_bin == FORCE_BIN)
21164 sprintf((char *)newval, " ++bin");
21165 else if (eap->force_bin == FORCE_NOBIN)
21166 sprintf((char *)newval, " ++nobin");
21167 else
21168 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021169
21170 if (eap->read_edit)
21171 STRCAT(newval, " ++edit");
21172
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021173 if (eap->force_ff != 0)
21174 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21175 eap->cmd + eap->force_ff);
21176# ifdef FEAT_MBYTE
21177 if (eap->force_enc != 0)
21178 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21179 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021180 if (eap->bad_char == BAD_KEEP)
21181 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21182 else if (eap->bad_char == BAD_DROP)
21183 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21184 else if (eap->bad_char != 0)
21185 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021186# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021187 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021188 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189}
21190#endif
21191
21192/*
21193 * Get the value of internal variable "name".
21194 * Return OK or FAIL.
21195 */
21196 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021197get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 char_u *name;
21199 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021200 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021201 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021202 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021203 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204{
21205 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021206 typval_T *tv = NULL;
21207 typval_T atv;
21208 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210
21211 /* truncate the name, so that we can use strcmp() */
21212 cc = name[len];
21213 name[len] = NUL;
21214
21215 /*
21216 * Check for "b:changedtick".
21217 */
21218 if (STRCMP(name, "b:changedtick") == 0)
21219 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021220 atv.v_type = VAR_NUMBER;
21221 atv.vval.v_number = curbuf->b_changedtick;
21222 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 }
21224
21225 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 * Check for user-defined variables.
21227 */
21228 else
21229 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021230 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021232 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021233 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021234 if (dip != NULL)
21235 *dip = v;
21236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 }
21238
Bram Moolenaare9a41262005-01-15 22:18:47 +000021239 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021241 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021242 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 ret = FAIL;
21244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021245 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021246 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247
21248 name[len] = cc;
21249
21250 return ret;
21251}
21252
21253/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021254 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21255 * Also handle function call with Funcref variable: func(expr)
21256 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21257 */
21258 static int
21259handle_subscript(arg, rettv, evaluate, verbose)
21260 char_u **arg;
21261 typval_T *rettv;
21262 int evaluate; /* do more than finding the end */
21263 int verbose; /* give error messages */
21264{
21265 int ret = OK;
21266 dict_T *selfdict = NULL;
21267 char_u *s;
21268 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021269 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021270
21271 while (ret == OK
21272 && (**arg == '['
21273 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021274 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021275 && !vim_iswhite(*(*arg - 1)))
21276 {
21277 if (**arg == '(')
21278 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021279 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021280 if (evaluate)
21281 {
21282 functv = *rettv;
21283 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021284
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021285 /* Invoke the function. Recursive! */
21286 s = functv.vval.v_string;
21287 }
21288 else
21289 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021290 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021291 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21292 &len, evaluate, selfdict);
21293
21294 /* Clear the funcref afterwards, so that deleting it while
21295 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021296 if (evaluate)
21297 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021298
21299 /* Stop the expression evaluation when immediately aborting on
21300 * error, or when an interrupt occurred or an exception was thrown
21301 * but not caught. */
21302 if (aborting())
21303 {
21304 if (ret == OK)
21305 clear_tv(rettv);
21306 ret = FAIL;
21307 }
21308 dict_unref(selfdict);
21309 selfdict = NULL;
21310 }
21311 else /* **arg == '[' || **arg == '.' */
21312 {
21313 dict_unref(selfdict);
21314 if (rettv->v_type == VAR_DICT)
21315 {
21316 selfdict = rettv->vval.v_dict;
21317 if (selfdict != NULL)
21318 ++selfdict->dv_refcount;
21319 }
21320 else
21321 selfdict = NULL;
21322 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21323 {
21324 clear_tv(rettv);
21325 ret = FAIL;
21326 }
21327 }
21328 }
21329 dict_unref(selfdict);
21330 return ret;
21331}
21332
21333/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021334 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021335 * value).
21336 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021337 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021338alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021339{
Bram Moolenaar33570922005-01-25 22:26:29 +000021340 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021341}
21342
21343/*
21344 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 * The string "s" must have been allocated, it is consumed.
21346 * Return NULL for out of memory, the variable otherwise.
21347 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021348 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021349alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 char_u *s;
21351{
Bram Moolenaar33570922005-01-25 22:26:29 +000021352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021354 rettv = alloc_tv();
21355 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021357 rettv->v_type = VAR_STRING;
21358 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 }
21360 else
21361 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363}
21364
21365/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021366 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021368 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021369free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021370 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371{
21372 if (varp != NULL)
21373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021374 switch (varp->v_type)
21375 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021376 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021377 func_unref(varp->vval.v_string);
21378 /*FALLTHROUGH*/
21379 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021380 vim_free(varp->vval.v_string);
21381 break;
21382 case VAR_LIST:
21383 list_unref(varp->vval.v_list);
21384 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021385 case VAR_DICT:
21386 dict_unref(varp->vval.v_dict);
21387 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021388 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021389#ifdef FEAT_FLOAT
21390 case VAR_FLOAT:
21391#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021392 case VAR_UNKNOWN:
21393 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021395 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021396 break;
21397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 vim_free(varp);
21399 }
21400}
21401
21402/*
21403 * Free the memory for a variable value and set the value to NULL or 0.
21404 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021405 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021406clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021407 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408{
21409 if (varp != NULL)
21410 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021411 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021413 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021414 func_unref(varp->vval.v_string);
21415 /*FALLTHROUGH*/
21416 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021417 vim_free(varp->vval.v_string);
21418 varp->vval.v_string = NULL;
21419 break;
21420 case VAR_LIST:
21421 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021422 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021423 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021424 case VAR_DICT:
21425 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021426 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021427 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021428 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021429 varp->vval.v_number = 0;
21430 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021431#ifdef FEAT_FLOAT
21432 case VAR_FLOAT:
21433 varp->vval.v_float = 0.0;
21434 break;
21435#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021436 case VAR_UNKNOWN:
21437 break;
21438 default:
21439 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021441 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 }
21443}
21444
21445/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021446 * Set the value of a variable to NULL without freeing items.
21447 */
21448 static void
21449init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021450 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021451{
21452 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021453 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021454}
21455
21456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 * Get the number value of a variable.
21458 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021459 * For incompatible types, return 0.
21460 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21461 * caller of incompatible types: it sets *denote to TRUE if "denote"
21462 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 */
21464 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021465get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021468 int error = FALSE;
21469
21470 return get_tv_number_chk(varp, &error); /* return 0L on error */
21471}
21472
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021473 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021474get_tv_number_chk(varp, denote)
21475 typval_T *varp;
21476 int *denote;
21477{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021478 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021480 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021482 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021483 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021484#ifdef FEAT_FLOAT
21485 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021486 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021487 break;
21488#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021489 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021490 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021491 break;
21492 case VAR_STRING:
21493 if (varp->vval.v_string != NULL)
21494 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021495 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021496 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021497 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021498 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021499 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021500 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021501 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021503 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021504 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021505 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021507 if (denote == NULL) /* useful for values that must be unsigned */
21508 n = -1;
21509 else
21510 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021511 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512}
21513
21514/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021515 * Get the lnum from the first argument.
21516 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021517 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 */
21519 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021520get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021521 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522{
Bram Moolenaar33570922005-01-25 22:26:29 +000021523 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 linenr_T lnum;
21525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021526 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 if (lnum == 0) /* no valid number, try using line() */
21528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021529 rettv.v_type = VAR_NUMBER;
21530 f_line(argvars, &rettv);
21531 lnum = rettv.vval.v_number;
21532 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 }
21534 return lnum;
21535}
21536
21537/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021538 * Get the lnum from the first argument.
21539 * Also accepts "$", then "buf" is used.
21540 * Returns 0 on error.
21541 */
21542 static linenr_T
21543get_tv_lnum_buf(argvars, buf)
21544 typval_T *argvars;
21545 buf_T *buf;
21546{
21547 if (argvars[0].v_type == VAR_STRING
21548 && argvars[0].vval.v_string != NULL
21549 && argvars[0].vval.v_string[0] == '$'
21550 && buf != NULL)
21551 return buf->b_ml.ml_line_count;
21552 return get_tv_number_chk(&argvars[0], NULL);
21553}
21554
21555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 * Get the string value of a variable.
21557 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021558 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21559 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 * If the String variable has never been set, return an empty string.
21561 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021562 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21563 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 */
21565 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021566get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021567 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568{
21569 static char_u mybuf[NUMBUFLEN];
21570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021571 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572}
21573
21574 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021575get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021576 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021577 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021579 char_u *res = get_tv_string_buf_chk(varp, buf);
21580
21581 return res != NULL ? res : (char_u *)"";
21582}
21583
Bram Moolenaar7d647822014-04-05 21:28:56 +020021584/*
21585 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21586 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021587 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021588get_tv_string_chk(varp)
21589 typval_T *varp;
21590{
21591 static char_u mybuf[NUMBUFLEN];
21592
21593 return get_tv_string_buf_chk(varp, mybuf);
21594}
21595
21596 static char_u *
21597get_tv_string_buf_chk(varp, buf)
21598 typval_T *varp;
21599 char_u *buf;
21600{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021601 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021603 case VAR_NUMBER:
21604 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21605 return buf;
21606 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021607 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021608 break;
21609 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021610 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021611 break;
21612 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021613 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021614 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021615#ifdef FEAT_FLOAT
21616 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021617 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021618 break;
21619#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021620 case VAR_STRING:
21621 if (varp->vval.v_string != NULL)
21622 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021623 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021626 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021628 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629}
21630
21631/*
21632 * Find variable "name" in the list of variables.
21633 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021634 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021635 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021638 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021639find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021641 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021642 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021645 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646
Bram Moolenaara7043832005-01-21 11:56:39 +000021647 ht = find_var_ht(name, &varname);
21648 if (htp != NULL)
21649 *htp = ht;
21650 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021652 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653}
21654
21655/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021656 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021657 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021660find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021661 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021662 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021663 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021664 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021665{
Bram Moolenaar33570922005-01-25 22:26:29 +000021666 hashitem_T *hi;
21667
21668 if (*varname == NUL)
21669 {
21670 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021671 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021672 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021673 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021674 case 'g': return &globvars_var;
21675 case 'v': return &vimvars_var;
21676 case 'b': return &curbuf->b_bufvar;
21677 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021678#ifdef FEAT_WINDOWS
21679 case 't': return &curtab->tp_winvar;
21680#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021681 case 'l': return current_funccal == NULL
21682 ? NULL : &current_funccal->l_vars_var;
21683 case 'a': return current_funccal == NULL
21684 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021685 }
21686 return NULL;
21687 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021688
21689 hi = hash_find(ht, varname);
21690 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021691 {
21692 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021693 * worked find the variable again. Don't auto-load a script if it was
21694 * loaded already, otherwise it would be loaded every time when
21695 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021696 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021697 {
21698 /* Note: script_autoload() may make "hi" invalid. It must either
21699 * be obtained again or not used. */
21700 if (!script_autoload(varname, FALSE) || aborting())
21701 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021702 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021703 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021704 if (HASHITEM_EMPTY(hi))
21705 return NULL;
21706 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021707 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021708}
21709
21710/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021711 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021712 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021713 * Set "varname" to the start of name without ':'.
21714 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021716find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717 char_u *name;
21718 char_u **varname;
21719{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021720 hashitem_T *hi;
21721
Bram Moolenaar73627d02015-08-11 15:46:09 +020021722 if (name[0] == NUL)
21723 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 if (name[1] != ':')
21725 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021726 /* The name must not start with a colon or #. */
21727 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 return NULL;
21729 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021730
21731 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021732 hi = hash_find(&compat_hashtab, name);
21733 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021734 return &compat_hashtab;
21735
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021737 return &globvarht; /* global variable */
21738 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 }
21740 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021741 if (*name == 'g') /* global variable */
21742 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021743 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21744 */
21745 if (vim_strchr(name + 2, ':') != NULL
21746 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021747 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021749 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021751 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021752#ifdef FEAT_WINDOWS
21753 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021754 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021755#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021756 if (*name == 'v') /* v: variable */
21757 return &vimvarht;
21758 if (*name == 'a' && current_funccal != NULL) /* function argument */
21759 return &current_funccal->l_avars.dv_hashtab;
21760 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21761 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 if (*name == 's' /* script variable */
21763 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21764 return &SCRIPT_VARS(current_SID);
21765 return NULL;
21766}
21767
21768/*
21769 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021770 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 * Returns NULL when it doesn't exist.
21772 */
21773 char_u *
21774get_var_value(name)
21775 char_u *name;
21776{
Bram Moolenaar33570922005-01-25 22:26:29 +000021777 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021779 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 if (v == NULL)
21781 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021782 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783}
21784
21785/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 * sourcing this script and when executing functions defined in the script.
21788 */
21789 void
21790new_script_vars(id)
21791 scid_T id;
21792{
Bram Moolenaara7043832005-01-21 11:56:39 +000021793 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021794 hashtab_T *ht;
21795 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021796
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21798 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021799 /* Re-allocating ga_data means that an ht_array pointing to
21800 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021801 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021802 for (i = 1; i <= ga_scripts.ga_len; ++i)
21803 {
21804 ht = &SCRIPT_VARS(i);
21805 if (ht->ht_mask == HT_INIT_SIZE - 1)
21806 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021807 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021808 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021809 }
21810
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 while (ga_scripts.ga_len < id)
21812 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021813 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021814 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021815 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 }
21818 }
21819}
21820
21821/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021822 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21823 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 */
21825 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021826init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 dict_T *dict;
21828 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021829 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830{
Bram Moolenaar33570922005-01-25 22:26:29 +000021831 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021832 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021833 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021834 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021835 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021836 dict_var->di_tv.vval.v_dict = dict;
21837 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021838 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21840 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841}
21842
21843/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021844 * Unreference a dictionary initialized by init_var_dict().
21845 */
21846 void
21847unref_var_dict(dict)
21848 dict_T *dict;
21849{
21850 /* Now the dict needs to be freed if no one else is using it, go back to
21851 * normal reference counting. */
21852 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21853 dict_unref(dict);
21854}
21855
21856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021858 * Frees all allocated variables and the value they contain.
21859 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 */
21861 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021862vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 hashtab_T *ht;
21864{
21865 vars_clear_ext(ht, TRUE);
21866}
21867
21868/*
21869 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21870 */
21871 static void
21872vars_clear_ext(ht, free_val)
21873 hashtab_T *ht;
21874 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875{
Bram Moolenaara7043832005-01-21 11:56:39 +000021876 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021877 hashitem_T *hi;
21878 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879
Bram Moolenaar33570922005-01-25 22:26:29 +000021880 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021881 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021882 for (hi = ht->ht_array; todo > 0; ++hi)
21883 {
21884 if (!HASHITEM_EMPTY(hi))
21885 {
21886 --todo;
21887
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021889 * ht_array might change then. hash_clear() takes care of it
21890 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021891 v = HI2DI(hi);
21892 if (free_val)
21893 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021894 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021895 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021896 }
21897 }
21898 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021899 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900}
21901
Bram Moolenaara7043832005-01-21 11:56:39 +000021902/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 * Delete a variable from hashtab "ht" at item "hi".
21904 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021907delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021908 hashtab_T *ht;
21909 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910{
Bram Moolenaar33570922005-01-25 22:26:29 +000021911 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021912
21913 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 clear_tv(&di->di_tv);
21915 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916}
21917
21918/*
21919 * List the value of one internal variable.
21920 */
21921 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021922list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021925 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021927 char_u *tofree;
21928 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021929 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021930
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021931 current_copyID += COPYID_INC;
21932 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021933 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021934 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021935 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936}
21937
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021939list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940 char_u *prefix;
21941 char_u *name;
21942 int type;
21943 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021944 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945{
Bram Moolenaar31859182007-08-14 20:41:13 +000021946 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21947 msg_start();
21948 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 if (name != NULL) /* "a:" vars don't have a name stored */
21950 msg_puts(name);
21951 msg_putchar(' ');
21952 msg_advance(22);
21953 if (type == VAR_NUMBER)
21954 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021955 else if (type == VAR_FUNC)
21956 msg_putchar('*');
21957 else if (type == VAR_LIST)
21958 {
21959 msg_putchar('[');
21960 if (*string == '[')
21961 ++string;
21962 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021963 else if (type == VAR_DICT)
21964 {
21965 msg_putchar('{');
21966 if (*string == '{')
21967 ++string;
21968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 else
21970 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021971
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021973
21974 if (type == VAR_FUNC)
21975 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021976 if (*first)
21977 {
21978 msg_clr_eos();
21979 *first = FALSE;
21980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981}
21982
21983/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021984 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 * If the variable already exists, the value is updated.
21986 * Otherwise the variable is created.
21987 */
21988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021989set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021992 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993{
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021996 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021998 ht = find_var_ht(name, &varname);
21999 if (ht == NULL || *varname == NUL)
22000 {
22001 EMSG2(_(e_illvar), name);
22002 return;
22003 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022004 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022005
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022006 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22007 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022008
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022011 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022012 if (var_check_ro(v->di_flags, name, FALSE)
22013 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022014 return;
22015 if (v->di_tv.v_type != tv->v_type
22016 && !((v->di_tv.v_type == VAR_STRING
22017 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022018 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022019 || tv->v_type == VAR_NUMBER))
22020#ifdef FEAT_FLOAT
22021 && !((v->di_tv.v_type == VAR_NUMBER
22022 || v->di_tv.v_type == VAR_FLOAT)
22023 && (tv->v_type == VAR_NUMBER
22024 || tv->v_type == VAR_FLOAT))
22025#endif
22026 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022027 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022028 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022029 return;
22030 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022031
22032 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022033 * Handle setting internal v: variables separately where needed to
22034 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022035 */
22036 if (ht == &vimvarht)
22037 {
22038 if (v->di_tv.v_type == VAR_STRING)
22039 {
22040 vim_free(v->di_tv.vval.v_string);
22041 if (copy || tv->v_type != VAR_STRING)
22042 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22043 else
22044 {
22045 /* Take over the string to avoid an extra alloc/free. */
22046 v->di_tv.vval.v_string = tv->vval.v_string;
22047 tv->vval.v_string = NULL;
22048 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022049 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022050 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022051 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022052 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022053 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022054 if (STRCMP(varname, "searchforward") == 0)
22055 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022056#ifdef FEAT_SEARCH_EXTRA
22057 else if (STRCMP(varname, "hlsearch") == 0)
22058 {
22059 no_hlsearch = !v->di_tv.vval.v_number;
22060 redraw_all_later(SOME_VALID);
22061 }
22062#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022063 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022064 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022065 else if (v->di_tv.v_type != tv->v_type)
22066 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022067 }
22068
22069 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 }
22071 else /* add a new variable */
22072 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022073 /* Can't add "v:" variable. */
22074 if (ht == &vimvarht)
22075 {
22076 EMSG2(_(e_illvar), name);
22077 return;
22078 }
22079
Bram Moolenaar92124a32005-06-17 22:03:40 +000022080 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022081 if (!valid_varname(varname))
22082 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022083
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022084 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22085 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022086 if (v == NULL)
22087 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022088 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022089 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022091 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 return;
22093 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022094 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022096
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022097 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022098 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022099 else
22100 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022101 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022102 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022103 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105}
22106
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022107/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022108 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022109 * Also give an error message.
22110 */
22111 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022112var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022113 int flags;
22114 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022115 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022116{
22117 if (flags & DI_FLAGS_RO)
22118 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022119 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022120 return TRUE;
22121 }
22122 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22123 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022124 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022125 return TRUE;
22126 }
22127 return FALSE;
22128}
22129
22130/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022131 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22132 * Also give an error message.
22133 */
22134 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022135var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022136 int flags;
22137 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022138 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022139{
22140 if (flags & DI_FLAGS_FIX)
22141 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022142 EMSG2(_("E795: Cannot delete variable %s"),
22143 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022144 return TRUE;
22145 }
22146 return FALSE;
22147}
22148
22149/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022150 * Check if a funcref is assigned to a valid variable name.
22151 * Return TRUE and give an error if not.
22152 */
22153 static int
22154var_check_func_name(name, new_var)
22155 char_u *name; /* points to start of variable name */
22156 int new_var; /* TRUE when creating the variable */
22157{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022158 /* Allow for w: b: s: and t:. */
22159 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022160 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22161 ? name[2] : name[0]))
22162 {
22163 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22164 name);
22165 return TRUE;
22166 }
22167 /* Don't allow hiding a function. When "v" is not NULL we might be
22168 * assigning another function to the same var, the type is checked
22169 * below. */
22170 if (new_var && function_exists(name))
22171 {
22172 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22173 name);
22174 return TRUE;
22175 }
22176 return FALSE;
22177}
22178
22179/*
22180 * Check if a variable name is valid.
22181 * Return FALSE and give an error if not.
22182 */
22183 static int
22184valid_varname(varname)
22185 char_u *varname;
22186{
22187 char_u *p;
22188
22189 for (p = varname; *p != NUL; ++p)
22190 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22191 && *p != AUTOLOAD_CHAR)
22192 {
22193 EMSG2(_(e_illvar), varname);
22194 return FALSE;
22195 }
22196 return TRUE;
22197}
22198
22199/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022200 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022201 * Also give an error message, using "name" or _("name") when use_gettext is
22202 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022203 */
22204 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022205tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022206 int lock;
22207 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022208 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022209{
22210 if (lock & VAR_LOCKED)
22211 {
22212 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022213 name == NULL ? (char_u *)_("Unknown")
22214 : use_gettext ? (char_u *)_(name)
22215 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022216 return TRUE;
22217 }
22218 if (lock & VAR_FIXED)
22219 {
22220 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022221 name == NULL ? (char_u *)_("Unknown")
22222 : use_gettext ? (char_u *)_(name)
22223 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022224 return TRUE;
22225 }
22226 return FALSE;
22227}
22228
22229/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022230 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022231 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022232 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022233 * It is OK for "from" and "to" to point to the same item. This is used to
22234 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022235 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022236 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022237copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022238 typval_T *from;
22239 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022241 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022242 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022243 switch (from->v_type)
22244 {
22245 case VAR_NUMBER:
22246 to->vval.v_number = from->vval.v_number;
22247 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022248#ifdef FEAT_FLOAT
22249 case VAR_FLOAT:
22250 to->vval.v_float = from->vval.v_float;
22251 break;
22252#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022253 case VAR_STRING:
22254 case VAR_FUNC:
22255 if (from->vval.v_string == NULL)
22256 to->vval.v_string = NULL;
22257 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022258 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022259 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022260 if (from->v_type == VAR_FUNC)
22261 func_ref(to->vval.v_string);
22262 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022263 break;
22264 case VAR_LIST:
22265 if (from->vval.v_list == NULL)
22266 to->vval.v_list = NULL;
22267 else
22268 {
22269 to->vval.v_list = from->vval.v_list;
22270 ++to->vval.v_list->lv_refcount;
22271 }
22272 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022273 case VAR_DICT:
22274 if (from->vval.v_dict == NULL)
22275 to->vval.v_dict = NULL;
22276 else
22277 {
22278 to->vval.v_dict = from->vval.v_dict;
22279 ++to->vval.v_dict->dv_refcount;
22280 }
22281 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022282 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022283 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022284 break;
22285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286}
22287
22288/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022289 * Make a copy of an item.
22290 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022291 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22292 * reference to an already copied list/dict can be used.
22293 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022294 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022295 static int
22296item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022297 typval_T *from;
22298 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022299 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022300 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022301{
22302 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022303 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022304
Bram Moolenaar33570922005-01-25 22:26:29 +000022305 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022306 {
22307 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022308 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022309 }
22310 ++recurse;
22311
22312 switch (from->v_type)
22313 {
22314 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022315#ifdef FEAT_FLOAT
22316 case VAR_FLOAT:
22317#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022318 case VAR_STRING:
22319 case VAR_FUNC:
22320 copy_tv(from, to);
22321 break;
22322 case VAR_LIST:
22323 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022324 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022325 if (from->vval.v_list == NULL)
22326 to->vval.v_list = NULL;
22327 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22328 {
22329 /* use the copy made earlier */
22330 to->vval.v_list = from->vval.v_list->lv_copylist;
22331 ++to->vval.v_list->lv_refcount;
22332 }
22333 else
22334 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22335 if (to->vval.v_list == NULL)
22336 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022337 break;
22338 case VAR_DICT:
22339 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022340 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022341 if (from->vval.v_dict == NULL)
22342 to->vval.v_dict = NULL;
22343 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22344 {
22345 /* use the copy made earlier */
22346 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22347 ++to->vval.v_dict->dv_refcount;
22348 }
22349 else
22350 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22351 if (to->vval.v_dict == NULL)
22352 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022353 break;
22354 default:
22355 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022356 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022357 }
22358 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022359 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022360}
22361
22362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 * ":echo expr1 ..." print each argument separated with a space, add a
22364 * newline at the end.
22365 * ":echon expr1 ..." print each argument plain.
22366 */
22367 void
22368ex_echo(eap)
22369 exarg_T *eap;
22370{
22371 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022372 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022373 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 char_u *p;
22375 int needclr = TRUE;
22376 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022377 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378
22379 if (eap->skip)
22380 ++emsg_skip;
22381 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22382 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022383 /* If eval1() causes an error message the text from the command may
22384 * still need to be cleared. E.g., "echo 22,44". */
22385 need_clr_eos = needclr;
22386
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022388 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 {
22390 /*
22391 * Report the invalid expression unless the expression evaluation
22392 * has been cancelled due to an aborting error, an interrupt, or an
22393 * exception.
22394 */
22395 if (!aborting())
22396 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022397 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 break;
22399 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022400 need_clr_eos = FALSE;
22401
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 if (!eap->skip)
22403 {
22404 if (atstart)
22405 {
22406 atstart = FALSE;
22407 /* Call msg_start() after eval1(), evaluating the expression
22408 * may cause a message to appear. */
22409 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022410 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022411 /* Mark the saved text as finishing the line, so that what
22412 * follows is displayed on a new line when scrolling back
22413 * at the more prompt. */
22414 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 }
22418 else if (eap->cmdidx == CMD_echo)
22419 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022420 current_copyID += COPYID_INC;
22421 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022422 if (p != NULL)
22423 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022425 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022427 if (*p != TAB && needclr)
22428 {
22429 /* remove any text still there from the command */
22430 msg_clr_eos();
22431 needclr = FALSE;
22432 }
22433 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 }
22435 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022436 {
22437#ifdef FEAT_MBYTE
22438 if (has_mbyte)
22439 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022440 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022441
22442 (void)msg_outtrans_len_attr(p, i, echo_attr);
22443 p += i - 1;
22444 }
22445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022447 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022450 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022452 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 arg = skipwhite(arg);
22454 }
22455 eap->nextcmd = check_nextcmd(arg);
22456
22457 if (eap->skip)
22458 --emsg_skip;
22459 else
22460 {
22461 /* remove text that may still be there from the command */
22462 if (needclr)
22463 msg_clr_eos();
22464 if (eap->cmdidx == CMD_echo)
22465 msg_end();
22466 }
22467}
22468
22469/*
22470 * ":echohl {name}".
22471 */
22472 void
22473ex_echohl(eap)
22474 exarg_T *eap;
22475{
22476 int id;
22477
22478 id = syn_name2id(eap->arg);
22479 if (id == 0)
22480 echo_attr = 0;
22481 else
22482 echo_attr = syn_id2attr(id);
22483}
22484
22485/*
22486 * ":execute expr1 ..." execute the result of an expression.
22487 * ":echomsg expr1 ..." Print a message
22488 * ":echoerr expr1 ..." Print an error
22489 * Each gets spaces around each argument and a newline at the end for
22490 * echo commands
22491 */
22492 void
22493ex_execute(eap)
22494 exarg_T *eap;
22495{
22496 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022497 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 int ret = OK;
22499 char_u *p;
22500 garray_T ga;
22501 int len;
22502 int save_did_emsg;
22503
22504 ga_init2(&ga, 1, 80);
22505
22506 if (eap->skip)
22507 ++emsg_skip;
22508 while (*arg != NUL && *arg != '|' && *arg != '\n')
22509 {
22510 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022511 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 {
22513 /*
22514 * Report the invalid expression unless the expression evaluation
22515 * has been cancelled due to an aborting error, an interrupt, or an
22516 * exception.
22517 */
22518 if (!aborting())
22519 EMSG2(_(e_invexpr2), p);
22520 ret = FAIL;
22521 break;
22522 }
22523
22524 if (!eap->skip)
22525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022526 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 len = (int)STRLEN(p);
22528 if (ga_grow(&ga, len + 2) == FAIL)
22529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022530 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 ret = FAIL;
22532 break;
22533 }
22534 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 ga.ga_len += len;
22538 }
22539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022540 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 arg = skipwhite(arg);
22542 }
22543
22544 if (ret != FAIL && ga.ga_data != NULL)
22545 {
22546 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022549 out_flush();
22550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 else if (eap->cmdidx == CMD_echoerr)
22552 {
22553 /* We don't want to abort following commands, restore did_emsg. */
22554 save_did_emsg = did_emsg;
22555 EMSG((char_u *)ga.ga_data);
22556 if (!force_abort)
22557 did_emsg = save_did_emsg;
22558 }
22559 else if (eap->cmdidx == CMD_execute)
22560 do_cmdline((char_u *)ga.ga_data,
22561 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22562 }
22563
22564 ga_clear(&ga);
22565
22566 if (eap->skip)
22567 --emsg_skip;
22568
22569 eap->nextcmd = check_nextcmd(arg);
22570}
22571
22572/*
22573 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22574 * "arg" points to the "&" or '+' when called, to "option" when returning.
22575 * Returns NULL when no option name found. Otherwise pointer to the char
22576 * after the option name.
22577 */
22578 static char_u *
22579find_option_end(arg, opt_flags)
22580 char_u **arg;
22581 int *opt_flags;
22582{
22583 char_u *p = *arg;
22584
22585 ++p;
22586 if (*p == 'g' && p[1] == ':')
22587 {
22588 *opt_flags = OPT_GLOBAL;
22589 p += 2;
22590 }
22591 else if (*p == 'l' && p[1] == ':')
22592 {
22593 *opt_flags = OPT_LOCAL;
22594 p += 2;
22595 }
22596 else
22597 *opt_flags = 0;
22598
22599 if (!ASCII_ISALPHA(*p))
22600 return NULL;
22601 *arg = p;
22602
22603 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22604 p += 4; /* termcap option */
22605 else
22606 while (ASCII_ISALPHA(*p))
22607 ++p;
22608 return p;
22609}
22610
22611/*
22612 * ":function"
22613 */
22614 void
22615ex_function(eap)
22616 exarg_T *eap;
22617{
22618 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022619 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 int j;
22621 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022623 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 char_u *name = NULL;
22625 char_u *p;
22626 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022627 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 garray_T newargs;
22629 garray_T newlines;
22630 int varargs = FALSE;
22631 int mustend = FALSE;
22632 int flags = 0;
22633 ufunc_T *fp;
22634 int indent;
22635 int nesting;
22636 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 dictitem_T *v;
22638 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022639 static int func_nr = 0; /* number for nameless function */
22640 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022641 hashtab_T *ht;
22642 int todo;
22643 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022644 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645
22646 /*
22647 * ":function" without argument: list functions.
22648 */
22649 if (ends_excmd(*eap->arg))
22650 {
22651 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022652 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022653 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022654 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022655 {
22656 if (!HASHITEM_EMPTY(hi))
22657 {
22658 --todo;
22659 fp = HI2UF(hi);
22660 if (!isdigit(*fp->uf_name))
22661 list_func_head(fp, FALSE);
22662 }
22663 }
22664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 eap->nextcmd = check_nextcmd(eap->arg);
22666 return;
22667 }
22668
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022669 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022670 * ":function /pat": list functions matching pattern.
22671 */
22672 if (*eap->arg == '/')
22673 {
22674 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22675 if (!eap->skip)
22676 {
22677 regmatch_T regmatch;
22678
22679 c = *p;
22680 *p = NUL;
22681 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22682 *p = c;
22683 if (regmatch.regprog != NULL)
22684 {
22685 regmatch.rm_ic = p_ic;
22686
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022687 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022688 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22689 {
22690 if (!HASHITEM_EMPTY(hi))
22691 {
22692 --todo;
22693 fp = HI2UF(hi);
22694 if (!isdigit(*fp->uf_name)
22695 && vim_regexec(&regmatch, fp->uf_name, 0))
22696 list_func_head(fp, FALSE);
22697 }
22698 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022699 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022700 }
22701 }
22702 if (*p == '/')
22703 ++p;
22704 eap->nextcmd = check_nextcmd(p);
22705 return;
22706 }
22707
22708 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022709 * Get the function name. There are these situations:
22710 * func normal function name
22711 * "name" == func, "fudi.fd_dict" == NULL
22712 * dict.func new dictionary entry
22713 * "name" == NULL, "fudi.fd_dict" set,
22714 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22715 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022716 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022717 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22718 * dict.func existing dict entry that's not a Funcref
22719 * "name" == NULL, "fudi.fd_dict" set,
22720 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022721 * s:func script-local function name
22722 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022725 name = trans_function_name(&p, eap->skip, 0, &fudi);
22726 paren = (vim_strchr(p, '(') != NULL);
22727 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 {
22729 /*
22730 * Return on an invalid expression in braces, unless the expression
22731 * evaluation has been cancelled due to an aborting error, an
22732 * interrupt, or an exception.
22733 */
22734 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022735 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022736 if (!eap->skip && fudi.fd_newkey != NULL)
22737 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022738 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 else
22742 eap->skip = TRUE;
22743 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022744
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 /* An error in a function call during evaluation of an expression in magic
22746 * braces should not cause the function not to be defined. */
22747 saved_did_emsg = did_emsg;
22748 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749
22750 /*
22751 * ":function func" with only function name: list function.
22752 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022753 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 {
22755 if (!ends_excmd(*skipwhite(p)))
22756 {
22757 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022758 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 }
22760 eap->nextcmd = check_nextcmd(p);
22761 if (eap->nextcmd != NULL)
22762 *p = NUL;
22763 if (!eap->skip && !got_int)
22764 {
22765 fp = find_func(name);
22766 if (fp != NULL)
22767 {
22768 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022769 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022771 if (FUNCLINE(fp, j) == NULL)
22772 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 msg_putchar('\n');
22774 msg_outnum((long)(j + 1));
22775 if (j < 9)
22776 msg_putchar(' ');
22777 if (j < 99)
22778 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022779 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 out_flush(); /* show a line at a time */
22781 ui_breakcheck();
22782 }
22783 if (!got_int)
22784 {
22785 msg_putchar('\n');
22786 msg_puts((char_u *)" endfunction");
22787 }
22788 }
22789 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022790 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022792 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 }
22794
22795 /*
22796 * ":function name(arg1, arg2)" Define function.
22797 */
22798 p = skipwhite(p);
22799 if (*p != '(')
22800 {
22801 if (!eap->skip)
22802 {
22803 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022804 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 }
22806 /* attempt to continue by skipping some text */
22807 if (vim_strchr(p, '(') != NULL)
22808 p = vim_strchr(p, '(');
22809 }
22810 p = skipwhite(p + 1);
22811
22812 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22813 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22814
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022815 if (!eap->skip)
22816 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022817 /* Check the name of the function. Unless it's a dictionary function
22818 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022819 if (name != NULL)
22820 arg = name;
22821 else
22822 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022823 if (arg != NULL && (fudi.fd_di == NULL
22824 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022825 {
22826 if (*arg == K_SPECIAL)
22827 j = 3;
22828 else
22829 j = 0;
22830 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22831 : eval_isnamec(arg[j])))
22832 ++j;
22833 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022834 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022835 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022836 /* Disallow using the g: dict. */
22837 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22838 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022839 }
22840
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 /*
22842 * Isolate the arguments: "arg1, arg2, ...)"
22843 */
22844 while (*p != ')')
22845 {
22846 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22847 {
22848 varargs = TRUE;
22849 p += 3;
22850 mustend = TRUE;
22851 }
22852 else
22853 {
22854 arg = p;
22855 while (ASCII_ISALNUM(*p) || *p == '_')
22856 ++p;
22857 if (arg == p || isdigit(*arg)
22858 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22859 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22860 {
22861 if (!eap->skip)
22862 EMSG2(_("E125: Illegal argument: %s"), arg);
22863 break;
22864 }
22865 if (ga_grow(&newargs, 1) == FAIL)
22866 goto erret;
22867 c = *p;
22868 *p = NUL;
22869 arg = vim_strsave(arg);
22870 if (arg == NULL)
22871 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022872
22873 /* Check for duplicate argument name. */
22874 for (i = 0; i < newargs.ga_len; ++i)
22875 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22876 {
22877 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022878 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022879 goto erret;
22880 }
22881
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22883 *p = c;
22884 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 if (*p == ',')
22886 ++p;
22887 else
22888 mustend = TRUE;
22889 }
22890 p = skipwhite(p);
22891 if (mustend && *p != ')')
22892 {
22893 if (!eap->skip)
22894 EMSG2(_(e_invarg2), eap->arg);
22895 break;
22896 }
22897 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022898 if (*p != ')')
22899 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 ++p; /* skip the ')' */
22901
Bram Moolenaare9a41262005-01-15 22:18:47 +000022902 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 for (;;)
22904 {
22905 p = skipwhite(p);
22906 if (STRNCMP(p, "range", 5) == 0)
22907 {
22908 flags |= FC_RANGE;
22909 p += 5;
22910 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022911 else if (STRNCMP(p, "dict", 4) == 0)
22912 {
22913 flags |= FC_DICT;
22914 p += 4;
22915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 else if (STRNCMP(p, "abort", 5) == 0)
22917 {
22918 flags |= FC_ABORT;
22919 p += 5;
22920 }
22921 else
22922 break;
22923 }
22924
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022925 /* When there is a line break use what follows for the function body.
22926 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22927 if (*p == '\n')
22928 line_arg = p + 1;
22929 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930 EMSG(_(e_trailing));
22931
22932 /*
22933 * Read the body of the function, until ":endfunction" is found.
22934 */
22935 if (KeyTyped)
22936 {
22937 /* Check if the function already exists, don't let the user type the
22938 * whole function before telling him it doesn't work! For a script we
22939 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022940 if (!eap->skip && !eap->forceit)
22941 {
22942 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22943 EMSG(_(e_funcdict));
22944 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022945 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022948 if (!eap->skip && did_emsg)
22949 goto erret;
22950
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 msg_putchar('\n'); /* don't overwrite the function name */
22952 cmdline_row = msg_row;
22953 }
22954
22955 indent = 2;
22956 nesting = 0;
22957 for (;;)
22958 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022959 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022960 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022961 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022962 saved_wait_return = FALSE;
22963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022965 sourcing_lnum_off = sourcing_lnum;
22966
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022967 if (line_arg != NULL)
22968 {
22969 /* Use eap->arg, split up in parts by line breaks. */
22970 theline = line_arg;
22971 p = vim_strchr(theline, '\n');
22972 if (p == NULL)
22973 line_arg += STRLEN(line_arg);
22974 else
22975 {
22976 *p = NUL;
22977 line_arg = p + 1;
22978 }
22979 }
22980 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 theline = getcmdline(':', 0L, indent);
22982 else
22983 theline = eap->getline(':', eap->cookie, indent);
22984 if (KeyTyped)
22985 lines_left = Rows - 1;
22986 if (theline == NULL)
22987 {
22988 EMSG(_("E126: Missing :endfunction"));
22989 goto erret;
22990 }
22991
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022992 /* Detect line continuation: sourcing_lnum increased more than one. */
22993 if (sourcing_lnum > sourcing_lnum_off + 1)
22994 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22995 else
22996 sourcing_lnum_off = 0;
22997
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 if (skip_until != NULL)
22999 {
23000 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23001 * don't check for ":endfunc". */
23002 if (STRCMP(theline, skip_until) == 0)
23003 {
23004 vim_free(skip_until);
23005 skip_until = NULL;
23006 }
23007 }
23008 else
23009 {
23010 /* skip ':' and blanks*/
23011 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23012 ;
23013
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023014 /* Check for "endfunction". */
23015 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023017 if (line_arg == NULL)
23018 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 break;
23020 }
23021
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023022 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 * at "end". */
23024 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23025 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023026 else if (STRNCMP(p, "if", 2) == 0
23027 || STRNCMP(p, "wh", 2) == 0
23028 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 || STRNCMP(p, "try", 3) == 0)
23030 indent += 2;
23031
23032 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023033 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023035 if (*p == '!')
23036 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023038 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23039 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023041 ++nesting;
23042 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 }
23044 }
23045
23046 /* Check for ":append" or ":insert". */
23047 p = skip_range(p, NULL);
23048 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23049 || (p[0] == 'i'
23050 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23051 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23052 skip_until = vim_strsave((char_u *)".");
23053
23054 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23055 arg = skipwhite(skiptowhite(p));
23056 if (arg[0] == '<' && arg[1] =='<'
23057 && ((p[0] == 'p' && p[1] == 'y'
23058 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23059 || (p[0] == 'p' && p[1] == 'e'
23060 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23061 || (p[0] == 't' && p[1] == 'c'
23062 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023063 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23064 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23066 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023067 || (p[0] == 'm' && p[1] == 'z'
23068 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069 ))
23070 {
23071 /* ":python <<" continues until a dot, like ":append" */
23072 p = skipwhite(arg + 2);
23073 if (*p == NUL)
23074 skip_until = vim_strsave((char_u *)".");
23075 else
23076 skip_until = vim_strsave(p);
23077 }
23078 }
23079
23080 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023081 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023082 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023083 if (line_arg == NULL)
23084 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023086 }
23087
23088 /* Copy the line to newly allocated memory. get_one_sourceline()
23089 * allocates 250 bytes per line, this saves 80% on average. The cost
23090 * is an extra alloc/free. */
23091 p = vim_strsave(theline);
23092 if (p != NULL)
23093 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023094 if (line_arg == NULL)
23095 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023096 theline = p;
23097 }
23098
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023099 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23100
23101 /* Add NULL lines for continuation lines, so that the line count is
23102 * equal to the index in the growarray. */
23103 while (sourcing_lnum_off-- > 0)
23104 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023105
23106 /* Check for end of eap->arg. */
23107 if (line_arg != NULL && *line_arg == NUL)
23108 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109 }
23110
23111 /* Don't define the function when skipping commands or when an error was
23112 * detected. */
23113 if (eap->skip || did_emsg)
23114 goto erret;
23115
23116 /*
23117 * If there are no errors, add the function
23118 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023119 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023120 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023121 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023122 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023123 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023124 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023125 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023126 goto erret;
23127 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023128
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023129 fp = find_func(name);
23130 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023132 if (!eap->forceit)
23133 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023134 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023135 goto erret;
23136 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023137 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023138 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023139 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023140 name);
23141 goto erret;
23142 }
23143 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023144 ga_clear_strings(&(fp->uf_args));
23145 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023146 vim_free(name);
23147 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 }
23150 else
23151 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023152 char numbuf[20];
23153
23154 fp = NULL;
23155 if (fudi.fd_newkey == NULL && !eap->forceit)
23156 {
23157 EMSG(_(e_funcdict));
23158 goto erret;
23159 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023160 if (fudi.fd_di == NULL)
23161 {
23162 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023163 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023164 goto erret;
23165 }
23166 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023167 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023168 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023169
23170 /* Give the function a sequential number. Can only be used with a
23171 * Funcref! */
23172 vim_free(name);
23173 sprintf(numbuf, "%d", ++func_nr);
23174 name = vim_strsave((char_u *)numbuf);
23175 if (name == NULL)
23176 goto erret;
23177 }
23178
23179 if (fp == NULL)
23180 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023181 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023182 {
23183 int slen, plen;
23184 char_u *scriptname;
23185
23186 /* Check that the autoload name matches the script name. */
23187 j = FAIL;
23188 if (sourcing_name != NULL)
23189 {
23190 scriptname = autoload_name(name);
23191 if (scriptname != NULL)
23192 {
23193 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023194 plen = (int)STRLEN(p);
23195 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023196 if (slen > plen && fnamecmp(p,
23197 sourcing_name + slen - plen) == 0)
23198 j = OK;
23199 vim_free(scriptname);
23200 }
23201 }
23202 if (j == FAIL)
23203 {
23204 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23205 goto erret;
23206 }
23207 }
23208
23209 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 if (fp == NULL)
23211 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023212
23213 if (fudi.fd_dict != NULL)
23214 {
23215 if (fudi.fd_di == NULL)
23216 {
23217 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023218 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023219 if (fudi.fd_di == NULL)
23220 {
23221 vim_free(fp);
23222 goto erret;
23223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023224 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23225 {
23226 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023227 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023228 goto erret;
23229 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023230 }
23231 else
23232 /* overwrite existing dict entry */
23233 clear_tv(&fudi.fd_di->di_tv);
23234 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023235 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023236 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023237 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023238
23239 /* behave like "dict" was used */
23240 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023241 }
23242
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023244 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023245 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23246 {
23247 vim_free(fp);
23248 goto erret;
23249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023251 fp->uf_args = newargs;
23252 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023253#ifdef FEAT_PROFILE
23254 fp->uf_tml_count = NULL;
23255 fp->uf_tml_total = NULL;
23256 fp->uf_tml_self = NULL;
23257 fp->uf_profiling = FALSE;
23258 if (prof_def_func())
23259 func_do_profile(fp);
23260#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023261 fp->uf_varargs = varargs;
23262 fp->uf_flags = flags;
23263 fp->uf_calls = 0;
23264 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023265 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266
23267erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 ga_clear_strings(&newargs);
23269 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023270ret_free:
23271 vim_free(skip_until);
23272 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023275 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276}
23277
23278/*
23279 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023280 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023282 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023283 * TFN_INT: internal function name OK
23284 * TFN_QUIET: be quiet
23285 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 * Advances "pp" to just after the function name (if no error).
23287 */
23288 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023289trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 char_u **pp;
23291 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023292 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023293 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023295 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 char_u *start;
23297 char_u *end;
23298 int lead;
23299 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302
23303 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023304 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023306
23307 /* Check for hard coded <SNR>: already translated function ID (from a user
23308 * command). */
23309 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23310 && (*pp)[2] == (int)KE_SNR)
23311 {
23312 *pp += 3;
23313 len = get_id_len(pp) + 3;
23314 return vim_strnsave(start, len);
23315 }
23316
23317 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23318 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023320 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023322
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023323 /* Note that TFN_ flags use the same values as GLV_ flags. */
23324 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023325 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023326 if (end == start)
23327 {
23328 if (!skip)
23329 EMSG(_("E129: Function name required"));
23330 goto theend;
23331 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023332 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023333 {
23334 /*
23335 * Report an invalid expression in braces, unless the expression
23336 * evaluation has been cancelled due to an aborting error, an
23337 * interrupt, or an exception.
23338 */
23339 if (!aborting())
23340 {
23341 if (end != NULL)
23342 EMSG2(_(e_invarg2), start);
23343 }
23344 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023345 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023346 goto theend;
23347 }
23348
23349 if (lv.ll_tv != NULL)
23350 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023351 if (fdp != NULL)
23352 {
23353 fdp->fd_dict = lv.ll_dict;
23354 fdp->fd_newkey = lv.ll_newkey;
23355 lv.ll_newkey = NULL;
23356 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023357 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023358 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23359 {
23360 name = vim_strsave(lv.ll_tv->vval.v_string);
23361 *pp = end;
23362 }
23363 else
23364 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023365 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23366 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023367 EMSG(_(e_funcref));
23368 else
23369 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023370 name = NULL;
23371 }
23372 goto theend;
23373 }
23374
23375 if (lv.ll_name == NULL)
23376 {
23377 /* Error found, but continue after the function name. */
23378 *pp = end;
23379 goto theend;
23380 }
23381
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023382 /* Check if the name is a Funcref. If so, use the value. */
23383 if (lv.ll_exp_name != NULL)
23384 {
23385 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023386 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023387 if (name == lv.ll_exp_name)
23388 name = NULL;
23389 }
23390 else
23391 {
23392 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023393 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023394 if (name == *pp)
23395 name = NULL;
23396 }
23397 if (name != NULL)
23398 {
23399 name = vim_strsave(name);
23400 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023401 if (STRNCMP(name, "<SNR>", 5) == 0)
23402 {
23403 /* Change "<SNR>" to the byte sequence. */
23404 name[0] = K_SPECIAL;
23405 name[1] = KS_EXTRA;
23406 name[2] = (int)KE_SNR;
23407 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23408 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023409 goto theend;
23410 }
23411
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023412 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023413 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023414 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023415 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23416 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23417 {
23418 /* When there was "s:" already or the name expanded to get a
23419 * leading "s:" then remove it. */
23420 lv.ll_name += 2;
23421 len -= 2;
23422 lead = 2;
23423 }
23424 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023425 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023426 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023427 /* skip over "s:" and "g:" */
23428 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023429 lv.ll_name += 2;
23430 len = (int)(end - lv.ll_name);
23431 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023432
23433 /*
23434 * Copy the function name to allocated memory.
23435 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23436 * Accept <SNR>123_name() outside a script.
23437 */
23438 if (skip)
23439 lead = 0; /* do nothing */
23440 else if (lead > 0)
23441 {
23442 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023443 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23444 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023445 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023446 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023447 if (current_SID <= 0)
23448 {
23449 EMSG(_(e_usingsid));
23450 goto theend;
23451 }
23452 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23453 lead += (int)STRLEN(sid_buf);
23454 }
23455 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023456 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023457 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023458 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023459 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023460 goto theend;
23461 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023462 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023463 {
23464 char_u *cp = vim_strchr(lv.ll_name, ':');
23465
23466 if (cp != NULL && cp < end)
23467 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023468 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023469 goto theend;
23470 }
23471 }
23472
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023473 name = alloc((unsigned)(len + lead + 1));
23474 if (name != NULL)
23475 {
23476 if (lead > 0)
23477 {
23478 name[0] = K_SPECIAL;
23479 name[1] = KS_EXTRA;
23480 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023481 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023482 STRCPY(name + 3, sid_buf);
23483 }
23484 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023485 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023486 }
23487 *pp = end;
23488
23489theend:
23490 clear_lval(&lv);
23491 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492}
23493
23494/*
23495 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23496 * Return 2 if "p" starts with "s:".
23497 * Return 0 otherwise.
23498 */
23499 static int
23500eval_fname_script(p)
23501 char_u *p;
23502{
23503 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23504 || STRNICMP(p + 1, "SNR>", 4) == 0))
23505 return 5;
23506 if (p[0] == 's' && p[1] == ':')
23507 return 2;
23508 return 0;
23509}
23510
23511/*
23512 * Return TRUE if "p" starts with "<SID>" or "s:".
23513 * Only works if eval_fname_script() returned non-zero for "p"!
23514 */
23515 static int
23516eval_fname_sid(p)
23517 char_u *p;
23518{
23519 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23520}
23521
23522/*
23523 * List the head of the function: "name(arg1, arg2)".
23524 */
23525 static void
23526list_func_head(fp, indent)
23527 ufunc_T *fp;
23528 int indent;
23529{
23530 int j;
23531
23532 msg_start();
23533 if (indent)
23534 MSG_PUTS(" ");
23535 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023536 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 {
23538 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023539 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 }
23541 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023542 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023544 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 {
23546 if (j)
23547 MSG_PUTS(", ");
23548 msg_puts(FUNCARG(fp, j));
23549 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023550 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 {
23552 if (j)
23553 MSG_PUTS(", ");
23554 MSG_PUTS("...");
23555 }
23556 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023557 if (fp->uf_flags & FC_ABORT)
23558 MSG_PUTS(" abort");
23559 if (fp->uf_flags & FC_RANGE)
23560 MSG_PUTS(" range");
23561 if (fp->uf_flags & FC_DICT)
23562 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023563 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023564 if (p_verbose > 0)
23565 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566}
23567
23568/*
23569 * Find a function by name, return pointer to it in ufuncs.
23570 * Return NULL for unknown function.
23571 */
23572 static ufunc_T *
23573find_func(name)
23574 char_u *name;
23575{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023576 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023578 hi = hash_find(&func_hashtab, name);
23579 if (!HASHITEM_EMPTY(hi))
23580 return HI2UF(hi);
23581 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582}
23583
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023584#if defined(EXITFREE) || defined(PROTO)
23585 void
23586free_all_functions()
23587{
23588 hashitem_T *hi;
23589
23590 /* Need to start all over every time, because func_free() may change the
23591 * hash table. */
23592 while (func_hashtab.ht_used > 0)
23593 for (hi = func_hashtab.ht_array; ; ++hi)
23594 if (!HASHITEM_EMPTY(hi))
23595 {
23596 func_free(HI2UF(hi));
23597 break;
23598 }
23599}
23600#endif
23601
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023602 int
23603translated_function_exists(name)
23604 char_u *name;
23605{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023606 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023607 return find_internal_func(name) >= 0;
23608 return find_func(name) != NULL;
23609}
23610
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023611/*
23612 * Return TRUE if a function "name" exists.
23613 */
23614 static int
23615function_exists(name)
23616 char_u *name;
23617{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023618 char_u *nm = name;
23619 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023620 int n = FALSE;
23621
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023622 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23623 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023624 nm = skipwhite(nm);
23625
23626 /* Only accept "funcname", "funcname ", "funcname (..." and
23627 * "funcname(...", not "funcname!...". */
23628 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023629 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023630 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023631 return n;
23632}
23633
Bram Moolenaara1544c02013-05-30 12:35:52 +020023634 char_u *
23635get_expanded_name(name, check)
23636 char_u *name;
23637 int check;
23638{
23639 char_u *nm = name;
23640 char_u *p;
23641
23642 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23643
23644 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023645 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023646 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023647
Bram Moolenaara1544c02013-05-30 12:35:52 +020023648 vim_free(p);
23649 return NULL;
23650}
23651
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023652/*
23653 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023654 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23655 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023656 */
23657 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023658builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023659 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023660 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023661{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023662 char_u *p;
23663
23664 if (!ASCII_ISLOWER(name[0]))
23665 return FALSE;
23666 p = vim_strchr(name, AUTOLOAD_CHAR);
23667 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023668}
23669
Bram Moolenaar05159a02005-02-26 23:04:13 +000023670#if defined(FEAT_PROFILE) || defined(PROTO)
23671/*
23672 * Start profiling function "fp".
23673 */
23674 static void
23675func_do_profile(fp)
23676 ufunc_T *fp;
23677{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023678 int len = fp->uf_lines.ga_len;
23679
23680 if (len == 0)
23681 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023682 fp->uf_tm_count = 0;
23683 profile_zero(&fp->uf_tm_self);
23684 profile_zero(&fp->uf_tm_total);
23685 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023686 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023687 if (fp->uf_tml_total == NULL)
23688 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023689 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023690 if (fp->uf_tml_self == NULL)
23691 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023692 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023693 fp->uf_tml_idx = -1;
23694 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23695 || fp->uf_tml_self == NULL)
23696 return; /* out of memory */
23697
23698 fp->uf_profiling = TRUE;
23699}
23700
23701/*
23702 * Dump the profiling results for all functions in file "fd".
23703 */
23704 void
23705func_dump_profile(fd)
23706 FILE *fd;
23707{
23708 hashitem_T *hi;
23709 int todo;
23710 ufunc_T *fp;
23711 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023712 ufunc_T **sorttab;
23713 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023714
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023715 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023716 if (todo == 0)
23717 return; /* nothing to dump */
23718
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023719 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023720
Bram Moolenaar05159a02005-02-26 23:04:13 +000023721 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23722 {
23723 if (!HASHITEM_EMPTY(hi))
23724 {
23725 --todo;
23726 fp = HI2UF(hi);
23727 if (fp->uf_profiling)
23728 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023729 if (sorttab != NULL)
23730 sorttab[st_len++] = fp;
23731
Bram Moolenaar05159a02005-02-26 23:04:13 +000023732 if (fp->uf_name[0] == K_SPECIAL)
23733 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23734 else
23735 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23736 if (fp->uf_tm_count == 1)
23737 fprintf(fd, "Called 1 time\n");
23738 else
23739 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23740 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23741 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23742 fprintf(fd, "\n");
23743 fprintf(fd, "count total (s) self (s)\n");
23744
23745 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23746 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023747 if (FUNCLINE(fp, i) == NULL)
23748 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023749 prof_func_line(fd, fp->uf_tml_count[i],
23750 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023751 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23752 }
23753 fprintf(fd, "\n");
23754 }
23755 }
23756 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023757
23758 if (sorttab != NULL && st_len > 0)
23759 {
23760 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23761 prof_total_cmp);
23762 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23763 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23764 prof_self_cmp);
23765 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23766 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023767
23768 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023769}
Bram Moolenaar73830342005-02-28 22:48:19 +000023770
23771 static void
23772prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23773 FILE *fd;
23774 ufunc_T **sorttab;
23775 int st_len;
23776 char *title;
23777 int prefer_self; /* when equal print only self time */
23778{
23779 int i;
23780 ufunc_T *fp;
23781
23782 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23783 fprintf(fd, "count total (s) self (s) function\n");
23784 for (i = 0; i < 20 && i < st_len; ++i)
23785 {
23786 fp = sorttab[i];
23787 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23788 prefer_self);
23789 if (fp->uf_name[0] == K_SPECIAL)
23790 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23791 else
23792 fprintf(fd, " %s()\n", fp->uf_name);
23793 }
23794 fprintf(fd, "\n");
23795}
23796
23797/*
23798 * Print the count and times for one function or function line.
23799 */
23800 static void
23801prof_func_line(fd, count, total, self, prefer_self)
23802 FILE *fd;
23803 int count;
23804 proftime_T *total;
23805 proftime_T *self;
23806 int prefer_self; /* when equal print only self time */
23807{
23808 if (count > 0)
23809 {
23810 fprintf(fd, "%5d ", count);
23811 if (prefer_self && profile_equal(total, self))
23812 fprintf(fd, " ");
23813 else
23814 fprintf(fd, "%s ", profile_msg(total));
23815 if (!prefer_self && profile_equal(total, self))
23816 fprintf(fd, " ");
23817 else
23818 fprintf(fd, "%s ", profile_msg(self));
23819 }
23820 else
23821 fprintf(fd, " ");
23822}
23823
23824/*
23825 * Compare function for total time sorting.
23826 */
23827 static int
23828#ifdef __BORLANDC__
23829_RTLENTRYF
23830#endif
23831prof_total_cmp(s1, s2)
23832 const void *s1;
23833 const void *s2;
23834{
23835 ufunc_T *p1, *p2;
23836
23837 p1 = *(ufunc_T **)s1;
23838 p2 = *(ufunc_T **)s2;
23839 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23840}
23841
23842/*
23843 * Compare function for self time sorting.
23844 */
23845 static int
23846#ifdef __BORLANDC__
23847_RTLENTRYF
23848#endif
23849prof_self_cmp(s1, s2)
23850 const void *s1;
23851 const void *s2;
23852{
23853 ufunc_T *p1, *p2;
23854
23855 p1 = *(ufunc_T **)s1;
23856 p2 = *(ufunc_T **)s2;
23857 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23858}
23859
Bram Moolenaar05159a02005-02-26 23:04:13 +000023860#endif
23861
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023862/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023863 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023864 * Return TRUE if a package was loaded.
23865 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023866 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023867script_autoload(name, reload)
23868 char_u *name;
23869 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023870{
23871 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023872 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023873 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023874 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023875
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023876 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023877 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023878 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023879 return FALSE;
23880
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023881 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023882
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023883 /* Find the name in the list of previously loaded package names. Skip
23884 * "autoload/", it's always the same. */
23885 for (i = 0; i < ga_loaded.ga_len; ++i)
23886 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23887 break;
23888 if (!reload && i < ga_loaded.ga_len)
23889 ret = FALSE; /* was loaded already */
23890 else
23891 {
23892 /* Remember the name if it wasn't loaded already. */
23893 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23894 {
23895 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23896 tofree = NULL;
23897 }
23898
23899 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023900 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023901 ret = TRUE;
23902 }
23903
23904 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023905 return ret;
23906}
23907
23908/*
23909 * Return the autoload script name for a function or variable name.
23910 * Returns NULL when out of memory.
23911 */
23912 static char_u *
23913autoload_name(name)
23914 char_u *name;
23915{
23916 char_u *p;
23917 char_u *scriptname;
23918
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023919 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023920 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23921 if (scriptname == NULL)
23922 return FALSE;
23923 STRCPY(scriptname, "autoload/");
23924 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023925 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023926 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023927 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023928 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023929 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023930}
23931
Bram Moolenaar071d4272004-06-13 20:20:40 +000023932#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23933
23934/*
23935 * Function given to ExpandGeneric() to obtain the list of user defined
23936 * function names.
23937 */
23938 char_u *
23939get_user_func_name(xp, idx)
23940 expand_T *xp;
23941 int idx;
23942{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023943 static long_u done;
23944 static hashitem_T *hi;
23945 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946
23947 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023949 done = 0;
23950 hi = func_hashtab.ht_array;
23951 }
23952 if (done < func_hashtab.ht_used)
23953 {
23954 if (done++ > 0)
23955 ++hi;
23956 while (HASHITEM_EMPTY(hi))
23957 ++hi;
23958 fp = HI2UF(hi);
23959
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023960 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023961 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023962
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023963 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23964 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965
23966 cat_func_name(IObuff, fp);
23967 if (xp->xp_context != EXPAND_USER_FUNC)
23968 {
23969 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023970 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971 STRCAT(IObuff, ")");
23972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 return IObuff;
23974 }
23975 return NULL;
23976}
23977
23978#endif /* FEAT_CMDL_COMPL */
23979
23980/*
23981 * Copy the function name of "fp" to buffer "buf".
23982 * "buf" must be able to hold the function name plus three bytes.
23983 * Takes care of script-local function names.
23984 */
23985 static void
23986cat_func_name(buf, fp)
23987 char_u *buf;
23988 ufunc_T *fp;
23989{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023990 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 {
23992 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023993 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 }
23995 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023996 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997}
23998
23999/*
24000 * ":delfunction {name}"
24001 */
24002 void
24003ex_delfunction(eap)
24004 exarg_T *eap;
24005{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024006 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007 char_u *p;
24008 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024009 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010
24011 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012 name = trans_function_name(&p, eap->skip, 0, &fudi);
24013 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024015 {
24016 if (fudi.fd_dict != NULL && !eap->skip)
24017 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 if (!ends_excmd(*skipwhite(p)))
24021 {
24022 vim_free(name);
24023 EMSG(_(e_trailing));
24024 return;
24025 }
24026 eap->nextcmd = check_nextcmd(p);
24027 if (eap->nextcmd != NULL)
24028 *p = NUL;
24029
24030 if (!eap->skip)
24031 fp = find_func(name);
24032 vim_free(name);
24033
24034 if (!eap->skip)
24035 {
24036 if (fp == NULL)
24037 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024038 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 return;
24040 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024041 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042 {
24043 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24044 return;
24045 }
24046
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024047 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024049 /* Delete the dict item that refers to the function, it will
24050 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024051 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024053 else
24054 func_free(fp);
24055 }
24056}
24057
24058/*
24059 * Free a function and remove it from the list of functions.
24060 */
24061 static void
24062func_free(fp)
24063 ufunc_T *fp;
24064{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024065 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024066
24067 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024068 ga_clear_strings(&(fp->uf_args));
24069 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024070#ifdef FEAT_PROFILE
24071 vim_free(fp->uf_tml_count);
24072 vim_free(fp->uf_tml_total);
24073 vim_free(fp->uf_tml_self);
24074#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024076 /* remove the function from the function hashtable */
24077 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24078 if (HASHITEM_EMPTY(hi))
24079 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024080 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024081 hash_remove(&func_hashtab, hi);
24082
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024083 vim_free(fp);
24084}
24085
24086/*
24087 * Unreference a Function: decrement the reference count and free it when it
24088 * becomes zero. Only for numbered functions.
24089 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024090 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024091func_unref(name)
24092 char_u *name;
24093{
24094 ufunc_T *fp;
24095
24096 if (name != NULL && isdigit(*name))
24097 {
24098 fp = find_func(name);
24099 if (fp == NULL)
24100 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024101 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024102 {
24103 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024104 * when "uf_calls" becomes zero. */
24105 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024106 func_free(fp);
24107 }
24108 }
24109}
24110
24111/*
24112 * Count a reference to a Function.
24113 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024114 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024115func_ref(name)
24116 char_u *name;
24117{
24118 ufunc_T *fp;
24119
24120 if (name != NULL && isdigit(*name))
24121 {
24122 fp = find_func(name);
24123 if (fp == NULL)
24124 EMSG2(_(e_intern2), "func_ref()");
24125 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024126 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127 }
24128}
24129
24130/*
24131 * Call a user function.
24132 */
24133 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024134call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 ufunc_T *fp; /* pointer to function */
24136 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024137 typval_T *argvars; /* arguments */
24138 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139 linenr_T firstline; /* first line of range */
24140 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024141 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142{
Bram Moolenaar33570922005-01-25 22:26:29 +000024143 char_u *save_sourcing_name;
24144 linenr_T save_sourcing_lnum;
24145 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024146 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024147 int save_did_emsg;
24148 static int depth = 0;
24149 dictitem_T *v;
24150 int fixvar_idx = 0; /* index in fixvar[] */
24151 int i;
24152 int ai;
24153 char_u numbuf[NUMBUFLEN];
24154 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024155 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024156#ifdef FEAT_PROFILE
24157 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024158 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160
24161 /* If depth of calling is getting too high, don't execute the function */
24162 if (depth >= p_mfd)
24163 {
24164 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024165 rettv->v_type = VAR_NUMBER;
24166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 return;
24168 }
24169 ++depth;
24170
24171 line_breakcheck(); /* check for CTRL-C hit */
24172
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024173 fc = (funccall_T *)alloc(sizeof(funccall_T));
24174 fc->caller = current_funccal;
24175 current_funccal = fc;
24176 fc->func = fp;
24177 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024178 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024179 fc->linenr = 0;
24180 fc->returned = FALSE;
24181 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024183 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24184 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185
Bram Moolenaar33570922005-01-25 22:26:29 +000024186 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024187 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024188 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24189 * each argument variable and saves a lot of time.
24190 */
24191 /*
24192 * Init l: variables.
24193 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024194 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024195 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024196 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024197 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24198 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024199 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024200 name = v->di_key;
24201 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024202 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024203 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024204 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024205 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024206 v->di_tv.vval.v_dict = selfdict;
24207 ++selfdict->dv_refcount;
24208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024209
Bram Moolenaar33570922005-01-25 22:26:29 +000024210 /*
24211 * Init a: variables.
24212 * Set a:0 to "argcount".
24213 * Set a:000 to a list with room for the "..." arguments.
24214 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024215 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024216 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024217 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024218 /* Use "name" to avoid a warning from some compiler that checks the
24219 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024220 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024221 name = v->di_key;
24222 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024223 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024224 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024225 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024226 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024227 v->di_tv.vval.v_list = &fc->l_varlist;
24228 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24229 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24230 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024231
24232 /*
24233 * Set a:firstline to "firstline" and a:lastline to "lastline".
24234 * Set a:name to named arguments.
24235 * Set a:N to the "..." arguments.
24236 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024237 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024238 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024239 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024240 (varnumber_T)lastline);
24241 for (i = 0; i < argcount; ++i)
24242 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024243 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024244 if (ai < 0)
24245 /* named argument a:name */
24246 name = FUNCARG(fp, i);
24247 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024248 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024249 /* "..." argument a:1, a:2, etc. */
24250 sprintf((char *)numbuf, "%d", ai + 1);
24251 name = numbuf;
24252 }
24253 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24254 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024255 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024256 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24257 }
24258 else
24259 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024260 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24261 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024262 if (v == NULL)
24263 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024264 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024265 }
24266 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024267 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024268
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024269 /* Note: the values are copied directly to avoid alloc/free.
24270 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024271 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024272 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024273
24274 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24275 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024276 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24277 fc->l_listitems[ai].li_tv = argvars[i];
24278 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024279 }
24280 }
24281
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 /* Don't redraw while executing the function. */
24283 ++RedrawingDisabled;
24284 save_sourcing_name = sourcing_name;
24285 save_sourcing_lnum = sourcing_lnum;
24286 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024287 /* need space for function name + ("function " + 3) or "[number]" */
24288 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24289 + STRLEN(fp->uf_name) + 20;
24290 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 if (sourcing_name != NULL)
24292 {
24293 if (save_sourcing_name != NULL
24294 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024295 sprintf((char *)sourcing_name, "%s[%d]..",
24296 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 else
24298 STRCPY(sourcing_name, "function ");
24299 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24300
24301 if (p_verbose >= 12)
24302 {
24303 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024304 verbose_enter_scroll();
24305
Bram Moolenaar555b2802005-05-19 21:08:39 +000024306 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 if (p_verbose >= 14)
24308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024310 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024311 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024312 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313
24314 msg_puts((char_u *)"(");
24315 for (i = 0; i < argcount; ++i)
24316 {
24317 if (i > 0)
24318 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024319 if (argvars[i].v_type == VAR_NUMBER)
24320 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 else
24322 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024323 /* Do not want errors such as E724 here. */
24324 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024325 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024326 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024327 if (s != NULL)
24328 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024329 if (vim_strsize(s) > MSG_BUF_CLEN)
24330 {
24331 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24332 s = buf;
24333 }
24334 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024335 vim_free(tofree);
24336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337 }
24338 }
24339 msg_puts((char_u *)")");
24340 }
24341 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024342
24343 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344 --no_wait_return;
24345 }
24346 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024347#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024348 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024349 {
24350 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24351 func_do_profile(fp);
24352 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024353 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024354 {
24355 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024356 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024357 profile_zero(&fp->uf_tm_children);
24358 }
24359 script_prof_save(&wait_start);
24360 }
24361#endif
24362
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024364 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 save_did_emsg = did_emsg;
24366 did_emsg = FALSE;
24367
24368 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024369 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24371
24372 --RedrawingDisabled;
24373
24374 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024375 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024377 clear_tv(rettv);
24378 rettv->v_type = VAR_NUMBER;
24379 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 }
24381
Bram Moolenaar05159a02005-02-26 23:04:13 +000024382#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024383 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024384 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024385 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024386 profile_end(&call_start);
24387 profile_sub_wait(&wait_start, &call_start);
24388 profile_add(&fp->uf_tm_total, &call_start);
24389 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024390 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024391 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024392 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24393 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024394 }
24395 }
24396#endif
24397
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 /* when being verbose, mention the return value */
24399 if (p_verbose >= 12)
24400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024402 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024405 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024406 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024407 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024408 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024409 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024411 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024412 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024413 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024414 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024415
Bram Moolenaar555b2802005-05-19 21:08:39 +000024416 /* The value may be very long. Skip the middle part, so that we
24417 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024418 * truncate it at the end. Don't want errors such as E724 here. */
24419 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024420 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024421 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024422 if (s != NULL)
24423 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024424 if (vim_strsize(s) > MSG_BUF_CLEN)
24425 {
24426 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24427 s = buf;
24428 }
24429 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024430 vim_free(tofree);
24431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024432 }
24433 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024434
24435 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 --no_wait_return;
24437 }
24438
24439 vim_free(sourcing_name);
24440 sourcing_name = save_sourcing_name;
24441 sourcing_lnum = save_sourcing_lnum;
24442 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024444 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024445 script_prof_restore(&wait_start);
24446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447
24448 if (p_verbose >= 12 && sourcing_name != NULL)
24449 {
24450 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024451 verbose_enter_scroll();
24452
Bram Moolenaar555b2802005-05-19 21:08:39 +000024453 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024455
24456 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457 --no_wait_return;
24458 }
24459
24460 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024461 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024463
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024464 /* If the a:000 list and the l: and a: dicts are not referenced we can
24465 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024466 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24467 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24468 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24469 {
24470 free_funccal(fc, FALSE);
24471 }
24472 else
24473 {
24474 hashitem_T *hi;
24475 listitem_T *li;
24476 int todo;
24477
24478 /* "fc" is still in use. This can happen when returning "a:000" or
24479 * assigning "l:" to a global variable.
24480 * Link "fc" in the list for garbage collection later. */
24481 fc->caller = previous_funccal;
24482 previous_funccal = fc;
24483
24484 /* Make a copy of the a: variables, since we didn't do that above. */
24485 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24486 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24487 {
24488 if (!HASHITEM_EMPTY(hi))
24489 {
24490 --todo;
24491 v = HI2DI(hi);
24492 copy_tv(&v->di_tv, &v->di_tv);
24493 }
24494 }
24495
24496 /* Make a copy of the a:000 items, since we didn't do that above. */
24497 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24498 copy_tv(&li->li_tv, &li->li_tv);
24499 }
24500}
24501
24502/*
24503 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024504 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024505 */
24506 static int
24507can_free_funccal(fc, copyID)
24508 funccall_T *fc;
24509 int copyID;
24510{
24511 return (fc->l_varlist.lv_copyID != copyID
24512 && fc->l_vars.dv_copyID != copyID
24513 && fc->l_avars.dv_copyID != copyID);
24514}
24515
24516/*
24517 * Free "fc" and what it contains.
24518 */
24519 static void
24520free_funccal(fc, free_val)
24521 funccall_T *fc;
24522 int free_val; /* a: vars were allocated */
24523{
24524 listitem_T *li;
24525
24526 /* The a: variables typevals may not have been allocated, only free the
24527 * allocated variables. */
24528 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24529
24530 /* free all l: variables */
24531 vars_clear(&fc->l_vars.dv_hashtab);
24532
24533 /* Free the a:000 variables if they were allocated. */
24534 if (free_val)
24535 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24536 clear_tv(&li->li_tv);
24537
24538 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539}
24540
24541/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024542 * Add a number variable "name" to dict "dp" with value "nr".
24543 */
24544 static void
24545add_nr_var(dp, v, name, nr)
24546 dict_T *dp;
24547 dictitem_T *v;
24548 char *name;
24549 varnumber_T nr;
24550{
24551 STRCPY(v->di_key, name);
24552 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24553 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24554 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024555 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024556 v->di_tv.vval.v_number = nr;
24557}
24558
24559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560 * ":return [expr]"
24561 */
24562 void
24563ex_return(eap)
24564 exarg_T *eap;
24565{
24566 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024567 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024568 int returning = FALSE;
24569
24570 if (current_funccal == NULL)
24571 {
24572 EMSG(_("E133: :return not inside a function"));
24573 return;
24574 }
24575
24576 if (eap->skip)
24577 ++emsg_skip;
24578
24579 eap->nextcmd = NULL;
24580 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024581 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582 {
24583 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024584 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024586 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587 }
24588 /* It's safer to return also on error. */
24589 else if (!eap->skip)
24590 {
24591 /*
24592 * Return unless the expression evaluation has been cancelled due to an
24593 * aborting error, an interrupt, or an exception.
24594 */
24595 if (!aborting())
24596 returning = do_return(eap, FALSE, TRUE, NULL);
24597 }
24598
24599 /* When skipping or the return gets pending, advance to the next command
24600 * in this line (!returning). Otherwise, ignore the rest of the line.
24601 * Following lines will be ignored by get_func_line(). */
24602 if (returning)
24603 eap->nextcmd = NULL;
24604 else if (eap->nextcmd == NULL) /* no argument */
24605 eap->nextcmd = check_nextcmd(arg);
24606
24607 if (eap->skip)
24608 --emsg_skip;
24609}
24610
24611/*
24612 * Return from a function. Possibly makes the return pending. Also called
24613 * for a pending return at the ":endtry" or after returning from an extra
24614 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024615 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024616 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 * FALSE when the return gets pending.
24618 */
24619 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024620do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 exarg_T *eap;
24622 int reanimate;
24623 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024624 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625{
24626 int idx;
24627 struct condstack *cstack = eap->cstack;
24628
24629 if (reanimate)
24630 /* Undo the return. */
24631 current_funccal->returned = FALSE;
24632
24633 /*
24634 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24635 * not in its finally clause (which then is to be executed next) is found.
24636 * In this case, make the ":return" pending for execution at the ":endtry".
24637 * Otherwise, return normally.
24638 */
24639 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24640 if (idx >= 0)
24641 {
24642 cstack->cs_pending[idx] = CSTP_RETURN;
24643
24644 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024645 /* A pending return again gets pending. "rettv" points to an
24646 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024648 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649 else
24650 {
24651 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024652 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024654 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024656 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657 {
24658 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024659 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024660 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 else
24662 EMSG(_(e_outofmem));
24663 }
24664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024665 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024666
24667 if (reanimate)
24668 {
24669 /* The pending return value could be overwritten by a ":return"
24670 * without argument in a finally clause; reset the default
24671 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024672 current_funccal->rettv->v_type = VAR_NUMBER;
24673 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674 }
24675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024676 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 }
24678 else
24679 {
24680 current_funccal->returned = TRUE;
24681
24682 /* If the return is carried out now, store the return value. For
24683 * a return immediately after reanimation, the value is already
24684 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024685 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024687 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024688 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024690 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691 }
24692 }
24693
24694 return idx < 0;
24695}
24696
24697/*
24698 * Free the variable with a pending return value.
24699 */
24700 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024701discard_pending_return(rettv)
24702 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703{
Bram Moolenaar33570922005-01-25 22:26:29 +000024704 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705}
24706
24707/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024708 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709 * is an allocated string. Used by report_pending() for verbose messages.
24710 */
24711 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024712get_return_cmd(rettv)
24713 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024715 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024716 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024717 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024719 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024720 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024721 if (s == NULL)
24722 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024723
24724 STRCPY(IObuff, ":return ");
24725 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24726 if (STRLEN(s) + 8 >= IOSIZE)
24727 STRCPY(IObuff + IOSIZE - 4, "...");
24728 vim_free(tofree);
24729 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730}
24731
24732/*
24733 * Get next function line.
24734 * Called by do_cmdline() to get the next line.
24735 * Returns allocated string, or NULL for end of function.
24736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737 char_u *
24738get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024739 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024741 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742{
Bram Moolenaar33570922005-01-25 22:26:29 +000024743 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024744 ufunc_T *fp = fcp->func;
24745 char_u *retval;
24746 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747
24748 /* If breakpoints have been added/deleted need to check for it. */
24749 if (fcp->dbg_tick != debug_tick)
24750 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024751 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024752 sourcing_lnum);
24753 fcp->dbg_tick = debug_tick;
24754 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024755#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024756 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024757 func_line_end(cookie);
24758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759
Bram Moolenaar05159a02005-02-26 23:04:13 +000024760 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024761 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24762 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 retval = NULL;
24764 else
24765 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024766 /* Skip NULL lines (continuation lines). */
24767 while (fcp->linenr < gap->ga_len
24768 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24769 ++fcp->linenr;
24770 if (fcp->linenr >= gap->ga_len)
24771 retval = NULL;
24772 else
24773 {
24774 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24775 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024776#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024777 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024778 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024779#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 }
24782
24783 /* Did we encounter a breakpoint? */
24784 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24785 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024786 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024788 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 sourcing_lnum);
24790 fcp->dbg_tick = debug_tick;
24791 }
24792
24793 return retval;
24794}
24795
Bram Moolenaar05159a02005-02-26 23:04:13 +000024796#if defined(FEAT_PROFILE) || defined(PROTO)
24797/*
24798 * Called when starting to read a function line.
24799 * "sourcing_lnum" must be correct!
24800 * When skipping lines it may not actually be executed, but we won't find out
24801 * until later and we need to store the time now.
24802 */
24803 void
24804func_line_start(cookie)
24805 void *cookie;
24806{
24807 funccall_T *fcp = (funccall_T *)cookie;
24808 ufunc_T *fp = fcp->func;
24809
24810 if (fp->uf_profiling && sourcing_lnum >= 1
24811 && sourcing_lnum <= fp->uf_lines.ga_len)
24812 {
24813 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024814 /* Skip continuation lines. */
24815 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24816 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024817 fp->uf_tml_execed = FALSE;
24818 profile_start(&fp->uf_tml_start);
24819 profile_zero(&fp->uf_tml_children);
24820 profile_get_wait(&fp->uf_tml_wait);
24821 }
24822}
24823
24824/*
24825 * Called when actually executing a function line.
24826 */
24827 void
24828func_line_exec(cookie)
24829 void *cookie;
24830{
24831 funccall_T *fcp = (funccall_T *)cookie;
24832 ufunc_T *fp = fcp->func;
24833
24834 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24835 fp->uf_tml_execed = TRUE;
24836}
24837
24838/*
24839 * Called when done with a function line.
24840 */
24841 void
24842func_line_end(cookie)
24843 void *cookie;
24844{
24845 funccall_T *fcp = (funccall_T *)cookie;
24846 ufunc_T *fp = fcp->func;
24847
24848 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24849 {
24850 if (fp->uf_tml_execed)
24851 {
24852 ++fp->uf_tml_count[fp->uf_tml_idx];
24853 profile_end(&fp->uf_tml_start);
24854 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024855 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024856 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24857 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024858 }
24859 fp->uf_tml_idx = -1;
24860 }
24861}
24862#endif
24863
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864/*
24865 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024866 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867 */
24868 int
24869func_has_ended(cookie)
24870 void *cookie;
24871{
Bram Moolenaar33570922005-01-25 22:26:29 +000024872 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873
24874 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24875 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024876 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877 || fcp->returned);
24878}
24879
24880/*
24881 * return TRUE if cookie indicates a function which "abort"s on errors.
24882 */
24883 int
24884func_has_abort(cookie)
24885 void *cookie;
24886{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024887 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888}
24889
24890#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24891typedef enum
24892{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024893 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24894 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24895 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896} var_flavour_T;
24897
24898static var_flavour_T var_flavour __ARGS((char_u *varname));
24899
24900 static var_flavour_T
24901var_flavour(varname)
24902 char_u *varname;
24903{
24904 char_u *p = varname;
24905
24906 if (ASCII_ISUPPER(*p))
24907 {
24908 while (*(++p))
24909 if (ASCII_ISLOWER(*p))
24910 return VAR_FLAVOUR_SESSION;
24911 return VAR_FLAVOUR_VIMINFO;
24912 }
24913 else
24914 return VAR_FLAVOUR_DEFAULT;
24915}
24916#endif
24917
24918#if defined(FEAT_VIMINFO) || defined(PROTO)
24919/*
24920 * Restore global vars that start with a capital from the viminfo file
24921 */
24922 int
24923read_viminfo_varlist(virp, writing)
24924 vir_T *virp;
24925 int writing;
24926{
24927 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024928 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024929 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930
24931 if (!writing && (find_viminfo_parameter('!') != NULL))
24932 {
24933 tab = vim_strchr(virp->vir_line + 1, '\t');
24934 if (tab != NULL)
24935 {
24936 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024937 switch (*tab)
24938 {
24939 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024940#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024941 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024942#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024943 case 'D': type = VAR_DICT; break;
24944 case 'L': type = VAR_LIST; break;
24945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946
24947 tab = vim_strchr(tab, '\t');
24948 if (tab != NULL)
24949 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024950 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024951 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024952 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024954#ifdef FEAT_FLOAT
24955 else if (type == VAR_FLOAT)
24956 (void)string2float(tab + 1, &tv.vval.v_float);
24957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024959 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024960 if (type == VAR_DICT || type == VAR_LIST)
24961 {
24962 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24963
24964 if (etv == NULL)
24965 /* Failed to parse back the dict or list, use it as a
24966 * string. */
24967 tv.v_type = VAR_STRING;
24968 else
24969 {
24970 vim_free(tv.vval.v_string);
24971 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024972 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024973 }
24974 }
24975
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024976 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024977
24978 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024979 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024980 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24981 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024982 }
24983 }
24984 }
24985
24986 return viminfo_readline(virp);
24987}
24988
24989/*
24990 * Write global vars that start with a capital to the viminfo file
24991 */
24992 void
24993write_viminfo_varlist(fp)
24994 FILE *fp;
24995{
Bram Moolenaar33570922005-01-25 22:26:29 +000024996 hashitem_T *hi;
24997 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024998 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024999 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025000 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025001 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025002 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003
25004 if (find_viminfo_parameter('!') == NULL)
25005 return;
25006
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025007 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025008
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025009 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025010 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025011 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025012 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025014 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025015 this_var = HI2DI(hi);
25016 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025017 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025018 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025019 {
25020 case VAR_STRING: s = "STR"; break;
25021 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025022#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025023 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025024#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025025 case VAR_DICT: s = "DIC"; break;
25026 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025027 default: continue;
25028 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025029 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025030 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025031 if (p != NULL)
25032 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025033 vim_free(tofree);
25034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035 }
25036 }
25037}
25038#endif
25039
25040#if defined(FEAT_SESSION) || defined(PROTO)
25041 int
25042store_session_globals(fd)
25043 FILE *fd;
25044{
Bram Moolenaar33570922005-01-25 22:26:29 +000025045 hashitem_T *hi;
25046 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025047 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025048 char_u *p, *t;
25049
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025050 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025051 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025053 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025055 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025056 this_var = HI2DI(hi);
25057 if ((this_var->di_tv.v_type == VAR_NUMBER
25058 || this_var->di_tv.v_type == VAR_STRING)
25059 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025060 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025061 /* Escape special characters with a backslash. Turn a LF and
25062 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025063 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025064 (char_u *)"\\\"\n\r");
25065 if (p == NULL) /* out of memory */
25066 break;
25067 for (t = p; *t != NUL; ++t)
25068 if (*t == '\n')
25069 *t = 'n';
25070 else if (*t == '\r')
25071 *t = 'r';
25072 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025073 this_var->di_key,
25074 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25075 : ' ',
25076 p,
25077 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25078 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025079 || put_eol(fd) == FAIL)
25080 {
25081 vim_free(p);
25082 return FAIL;
25083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 vim_free(p);
25085 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025086#ifdef FEAT_FLOAT
25087 else if (this_var->di_tv.v_type == VAR_FLOAT
25088 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25089 {
25090 float_T f = this_var->di_tv.vval.v_float;
25091 int sign = ' ';
25092
25093 if (f < 0)
25094 {
25095 f = -f;
25096 sign = '-';
25097 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025098 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025099 this_var->di_key, sign, f) < 0)
25100 || put_eol(fd) == FAIL)
25101 return FAIL;
25102 }
25103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 }
25105 }
25106 return OK;
25107}
25108#endif
25109
Bram Moolenaar661b1822005-07-28 22:36:45 +000025110/*
25111 * Display script name where an item was last set.
25112 * Should only be invoked when 'verbose' is non-zero.
25113 */
25114 void
25115last_set_msg(scriptID)
25116 scid_T scriptID;
25117{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025118 char_u *p;
25119
Bram Moolenaar661b1822005-07-28 22:36:45 +000025120 if (scriptID != 0)
25121 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025122 p = home_replace_save(NULL, get_scriptname(scriptID));
25123 if (p != NULL)
25124 {
25125 verbose_enter();
25126 MSG_PUTS(_("\n\tLast set from "));
25127 MSG_PUTS(p);
25128 vim_free(p);
25129 verbose_leave();
25130 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025131 }
25132}
25133
Bram Moolenaard812df62008-11-09 12:46:09 +000025134/*
25135 * List v:oldfiles in a nice way.
25136 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025137 void
25138ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025139 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025140{
25141 list_T *l = vimvars[VV_OLDFILES].vv_list;
25142 listitem_T *li;
25143 int nr = 0;
25144
25145 if (l == NULL)
25146 msg((char_u *)_("No old files"));
25147 else
25148 {
25149 msg_start();
25150 msg_scroll = TRUE;
25151 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25152 {
25153 msg_outnum((long)++nr);
25154 MSG_PUTS(": ");
25155 msg_outtrans(get_tv_string(&li->li_tv));
25156 msg_putchar('\n');
25157 out_flush(); /* output one line at a time */
25158 ui_breakcheck();
25159 }
25160 /* Assume "got_int" was set to truncate the listing. */
25161 got_int = FALSE;
25162
25163#ifdef FEAT_BROWSE_CMD
25164 if (cmdmod.browse)
25165 {
25166 quit_more = FALSE;
25167 nr = prompt_for_number(FALSE);
25168 msg_starthere();
25169 if (nr > 0)
25170 {
25171 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25172 (long)nr);
25173
25174 if (p != NULL)
25175 {
25176 p = expand_env_save(p);
25177 eap->arg = p;
25178 eap->cmdidx = CMD_edit;
25179 cmdmod.browse = FALSE;
25180 do_exedit(eap, NULL);
25181 vim_free(p);
25182 }
25183 }
25184 }
25185#endif
25186 }
25187}
25188
Bram Moolenaar53744302015-07-17 17:38:22 +020025189/* reset v:option_new, v:option_old and v:option_type */
25190 void
25191reset_v_option_vars()
25192{
25193 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25194 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25195 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25196}
25197
25198
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199#endif /* FEAT_EVAL */
25200
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025202#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203
25204#ifdef WIN3264
25205/*
25206 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25207 */
25208static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25209static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25210static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25211
25212/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025213 * Get the short path (8.3) for the filename in "fnamep".
25214 * Only works for a valid file name.
25215 * When the path gets longer "fnamep" is changed and the allocated buffer
25216 * is put in "bufp".
25217 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25218 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219 */
25220 static int
25221get_short_pathname(fnamep, bufp, fnamelen)
25222 char_u **fnamep;
25223 char_u **bufp;
25224 int *fnamelen;
25225{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025226 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 char_u *newbuf;
25228
25229 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230 l = GetShortPathName(*fnamep, *fnamep, len);
25231 if (l > len - 1)
25232 {
25233 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025234 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 newbuf = vim_strnsave(*fnamep, l);
25236 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025237 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238
25239 vim_free(*bufp);
25240 *fnamep = *bufp = newbuf;
25241
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025242 /* Really should always succeed, as the buffer is big enough. */
25243 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244 }
25245
25246 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025247 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248}
25249
25250/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025251 * Get the short path (8.3) for the filename in "fname". The converted
25252 * path is returned in "bufp".
25253 *
25254 * Some of the directories specified in "fname" may not exist. This function
25255 * will shorten the existing directories at the beginning of the path and then
25256 * append the remaining non-existing path.
25257 *
25258 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025259 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025260 * bufp - Pointer to an allocated buffer for the filename.
25261 * fnamelen - Length of the filename pointed to by fname
25262 *
25263 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 */
25265 static int
25266shortpath_for_invalid_fname(fname, bufp, fnamelen)
25267 char_u **fname;
25268 char_u **bufp;
25269 int *fnamelen;
25270{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025271 char_u *short_fname, *save_fname, *pbuf_unused;
25272 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025274 int old_len, len;
25275 int new_len, sfx_len;
25276 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277
25278 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025279 old_len = *fnamelen;
25280 save_fname = vim_strnsave(*fname, old_len);
25281 pbuf_unused = NULL;
25282 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025284 endp = save_fname + old_len - 1; /* Find the end of the copy */
25285 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025287 /*
25288 * Try shortening the supplied path till it succeeds by removing one
25289 * directory at a time from the tail of the path.
25290 */
25291 len = 0;
25292 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025294 /* go back one path-separator */
25295 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25296 --endp;
25297 if (endp <= save_fname)
25298 break; /* processed the complete path */
25299
25300 /*
25301 * Replace the path separator with a NUL and try to shorten the
25302 * resulting path.
25303 */
25304 ch = *endp;
25305 *endp = 0;
25306 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025307 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025308 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25309 {
25310 retval = FAIL;
25311 goto theend;
25312 }
25313 *endp = ch; /* preserve the string */
25314
25315 if (len > 0)
25316 break; /* successfully shortened the path */
25317
25318 /* failed to shorten the path. Skip the path separator */
25319 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 }
25321
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025322 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025324 /*
25325 * Succeeded in shortening the path. Now concatenate the shortened
25326 * path with the remaining path at the tail.
25327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025328
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025329 /* Compute the length of the new path. */
25330 sfx_len = (int)(save_endp - endp) + 1;
25331 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025333 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025335 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025337 /* There is not enough space in the currently allocated string,
25338 * copy it to a buffer big enough. */
25339 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025341 {
25342 retval = FAIL;
25343 goto theend;
25344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345 }
25346 else
25347 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025348 /* Transfer short_fname to the main buffer (it's big enough),
25349 * unless get_short_pathname() did its work in-place. */
25350 *fname = *bufp = save_fname;
25351 if (short_fname != save_fname)
25352 vim_strncpy(save_fname, short_fname, len);
25353 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025355
25356 /* concat the not-shortened part of the path */
25357 vim_strncpy(*fname + len, endp, sfx_len);
25358 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025360
25361theend:
25362 vim_free(pbuf_unused);
25363 vim_free(save_fname);
25364
25365 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366}
25367
25368/*
25369 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025370 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371 */
25372 static int
25373shortpath_for_partial(fnamep, bufp, fnamelen)
25374 char_u **fnamep;
25375 char_u **bufp;
25376 int *fnamelen;
25377{
25378 int sepcount, len, tflen;
25379 char_u *p;
25380 char_u *pbuf, *tfname;
25381 int hasTilde;
25382
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025383 /* Count up the path separators from the RHS.. so we know which part
25384 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025386 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387 if (vim_ispathsep(*p))
25388 ++sepcount;
25389
25390 /* Need full path first (use expand_env() to remove a "~/") */
25391 hasTilde = (**fnamep == '~');
25392 if (hasTilde)
25393 pbuf = tfname = expand_env_save(*fnamep);
25394 else
25395 pbuf = tfname = FullName_save(*fnamep, FALSE);
25396
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025397 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025399 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401
25402 if (len == 0)
25403 {
25404 /* Don't have a valid filename, so shorten the rest of the
25405 * path if we can. This CAN give us invalid 8.3 filenames, but
25406 * there's not a lot of point in guessing what it might be.
25407 */
25408 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025409 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25410 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025411 }
25412
25413 /* Count the paths backward to find the beginning of the desired string. */
25414 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025415 {
25416#ifdef FEAT_MBYTE
25417 if (has_mbyte)
25418 p -= mb_head_off(tfname, p);
25419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420 if (vim_ispathsep(*p))
25421 {
25422 if (sepcount == 0 || (hasTilde && sepcount == 1))
25423 break;
25424 else
25425 sepcount --;
25426 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428 if (hasTilde)
25429 {
25430 --p;
25431 if (p >= tfname)
25432 *p = '~';
25433 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 }
25436 else
25437 ++p;
25438
25439 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25440 vim_free(*bufp);
25441 *fnamelen = (int)STRLEN(p);
25442 *bufp = pbuf;
25443 *fnamep = p;
25444
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025445 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446}
25447#endif /* WIN3264 */
25448
25449/*
25450 * Adjust a filename, according to a string of modifiers.
25451 * *fnamep must be NUL terminated when called. When returning, the length is
25452 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025453 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 * When there is an error, *fnamep is set to NULL.
25455 */
25456 int
25457modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25458 char_u *src; /* string with modifiers */
25459 int *usedlen; /* characters after src that are used */
25460 char_u **fnamep; /* file name so far */
25461 char_u **bufp; /* buffer for allocated file name or NULL */
25462 int *fnamelen; /* length of fnamep */
25463{
25464 int valid = 0;
25465 char_u *tail;
25466 char_u *s, *p, *pbuf;
25467 char_u dirname[MAXPATHL];
25468 int c;
25469 int has_fullname = 0;
25470#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025471 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472 int has_shortname = 0;
25473#endif
25474
25475repeat:
25476 /* ":p" - full path/file_name */
25477 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25478 {
25479 has_fullname = 1;
25480
25481 valid |= VALID_PATH;
25482 *usedlen += 2;
25483
25484 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25485 if ((*fnamep)[0] == '~'
25486#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25487 && ((*fnamep)[1] == '/'
25488# ifdef BACKSLASH_IN_FILENAME
25489 || (*fnamep)[1] == '\\'
25490# endif
25491 || (*fnamep)[1] == NUL)
25492
25493#endif
25494 )
25495 {
25496 *fnamep = expand_env_save(*fnamep);
25497 vim_free(*bufp); /* free any allocated file name */
25498 *bufp = *fnamep;
25499 if (*fnamep == NULL)
25500 return -1;
25501 }
25502
25503 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025504 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 {
25506 if (vim_ispathsep(*p)
25507 && p[1] == '.'
25508 && (p[2] == NUL
25509 || vim_ispathsep(p[2])
25510 || (p[2] == '.'
25511 && (p[3] == NUL || vim_ispathsep(p[3])))))
25512 break;
25513 }
25514
25515 /* FullName_save() is slow, don't use it when not needed. */
25516 if (*p != NUL || !vim_isAbsName(*fnamep))
25517 {
25518 *fnamep = FullName_save(*fnamep, *p != NUL);
25519 vim_free(*bufp); /* free any allocated file name */
25520 *bufp = *fnamep;
25521 if (*fnamep == NULL)
25522 return -1;
25523 }
25524
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025525#ifdef WIN3264
25526# if _WIN32_WINNT >= 0x0500
25527 if (vim_strchr(*fnamep, '~') != NULL)
25528 {
25529 /* Expand 8.3 filename to full path. Needed to make sure the same
25530 * file does not have two different names.
25531 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25532 p = alloc(_MAX_PATH + 1);
25533 if (p != NULL)
25534 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025535 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025536 {
25537 vim_free(*bufp);
25538 *bufp = *fnamep = p;
25539 }
25540 else
25541 vim_free(p);
25542 }
25543 }
25544# endif
25545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 /* Append a path separator to a directory. */
25547 if (mch_isdir(*fnamep))
25548 {
25549 /* Make room for one or two extra characters. */
25550 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25551 vim_free(*bufp); /* free any allocated file name */
25552 *bufp = *fnamep;
25553 if (*fnamep == NULL)
25554 return -1;
25555 add_pathsep(*fnamep);
25556 }
25557 }
25558
25559 /* ":." - path relative to the current directory */
25560 /* ":~" - path relative to the home directory */
25561 /* ":8" - shortname path - postponed till after */
25562 while (src[*usedlen] == ':'
25563 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25564 {
25565 *usedlen += 2;
25566 if (c == '8')
25567 {
25568#ifdef WIN3264
25569 has_shortname = 1; /* Postpone this. */
25570#endif
25571 continue;
25572 }
25573 pbuf = NULL;
25574 /* Need full path first (use expand_env() to remove a "~/") */
25575 if (!has_fullname)
25576 {
25577 if (c == '.' && **fnamep == '~')
25578 p = pbuf = expand_env_save(*fnamep);
25579 else
25580 p = pbuf = FullName_save(*fnamep, FALSE);
25581 }
25582 else
25583 p = *fnamep;
25584
25585 has_fullname = 0;
25586
25587 if (p != NULL)
25588 {
25589 if (c == '.')
25590 {
25591 mch_dirname(dirname, MAXPATHL);
25592 s = shorten_fname(p, dirname);
25593 if (s != NULL)
25594 {
25595 *fnamep = s;
25596 if (pbuf != NULL)
25597 {
25598 vim_free(*bufp); /* free any allocated file name */
25599 *bufp = pbuf;
25600 pbuf = NULL;
25601 }
25602 }
25603 }
25604 else
25605 {
25606 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25607 /* Only replace it when it starts with '~' */
25608 if (*dirname == '~')
25609 {
25610 s = vim_strsave(dirname);
25611 if (s != NULL)
25612 {
25613 *fnamep = s;
25614 vim_free(*bufp);
25615 *bufp = s;
25616 }
25617 }
25618 }
25619 vim_free(pbuf);
25620 }
25621 }
25622
25623 tail = gettail(*fnamep);
25624 *fnamelen = (int)STRLEN(*fnamep);
25625
25626 /* ":h" - head, remove "/file_name", can be repeated */
25627 /* Don't remove the first "/" or "c:\" */
25628 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25629 {
25630 valid |= VALID_HEAD;
25631 *usedlen += 2;
25632 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025633 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025634 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635 *fnamelen = (int)(tail - *fnamep);
25636#ifdef VMS
25637 if (*fnamelen > 0)
25638 *fnamelen += 1; /* the path separator is part of the path */
25639#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025640 if (*fnamelen == 0)
25641 {
25642 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25643 p = vim_strsave((char_u *)".");
25644 if (p == NULL)
25645 return -1;
25646 vim_free(*bufp);
25647 *bufp = *fnamep = tail = p;
25648 *fnamelen = 1;
25649 }
25650 else
25651 {
25652 while (tail > s && !after_pathsep(s, tail))
25653 mb_ptr_back(*fnamep, tail);
25654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 }
25656
25657 /* ":8" - shortname */
25658 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25659 {
25660 *usedlen += 2;
25661#ifdef WIN3264
25662 has_shortname = 1;
25663#endif
25664 }
25665
25666#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025667 /*
25668 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669 */
25670 if (has_shortname)
25671 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025672 /* Copy the string if it is shortened by :h and when it wasn't copied
25673 * yet, because we are going to change it in place. Avoids changing
25674 * the buffer name for "%:8". */
25675 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676 {
25677 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025678 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025679 return -1;
25680 vim_free(*bufp);
25681 *bufp = *fnamep = p;
25682 }
25683
25684 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025685 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686 if (!has_fullname && !vim_isAbsName(*fnamep))
25687 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025688 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689 return -1;
25690 }
25691 else
25692 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025693 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694
Bram Moolenaardc935552011-08-17 15:23:23 +020025695 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025696 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025697 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698 return -1;
25699
25700 if (l == 0)
25701 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025702 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025704 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025705 return -1;
25706 }
25707 *fnamelen = l;
25708 }
25709 }
25710#endif /* WIN3264 */
25711
25712 /* ":t" - tail, just the basename */
25713 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25714 {
25715 *usedlen += 2;
25716 *fnamelen -= (int)(tail - *fnamep);
25717 *fnamep = tail;
25718 }
25719
25720 /* ":e" - extension, can be repeated */
25721 /* ":r" - root, without extension, can be repeated */
25722 while (src[*usedlen] == ':'
25723 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25724 {
25725 /* find a '.' in the tail:
25726 * - for second :e: before the current fname
25727 * - otherwise: The last '.'
25728 */
25729 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25730 s = *fnamep - 2;
25731 else
25732 s = *fnamep + *fnamelen - 1;
25733 for ( ; s > tail; --s)
25734 if (s[0] == '.')
25735 break;
25736 if (src[*usedlen + 1] == 'e') /* :e */
25737 {
25738 if (s > tail)
25739 {
25740 *fnamelen += (int)(*fnamep - (s + 1));
25741 *fnamep = s + 1;
25742#ifdef VMS
25743 /* cut version from the extension */
25744 s = *fnamep + *fnamelen - 1;
25745 for ( ; s > *fnamep; --s)
25746 if (s[0] == ';')
25747 break;
25748 if (s > *fnamep)
25749 *fnamelen = s - *fnamep;
25750#endif
25751 }
25752 else if (*fnamep <= tail)
25753 *fnamelen = 0;
25754 }
25755 else /* :r */
25756 {
25757 if (s > tail) /* remove one extension */
25758 *fnamelen = (int)(s - *fnamep);
25759 }
25760 *usedlen += 2;
25761 }
25762
25763 /* ":s?pat?foo?" - substitute */
25764 /* ":gs?pat?foo?" - global substitute */
25765 if (src[*usedlen] == ':'
25766 && (src[*usedlen + 1] == 's'
25767 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25768 {
25769 char_u *str;
25770 char_u *pat;
25771 char_u *sub;
25772 int sep;
25773 char_u *flags;
25774 int didit = FALSE;
25775
25776 flags = (char_u *)"";
25777 s = src + *usedlen + 2;
25778 if (src[*usedlen + 1] == 'g')
25779 {
25780 flags = (char_u *)"g";
25781 ++s;
25782 }
25783
25784 sep = *s++;
25785 if (sep)
25786 {
25787 /* find end of pattern */
25788 p = vim_strchr(s, sep);
25789 if (p != NULL)
25790 {
25791 pat = vim_strnsave(s, (int)(p - s));
25792 if (pat != NULL)
25793 {
25794 s = p + 1;
25795 /* find end of substitution */
25796 p = vim_strchr(s, sep);
25797 if (p != NULL)
25798 {
25799 sub = vim_strnsave(s, (int)(p - s));
25800 str = vim_strnsave(*fnamep, *fnamelen);
25801 if (sub != NULL && str != NULL)
25802 {
25803 *usedlen = (int)(p + 1 - src);
25804 s = do_string_sub(str, pat, sub, flags);
25805 if (s != NULL)
25806 {
25807 *fnamep = s;
25808 *fnamelen = (int)STRLEN(s);
25809 vim_free(*bufp);
25810 *bufp = s;
25811 didit = TRUE;
25812 }
25813 }
25814 vim_free(sub);
25815 vim_free(str);
25816 }
25817 vim_free(pat);
25818 }
25819 }
25820 /* after using ":s", repeat all the modifiers */
25821 if (didit)
25822 goto repeat;
25823 }
25824 }
25825
Bram Moolenaar26df0922014-02-23 23:39:13 +010025826 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25827 {
25828 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25829 if (p == NULL)
25830 return -1;
25831 vim_free(*bufp);
25832 *bufp = *fnamep = p;
25833 *fnamelen = (int)STRLEN(p);
25834 *usedlen += 2;
25835 }
25836
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837 return valid;
25838}
25839
25840/*
25841 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25842 * "flags" can be "g" to do a global substitute.
25843 * Returns an allocated string, NULL for error.
25844 */
25845 char_u *
25846do_string_sub(str, pat, sub, flags)
25847 char_u *str;
25848 char_u *pat;
25849 char_u *sub;
25850 char_u *flags;
25851{
25852 int sublen;
25853 regmatch_T regmatch;
25854 int i;
25855 int do_all;
25856 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025857 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 garray_T ga;
25859 char_u *ret;
25860 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025861 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862
25863 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25864 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025865 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866
25867 ga_init2(&ga, 1, 200);
25868
25869 do_all = (flags[0] == 'g');
25870
25871 regmatch.rm_ic = p_ic;
25872 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25873 if (regmatch.regprog != NULL)
25874 {
25875 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025876 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025877 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25878 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025879 /* Skip empty match except for first match. */
25880 if (regmatch.startp[0] == regmatch.endp[0])
25881 {
25882 if (zero_width == regmatch.startp[0])
25883 {
25884 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025885 i = MB_PTR2LEN(tail);
25886 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25887 (size_t)i);
25888 ga.ga_len += i;
25889 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025890 continue;
25891 }
25892 zero_width = regmatch.startp[0];
25893 }
25894
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895 /*
25896 * Get some space for a temporary buffer to do the substitution
25897 * into. It will contain:
25898 * - The text up to where the match is.
25899 * - The substituted text.
25900 * - The text after the match.
25901 */
25902 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025903 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25905 {
25906 ga_clear(&ga);
25907 break;
25908 }
25909
25910 /* copy the text up to where the match is */
25911 i = (int)(regmatch.startp[0] - tail);
25912 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25913 /* add the substituted text */
25914 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25915 + ga.ga_len + i, TRUE, TRUE, FALSE);
25916 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025917 tail = regmatch.endp[0];
25918 if (*tail == NUL)
25919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025920 if (!do_all)
25921 break;
25922 }
25923
25924 if (ga.ga_data != NULL)
25925 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25926
Bram Moolenaar473de612013-06-08 18:19:48 +020025927 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025928 }
25929
25930 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25931 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025932 if (p_cpo == empty_option)
25933 p_cpo = save_cpo;
25934 else
25935 /* Darn, evaluating {sub} expression changed the value. */
25936 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937
25938 return ret;
25939}
25940
25941#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */