blob: ecf7a9a2f0075ed2ff1a33dd1afaf736f6bec01d [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",
12997#endif
12998#ifdef FEAT_CSCOPE
12999 "cscope",
13000#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013001#ifdef FEAT_CURSORBIND
13002 "cursorbind",
13003#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013004#ifdef CURSOR_SHAPE
13005 "cursorshape",
13006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007#ifdef DEBUG
13008 "debug",
13009#endif
13010#ifdef FEAT_CON_DIALOG
13011 "dialog_con",
13012#endif
13013#ifdef FEAT_GUI_DIALOG
13014 "dialog_gui",
13015#endif
13016#ifdef FEAT_DIFF
13017 "diff",
13018#endif
13019#ifdef FEAT_DIGRAPHS
13020 "digraphs",
13021#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013022#ifdef FEAT_DIRECTX
13023 "directx",
13024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025#ifdef FEAT_DND
13026 "dnd",
13027#endif
13028#ifdef FEAT_EMACS_TAGS
13029 "emacs_tags",
13030#endif
13031 "eval", /* always present, of course! */
13032#ifdef FEAT_EX_EXTRA
13033 "ex_extra",
13034#endif
13035#ifdef FEAT_SEARCH_EXTRA
13036 "extra_search",
13037#endif
13038#ifdef FEAT_FKMAP
13039 "farsi",
13040#endif
13041#ifdef FEAT_SEARCHPATH
13042 "file_in_path",
13043#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013044#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013045 "filterpipe",
13046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047#ifdef FEAT_FIND_ID
13048 "find_in_path",
13049#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013050#ifdef FEAT_FLOAT
13051 "float",
13052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053#ifdef FEAT_FOLDING
13054 "folding",
13055#endif
13056#ifdef FEAT_FOOTER
13057 "footer",
13058#endif
13059#if !defined(USE_SYSTEM) && defined(UNIX)
13060 "fork",
13061#endif
13062#ifdef FEAT_GETTEXT
13063 "gettext",
13064#endif
13065#ifdef FEAT_GUI
13066 "gui",
13067#endif
13068#ifdef FEAT_GUI_ATHENA
13069# ifdef FEAT_GUI_NEXTAW
13070 "gui_neXtaw",
13071# else
13072 "gui_athena",
13073# endif
13074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075#ifdef FEAT_GUI_GTK
13076 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013079#ifdef FEAT_GUI_GNOME
13080 "gui_gnome",
13081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082#ifdef FEAT_GUI_MAC
13083 "gui_mac",
13084#endif
13085#ifdef FEAT_GUI_MOTIF
13086 "gui_motif",
13087#endif
13088#ifdef FEAT_GUI_PHOTON
13089 "gui_photon",
13090#endif
13091#ifdef FEAT_GUI_W16
13092 "gui_win16",
13093#endif
13094#ifdef FEAT_GUI_W32
13095 "gui_win32",
13096#endif
13097#ifdef FEAT_HANGULIN
13098 "hangul_input",
13099#endif
13100#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13101 "iconv",
13102#endif
13103#ifdef FEAT_INS_EXPAND
13104 "insert_expand",
13105#endif
13106#ifdef FEAT_JUMPLIST
13107 "jumplist",
13108#endif
13109#ifdef FEAT_KEYMAP
13110 "keymap",
13111#endif
13112#ifdef FEAT_LANGMAP
13113 "langmap",
13114#endif
13115#ifdef FEAT_LIBCALL
13116 "libcall",
13117#endif
13118#ifdef FEAT_LINEBREAK
13119 "linebreak",
13120#endif
13121#ifdef FEAT_LISP
13122 "lispindent",
13123#endif
13124#ifdef FEAT_LISTCMDS
13125 "listcmds",
13126#endif
13127#ifdef FEAT_LOCALMAP
13128 "localmap",
13129#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013130#ifdef FEAT_LUA
13131# ifndef DYNAMIC_LUA
13132 "lua",
13133# endif
13134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135#ifdef FEAT_MENU
13136 "menu",
13137#endif
13138#ifdef FEAT_SESSION
13139 "mksession",
13140#endif
13141#ifdef FEAT_MODIFY_FNAME
13142 "modify_fname",
13143#endif
13144#ifdef FEAT_MOUSE
13145 "mouse",
13146#endif
13147#ifdef FEAT_MOUSESHAPE
13148 "mouseshape",
13149#endif
13150#if defined(UNIX) || defined(VMS)
13151# ifdef FEAT_MOUSE_DEC
13152 "mouse_dec",
13153# endif
13154# ifdef FEAT_MOUSE_GPM
13155 "mouse_gpm",
13156# endif
13157# ifdef FEAT_MOUSE_JSB
13158 "mouse_jsbterm",
13159# endif
13160# ifdef FEAT_MOUSE_NET
13161 "mouse_netterm",
13162# endif
13163# ifdef FEAT_MOUSE_PTERM
13164 "mouse_pterm",
13165# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013166# ifdef FEAT_MOUSE_SGR
13167 "mouse_sgr",
13168# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013169# ifdef FEAT_SYSMOUSE
13170 "mouse_sysmouse",
13171# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013172# ifdef FEAT_MOUSE_URXVT
13173 "mouse_urxvt",
13174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175# ifdef FEAT_MOUSE_XTERM
13176 "mouse_xterm",
13177# endif
13178#endif
13179#ifdef FEAT_MBYTE
13180 "multi_byte",
13181#endif
13182#ifdef FEAT_MBYTE_IME
13183 "multi_byte_ime",
13184#endif
13185#ifdef FEAT_MULTI_LANG
13186 "multi_lang",
13187#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013188#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013189#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013190 "mzscheme",
13191#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193#ifdef FEAT_OLE
13194 "ole",
13195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196#ifdef FEAT_PATH_EXTRA
13197 "path_extra",
13198#endif
13199#ifdef FEAT_PERL
13200#ifndef DYNAMIC_PERL
13201 "perl",
13202#endif
13203#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013204#ifdef FEAT_PERSISTENT_UNDO
13205 "persistent_undo",
13206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207#ifdef FEAT_PYTHON
13208#ifndef DYNAMIC_PYTHON
13209 "python",
13210#endif
13211#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013212#ifdef FEAT_PYTHON3
13213#ifndef DYNAMIC_PYTHON3
13214 "python3",
13215#endif
13216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217#ifdef FEAT_POSTSCRIPT
13218 "postscript",
13219#endif
13220#ifdef FEAT_PRINTER
13221 "printer",
13222#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013223#ifdef FEAT_PROFILE
13224 "profile",
13225#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013226#ifdef FEAT_RELTIME
13227 "reltime",
13228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229#ifdef FEAT_QUICKFIX
13230 "quickfix",
13231#endif
13232#ifdef FEAT_RIGHTLEFT
13233 "rightleft",
13234#endif
13235#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13236 "ruby",
13237#endif
13238#ifdef FEAT_SCROLLBIND
13239 "scrollbind",
13240#endif
13241#ifdef FEAT_CMDL_INFO
13242 "showcmd",
13243 "cmdline_info",
13244#endif
13245#ifdef FEAT_SIGNS
13246 "signs",
13247#endif
13248#ifdef FEAT_SMARTINDENT
13249 "smartindent",
13250#endif
13251#ifdef FEAT_SNIFF
13252 "sniff",
13253#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013254#ifdef STARTUPTIME
13255 "startuptime",
13256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257#ifdef FEAT_STL_OPT
13258 "statusline",
13259#endif
13260#ifdef FEAT_SUN_WORKSHOP
13261 "sun_workshop",
13262#endif
13263#ifdef FEAT_NETBEANS_INTG
13264 "netbeans_intg",
13265#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013266#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013267 "spell",
13268#endif
13269#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 "syntax",
13271#endif
13272#if defined(USE_SYSTEM) || !defined(UNIX)
13273 "system",
13274#endif
13275#ifdef FEAT_TAG_BINS
13276 "tag_binary",
13277#endif
13278#ifdef FEAT_TAG_OLDSTATIC
13279 "tag_old_static",
13280#endif
13281#ifdef FEAT_TAG_ANYWHITE
13282 "tag_any_white",
13283#endif
13284#ifdef FEAT_TCL
13285# ifndef DYNAMIC_TCL
13286 "tcl",
13287# endif
13288#endif
13289#ifdef TERMINFO
13290 "terminfo",
13291#endif
13292#ifdef FEAT_TERMRESPONSE
13293 "termresponse",
13294#endif
13295#ifdef FEAT_TEXTOBJ
13296 "textobjects",
13297#endif
13298#ifdef HAVE_TGETENT
13299 "tgetent",
13300#endif
13301#ifdef FEAT_TITLE
13302 "title",
13303#endif
13304#ifdef FEAT_TOOLBAR
13305 "toolbar",
13306#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013307#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13308 "unnamedplus",
13309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#ifdef FEAT_USR_CMDS
13311 "user-commands", /* was accidentally included in 5.4 */
13312 "user_commands",
13313#endif
13314#ifdef FEAT_VIMINFO
13315 "viminfo",
13316#endif
13317#ifdef FEAT_VERTSPLIT
13318 "vertsplit",
13319#endif
13320#ifdef FEAT_VIRTUALEDIT
13321 "virtualedit",
13322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#ifdef FEAT_VISUALEXTRA
13325 "visualextra",
13326#endif
13327#ifdef FEAT_VREPLACE
13328 "vreplace",
13329#endif
13330#ifdef FEAT_WILDIGN
13331 "wildignore",
13332#endif
13333#ifdef FEAT_WILDMENU
13334 "wildmenu",
13335#endif
13336#ifdef FEAT_WINDOWS
13337 "windows",
13338#endif
13339#ifdef FEAT_WAK
13340 "winaltkeys",
13341#endif
13342#ifdef FEAT_WRITEBACKUP
13343 "writebackup",
13344#endif
13345#ifdef FEAT_XIM
13346 "xim",
13347#endif
13348#ifdef FEAT_XFONTSET
13349 "xfontset",
13350#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013351#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013352 "xpm",
13353 "xpm_w32", /* for backward compatibility */
13354#else
13355# if defined(HAVE_XPM)
13356 "xpm",
13357# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359#ifdef USE_XSMP
13360 "xsmp",
13361#endif
13362#ifdef USE_XSMP_INTERACT
13363 "xsmp_interact",
13364#endif
13365#ifdef FEAT_XCLIPBOARD
13366 "xterm_clipboard",
13367#endif
13368#ifdef FEAT_XTERM_SAVE
13369 "xterm_save",
13370#endif
13371#if defined(UNIX) && defined(FEAT_X11)
13372 "X11",
13373#endif
13374 NULL
13375 };
13376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 for (i = 0; has_list[i] != NULL; ++i)
13379 if (STRICMP(name, has_list[i]) == 0)
13380 {
13381 n = TRUE;
13382 break;
13383 }
13384
13385 if (n == FALSE)
13386 {
13387 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013388 {
13389 if (name[5] == '-'
13390 && STRLEN(name) > 11
13391 && vim_isdigit(name[6])
13392 && vim_isdigit(name[8])
13393 && vim_isdigit(name[10]))
13394 {
13395 int major = atoi((char *)name + 6);
13396 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013397
13398 /* Expect "patch-9.9.01234". */
13399 n = (major < VIM_VERSION_MAJOR
13400 || (major == VIM_VERSION_MAJOR
13401 && (minor < VIM_VERSION_MINOR
13402 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013403 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013404 }
13405 else
13406 n = has_patch(atoi((char *)name + 5));
13407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 else if (STRICMP(name, "vim_starting") == 0)
13409 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013410#ifdef FEAT_MBYTE
13411 else if (STRICMP(name, "multi_byte_encoding") == 0)
13412 n = has_mbyte;
13413#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013414#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13415 else if (STRICMP(name, "balloon_multiline") == 0)
13416 n = multiline_balloon_available();
13417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418#ifdef DYNAMIC_TCL
13419 else if (STRICMP(name, "tcl") == 0)
13420 n = tcl_enabled(FALSE);
13421#endif
13422#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13423 else if (STRICMP(name, "iconv") == 0)
13424 n = iconv_enabled(FALSE);
13425#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013426#ifdef DYNAMIC_LUA
13427 else if (STRICMP(name, "lua") == 0)
13428 n = lua_enabled(FALSE);
13429#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013430#ifdef DYNAMIC_MZSCHEME
13431 else if (STRICMP(name, "mzscheme") == 0)
13432 n = mzscheme_enabled(FALSE);
13433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434#ifdef DYNAMIC_RUBY
13435 else if (STRICMP(name, "ruby") == 0)
13436 n = ruby_enabled(FALSE);
13437#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013438#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439#ifdef DYNAMIC_PYTHON
13440 else if (STRICMP(name, "python") == 0)
13441 n = python_enabled(FALSE);
13442#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013443#endif
13444#ifdef FEAT_PYTHON3
13445#ifdef DYNAMIC_PYTHON3
13446 else if (STRICMP(name, "python3") == 0)
13447 n = python3_enabled(FALSE);
13448#endif
13449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450#ifdef DYNAMIC_PERL
13451 else if (STRICMP(name, "perl") == 0)
13452 n = perl_enabled(FALSE);
13453#endif
13454#ifdef FEAT_GUI
13455 else if (STRICMP(name, "gui_running") == 0)
13456 n = (gui.in_use || gui.starting);
13457# ifdef FEAT_GUI_W32
13458 else if (STRICMP(name, "gui_win32s") == 0)
13459 n = gui_is_win32s();
13460# endif
13461# ifdef FEAT_BROWSE
13462 else if (STRICMP(name, "browse") == 0)
13463 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13464# endif
13465#endif
13466#ifdef FEAT_SYN_HL
13467 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013468 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469#endif
13470#if defined(WIN3264)
13471 else if (STRICMP(name, "win95") == 0)
13472 n = mch_windows95();
13473#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013474#ifdef FEAT_NETBEANS_INTG
13475 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013476 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 }
13479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481}
13482
13483/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013484 * "has_key()" function
13485 */
13486 static void
13487f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013488 typval_T *argvars;
13489 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013490{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013491 if (argvars[0].v_type != VAR_DICT)
13492 {
13493 EMSG(_(e_dictreq));
13494 return;
13495 }
13496 if (argvars[0].vval.v_dict == NULL)
13497 return;
13498
13499 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013500 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013501}
13502
13503/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013504 * "haslocaldir()" function
13505 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013506 static void
13507f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013508 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013509 typval_T *rettv;
13510{
13511 rettv->vval.v_number = (curwin->w_localdir != NULL);
13512}
13513
13514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 * "hasmapto()" function
13516 */
13517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013519 typval_T *argvars;
13520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521{
13522 char_u *name;
13523 char_u *mode;
13524 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013525 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013528 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 mode = (char_u *)"nvo";
13530 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013533 if (argvars[2].v_type != VAR_UNKNOWN)
13534 abbr = get_tv_number(&argvars[2]);
13535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536
Bram Moolenaar2c932302006-03-18 21:42:09 +000013537 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541}
13542
13543/*
13544 * "histadd()" function
13545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013547f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013548 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550{
13551#ifdef FEAT_CMDHIST
13552 int histype;
13553 char_u *str;
13554 char_u buf[NUMBUFLEN];
13555#endif
13556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 if (check_restricted() || check_secure())
13559 return;
13560#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13562 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 if (histype >= 0)
13564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 if (*str != NUL)
13567 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013568 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 return;
13572 }
13573 }
13574#endif
13575}
13576
13577/*
13578 * "histdel()" function
13579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013582 typval_T *argvars UNUSED;
13583 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584{
13585#ifdef FEAT_CMDHIST
13586 int n;
13587 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13591 if (str == NULL)
13592 n = 0;
13593 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013596 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 else
13601 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013602 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 get_tv_string_buf(&argvars[1], buf));
13604 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605#endif
13606}
13607
13608/*
13609 * "histget()" function
13610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013613 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615{
13616#ifdef FEAT_CMDHIST
13617 int type;
13618 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013619 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13622 if (str == NULL)
13623 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 {
13626 type = get_histtype(str);
13627 if (argvars[1].v_type == VAR_UNKNOWN)
13628 idx = get_history_idx(type);
13629 else
13630 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13631 /* -1 on type error */
13632 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638}
13639
13640/*
13641 * "histnr()" function
13642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013645 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647{
13648 int i;
13649
13650#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013651 char_u *history = get_tv_string_chk(&argvars[0]);
13652
13653 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 if (i >= HIST_CMD && i < HIST_COUNT)
13655 i = get_history_idx(i);
13656 else
13657#endif
13658 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660}
13661
13662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 * "highlightID(name)" function
13664 */
13665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013667 typval_T *argvars;
13668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671}
13672
13673/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013674 * "highlight_exists()" function
13675 */
13676 static void
13677f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013678 typval_T *argvars;
13679 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013680{
13681 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13682}
13683
13684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 * "hostname()" function
13686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691{
13692 char_u hostname[256];
13693
13694 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013695 rettv->v_type = VAR_STRING;
13696 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697}
13698
13699/*
13700 * iconv() function
13701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013704 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706{
13707#ifdef FEAT_MBYTE
13708 char_u buf1[NUMBUFLEN];
13709 char_u buf2[NUMBUFLEN];
13710 char_u *from, *to, *str;
13711 vimconv_T vimconv;
13712#endif
13713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714 rettv->v_type = VAR_STRING;
13715 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716
13717#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 str = get_tv_string(&argvars[0]);
13719 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13720 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 vimconv.vc_type = CONV_NONE;
13722 convert_setup(&vimconv, from, to);
13723
13724 /* If the encodings are equal, no conversion needed. */
13725 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729
13730 convert_setup(&vimconv, NULL, NULL);
13731 vim_free(from);
13732 vim_free(to);
13733#endif
13734}
13735
13736/*
13737 * "indent()" function
13738 */
13739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013740f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *argvars;
13742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743{
13744 linenr_T lnum;
13745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751}
13752
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013753/*
13754 * "index()" function
13755 */
13756 static void
13757f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 typval_T *argvars;
13759 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013760{
Bram Moolenaar33570922005-01-25 22:26:29 +000013761 list_T *l;
13762 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013763 long idx = 0;
13764 int ic = FALSE;
13765
13766 rettv->vval.v_number = -1;
13767 if (argvars[0].v_type != VAR_LIST)
13768 {
13769 EMSG(_(e_listreq));
13770 return;
13771 }
13772 l = argvars[0].vval.v_list;
13773 if (l != NULL)
13774 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013775 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013776 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778 int error = FALSE;
13779
Bram Moolenaar758711c2005-02-02 23:11:38 +000013780 /* Start at specified item. Use the cached index that list_find()
13781 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013783 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013784 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 ic = get_tv_number_chk(&argvars[3], &error);
13786 if (error)
13787 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013788 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013789
Bram Moolenaar758711c2005-02-02 23:11:38 +000013790 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013791 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013792 {
13793 rettv->vval.v_number = idx;
13794 break;
13795 }
13796 }
13797}
13798
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799static int inputsecret_flag = 0;
13800
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013801static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13802
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013804 * This function is used by f_input() and f_inputdialog() functions. The third
13805 * argument to f_input() specifies the type of completion to use at the
13806 * prompt. The third argument to f_inputdialog() specifies the value to return
13807 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 */
13809 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013810get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013811 typval_T *argvars;
13812 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013813 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 char_u *p = NULL;
13817 int c;
13818 char_u buf[NUMBUFLEN];
13819 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013821 int xp_type = EXPAND_NOTHING;
13822 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013825 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826
13827#ifdef NO_CONSOLE_INPUT
13828 /* While starting up, there is no place to enter text. */
13829 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831#endif
13832
13833 cmd_silent = FALSE; /* Want to see the prompt. */
13834 if (prompt != NULL)
13835 {
13836 /* Only the part of the message after the last NL is considered as
13837 * prompt for the command line */
13838 p = vim_strrchr(prompt, '\n');
13839 if (p == NULL)
13840 p = prompt;
13841 else
13842 {
13843 ++p;
13844 c = *p;
13845 *p = NUL;
13846 msg_start();
13847 msg_clr_eos();
13848 msg_puts_attr(prompt, echo_attr);
13849 msg_didout = FALSE;
13850 msg_starthere();
13851 *p = c;
13852 }
13853 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013855 if (argvars[1].v_type != VAR_UNKNOWN)
13856 {
13857 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13858 if (defstr != NULL)
13859 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013861 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013862 {
13863 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013864 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013865 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013866
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013867 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013868 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013869
Bram Moolenaar4463f292005-09-25 22:20:24 +000013870 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13871 if (xp_name == NULL)
13872 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013873
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013874 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013875
Bram Moolenaar4463f292005-09-25 22:20:24 +000013876 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13877 &xp_arg) == FAIL)
13878 return;
13879 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013880 }
13881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013882 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013883 {
13884# ifdef FEAT_EX_EXTRA
13885 int save_ex_normal_busy = ex_normal_busy;
13886 ex_normal_busy = 0;
13887# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013888 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013889 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13890 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013891# ifdef FEAT_EX_EXTRA
13892 ex_normal_busy = save_ex_normal_busy;
13893# endif
13894 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013895 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013896 && argvars[1].v_type != VAR_UNKNOWN
13897 && argvars[2].v_type != VAR_UNKNOWN)
13898 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13899 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013900
13901 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013903 /* since the user typed this, no need to wait for return */
13904 need_wait_return = FALSE;
13905 msg_didout = FALSE;
13906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 cmd_silent = cmd_silent_save;
13908}
13909
13910/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013911 * "input()" function
13912 * Also handles inputsecret() when inputsecret is set.
13913 */
13914 static void
13915f_input(argvars, rettv)
13916 typval_T *argvars;
13917 typval_T *rettv;
13918{
13919 get_user_input(argvars, rettv, FALSE);
13920}
13921
13922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 * "inputdialog()" function
13924 */
13925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013927 typval_T *argvars;
13928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929{
13930#if defined(FEAT_GUI_TEXTDIALOG)
13931 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13932 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13933 {
13934 char_u *message;
13935 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013936 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013938 message = get_tv_string_chk(&argvars[0]);
13939 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013940 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013941 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942 else
13943 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 if (message != NULL && defstr != NULL
13945 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013946 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013947 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 else
13949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950 if (message != NULL && defstr != NULL
13951 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013952 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013953 rettv->vval.v_string = vim_strsave(
13954 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 }
13960 else
13961#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013962 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963}
13964
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013965/*
13966 * "inputlist()" function
13967 */
13968 static void
13969f_inputlist(argvars, rettv)
13970 typval_T *argvars;
13971 typval_T *rettv;
13972{
13973 listitem_T *li;
13974 int selected;
13975 int mouse_used;
13976
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013977#ifdef NO_CONSOLE_INPUT
13978 /* While starting up, there is no place to enter text. */
13979 if (no_console_input())
13980 return;
13981#endif
13982 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13983 {
13984 EMSG2(_(e_listarg), "inputlist()");
13985 return;
13986 }
13987
13988 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013989 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013990 lines_left = Rows; /* avoid more prompt */
13991 msg_scroll = TRUE;
13992 msg_clr_eos();
13993
13994 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13995 {
13996 msg_puts(get_tv_string(&li->li_tv));
13997 msg_putchar('\n');
13998 }
13999
14000 /* Ask for choice. */
14001 selected = prompt_for_number(&mouse_used);
14002 if (mouse_used)
14003 selected -= lines_left;
14004
14005 rettv->vval.v_number = selected;
14006}
14007
14008
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14010
14011/*
14012 * "inputrestore()" function
14013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014016 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018{
14019 if (ga_userinput.ga_len > 0)
14020 {
14021 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14023 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014024 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 }
14026 else if (p_verbose > 1)
14027 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014028 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014029 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 }
14031}
14032
14033/*
14034 * "inputsave()" function
14035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014041 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 if (ga_grow(&ga_userinput, 1) == OK)
14043 {
14044 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14045 + ga_userinput.ga_len);
14046 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014047 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 }
14049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014050 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051}
14052
14053/*
14054 * "inputsecret()" function
14055 */
14056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 typval_T *argvars;
14059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060{
14061 ++cmdline_star;
14062 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014063 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 --cmdline_star;
14065 --inputsecret_flag;
14066}
14067
14068/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014069 * "insert()" function
14070 */
14071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014072f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014073 typval_T *argvars;
14074 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014075{
14076 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014077 listitem_T *item;
14078 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014080
14081 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014083 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014084 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014085 {
14086 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014087 before = get_tv_number_chk(&argvars[2], &error);
14088 if (error)
14089 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014090
Bram Moolenaar758711c2005-02-02 23:11:38 +000014091 if (before == l->lv_len)
14092 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014093 else
14094 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014095 item = list_find(l, before);
14096 if (item == NULL)
14097 {
14098 EMSGN(_(e_listidx), before);
14099 l = NULL;
14100 }
14101 }
14102 if (l != NULL)
14103 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014104 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014105 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014106 }
14107 }
14108}
14109
14110/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014111 * "invert(expr)" function
14112 */
14113 static void
14114f_invert(argvars, rettv)
14115 typval_T *argvars;
14116 typval_T *rettv;
14117{
14118 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14119}
14120
14121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 * "isdirectory()" function
14123 */
14124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 typval_T *argvars;
14127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130}
14131
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014132/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014133 * "islocked()" function
14134 */
14135 static void
14136f_islocked(argvars, rettv)
14137 typval_T *argvars;
14138 typval_T *rettv;
14139{
14140 lval_T lv;
14141 char_u *end;
14142 dictitem_T *di;
14143
14144 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014145 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14146 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014147 if (end != NULL && lv.ll_name != NULL)
14148 {
14149 if (*end != NUL)
14150 EMSG(_(e_trailing));
14151 else
14152 {
14153 if (lv.ll_tv == NULL)
14154 {
14155 if (check_changedtick(lv.ll_name))
14156 rettv->vval.v_number = 1; /* always locked */
14157 else
14158 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014159 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014160 if (di != NULL)
14161 {
14162 /* Consider a variable locked when:
14163 * 1. the variable itself is locked
14164 * 2. the value of the variable is locked.
14165 * 3. the List or Dict value is locked.
14166 */
14167 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14168 || tv_islocked(&di->di_tv));
14169 }
14170 }
14171 }
14172 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014173 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014174 else if (lv.ll_newkey != NULL)
14175 EMSG2(_(e_dictkey), lv.ll_newkey);
14176 else if (lv.ll_list != NULL)
14177 /* List item. */
14178 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14179 else
14180 /* Dictionary item. */
14181 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14182 }
14183 }
14184
14185 clear_lval(&lv);
14186}
14187
Bram Moolenaar33570922005-01-25 22:26:29 +000014188static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014189
14190/*
14191 * Turn a dict into a list:
14192 * "what" == 0: list of keys
14193 * "what" == 1: list of values
14194 * "what" == 2: list of items
14195 */
14196 static void
14197dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014198 typval_T *argvars;
14199 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014200 int what;
14201{
Bram Moolenaar33570922005-01-25 22:26:29 +000014202 list_T *l2;
14203 dictitem_T *di;
14204 hashitem_T *hi;
14205 listitem_T *li;
14206 listitem_T *li2;
14207 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014208 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014209
Bram Moolenaar8c711452005-01-14 21:53:12 +000014210 if (argvars[0].v_type != VAR_DICT)
14211 {
14212 EMSG(_(e_dictreq));
14213 return;
14214 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014215 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014216 return;
14217
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014218 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014219 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014220
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014221 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014222 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014223 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014224 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014225 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014226 --todo;
14227 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014228
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014229 li = listitem_alloc();
14230 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014231 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014232 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014233
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014234 if (what == 0)
14235 {
14236 /* keys() */
14237 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014238 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014239 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14240 }
14241 else if (what == 1)
14242 {
14243 /* values() */
14244 copy_tv(&di->di_tv, &li->li_tv);
14245 }
14246 else
14247 {
14248 /* items() */
14249 l2 = list_alloc();
14250 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014251 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014252 li->li_tv.vval.v_list = l2;
14253 if (l2 == NULL)
14254 break;
14255 ++l2->lv_refcount;
14256
14257 li2 = listitem_alloc();
14258 if (li2 == NULL)
14259 break;
14260 list_append(l2, li2);
14261 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014262 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014263 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14264
14265 li2 = listitem_alloc();
14266 if (li2 == NULL)
14267 break;
14268 list_append(l2, li2);
14269 copy_tv(&di->di_tv, &li2->li_tv);
14270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014271 }
14272 }
14273}
14274
14275/*
14276 * "items(dict)" function
14277 */
14278 static void
14279f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014280 typval_T *argvars;
14281 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014282{
14283 dict_list(argvars, rettv, 2);
14284}
14285
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014287 * "join()" function
14288 */
14289 static void
14290f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014291 typval_T *argvars;
14292 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014293{
14294 garray_T ga;
14295 char_u *sep;
14296
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014297 if (argvars[0].v_type != VAR_LIST)
14298 {
14299 EMSG(_(e_listreq));
14300 return;
14301 }
14302 if (argvars[0].vval.v_list == NULL)
14303 return;
14304 if (argvars[1].v_type == VAR_UNKNOWN)
14305 sep = (char_u *)" ";
14306 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014307 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014308
14309 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310
14311 if (sep != NULL)
14312 {
14313 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014314 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 ga_append(&ga, NUL);
14316 rettv->vval.v_string = (char_u *)ga.ga_data;
14317 }
14318 else
14319 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014320}
14321
14322/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014323 * "keys()" function
14324 */
14325 static void
14326f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014327 typval_T *argvars;
14328 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014329{
14330 dict_list(argvars, rettv, 0);
14331}
14332
14333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 * "last_buffer_nr()" function.
14335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014338 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340{
14341 int n = 0;
14342 buf_T *buf;
14343
14344 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14345 if (n < buf->b_fnum)
14346 n = buf->b_fnum;
14347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014349}
14350
14351/*
14352 * "len()" function
14353 */
14354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014355f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014356 typval_T *argvars;
14357 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014358{
14359 switch (argvars[0].v_type)
14360 {
14361 case VAR_STRING:
14362 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 rettv->vval.v_number = (varnumber_T)STRLEN(
14364 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014365 break;
14366 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014368 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014369 case VAR_DICT:
14370 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14371 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014372 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014373 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014374 break;
14375 }
14376}
14377
Bram Moolenaar33570922005-01-25 22:26:29 +000014378static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014379
14380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 typval_T *argvars;
14383 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014384 int type;
14385{
14386#ifdef FEAT_LIBCALL
14387 char_u *string_in;
14388 char_u **string_result;
14389 int nr_result;
14390#endif
14391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014392 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014393 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014395
14396 if (check_restricted() || check_secure())
14397 return;
14398
14399#ifdef FEAT_LIBCALL
14400 /* The first two args must be strings, otherwise its meaningless */
14401 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14402 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014403 string_in = NULL;
14404 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014405 string_in = argvars[2].vval.v_string;
14406 if (type == VAR_NUMBER)
14407 string_result = NULL;
14408 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014410 if (mch_libcall(argvars[0].vval.v_string,
14411 argvars[1].vval.v_string,
14412 string_in,
14413 argvars[2].vval.v_number,
14414 string_result,
14415 &nr_result) == OK
14416 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014418 }
14419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420}
14421
14422/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014423 * "libcall()" function
14424 */
14425 static void
14426f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014427 typval_T *argvars;
14428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014429{
14430 libcall_common(argvars, rettv, VAR_STRING);
14431}
14432
14433/*
14434 * "libcallnr()" function
14435 */
14436 static void
14437f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014438 typval_T *argvars;
14439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014440{
14441 libcall_common(argvars, rettv, VAR_NUMBER);
14442}
14443
14444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 * "line(string)" function
14446 */
14447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 typval_T *argvars;
14450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451{
14452 linenr_T lnum = 0;
14453 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014454 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014456 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 if (fp != NULL)
14458 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460}
14461
14462/*
14463 * "line2byte(lnum)" function
14464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014467 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469{
14470#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472#else
14473 linenr_T lnum;
14474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14480 if (rettv->vval.v_number >= 0)
14481 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482#endif
14483}
14484
14485/*
14486 * "lispindent(lnum)" function
14487 */
14488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014489f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014490 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492{
14493#ifdef FEAT_LISP
14494 pos_T pos;
14495 linenr_T lnum;
14496
14497 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14500 {
14501 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 curwin->w_cursor = pos;
14504 }
14505 else
14506#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508}
14509
14510/*
14511 * "localtime()" function
14512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014515 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519}
14520
Bram Moolenaar33570922005-01-25 22:26:29 +000014521static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522
14523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014525 typval_T *argvars;
14526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 int exact;
14528{
14529 char_u *keys;
14530 char_u *which;
14531 char_u buf[NUMBUFLEN];
14532 char_u *keys_buf = NULL;
14533 char_u *rhs;
14534 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014535 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014536 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014537 mapblock_T *mp;
14538 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539
14540 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014541 rettv->v_type = VAR_STRING;
14542 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014544 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 if (*keys == NUL)
14546 return;
14547
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014548 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014552 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014553 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014554 if (argvars[3].v_type != VAR_UNKNOWN)
14555 get_dict = get_tv_number(&argvars[3]);
14556 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 else
14559 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 if (which == NULL)
14561 return;
14562
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563 mode = get_map_mode(&which, 0);
14564
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014565 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014566 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014568
14569 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014571 /* Return a string. */
14572 if (rhs != NULL)
14573 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574
Bram Moolenaarbd743252010-10-20 21:23:33 +020014575 }
14576 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14577 {
14578 /* Return a dictionary. */
14579 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14580 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14581 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582
Bram Moolenaarbd743252010-10-20 21:23:33 +020014583 dict_add_nr_str(dict, "lhs", 0L, lhs);
14584 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14585 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14586 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14587 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14588 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14589 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014590 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014591 dict_add_nr_str(dict, "mode", 0L, mapmode);
14592
14593 vim_free(lhs);
14594 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 }
14596}
14597
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014598#ifdef FEAT_FLOAT
14599/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014600 * "log()" function
14601 */
14602 static void
14603f_log(argvars, rettv)
14604 typval_T *argvars;
14605 typval_T *rettv;
14606{
14607 float_T f;
14608
14609 rettv->v_type = VAR_FLOAT;
14610 if (get_float_arg(argvars, &f) == OK)
14611 rettv->vval.v_float = log(f);
14612 else
14613 rettv->vval.v_float = 0.0;
14614}
14615
14616/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014617 * "log10()" function
14618 */
14619 static void
14620f_log10(argvars, rettv)
14621 typval_T *argvars;
14622 typval_T *rettv;
14623{
14624 float_T f;
14625
14626 rettv->v_type = VAR_FLOAT;
14627 if (get_float_arg(argvars, &f) == OK)
14628 rettv->vval.v_float = log10(f);
14629 else
14630 rettv->vval.v_float = 0.0;
14631}
14632#endif
14633
Bram Moolenaar1dced572012-04-05 16:54:08 +020014634#ifdef FEAT_LUA
14635/*
14636 * "luaeval()" function
14637 */
14638 static void
14639f_luaeval(argvars, rettv)
14640 typval_T *argvars;
14641 typval_T *rettv;
14642{
14643 char_u *str;
14644 char_u buf[NUMBUFLEN];
14645
14646 str = get_tv_string_buf(&argvars[0], buf);
14647 do_luaeval(str, argvars + 1, rettv);
14648}
14649#endif
14650
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014652 * "map()" function
14653 */
14654 static void
14655f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014656 typval_T *argvars;
14657 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014658{
14659 filter_map(argvars, rettv, TRUE);
14660}
14661
14662/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014663 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 */
14665 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014666f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014667 typval_T *argvars;
14668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671}
14672
14673/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014674 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 */
14676 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014677f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 typval_T *argvars;
14679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682}
14683
Bram Moolenaar33570922005-01-25 22:26:29 +000014684static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685
14686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 typval_T *argvars;
14689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 int type;
14691{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014692 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014693 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014694 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 char_u *pat;
14696 regmatch_T regmatch;
14697 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014698 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 char_u *save_cpo;
14700 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014701 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014702 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014703 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014704 list_T *l = NULL;
14705 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014706 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014707 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
14709 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14710 save_cpo = p_cpo;
14711 p_cpo = (char_u *)"";
14712
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014713 rettv->vval.v_number = -1;
14714 if (type == 3)
14715 {
14716 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014717 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014718 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014719 }
14720 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014722 rettv->v_type = VAR_STRING;
14723 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014726 if (argvars[0].v_type == VAR_LIST)
14727 {
14728 if ((l = argvars[0].vval.v_list) == NULL)
14729 goto theend;
14730 li = l->lv_first;
14731 }
14732 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014733 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014734 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014735 len = (long)STRLEN(str);
14736 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014738 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14739 if (pat == NULL)
14740 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014741
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014742 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 int error = FALSE;
14745
14746 start = get_tv_number_chk(&argvars[2], &error);
14747 if (error)
14748 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014749 if (l != NULL)
14750 {
14751 li = list_find(l, start);
14752 if (li == NULL)
14753 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014754 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014755 }
14756 else
14757 {
14758 if (start < 0)
14759 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014760 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014761 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014762 /* When "count" argument is there ignore matches before "start",
14763 * otherwise skip part of the string. Differs when pattern is "^"
14764 * or "\<". */
14765 if (argvars[3].v_type != VAR_UNKNOWN)
14766 startcol = start;
14767 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014768 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014769 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014770 len -= start;
14771 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014772 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014773
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014774 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014775 nth = get_tv_number_chk(&argvars[3], &error);
14776 if (error)
14777 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 }
14779
14780 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14781 if (regmatch.regprog != NULL)
14782 {
14783 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014784
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014785 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014786 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014787 if (l != NULL)
14788 {
14789 if (li == NULL)
14790 {
14791 match = FALSE;
14792 break;
14793 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014794 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014795 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014796 if (str == NULL)
14797 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014798 }
14799
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014800 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014801
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014802 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014803 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014804 if (l == NULL && !match)
14805 break;
14806
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014807 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014808 if (l != NULL)
14809 {
14810 li = li->li_next;
14811 ++idx;
14812 }
14813 else
14814 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014815#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014816 startcol = (colnr_T)(regmatch.startp[0]
14817 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014818#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014819 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014820#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014821 if (startcol > (colnr_T)len
14822 || str + startcol <= regmatch.startp[0])
14823 {
14824 match = FALSE;
14825 break;
14826 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014827 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014828 }
14829
14830 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014832 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014833 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014834 int i;
14835
14836 /* return list with matched string and submatches */
14837 for (i = 0; i < NSUBEXP; ++i)
14838 {
14839 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014840 {
14841 if (list_append_string(rettv->vval.v_list,
14842 (char_u *)"", 0) == FAIL)
14843 break;
14844 }
14845 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014846 regmatch.startp[i],
14847 (int)(regmatch.endp[i] - regmatch.startp[i]))
14848 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014849 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014850 }
14851 }
14852 else if (type == 2)
14853 {
14854 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014855 if (l != NULL)
14856 copy_tv(&li->li_tv, rettv);
14857 else
14858 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014860 }
14861 else if (l != NULL)
14862 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 else
14864 {
14865 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014866 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 (varnumber_T)(regmatch.startp[0] - str);
14868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014869 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014871 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 }
14873 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014874 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875 }
14876
14877theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014878 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879 p_cpo = save_cpo;
14880}
14881
14882/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014883 * "match()" function
14884 */
14885 static void
14886f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014887 typval_T *argvars;
14888 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014889{
14890 find_some_match(argvars, rettv, 1);
14891}
14892
14893/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014894 * "matchadd()" function
14895 */
14896 static void
14897f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014898 typval_T *argvars UNUSED;
14899 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014900{
14901#ifdef FEAT_SEARCH_EXTRA
14902 char_u buf[NUMBUFLEN];
14903 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14904 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14905 int prio = 10; /* default priority */
14906 int id = -1;
14907 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014908 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014909
14910 rettv->vval.v_number = -1;
14911
14912 if (grp == NULL || pat == NULL)
14913 return;
14914 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014915 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014916 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014917 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014918 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014919 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014920 if (argvars[4].v_type != VAR_UNKNOWN)
14921 {
14922 if (argvars[4].v_type != VAR_DICT)
14923 {
14924 EMSG(_(e_dictreq));
14925 return;
14926 }
14927 if (dict_find(argvars[4].vval.v_dict,
14928 (char_u *)"conceal", -1) != NULL)
14929 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14930 (char_u *)"conceal", FALSE);
14931 }
14932 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014933 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014934 if (error == TRUE)
14935 return;
14936 if (id >= 1 && id <= 3)
14937 {
14938 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14939 return;
14940 }
14941
Bram Moolenaar6561d522015-07-21 15:48:27 +020014942 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14943 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014944#endif
14945}
14946
14947/*
14948 * "matchaddpos()" function
14949 */
14950 static void
14951f_matchaddpos(argvars, rettv)
14952 typval_T *argvars UNUSED;
14953 typval_T *rettv UNUSED;
14954{
14955#ifdef FEAT_SEARCH_EXTRA
14956 char_u buf[NUMBUFLEN];
14957 char_u *group;
14958 int prio = 10;
14959 int id = -1;
14960 int error = FALSE;
14961 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014962 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014963
14964 rettv->vval.v_number = -1;
14965
14966 group = get_tv_string_buf_chk(&argvars[0], buf);
14967 if (group == NULL)
14968 return;
14969
14970 if (argvars[1].v_type != VAR_LIST)
14971 {
14972 EMSG2(_(e_listarg), "matchaddpos()");
14973 return;
14974 }
14975 l = argvars[1].vval.v_list;
14976 if (l == NULL)
14977 return;
14978
14979 if (argvars[2].v_type != VAR_UNKNOWN)
14980 {
14981 prio = get_tv_number_chk(&argvars[2], &error);
14982 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014983 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014984 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014985 if (argvars[4].v_type != VAR_UNKNOWN)
14986 {
14987 if (argvars[4].v_type != VAR_DICT)
14988 {
14989 EMSG(_(e_dictreq));
14990 return;
14991 }
14992 if (dict_find(argvars[4].vval.v_dict,
14993 (char_u *)"conceal", -1) != NULL)
14994 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14995 (char_u *)"conceal", FALSE);
14996 }
14997 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014998 }
14999 if (error == TRUE)
15000 return;
15001
15002 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15003 if (id == 1 || id == 2)
15004 {
15005 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15006 return;
15007 }
15008
Bram Moolenaar6561d522015-07-21 15:48:27 +020015009 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15010 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015011#endif
15012}
15013
15014/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015015 * "matcharg()" function
15016 */
15017 static void
15018f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015019 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015020 typval_T *rettv;
15021{
15022 if (rettv_list_alloc(rettv) == OK)
15023 {
15024#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015025 int id = get_tv_number(&argvars[0]);
15026 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015027
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015028 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015029 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015030 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15031 {
15032 list_append_string(rettv->vval.v_list,
15033 syn_id2name(m->hlg_id), -1);
15034 list_append_string(rettv->vval.v_list, m->pattern, -1);
15035 }
15036 else
15037 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015038 list_append_string(rettv->vval.v_list, NULL, -1);
15039 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015040 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015041 }
15042#endif
15043 }
15044}
15045
15046/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015047 * "matchdelete()" function
15048 */
15049 static void
15050f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015051 typval_T *argvars UNUSED;
15052 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015053{
15054#ifdef FEAT_SEARCH_EXTRA
15055 rettv->vval.v_number = match_delete(curwin,
15056 (int)get_tv_number(&argvars[0]), TRUE);
15057#endif
15058}
15059
15060/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061 * "matchend()" function
15062 */
15063 static void
15064f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015065 typval_T *argvars;
15066 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067{
15068 find_some_match(argvars, rettv, 0);
15069}
15070
15071/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015072 * "matchlist()" function
15073 */
15074 static void
15075f_matchlist(argvars, rettv)
15076 typval_T *argvars;
15077 typval_T *rettv;
15078{
15079 find_some_match(argvars, rettv, 3);
15080}
15081
15082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 * "matchstr()" function
15084 */
15085 static void
15086f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015087 typval_T *argvars;
15088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089{
15090 find_some_match(argvars, rettv, 2);
15091}
15092
Bram Moolenaar33570922005-01-25 22:26:29 +000015093static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015094
15095 static void
15096max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015097 typval_T *argvars;
15098 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015099 int domax;
15100{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015101 long n = 0;
15102 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015104
15105 if (argvars[0].v_type == VAR_LIST)
15106 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015107 list_T *l;
15108 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015109
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015110 l = argvars[0].vval.v_list;
15111 if (l != NULL)
15112 {
15113 li = l->lv_first;
15114 if (li != NULL)
15115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015116 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015117 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015118 {
15119 li = li->li_next;
15120 if (li == NULL)
15121 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015122 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015123 if (domax ? i > n : i < n)
15124 n = i;
15125 }
15126 }
15127 }
15128 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015129 else if (argvars[0].v_type == VAR_DICT)
15130 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015131 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015132 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015133 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015134 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015135
15136 d = argvars[0].vval.v_dict;
15137 if (d != NULL)
15138 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015139 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015140 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015141 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015142 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015143 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015144 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015145 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015146 if (first)
15147 {
15148 n = i;
15149 first = FALSE;
15150 }
15151 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015152 n = i;
15153 }
15154 }
15155 }
15156 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015157 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015158 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015159 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015160}
15161
15162/*
15163 * "max()" function
15164 */
15165 static void
15166f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015167 typval_T *argvars;
15168 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015169{
15170 max_min(argvars, rettv, TRUE);
15171}
15172
15173/*
15174 * "min()" function
15175 */
15176 static void
15177f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015178 typval_T *argvars;
15179 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015180{
15181 max_min(argvars, rettv, FALSE);
15182}
15183
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015184static int mkdir_recurse __ARGS((char_u *dir, int prot));
15185
15186/*
15187 * Create the directory in which "dir" is located, and higher levels when
15188 * needed.
15189 */
15190 static int
15191mkdir_recurse(dir, prot)
15192 char_u *dir;
15193 int prot;
15194{
15195 char_u *p;
15196 char_u *updir;
15197 int r = FAIL;
15198
15199 /* Get end of directory name in "dir".
15200 * We're done when it's "/" or "c:/". */
15201 p = gettail_sep(dir);
15202 if (p <= get_past_head(dir))
15203 return OK;
15204
15205 /* If the directory exists we're done. Otherwise: create it.*/
15206 updir = vim_strnsave(dir, (int)(p - dir));
15207 if (updir == NULL)
15208 return FAIL;
15209 if (mch_isdir(updir))
15210 r = OK;
15211 else if (mkdir_recurse(updir, prot) == OK)
15212 r = vim_mkdir_emsg(updir, prot);
15213 vim_free(updir);
15214 return r;
15215}
15216
15217#ifdef vim_mkdir
15218/*
15219 * "mkdir()" function
15220 */
15221 static void
15222f_mkdir(argvars, rettv)
15223 typval_T *argvars;
15224 typval_T *rettv;
15225{
15226 char_u *dir;
15227 char_u buf[NUMBUFLEN];
15228 int prot = 0755;
15229
15230 rettv->vval.v_number = FAIL;
15231 if (check_restricted() || check_secure())
15232 return;
15233
15234 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015235 if (*dir == NUL)
15236 rettv->vval.v_number = FAIL;
15237 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015238 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015239 if (*gettail(dir) == NUL)
15240 /* remove trailing slashes */
15241 *gettail_sep(dir) = NUL;
15242
15243 if (argvars[1].v_type != VAR_UNKNOWN)
15244 {
15245 if (argvars[2].v_type != VAR_UNKNOWN)
15246 prot = get_tv_number_chk(&argvars[2], NULL);
15247 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15248 mkdir_recurse(dir, prot);
15249 }
15250 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015251 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015252}
15253#endif
15254
Bram Moolenaar0d660222005-01-07 21:51:51 +000015255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 * "mode()" function
15257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 typval_T *argvars;
15261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015263 char_u buf[3];
15264
15265 buf[1] = NUL;
15266 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 if (VIsual_active)
15269 {
15270 if (VIsual_select)
15271 buf[0] = VIsual_mode + 's' - 'v';
15272 else
15273 buf[0] = VIsual_mode;
15274 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015275 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015276 || State == CONFIRM)
15277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015279 if (State == ASKMORE)
15280 buf[1] = 'm';
15281 else if (State == CONFIRM)
15282 buf[1] = '?';
15283 }
15284 else if (State == EXTERNCMD)
15285 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286 else if (State & INSERT)
15287 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015288#ifdef FEAT_VREPLACE
15289 if (State & VREPLACE_FLAG)
15290 {
15291 buf[0] = 'R';
15292 buf[1] = 'v';
15293 }
15294 else
15295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296 if (State & REPLACE_FLAG)
15297 buf[0] = 'R';
15298 else
15299 buf[0] = 'i';
15300 }
15301 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015304 if (exmode_active)
15305 buf[1] = 'v';
15306 }
15307 else if (exmode_active)
15308 {
15309 buf[0] = 'c';
15310 buf[1] = 'e';
15311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015315 if (finish_op)
15316 buf[1] = 'o';
15317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015319 /* Clear out the minor mode when the argument is not a non-zero number or
15320 * non-empty string. */
15321 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015322 buf[1] = NUL;
15323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015324 rettv->vval.v_string = vim_strsave(buf);
15325 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326}
15327
Bram Moolenaar429fa852013-04-15 12:27:36 +020015328#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015329/*
15330 * "mzeval()" function
15331 */
15332 static void
15333f_mzeval(argvars, rettv)
15334 typval_T *argvars;
15335 typval_T *rettv;
15336{
15337 char_u *str;
15338 char_u buf[NUMBUFLEN];
15339
15340 str = get_tv_string_buf(&argvars[0], buf);
15341 do_mzeval(str, rettv);
15342}
Bram Moolenaar75676462013-01-30 14:55:42 +010015343
15344 void
15345mzscheme_call_vim(name, args, rettv)
15346 char_u *name;
15347 typval_T *args;
15348 typval_T *rettv;
15349{
15350 typval_T argvars[3];
15351
15352 argvars[0].v_type = VAR_STRING;
15353 argvars[0].vval.v_string = name;
15354 copy_tv(args, &argvars[1]);
15355 argvars[2].v_type = VAR_UNKNOWN;
15356 f_call(argvars, rettv);
15357 clear_tv(&argvars[1]);
15358}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015359#endif
15360
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015362 * "nextnonblank()" function
15363 */
15364 static void
15365f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015366 typval_T *argvars;
15367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015368{
15369 linenr_T lnum;
15370
15371 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015373 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015374 {
15375 lnum = 0;
15376 break;
15377 }
15378 if (*skipwhite(ml_get(lnum)) != NUL)
15379 break;
15380 }
15381 rettv->vval.v_number = lnum;
15382}
15383
15384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 * "nr2char()" function
15386 */
15387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015389 typval_T *argvars;
15390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391{
15392 char_u buf[NUMBUFLEN];
15393
15394#ifdef FEAT_MBYTE
15395 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015396 {
15397 int utf8 = 0;
15398
15399 if (argvars[1].v_type != VAR_UNKNOWN)
15400 utf8 = get_tv_number_chk(&argvars[1], NULL);
15401 if (utf8)
15402 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15403 else
15404 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 else
15407#endif
15408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 buf[1] = NUL;
15411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412 rettv->v_type = VAR_STRING;
15413 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015414}
15415
15416/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015417 * "or(expr, expr)" function
15418 */
15419 static void
15420f_or(argvars, rettv)
15421 typval_T *argvars;
15422 typval_T *rettv;
15423{
15424 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15425 | get_tv_number_chk(&argvars[1], NULL);
15426}
15427
15428/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015429 * "pathshorten()" function
15430 */
15431 static void
15432f_pathshorten(argvars, rettv)
15433 typval_T *argvars;
15434 typval_T *rettv;
15435{
15436 char_u *p;
15437
15438 rettv->v_type = VAR_STRING;
15439 p = get_tv_string_chk(&argvars[0]);
15440 if (p == NULL)
15441 rettv->vval.v_string = NULL;
15442 else
15443 {
15444 p = vim_strsave(p);
15445 rettv->vval.v_string = p;
15446 if (p != NULL)
15447 shorten_dir(p);
15448 }
15449}
15450
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015451#ifdef FEAT_FLOAT
15452/*
15453 * "pow()" function
15454 */
15455 static void
15456f_pow(argvars, rettv)
15457 typval_T *argvars;
15458 typval_T *rettv;
15459{
15460 float_T fx, fy;
15461
15462 rettv->v_type = VAR_FLOAT;
15463 if (get_float_arg(argvars, &fx) == OK
15464 && get_float_arg(&argvars[1], &fy) == OK)
15465 rettv->vval.v_float = pow(fx, fy);
15466 else
15467 rettv->vval.v_float = 0.0;
15468}
15469#endif
15470
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015471/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015472 * "prevnonblank()" function
15473 */
15474 static void
15475f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015476 typval_T *argvars;
15477 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015478{
15479 linenr_T lnum;
15480
15481 lnum = get_tv_lnum(argvars);
15482 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15483 lnum = 0;
15484 else
15485 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15486 --lnum;
15487 rettv->vval.v_number = lnum;
15488}
15489
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015490#ifdef HAVE_STDARG_H
15491/* This dummy va_list is here because:
15492 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15493 * - locally in the function results in a "used before set" warning
15494 * - using va_start() to initialize it gives "function with fixed args" error */
15495static va_list ap;
15496#endif
15497
Bram Moolenaar8c711452005-01-14 21:53:12 +000015498/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015499 * "printf()" function
15500 */
15501 static void
15502f_printf(argvars, rettv)
15503 typval_T *argvars;
15504 typval_T *rettv;
15505{
15506 rettv->v_type = VAR_STRING;
15507 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015508#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015509 {
15510 char_u buf[NUMBUFLEN];
15511 int len;
15512 char_u *s;
15513 int saved_did_emsg = did_emsg;
15514 char *fmt;
15515
15516 /* Get the required length, allocate the buffer and do it for real. */
15517 did_emsg = FALSE;
15518 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015519 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015520 if (!did_emsg)
15521 {
15522 s = alloc(len + 1);
15523 if (s != NULL)
15524 {
15525 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015526 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015527 }
15528 }
15529 did_emsg |= saved_did_emsg;
15530 }
15531#endif
15532}
15533
15534/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015535 * "pumvisible()" function
15536 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015537 static void
15538f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015539 typval_T *argvars UNUSED;
15540 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015541{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015542#ifdef FEAT_INS_EXPAND
15543 if (pum_visible())
15544 rettv->vval.v_number = 1;
15545#endif
15546}
15547
Bram Moolenaardb913952012-06-29 12:54:53 +020015548#ifdef FEAT_PYTHON3
15549/*
15550 * "py3eval()" function
15551 */
15552 static void
15553f_py3eval(argvars, rettv)
15554 typval_T *argvars;
15555 typval_T *rettv;
15556{
15557 char_u *str;
15558 char_u buf[NUMBUFLEN];
15559
15560 str = get_tv_string_buf(&argvars[0], buf);
15561 do_py3eval(str, rettv);
15562}
15563#endif
15564
15565#ifdef FEAT_PYTHON
15566/*
15567 * "pyeval()" function
15568 */
15569 static void
15570f_pyeval(argvars, rettv)
15571 typval_T *argvars;
15572 typval_T *rettv;
15573{
15574 char_u *str;
15575 char_u buf[NUMBUFLEN];
15576
15577 str = get_tv_string_buf(&argvars[0], buf);
15578 do_pyeval(str, rettv);
15579}
15580#endif
15581
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015582/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015583 * "range()" function
15584 */
15585 static void
15586f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015587 typval_T *argvars;
15588 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015589{
15590 long start;
15591 long end;
15592 long stride = 1;
15593 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015596 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015597 if (argvars[1].v_type == VAR_UNKNOWN)
15598 {
15599 end = start - 1;
15600 start = 0;
15601 }
15602 else
15603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015604 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015605 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015606 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015607 }
15608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015609 if (error)
15610 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015611 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015612 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015613 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015614 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015615 else
15616 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015617 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015618 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015619 if (list_append_number(rettv->vval.v_list,
15620 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015621 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015622 }
15623}
15624
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015625/*
15626 * "readfile()" function
15627 */
15628 static void
15629f_readfile(argvars, rettv)
15630 typval_T *argvars;
15631 typval_T *rettv;
15632{
15633 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015634 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015635 char_u *fname;
15636 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015637 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15638 int io_size = sizeof(buf);
15639 int readlen; /* size of last fread() */
15640 char_u *prev = NULL; /* previously read bytes, if any */
15641 long prevlen = 0; /* length of data in prev */
15642 long prevsize = 0; /* size of prev buffer */
15643 long maxline = MAXLNUM;
15644 long cnt = 0;
15645 char_u *p; /* position in buf */
15646 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015647
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015648 if (argvars[1].v_type != VAR_UNKNOWN)
15649 {
15650 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15651 binary = TRUE;
15652 if (argvars[2].v_type != VAR_UNKNOWN)
15653 maxline = get_tv_number(&argvars[2]);
15654 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015655
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015656 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015657 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015658
15659 /* Always open the file in binary mode, library functions have a mind of
15660 * their own about CR-LF conversion. */
15661 fname = get_tv_string(&argvars[0]);
15662 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15663 {
15664 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15665 return;
15666 }
15667
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015668 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015669 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015670 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015671
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015672 /* This for loop processes what was read, but is also entered at end
15673 * of file so that either:
15674 * - an incomplete line gets written
15675 * - a "binary" file gets an empty line at the end if it ends in a
15676 * newline. */
15677 for (p = buf, start = buf;
15678 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15679 ++p)
15680 {
15681 if (*p == '\n' || readlen <= 0)
15682 {
15683 listitem_T *li;
15684 char_u *s = NULL;
15685 long_u len = p - start;
15686
15687 /* Finished a line. Remove CRs before NL. */
15688 if (readlen > 0 && !binary)
15689 {
15690 while (len > 0 && start[len - 1] == '\r')
15691 --len;
15692 /* removal may cross back to the "prev" string */
15693 if (len == 0)
15694 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15695 --prevlen;
15696 }
15697 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015698 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015699 else
15700 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015701 /* Change "prev" buffer to be the right size. This way
15702 * the bytes are only copied once, and very long lines are
15703 * allocated only once. */
15704 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015705 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015706 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015707 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015708 prev = NULL; /* the list will own the string */
15709 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015710 }
15711 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015712 if (s == NULL)
15713 {
15714 do_outofmem_msg((long_u) prevlen + len + 1);
15715 failed = TRUE;
15716 break;
15717 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015718
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015719 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015720 {
15721 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015722 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015723 break;
15724 }
15725 li->li_tv.v_type = VAR_STRING;
15726 li->li_tv.v_lock = 0;
15727 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015728 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015729
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015730 start = p + 1; /* step over newline */
15731 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015732 break;
15733 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015734 else if (*p == NUL)
15735 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015736#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015737 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15738 * when finding the BF and check the previous two bytes. */
15739 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015740 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015741 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15742 * + 1, these may be in the "prev" string. */
15743 char_u back1 = p >= buf + 1 ? p[-1]
15744 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15745 char_u back2 = p >= buf + 2 ? p[-2]
15746 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15747 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015748
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015749 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015750 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015751 char_u *dest = p - 2;
15752
15753 /* Usually a BOM is at the beginning of a file, and so at
15754 * the beginning of a line; then we can just step over it.
15755 */
15756 if (start == dest)
15757 start = p + 1;
15758 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015759 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015760 /* have to shuffle buf to close gap */
15761 int adjust_prevlen = 0;
15762
15763 if (dest < buf)
15764 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015765 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015766 dest = buf;
15767 }
15768 if (readlen > p - buf + 1)
15769 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15770 readlen -= 3 - adjust_prevlen;
15771 prevlen -= adjust_prevlen;
15772 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015773 }
15774 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015775 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015776#endif
15777 } /* for */
15778
15779 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15780 break;
15781 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015782 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015783 /* There's part of a line in buf, store it in "prev". */
15784 if (p - start + prevlen >= prevsize)
15785 {
15786 /* need bigger "prev" buffer */
15787 char_u *newprev;
15788
15789 /* A common use case is ordinary text files and "prev" gets a
15790 * fragment of a line, so the first allocation is made
15791 * small, to avoid repeatedly 'allocing' large and
15792 * 'reallocing' small. */
15793 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015794 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015795 else
15796 {
15797 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015798 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015799 prevsize = grow50pc > growmin ? grow50pc : growmin;
15800 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015801 newprev = prev == NULL ? alloc(prevsize)
15802 : vim_realloc(prev, prevsize);
15803 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015804 {
15805 do_outofmem_msg((long_u)prevsize);
15806 failed = TRUE;
15807 break;
15808 }
15809 prev = newprev;
15810 }
15811 /* Add the line part to end of "prev". */
15812 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015813 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015814 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015815 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015816
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015817 /*
15818 * For a negative line count use only the lines at the end of the file,
15819 * free the rest.
15820 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015821 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015822 while (cnt > -maxline)
15823 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015824 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015825 --cnt;
15826 }
15827
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015828 if (failed)
15829 {
15830 list_free(rettv->vval.v_list, TRUE);
15831 /* readfile doc says an empty list is returned on error */
15832 rettv->vval.v_list = list_alloc();
15833 }
15834
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015835 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015836 fclose(fd);
15837}
15838
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015839#if defined(FEAT_RELTIME)
15840static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15841
15842/*
15843 * Convert a List to proftime_T.
15844 * Return FAIL when there is something wrong.
15845 */
15846 static int
15847list2proftime(arg, tm)
15848 typval_T *arg;
15849 proftime_T *tm;
15850{
15851 long n1, n2;
15852 int error = FALSE;
15853
15854 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15855 || arg->vval.v_list->lv_len != 2)
15856 return FAIL;
15857 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15858 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15859# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015860 tm->HighPart = n1;
15861 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015862# else
15863 tm->tv_sec = n1;
15864 tm->tv_usec = n2;
15865# endif
15866 return error ? FAIL : OK;
15867}
15868#endif /* FEAT_RELTIME */
15869
15870/*
15871 * "reltime()" function
15872 */
15873 static void
15874f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015875 typval_T *argvars UNUSED;
15876 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015877{
15878#ifdef FEAT_RELTIME
15879 proftime_T res;
15880 proftime_T start;
15881
15882 if (argvars[0].v_type == VAR_UNKNOWN)
15883 {
15884 /* No arguments: get current time. */
15885 profile_start(&res);
15886 }
15887 else if (argvars[1].v_type == VAR_UNKNOWN)
15888 {
15889 if (list2proftime(&argvars[0], &res) == FAIL)
15890 return;
15891 profile_end(&res);
15892 }
15893 else
15894 {
15895 /* Two arguments: compute the difference. */
15896 if (list2proftime(&argvars[0], &start) == FAIL
15897 || list2proftime(&argvars[1], &res) == FAIL)
15898 return;
15899 profile_sub(&res, &start);
15900 }
15901
15902 if (rettv_list_alloc(rettv) == OK)
15903 {
15904 long n1, n2;
15905
15906# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015907 n1 = res.HighPart;
15908 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015909# else
15910 n1 = res.tv_sec;
15911 n2 = res.tv_usec;
15912# endif
15913 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15914 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15915 }
15916#endif
15917}
15918
15919/*
15920 * "reltimestr()" function
15921 */
15922 static void
15923f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015924 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015925 typval_T *rettv;
15926{
15927#ifdef FEAT_RELTIME
15928 proftime_T tm;
15929#endif
15930
15931 rettv->v_type = VAR_STRING;
15932 rettv->vval.v_string = NULL;
15933#ifdef FEAT_RELTIME
15934 if (list2proftime(&argvars[0], &tm) == OK)
15935 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15936#endif
15937}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015938
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15940static void make_connection __ARGS((void));
15941static int check_connection __ARGS((void));
15942
15943 static void
15944make_connection()
15945{
15946 if (X_DISPLAY == NULL
15947# ifdef FEAT_GUI
15948 && !gui.in_use
15949# endif
15950 )
15951 {
15952 x_force_connect = TRUE;
15953 setup_term_clip();
15954 x_force_connect = FALSE;
15955 }
15956}
15957
15958 static int
15959check_connection()
15960{
15961 make_connection();
15962 if (X_DISPLAY == NULL)
15963 {
15964 EMSG(_("E240: No connection to Vim server"));
15965 return FAIL;
15966 }
15967 return OK;
15968}
15969#endif
15970
15971#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015972static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015973
15974 static void
15975remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 typval_T *argvars;
15977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015978 int expr;
15979{
15980 char_u *server_name;
15981 char_u *keys;
15982 char_u *r = NULL;
15983 char_u buf[NUMBUFLEN];
15984# ifdef WIN32
15985 HWND w;
15986# else
15987 Window w;
15988# endif
15989
15990 if (check_restricted() || check_secure())
15991 return;
15992
15993# ifdef FEAT_X11
15994 if (check_connection() == FAIL)
15995 return;
15996# endif
15997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015998 server_name = get_tv_string_chk(&argvars[0]);
15999 if (server_name == NULL)
16000 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016001 keys = get_tv_string_buf(&argvars[1], buf);
16002# ifdef WIN32
16003 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16004# else
16005 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16006 < 0)
16007# endif
16008 {
16009 if (r != NULL)
16010 EMSG(r); /* sending worked but evaluation failed */
16011 else
16012 EMSG2(_("E241: Unable to send to %s"), server_name);
16013 return;
16014 }
16015
16016 rettv->vval.v_string = r;
16017
16018 if (argvars[2].v_type != VAR_UNKNOWN)
16019 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016020 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016021 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016022 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016023
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016024 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016025 v.di_tv.v_type = VAR_STRING;
16026 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016027 idvar = get_tv_string_chk(&argvars[2]);
16028 if (idvar != NULL)
16029 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016030 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016031 }
16032}
16033#endif
16034
16035/*
16036 * "remote_expr()" function
16037 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016038 static void
16039f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016040 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016041 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016042{
16043 rettv->v_type = VAR_STRING;
16044 rettv->vval.v_string = NULL;
16045#ifdef FEAT_CLIENTSERVER
16046 remote_common(argvars, rettv, TRUE);
16047#endif
16048}
16049
16050/*
16051 * "remote_foreground()" function
16052 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016053 static void
16054f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016055 typval_T *argvars UNUSED;
16056 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016057{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058#ifdef FEAT_CLIENTSERVER
16059# ifdef WIN32
16060 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016061 {
16062 char_u *server_name = get_tv_string_chk(&argvars[0]);
16063
16064 if (server_name != NULL)
16065 serverForeground(server_name);
16066 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016067# else
16068 /* Send a foreground() expression to the server. */
16069 argvars[1].v_type = VAR_STRING;
16070 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16071 argvars[2].v_type = VAR_UNKNOWN;
16072 remote_common(argvars, rettv, TRUE);
16073 vim_free(argvars[1].vval.v_string);
16074# endif
16075#endif
16076}
16077
Bram Moolenaar0d660222005-01-07 21:51:51 +000016078 static void
16079f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016080 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016081 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016082{
16083#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016084 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085 char_u *s = NULL;
16086# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016087 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016089 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090
16091 if (check_restricted() || check_secure())
16092 {
16093 rettv->vval.v_number = -1;
16094 return;
16095 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016096 serverid = get_tv_string_chk(&argvars[0]);
16097 if (serverid == NULL)
16098 {
16099 rettv->vval.v_number = -1;
16100 return; /* type error; errmsg already given */
16101 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016102# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016103 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104 if (n == 0)
16105 rettv->vval.v_number = -1;
16106 else
16107 {
16108 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16109 rettv->vval.v_number = (s != NULL);
16110 }
16111# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016112 if (check_connection() == FAIL)
16113 return;
16114
16115 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016116 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016117# endif
16118
16119 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016121 char_u *retvar;
16122
Bram Moolenaar33570922005-01-25 22:26:29 +000016123 v.di_tv.v_type = VAR_STRING;
16124 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016125 retvar = get_tv_string_chk(&argvars[1]);
16126 if (retvar != NULL)
16127 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016128 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129 }
16130#else
16131 rettv->vval.v_number = -1;
16132#endif
16133}
16134
Bram Moolenaar0d660222005-01-07 21:51:51 +000016135 static void
16136f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016139{
16140 char_u *r = NULL;
16141
16142#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016143 char_u *serverid = get_tv_string_chk(&argvars[0]);
16144
16145 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146 {
16147# ifdef WIN32
16148 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016149 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016151 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152 if (n != 0)
16153 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16154 if (r == NULL)
16155# else
16156 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016157 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158# endif
16159 EMSG(_("E277: Unable to read a server reply"));
16160 }
16161#endif
16162 rettv->v_type = VAR_STRING;
16163 rettv->vval.v_string = r;
16164}
16165
16166/*
16167 * "remote_send()" function
16168 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169 static void
16170f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016171 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173{
16174 rettv->v_type = VAR_STRING;
16175 rettv->vval.v_string = NULL;
16176#ifdef FEAT_CLIENTSERVER
16177 remote_common(argvars, rettv, FALSE);
16178#endif
16179}
16180
16181/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016182 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016183 */
16184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016186 typval_T *argvars;
16187 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016188{
Bram Moolenaar33570922005-01-25 22:26:29 +000016189 list_T *l;
16190 listitem_T *item, *item2;
16191 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016192 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016193 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016194 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016195 dict_T *d;
16196 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016197 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016198
Bram Moolenaar8c711452005-01-14 21:53:12 +000016199 if (argvars[0].v_type == VAR_DICT)
16200 {
16201 if (argvars[2].v_type != VAR_UNKNOWN)
16202 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016203 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016204 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016206 key = get_tv_string_chk(&argvars[1]);
16207 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016209 di = dict_find(d, key, -1);
16210 if (di == NULL)
16211 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016212 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16213 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016214 {
16215 *rettv = di->di_tv;
16216 init_tv(&di->di_tv);
16217 dictitem_remove(d, di);
16218 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016219 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016220 }
16221 }
16222 else if (argvars[0].v_type != VAR_LIST)
16223 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016224 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016225 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 int error = FALSE;
16228
16229 idx = get_tv_number_chk(&argvars[1], &error);
16230 if (error)
16231 ; /* type error: do nothing, errmsg already given */
16232 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016233 EMSGN(_(e_listidx), idx);
16234 else
16235 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016236 if (argvars[2].v_type == VAR_UNKNOWN)
16237 {
16238 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016239 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016240 *rettv = item->li_tv;
16241 vim_free(item);
16242 }
16243 else
16244 {
16245 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 end = get_tv_number_chk(&argvars[2], &error);
16247 if (error)
16248 ; /* type error: do nothing */
16249 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016250 EMSGN(_(e_listidx), end);
16251 else
16252 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016253 int cnt = 0;
16254
16255 for (li = item; li != NULL; li = li->li_next)
16256 {
16257 ++cnt;
16258 if (li == item2)
16259 break;
16260 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016261 if (li == NULL) /* didn't find "item2" after "item" */
16262 EMSG(_(e_invrange));
16263 else
16264 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016265 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016266 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016267 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016268 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016269 l->lv_first = item;
16270 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016271 item->li_prev = NULL;
16272 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016273 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016274 }
16275 }
16276 }
16277 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016278 }
16279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280}
16281
16282/*
16283 * "rename({from}, {to})" function
16284 */
16285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016286f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016287 typval_T *argvars;
16288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289{
16290 char_u buf[NUMBUFLEN];
16291
16292 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016293 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016295 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16296 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297}
16298
16299/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016300 * "repeat()" function
16301 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016302 static void
16303f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 typval_T *argvars;
16305 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016306{
16307 char_u *p;
16308 int n;
16309 int slen;
16310 int len;
16311 char_u *r;
16312 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016313
16314 n = get_tv_number(&argvars[1]);
16315 if (argvars[0].v_type == VAR_LIST)
16316 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016317 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016318 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016319 if (list_extend(rettv->vval.v_list,
16320 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016321 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016322 }
16323 else
16324 {
16325 p = get_tv_string(&argvars[0]);
16326 rettv->v_type = VAR_STRING;
16327 rettv->vval.v_string = NULL;
16328
16329 slen = (int)STRLEN(p);
16330 len = slen * n;
16331 if (len <= 0)
16332 return;
16333
16334 r = alloc(len + 1);
16335 if (r != NULL)
16336 {
16337 for (i = 0; i < n; i++)
16338 mch_memmove(r + i * slen, p, (size_t)slen);
16339 r[len] = NUL;
16340 }
16341
16342 rettv->vval.v_string = r;
16343 }
16344}
16345
16346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 * "resolve()" function
16348 */
16349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016351 typval_T *argvars;
16352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353{
16354 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016355#ifdef HAVE_READLINK
16356 char_u *buf = NULL;
16357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016359 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360#ifdef FEAT_SHORTCUT
16361 {
16362 char_u *v = NULL;
16363
16364 v = mch_resolve_shortcut(p);
16365 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016366 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016368 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 }
16370#else
16371# ifdef HAVE_READLINK
16372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373 char_u *cpy;
16374 int len;
16375 char_u *remain = NULL;
16376 char_u *q;
16377 int is_relative_to_current = FALSE;
16378 int has_trailing_pathsep = FALSE;
16379 int limit = 100;
16380
16381 p = vim_strsave(p);
16382
16383 if (p[0] == '.' && (vim_ispathsep(p[1])
16384 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16385 is_relative_to_current = TRUE;
16386
16387 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016388 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016391 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393
16394 q = getnextcomp(p);
16395 if (*q != NUL)
16396 {
16397 /* Separate the first path component in "p", and keep the
16398 * remainder (beginning with the path separator). */
16399 remain = vim_strsave(q - 1);
16400 q[-1] = NUL;
16401 }
16402
Bram Moolenaard9462e32011-04-11 21:35:11 +020016403 buf = alloc(MAXPATHL + 1);
16404 if (buf == NULL)
16405 goto fail;
16406
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 for (;;)
16408 {
16409 for (;;)
16410 {
16411 len = readlink((char *)p, (char *)buf, MAXPATHL);
16412 if (len <= 0)
16413 break;
16414 buf[len] = NUL;
16415
16416 if (limit-- == 0)
16417 {
16418 vim_free(p);
16419 vim_free(remain);
16420 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422 goto fail;
16423 }
16424
16425 /* Ensure that the result will have a trailing path separator
16426 * if the argument has one. */
16427 if (remain == NULL && has_trailing_pathsep)
16428 add_pathsep(buf);
16429
16430 /* Separate the first path component in the link value and
16431 * concatenate the remainders. */
16432 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16433 if (*q != NUL)
16434 {
16435 if (remain == NULL)
16436 remain = vim_strsave(q - 1);
16437 else
16438 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016439 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 if (cpy != NULL)
16441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442 vim_free(remain);
16443 remain = cpy;
16444 }
16445 }
16446 q[-1] = NUL;
16447 }
16448
16449 q = gettail(p);
16450 if (q > p && *q == NUL)
16451 {
16452 /* Ignore trailing path separator. */
16453 q[-1] = NUL;
16454 q = gettail(p);
16455 }
16456 if (q > p && !mch_isFullName(buf))
16457 {
16458 /* symlink is relative to directory of argument */
16459 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16460 if (cpy != NULL)
16461 {
16462 STRCPY(cpy, p);
16463 STRCPY(gettail(cpy), buf);
16464 vim_free(p);
16465 p = cpy;
16466 }
16467 }
16468 else
16469 {
16470 vim_free(p);
16471 p = vim_strsave(buf);
16472 }
16473 }
16474
16475 if (remain == NULL)
16476 break;
16477
16478 /* Append the first path component of "remain" to "p". */
16479 q = getnextcomp(remain + 1);
16480 len = q - remain - (*q != NUL);
16481 cpy = vim_strnsave(p, STRLEN(p) + len);
16482 if (cpy != NULL)
16483 {
16484 STRNCAT(cpy, remain, len);
16485 vim_free(p);
16486 p = cpy;
16487 }
16488 /* Shorten "remain". */
16489 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016490 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 else
16492 {
16493 vim_free(remain);
16494 remain = NULL;
16495 }
16496 }
16497
16498 /* If the result is a relative path name, make it explicitly relative to
16499 * the current directory if and only if the argument had this form. */
16500 if (!vim_ispathsep(*p))
16501 {
16502 if (is_relative_to_current
16503 && *p != NUL
16504 && !(p[0] == '.'
16505 && (p[1] == NUL
16506 || vim_ispathsep(p[1])
16507 || (p[1] == '.'
16508 && (p[2] == NUL
16509 || vim_ispathsep(p[2]))))))
16510 {
16511 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016512 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 if (cpy != NULL)
16514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515 vim_free(p);
16516 p = cpy;
16517 }
16518 }
16519 else if (!is_relative_to_current)
16520 {
16521 /* Strip leading "./". */
16522 q = p;
16523 while (q[0] == '.' && vim_ispathsep(q[1]))
16524 q += 2;
16525 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016526 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016527 }
16528 }
16529
16530 /* Ensure that the result will have no trailing path separator
16531 * if the argument had none. But keep "/" or "//". */
16532 if (!has_trailing_pathsep)
16533 {
16534 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016535 if (after_pathsep(p, q))
16536 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 }
16538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016539 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 }
16541# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543# endif
16544#endif
16545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016546 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547
16548#ifdef HAVE_READLINK
16549fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016550 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016552 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553}
16554
16555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016556 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 */
16558 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016560 typval_T *argvars;
16561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562{
Bram Moolenaar33570922005-01-25 22:26:29 +000016563 list_T *l;
16564 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565
Bram Moolenaar0d660222005-01-07 21:51:51 +000016566 if (argvars[0].v_type != VAR_LIST)
16567 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016568 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016569 && !tv_check_lock(l->lv_lock,
16570 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016571 {
16572 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016573 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016574 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016575 while (li != NULL)
16576 {
16577 ni = li->li_prev;
16578 list_append(l, li);
16579 li = ni;
16580 }
16581 rettv->vval.v_list = l;
16582 rettv->v_type = VAR_LIST;
16583 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016584 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586}
16587
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016588#define SP_NOMOVE 0x01 /* don't move cursor */
16589#define SP_REPEAT 0x02 /* repeat to find outer pair */
16590#define SP_RETCOUNT 0x04 /* return matchcount */
16591#define SP_SETPCMARK 0x08 /* set previous context mark */
16592#define SP_START 0x10 /* accept match at start position */
16593#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16594#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016595#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016596
Bram Moolenaar33570922005-01-25 22:26:29 +000016597static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598
16599/*
16600 * Get flags for a search function.
16601 * Possibly sets "p_ws".
16602 * Returns BACKWARD, FORWARD or zero (for an error).
16603 */
16604 static int
16605get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016606 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607 int *flagsp;
16608{
16609 int dir = FORWARD;
16610 char_u *flags;
16611 char_u nbuf[NUMBUFLEN];
16612 int mask;
16613
16614 if (varp->v_type != VAR_UNKNOWN)
16615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016616 flags = get_tv_string_buf_chk(varp, nbuf);
16617 if (flags == NULL)
16618 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016619 while (*flags != NUL)
16620 {
16621 switch (*flags)
16622 {
16623 case 'b': dir = BACKWARD; break;
16624 case 'w': p_ws = TRUE; break;
16625 case 'W': p_ws = FALSE; break;
16626 default: mask = 0;
16627 if (flagsp != NULL)
16628 switch (*flags)
16629 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016630 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016631 case 'e': mask = SP_END; break;
16632 case 'm': mask = SP_RETCOUNT; break;
16633 case 'n': mask = SP_NOMOVE; break;
16634 case 'p': mask = SP_SUBPAT; break;
16635 case 'r': mask = SP_REPEAT; break;
16636 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016637 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016638 }
16639 if (mask == 0)
16640 {
16641 EMSG2(_(e_invarg2), flags);
16642 dir = 0;
16643 }
16644 else
16645 *flagsp |= mask;
16646 }
16647 if (dir == 0)
16648 break;
16649 ++flags;
16650 }
16651 }
16652 return dir;
16653}
16654
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016656 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016658 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016659search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016660 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016661 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016662 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016664 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 char_u *pat;
16666 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016667 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 int save_p_ws = p_ws;
16669 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016670 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016671 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016672 proftime_T tm;
16673#ifdef FEAT_RELTIME
16674 long time_limit = 0;
16675#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016676 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016677 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016679 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016680 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016681 if (dir == 0)
16682 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016683 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016684 if (flags & SP_START)
16685 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016686 if (flags & SP_END)
16687 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016688 if (flags & SP_COLUMN)
16689 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016690
Bram Moolenaar76929292008-01-06 19:07:36 +000016691 /* Optional arguments: line number to stop searching and timeout. */
16692 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016693 {
16694 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16695 if (lnum_stop < 0)
16696 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016697#ifdef FEAT_RELTIME
16698 if (argvars[3].v_type != VAR_UNKNOWN)
16699 {
16700 time_limit = get_tv_number_chk(&argvars[3], NULL);
16701 if (time_limit < 0)
16702 goto theend;
16703 }
16704#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016705 }
16706
Bram Moolenaar76929292008-01-06 19:07:36 +000016707#ifdef FEAT_RELTIME
16708 /* Set the time limit, if there is one. */
16709 profile_setlimit(time_limit, &tm);
16710#endif
16711
Bram Moolenaar231334e2005-07-25 20:46:57 +000016712 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016713 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016714 * Check to make sure only those flags are set.
16715 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16716 * flags cannot be set. Check for that condition also.
16717 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016718 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016719 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016721 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016722 goto theend;
16723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016725 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016726 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016727 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016728 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016730 if (flags & SP_SUBPAT)
16731 retval = subpatnum;
16732 else
16733 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016734 if (flags & SP_SETPCMARK)
16735 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016737 if (match_pos != NULL)
16738 {
16739 /* Store the match cursor position */
16740 match_pos->lnum = pos.lnum;
16741 match_pos->col = pos.col + 1;
16742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 /* "/$" will put the cursor after the end of the line, may need to
16744 * correct that here */
16745 check_cursor();
16746 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016747
16748 /* If 'n' flag is used: restore cursor position. */
16749 if (flags & SP_NOMOVE)
16750 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016751 else
16752 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016753theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016755
16756 return retval;
16757}
16758
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016759#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016760
16761/*
16762 * round() is not in C90, use ceil() or floor() instead.
16763 */
16764 float_T
16765vim_round(f)
16766 float_T f;
16767{
16768 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16769}
16770
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016771/*
16772 * "round({float})" function
16773 */
16774 static void
16775f_round(argvars, rettv)
16776 typval_T *argvars;
16777 typval_T *rettv;
16778{
16779 float_T f;
16780
16781 rettv->v_type = VAR_FLOAT;
16782 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016783 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016784 else
16785 rettv->vval.v_float = 0.0;
16786}
16787#endif
16788
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016789/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016790 * "screenattr()" function
16791 */
16792 static void
16793f_screenattr(argvars, rettv)
16794 typval_T *argvars UNUSED;
16795 typval_T *rettv;
16796{
16797 int row;
16798 int col;
16799 int c;
16800
16801 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16802 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16803 if (row < 0 || row >= screen_Rows
16804 || col < 0 || col >= screen_Columns)
16805 c = -1;
16806 else
16807 c = ScreenAttrs[LineOffset[row] + col];
16808 rettv->vval.v_number = c;
16809}
16810
16811/*
16812 * "screenchar()" function
16813 */
16814 static void
16815f_screenchar(argvars, rettv)
16816 typval_T *argvars UNUSED;
16817 typval_T *rettv;
16818{
16819 int row;
16820 int col;
16821 int off;
16822 int c;
16823
16824 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16825 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16826 if (row < 0 || row >= screen_Rows
16827 || col < 0 || col >= screen_Columns)
16828 c = -1;
16829 else
16830 {
16831 off = LineOffset[row] + col;
16832#ifdef FEAT_MBYTE
16833 if (enc_utf8 && ScreenLinesUC[off] != 0)
16834 c = ScreenLinesUC[off];
16835 else
16836#endif
16837 c = ScreenLines[off];
16838 }
16839 rettv->vval.v_number = c;
16840}
16841
16842/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016843 * "screencol()" function
16844 *
16845 * First column is 1 to be consistent with virtcol().
16846 */
16847 static void
16848f_screencol(argvars, rettv)
16849 typval_T *argvars UNUSED;
16850 typval_T *rettv;
16851{
16852 rettv->vval.v_number = screen_screencol() + 1;
16853}
16854
16855/*
16856 * "screenrow()" function
16857 */
16858 static void
16859f_screenrow(argvars, rettv)
16860 typval_T *argvars UNUSED;
16861 typval_T *rettv;
16862{
16863 rettv->vval.v_number = screen_screenrow() + 1;
16864}
16865
16866/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016867 * "search()" function
16868 */
16869 static void
16870f_search(argvars, rettv)
16871 typval_T *argvars;
16872 typval_T *rettv;
16873{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016874 int flags = 0;
16875
16876 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877}
16878
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016880 * "searchdecl()" function
16881 */
16882 static void
16883f_searchdecl(argvars, rettv)
16884 typval_T *argvars;
16885 typval_T *rettv;
16886{
16887 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016888 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016889 int error = FALSE;
16890 char_u *name;
16891
16892 rettv->vval.v_number = 1; /* default: FAIL */
16893
16894 name = get_tv_string_chk(&argvars[0]);
16895 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016896 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016897 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016898 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16899 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16900 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016901 if (!error && name != NULL)
16902 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016903 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016904}
16905
16906/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016907 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016909 static int
16910searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016911 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016912 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913{
16914 char_u *spat, *mpat, *epat;
16915 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 int dir;
16918 int flags = 0;
16919 char_u nbuf1[NUMBUFLEN];
16920 char_u nbuf2[NUMBUFLEN];
16921 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016922 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016923 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016924 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016927 spat = get_tv_string_chk(&argvars[0]);
16928 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16929 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16930 if (spat == NULL || mpat == NULL || epat == NULL)
16931 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 /* Handle the optional fourth argument: flags */
16934 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016935 if (dir == 0)
16936 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016937
16938 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016939 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16940 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016941 if ((flags & (SP_END | SP_SUBPAT)) != 0
16942 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016943 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016944 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016945 goto theend;
16946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016948 /* Using 'r' implies 'W', otherwise it doesn't work. */
16949 if (flags & SP_REPEAT)
16950 p_ws = FALSE;
16951
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016952 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016953 if (argvars[3].v_type == VAR_UNKNOWN
16954 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 skip = (char_u *)"";
16956 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016958 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016959 if (argvars[5].v_type != VAR_UNKNOWN)
16960 {
16961 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16962 if (lnum_stop < 0)
16963 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016964#ifdef FEAT_RELTIME
16965 if (argvars[6].v_type != VAR_UNKNOWN)
16966 {
16967 time_limit = get_tv_number_chk(&argvars[6], NULL);
16968 if (time_limit < 0)
16969 goto theend;
16970 }
16971#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016972 }
16973 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016974 if (skip == NULL)
16975 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016977 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016978 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016979
16980theend:
16981 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016982
16983 return retval;
16984}
16985
16986/*
16987 * "searchpair()" function
16988 */
16989 static void
16990f_searchpair(argvars, rettv)
16991 typval_T *argvars;
16992 typval_T *rettv;
16993{
16994 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16995}
16996
16997/*
16998 * "searchpairpos()" function
16999 */
17000 static void
17001f_searchpairpos(argvars, rettv)
17002 typval_T *argvars;
17003 typval_T *rettv;
17004{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017005 pos_T match_pos;
17006 int lnum = 0;
17007 int col = 0;
17008
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017009 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017010 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017011
17012 if (searchpair_cmn(argvars, &match_pos) > 0)
17013 {
17014 lnum = match_pos.lnum;
17015 col = match_pos.col;
17016 }
17017
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017018 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17019 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017020}
17021
17022/*
17023 * Search for a start/middle/end thing.
17024 * Used by searchpair(), see its documentation for the details.
17025 * Returns 0 or -1 for no match,
17026 */
17027 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017028do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17029 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017030 char_u *spat; /* start pattern */
17031 char_u *mpat; /* middle pattern */
17032 char_u *epat; /* end pattern */
17033 int dir; /* BACKWARD or FORWARD */
17034 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017035 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017036 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017037 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017038 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017039{
17040 char_u *save_cpo;
17041 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17042 long retval = 0;
17043 pos_T pos;
17044 pos_T firstpos;
17045 pos_T foundpos;
17046 pos_T save_cursor;
17047 pos_T save_pos;
17048 int n;
17049 int r;
17050 int nest = 1;
17051 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017052 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017053 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017054
17055 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17056 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017057 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017058
Bram Moolenaar76929292008-01-06 19:07:36 +000017059#ifdef FEAT_RELTIME
17060 /* Set the time limit, if there is one. */
17061 profile_setlimit(time_limit, &tm);
17062#endif
17063
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017064 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17065 * start/middle/end (pat3, for the top pair). */
17066 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17067 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17068 if (pat2 == NULL || pat3 == NULL)
17069 goto theend;
17070 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17071 if (*mpat == NUL)
17072 STRCPY(pat3, pat2);
17073 else
17074 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17075 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017076 if (flags & SP_START)
17077 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017078
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 save_cursor = curwin->w_cursor;
17080 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017081 clearpos(&firstpos);
17082 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 pat = pat3;
17084 for (;;)
17085 {
17086 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017087 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17089 /* didn't find it or found the first match again: FAIL */
17090 break;
17091
17092 if (firstpos.lnum == 0)
17093 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017094 if (equalpos(pos, foundpos))
17095 {
17096 /* Found the same position again. Can happen with a pattern that
17097 * has "\zs" at the end and searching backwards. Advance one
17098 * character and try again. */
17099 if (dir == BACKWARD)
17100 decl(&pos);
17101 else
17102 incl(&pos);
17103 }
17104 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017106 /* clear the start flag to avoid getting stuck here */
17107 options &= ~SEARCH_START;
17108
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 /* If the skip pattern matches, ignore this match. */
17110 if (*skip != NUL)
17111 {
17112 save_pos = curwin->w_cursor;
17113 curwin->w_cursor = pos;
17114 r = eval_to_bool(skip, &err, NULL, FALSE);
17115 curwin->w_cursor = save_pos;
17116 if (err)
17117 {
17118 /* Evaluating {skip} caused an error, break here. */
17119 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017120 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 break;
17122 }
17123 if (r)
17124 continue;
17125 }
17126
17127 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17128 {
17129 /* Found end when searching backwards or start when searching
17130 * forward: nested pair. */
17131 ++nest;
17132 pat = pat2; /* nested, don't search for middle */
17133 }
17134 else
17135 {
17136 /* Found end when searching forward or start when searching
17137 * backward: end of (nested) pair; or found middle in outer pair. */
17138 if (--nest == 1)
17139 pat = pat3; /* outer level, search for middle */
17140 }
17141
17142 if (nest == 0)
17143 {
17144 /* Found the match: return matchcount or line number. */
17145 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017146 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017148 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017149 if (flags & SP_SETPCMARK)
17150 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 curwin->w_cursor = pos;
17152 if (!(flags & SP_REPEAT))
17153 break;
17154 nest = 1; /* search for next unmatched */
17155 }
17156 }
17157
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017158 if (match_pos != NULL)
17159 {
17160 /* Store the match cursor position */
17161 match_pos->lnum = curwin->w_cursor.lnum;
17162 match_pos->col = curwin->w_cursor.col + 1;
17163 }
17164
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017166 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 curwin->w_cursor = save_cursor;
17168
17169theend:
17170 vim_free(pat2);
17171 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017172 if (p_cpo == empty_option)
17173 p_cpo = save_cpo;
17174 else
17175 /* Darn, evaluating the {skip} expression changed the value. */
17176 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017177
17178 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179}
17180
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017181/*
17182 * "searchpos()" function
17183 */
17184 static void
17185f_searchpos(argvars, rettv)
17186 typval_T *argvars;
17187 typval_T *rettv;
17188{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017189 pos_T match_pos;
17190 int lnum = 0;
17191 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017192 int n;
17193 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017194
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017195 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017196 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017197
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017198 n = search_cmn(argvars, &match_pos, &flags);
17199 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017200 {
17201 lnum = match_pos.lnum;
17202 col = match_pos.col;
17203 }
17204
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017205 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17206 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017207 if (flags & SP_SUBPAT)
17208 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017209}
17210
17211
Bram Moolenaar0d660222005-01-07 21:51:51 +000017212 static void
17213f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017214 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017217#ifdef FEAT_CLIENTSERVER
17218 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017219 char_u *server = get_tv_string_chk(&argvars[0]);
17220 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017223 if (server == NULL || reply == NULL)
17224 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225 if (check_restricted() || check_secure())
17226 return;
17227# ifdef FEAT_X11
17228 if (check_connection() == FAIL)
17229 return;
17230# endif
17231
17232 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017234 EMSG(_("E258: Unable to send to client"));
17235 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237 rettv->vval.v_number = 0;
17238#else
17239 rettv->vval.v_number = -1;
17240#endif
17241}
17242
Bram Moolenaar0d660222005-01-07 21:51:51 +000017243 static void
17244f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017245 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017246 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017247{
17248 char_u *r = NULL;
17249
17250#ifdef FEAT_CLIENTSERVER
17251# ifdef WIN32
17252 r = serverGetVimNames();
17253# else
17254 make_connection();
17255 if (X_DISPLAY != NULL)
17256 r = serverGetVimNames(X_DISPLAY);
17257# endif
17258#endif
17259 rettv->v_type = VAR_STRING;
17260 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261}
17262
17263/*
17264 * "setbufvar()" function
17265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017267f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017268 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017269 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270{
17271 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017274 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 char_u nbuf[NUMBUFLEN];
17276
17277 if (check_restricted() || check_secure())
17278 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17280 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017281 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 varp = &argvars[2];
17283
17284 if (buf != NULL && varname != NULL && varp != NULL)
17285 {
17286 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288
17289 if (*varname == '&')
17290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017291 long numval;
17292 char_u *strval;
17293 int error = FALSE;
17294
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017296 numval = get_tv_number_chk(varp, &error);
17297 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017298 if (!error && strval != NULL)
17299 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 }
17301 else
17302 {
17303 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17304 if (bufvarname != NULL)
17305 {
17306 STRCPY(bufvarname, "b:");
17307 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017308 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 vim_free(bufvarname);
17310 }
17311 }
17312
17313 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316}
17317
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017318 static void
17319f_setcharsearch(argvars, rettv)
17320 typval_T *argvars;
17321 typval_T *rettv UNUSED;
17322{
17323 dict_T *d;
17324 dictitem_T *di;
17325 char_u *csearch;
17326
17327 if (argvars[0].v_type != VAR_DICT)
17328 {
17329 EMSG(_(e_dictreq));
17330 return;
17331 }
17332
17333 if ((d = argvars[0].vval.v_dict) != NULL)
17334 {
17335 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17336 if (csearch != NULL)
17337 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017338#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017339 if (enc_utf8)
17340 {
17341 int pcc[MAX_MCO];
17342 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017343
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017344 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17345 }
17346 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017347#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017348 set_last_csearch(PTR2CHAR(csearch),
17349 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017350 }
17351
17352 di = dict_find(d, (char_u *)"forward", -1);
17353 if (di != NULL)
17354 set_csearch_direction(get_tv_number(&di->di_tv)
17355 ? FORWARD : BACKWARD);
17356
17357 di = dict_find(d, (char_u *)"until", -1);
17358 if (di != NULL)
17359 set_csearch_until(!!get_tv_number(&di->di_tv));
17360 }
17361}
17362
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363/*
17364 * "setcmdpos()" function
17365 */
17366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017367f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 typval_T *argvars;
17369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 int pos = (int)get_tv_number(&argvars[0]) - 1;
17372
17373 if (pos >= 0)
17374 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375}
17376
17377/*
17378 * "setline()" function
17379 */
17380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 typval_T *argvars;
17383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384{
17385 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017386 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017387 list_T *l = NULL;
17388 listitem_T *li = NULL;
17389 long added = 0;
17390 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017392 lnum = get_tv_lnum(&argvars[0]);
17393 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017395 l = argvars[1].vval.v_list;
17396 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017398 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017399 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017400
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017401 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017402 for (;;)
17403 {
17404 if (l != NULL)
17405 {
17406 /* list argument, get next string */
17407 if (li == NULL)
17408 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017409 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017410 li = li->li_next;
17411 }
17412
17413 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017414 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017415 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017416
17417 /* When coming here from Insert mode, sync undo, so that this can be
17418 * undone separately from what was previously inserted. */
17419 if (u_sync_once == 2)
17420 {
17421 u_sync_once = 1; /* notify that u_sync() was called */
17422 u_sync(TRUE);
17423 }
17424
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017425 if (lnum <= curbuf->b_ml.ml_line_count)
17426 {
17427 /* existing line, replace it */
17428 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17429 {
17430 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017431 if (lnum == curwin->w_cursor.lnum)
17432 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017433 rettv->vval.v_number = 0; /* OK */
17434 }
17435 }
17436 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17437 {
17438 /* lnum is one past the last line, append the line */
17439 ++added;
17440 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17441 rettv->vval.v_number = 0; /* OK */
17442 }
17443
17444 if (l == NULL) /* only one string argument */
17445 break;
17446 ++lnum;
17447 }
17448
17449 if (added > 0)
17450 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451}
17452
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017453static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17454
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017456 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017457 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017458 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017459set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017460 win_T *wp UNUSED;
17461 typval_T *list_arg UNUSED;
17462 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017463 typval_T *rettv;
17464{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017465#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017466 char_u *act;
17467 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017468#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017469
Bram Moolenaar2641f772005-03-25 21:58:17 +000017470 rettv->vval.v_number = -1;
17471
17472#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017473 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017474 EMSG(_(e_listreq));
17475 else
17476 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017477 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017478
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017479 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017480 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017481 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017482 if (act == NULL)
17483 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017484 if (*act == 'a' || *act == 'r')
17485 action = *act;
17486 }
17487
Bram Moolenaar81484f42012-12-05 15:16:47 +010017488 if (l != NULL && set_errorlist(wp, l, action,
17489 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017490 rettv->vval.v_number = 0;
17491 }
17492#endif
17493}
17494
17495/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017496 * "setloclist()" function
17497 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017498 static void
17499f_setloclist(argvars, rettv)
17500 typval_T *argvars;
17501 typval_T *rettv;
17502{
17503 win_T *win;
17504
17505 rettv->vval.v_number = -1;
17506
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017507 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017508 if (win != NULL)
17509 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17510}
17511
17512/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017513 * "setmatches()" function
17514 */
17515 static void
17516f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017517 typval_T *argvars UNUSED;
17518 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017519{
17520#ifdef FEAT_SEARCH_EXTRA
17521 list_T *l;
17522 listitem_T *li;
17523 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017524 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017525
17526 rettv->vval.v_number = -1;
17527 if (argvars[0].v_type != VAR_LIST)
17528 {
17529 EMSG(_(e_listreq));
17530 return;
17531 }
17532 if ((l = argvars[0].vval.v_list) != NULL)
17533 {
17534
17535 /* To some extent make sure that we are dealing with a list from
17536 * "getmatches()". */
17537 li = l->lv_first;
17538 while (li != NULL)
17539 {
17540 if (li->li_tv.v_type != VAR_DICT
17541 || (d = li->li_tv.vval.v_dict) == NULL)
17542 {
17543 EMSG(_(e_invarg));
17544 return;
17545 }
17546 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017547 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17548 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017549 && dict_find(d, (char_u *)"priority", -1) != NULL
17550 && dict_find(d, (char_u *)"id", -1) != NULL))
17551 {
17552 EMSG(_(e_invarg));
17553 return;
17554 }
17555 li = li->li_next;
17556 }
17557
17558 clear_matches(curwin);
17559 li = l->lv_first;
17560 while (li != NULL)
17561 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017562 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017563 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017564 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017565 char_u *group;
17566 int priority;
17567 int id;
17568 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017569
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017570 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017571 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17572 {
17573 if (s == NULL)
17574 {
17575 s = list_alloc();
17576 if (s == NULL)
17577 return;
17578 }
17579
17580 /* match from matchaddpos() */
17581 for (i = 1; i < 9; i++)
17582 {
17583 sprintf((char *)buf, (char *)"pos%d", i);
17584 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17585 {
17586 if (di->di_tv.v_type != VAR_LIST)
17587 return;
17588
17589 list_append_tv(s, &di->di_tv);
17590 s->lv_refcount++;
17591 }
17592 else
17593 break;
17594 }
17595 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017596
17597 group = get_dict_string(d, (char_u *)"group", FALSE);
17598 priority = (int)get_dict_number(d, (char_u *)"priority");
17599 id = (int)get_dict_number(d, (char_u *)"id");
17600 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17601 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17602 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017603 if (i == 0)
17604 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017605 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017606 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017607 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017608 }
17609 else
17610 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017611 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017612 list_unref(s);
17613 s = NULL;
17614 }
17615
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017616 li = li->li_next;
17617 }
17618 rettv->vval.v_number = 0;
17619 }
17620#endif
17621}
17622
17623/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017624 * "setpos()" function
17625 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017626 static void
17627f_setpos(argvars, rettv)
17628 typval_T *argvars;
17629 typval_T *rettv;
17630{
17631 pos_T pos;
17632 int fnum;
17633 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017634 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017635
Bram Moolenaar08250432008-02-13 11:42:46 +000017636 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017637 name = get_tv_string_chk(argvars);
17638 if (name != NULL)
17639 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017640 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017641 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017642 if (--pos.col < 0)
17643 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017644 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017645 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017646 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017647 if (fnum == curbuf->b_fnum)
17648 {
17649 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017650 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017651 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017652 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017653 curwin->w_set_curswant = FALSE;
17654 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017655 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017656 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017657 }
17658 else
17659 EMSG(_(e_invarg));
17660 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017661 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17662 {
17663 /* set mark */
17664 if (setmark_pos(name[1], &pos, fnum) == OK)
17665 rettv->vval.v_number = 0;
17666 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017667 else
17668 EMSG(_(e_invarg));
17669 }
17670 }
17671}
17672
17673/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017674 * "setqflist()" function
17675 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017676 static void
17677f_setqflist(argvars, rettv)
17678 typval_T *argvars;
17679 typval_T *rettv;
17680{
17681 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17682}
17683
17684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 * "setreg()" function
17686 */
17687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017689 typval_T *argvars;
17690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691{
17692 int regname;
17693 char_u *strregname;
17694 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017695 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 int append;
17697 char_u yank_type;
17698 long block_len;
17699
17700 block_len = -1;
17701 yank_type = MAUTO;
17702 append = FALSE;
17703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017704 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017705 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017707 if (strregname == NULL)
17708 return; /* type error; errmsg already given */
17709 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 if (regname == 0 || regname == '@')
17711 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017713 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017715 stropt = get_tv_string_chk(&argvars[2]);
17716 if (stropt == NULL)
17717 return; /* type error */
17718 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 switch (*stropt)
17720 {
17721 case 'a': case 'A': /* append */
17722 append = TRUE;
17723 break;
17724 case 'v': case 'c': /* character-wise selection */
17725 yank_type = MCHAR;
17726 break;
17727 case 'V': case 'l': /* line-wise selection */
17728 yank_type = MLINE;
17729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 case 'b': case Ctrl_V: /* block-wise selection */
17731 yank_type = MBLOCK;
17732 if (VIM_ISDIGIT(stropt[1]))
17733 {
17734 ++stropt;
17735 block_len = getdigits(&stropt) - 1;
17736 --stropt;
17737 }
17738 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 }
17740 }
17741
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017742 if (argvars[1].v_type == VAR_LIST)
17743 {
17744 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017745 char_u **allocval;
17746 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017747 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017748 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017749 int len = argvars[1].vval.v_list->lv_len;
17750 listitem_T *li;
17751
Bram Moolenaar7d647822014-04-05 21:28:56 +020017752 /* First half: use for pointers to result lines; second half: use for
17753 * pointers to allocated copies. */
17754 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017755 if (lstval == NULL)
17756 return;
17757 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017758 allocval = lstval + len + 2;
17759 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017760
17761 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17762 li = li->li_next)
17763 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017764 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017765 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017766 goto free_lstval;
17767 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017768 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017769 /* Need to make a copy, next get_tv_string_buf_chk() will
17770 * overwrite the string. */
17771 strval = vim_strsave(buf);
17772 if (strval == NULL)
17773 goto free_lstval;
17774 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017775 }
17776 *curval++ = strval;
17777 }
17778 *curval++ = NULL;
17779
17780 write_reg_contents_lst(regname, lstval, -1,
17781 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017782free_lstval:
17783 while (curallocval > allocval)
17784 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017785 vim_free(lstval);
17786 }
17787 else
17788 {
17789 strval = get_tv_string_chk(&argvars[1]);
17790 if (strval == NULL)
17791 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017792 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017795 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796}
17797
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017798/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017799 * "settabvar()" function
17800 */
17801 static void
17802f_settabvar(argvars, rettv)
17803 typval_T *argvars;
17804 typval_T *rettv;
17805{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017806#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017807 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017808 tabpage_T *tp;
17809#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017810 char_u *varname, *tabvarname;
17811 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017812
17813 rettv->vval.v_number = 0;
17814
17815 if (check_restricted() || check_secure())
17816 return;
17817
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017818#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017819 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017820#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017821 varname = get_tv_string_chk(&argvars[1]);
17822 varp = &argvars[2];
17823
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017824 if (varname != NULL && varp != NULL
17825#ifdef FEAT_WINDOWS
17826 && tp != NULL
17827#endif
17828 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017829 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017830#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017831 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017832 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017833#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017834
17835 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17836 if (tabvarname != NULL)
17837 {
17838 STRCPY(tabvarname, "t:");
17839 STRCPY(tabvarname + 2, varname);
17840 set_var(tabvarname, varp, TRUE);
17841 vim_free(tabvarname);
17842 }
17843
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017844#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017845 /* Restore current tabpage */
17846 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017847 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017848#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017849 }
17850}
17851
17852/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017853 * "settabwinvar()" function
17854 */
17855 static void
17856f_settabwinvar(argvars, rettv)
17857 typval_T *argvars;
17858 typval_T *rettv;
17859{
17860 setwinvar(argvars, rettv, 1);
17861}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862
17863/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017864 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017867f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017868 typval_T *argvars;
17869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017871 setwinvar(argvars, rettv, 0);
17872}
17873
17874/*
17875 * "setwinvar()" and "settabwinvar()" functions
17876 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017877
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017878 static void
17879setwinvar(argvars, rettv, off)
17880 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017881 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017882 int off;
17883{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 win_T *win;
17885#ifdef FEAT_WINDOWS
17886 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017887 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017888 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889#endif
17890 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017891 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017893 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894
17895 if (check_restricted() || check_secure())
17896 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017897
17898#ifdef FEAT_WINDOWS
17899 if (off == 1)
17900 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17901 else
17902 tp = curtab;
17903#endif
17904 win = find_win_by_nr(&argvars[off], tp);
17905 varname = get_tv_string_chk(&argvars[off + 1]);
17906 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907
17908 if (win != NULL && varname != NULL && varp != NULL)
17909 {
17910#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017911 need_switch_win = !(tp == curtab && win == curwin);
17912 if (!need_switch_win
17913 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017916 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017918 long numval;
17919 char_u *strval;
17920 int error = FALSE;
17921
17922 ++varname;
17923 numval = get_tv_number_chk(varp, &error);
17924 strval = get_tv_string_buf_chk(varp, nbuf);
17925 if (!error && strval != NULL)
17926 set_option_value(varname, numval, strval, OPT_LOCAL);
17927 }
17928 else
17929 {
17930 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17931 if (winvarname != NULL)
17932 {
17933 STRCPY(winvarname, "w:");
17934 STRCPY(winvarname + 2, varname);
17935 set_var(winvarname, varp, TRUE);
17936 vim_free(winvarname);
17937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 }
17939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017941 if (need_switch_win)
17942 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943#endif
17944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945}
17946
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017947#ifdef FEAT_CRYPT
17948/*
17949 * "sha256({string})" function
17950 */
17951 static void
17952f_sha256(argvars, rettv)
17953 typval_T *argvars;
17954 typval_T *rettv;
17955{
17956 char_u *p;
17957
17958 p = get_tv_string(&argvars[0]);
17959 rettv->vval.v_string = vim_strsave(
17960 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17961 rettv->v_type = VAR_STRING;
17962}
17963#endif /* FEAT_CRYPT */
17964
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017966 * "shellescape({string})" function
17967 */
17968 static void
17969f_shellescape(argvars, rettv)
17970 typval_T *argvars;
17971 typval_T *rettv;
17972{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017973 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017974 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017975 rettv->v_type = VAR_STRING;
17976}
17977
17978/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017979 * shiftwidth() function
17980 */
17981 static void
17982f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017983 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017984 typval_T *rettv;
17985{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017986 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017987}
17988
17989/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017990 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 */
17992 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017993f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 typval_T *argvars;
17995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998
Bram Moolenaar0d660222005-01-07 21:51:51 +000017999 p = get_tv_string(&argvars[0]);
18000 rettv->vval.v_string = vim_strsave(p);
18001 simplify_filename(rettv->vval.v_string); /* simplify in place */
18002 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003}
18004
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018005#ifdef FEAT_FLOAT
18006/*
18007 * "sin()" function
18008 */
18009 static void
18010f_sin(argvars, rettv)
18011 typval_T *argvars;
18012 typval_T *rettv;
18013{
18014 float_T f;
18015
18016 rettv->v_type = VAR_FLOAT;
18017 if (get_float_arg(argvars, &f) == OK)
18018 rettv->vval.v_float = sin(f);
18019 else
18020 rettv->vval.v_float = 0.0;
18021}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018022
18023/*
18024 * "sinh()" function
18025 */
18026 static void
18027f_sinh(argvars, rettv)
18028 typval_T *argvars;
18029 typval_T *rettv;
18030{
18031 float_T f;
18032
18033 rettv->v_type = VAR_FLOAT;
18034 if (get_float_arg(argvars, &f) == OK)
18035 rettv->vval.v_float = sinh(f);
18036 else
18037 rettv->vval.v_float = 0.0;
18038}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018039#endif
18040
Bram Moolenaar0d660222005-01-07 21:51:51 +000018041static int
18042#ifdef __BORLANDC__
18043 _RTLENTRYF
18044#endif
18045 item_compare __ARGS((const void *s1, const void *s2));
18046static int
18047#ifdef __BORLANDC__
18048 _RTLENTRYF
18049#endif
18050 item_compare2 __ARGS((const void *s1, const void *s2));
18051
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018052/* struct used in the array that's given to qsort() */
18053typedef struct
18054{
18055 listitem_T *item;
18056 int idx;
18057} sortItem_T;
18058
Bram Moolenaar0d660222005-01-07 21:51:51 +000018059static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018060static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018061static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018062static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018063static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018064static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018065static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018066static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018067#define ITEM_COMPARE_FAIL 999
18068
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018070 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018072 static int
18073#ifdef __BORLANDC__
18074_RTLENTRYF
18075#endif
18076item_compare(s1, s2)
18077 const void *s1;
18078 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018080 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018081 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018082 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018083 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084 int res;
18085 char_u numbuf1[NUMBUFLEN];
18086 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018088 si1 = (sortItem_T *)s1;
18089 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018090 tv1 = &si1->item->li_tv;
18091 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018092
18093 if (item_compare_numbers)
18094 {
18095 long v1 = get_tv_number(tv1);
18096 long v2 = get_tv_number(tv2);
18097
18098 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18099 }
18100
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018101 /* tv2string() puts quotes around a string and allocates memory. Don't do
18102 * that for string variables. Use a single quote when comparing with a
18103 * non-string to do what the docs promise. */
18104 if (tv1->v_type == VAR_STRING)
18105 {
18106 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18107 p1 = (char_u *)"'";
18108 else
18109 p1 = tv1->vval.v_string;
18110 }
18111 else
18112 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18113 if (tv2->v_type == VAR_STRING)
18114 {
18115 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18116 p2 = (char_u *)"'";
18117 else
18118 p2 = tv2->vval.v_string;
18119 }
18120 else
18121 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018122 if (p1 == NULL)
18123 p1 = (char_u *)"";
18124 if (p2 == NULL)
18125 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018126 if (!item_compare_numeric)
18127 {
18128 if (item_compare_ic)
18129 res = STRICMP(p1, p2);
18130 else
18131 res = STRCMP(p1, p2);
18132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018134 {
18135 double n1, n2;
18136 n1 = strtod((char *)p1, (char **)&p1);
18137 n2 = strtod((char *)p2, (char **)&p2);
18138 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18139 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018140
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018141 /* When the result would be zero, compare the item indexes. Makes the
18142 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018143 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018144 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018145
Bram Moolenaar0d660222005-01-07 21:51:51 +000018146 vim_free(tofree1);
18147 vim_free(tofree2);
18148 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149}
18150
18151 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018152#ifdef __BORLANDC__
18153_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155item_compare2(s1, s2)
18156 const void *s1;
18157 const void *s2;
18158{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018159 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018161 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018162 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018163 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018165 /* shortcut after failure in previous call; compare all items equal */
18166 if (item_compare_func_err)
18167 return 0;
18168
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018169 si1 = (sortItem_T *)s1;
18170 si2 = (sortItem_T *)s2;
18171
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018172 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018173 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018174 copy_tv(&si1->item->li_tv, &argv[0]);
18175 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018176
18177 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018178 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018179 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18180 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181 clear_tv(&argv[0]);
18182 clear_tv(&argv[1]);
18183
18184 if (res == FAIL)
18185 res = ITEM_COMPARE_FAIL;
18186 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018187 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18188 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018189 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018190 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018191
18192 /* When the result would be zero, compare the pointers themselves. Makes
18193 * the sort stable. */
18194 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018195 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018196
Bram Moolenaar0d660222005-01-07 21:51:51 +000018197 return res;
18198}
18199
18200/*
18201 * "sort({list})" function
18202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018204do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018205 typval_T *argvars;
18206 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018207 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208{
Bram Moolenaar33570922005-01-25 22:26:29 +000018209 list_T *l;
18210 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018211 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018212 long len;
18213 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214
Bram Moolenaar0d660222005-01-07 21:51:51 +000018215 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018216 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 else
18218 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018219 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018220 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018221 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18222 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018223 return;
18224 rettv->vval.v_list = l;
18225 rettv->v_type = VAR_LIST;
18226 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227
Bram Moolenaar0d660222005-01-07 21:51:51 +000018228 len = list_len(l);
18229 if (len <= 1)
18230 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231
Bram Moolenaar0d660222005-01-07 21:51:51 +000018232 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018233 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018234 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018235 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018236 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237 if (argvars[1].v_type != VAR_UNKNOWN)
18238 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018239 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018240 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018241 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018242 else
18243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018244 int error = FALSE;
18245
18246 i = get_tv_number_chk(&argvars[1], &error);
18247 if (error)
18248 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018249 if (i == 1)
18250 item_compare_ic = TRUE;
18251 else
18252 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018253 if (item_compare_func != NULL)
18254 {
18255 if (STRCMP(item_compare_func, "n") == 0)
18256 {
18257 item_compare_func = NULL;
18258 item_compare_numeric = TRUE;
18259 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018260 else if (STRCMP(item_compare_func, "N") == 0)
18261 {
18262 item_compare_func = NULL;
18263 item_compare_numbers = TRUE;
18264 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018265 else if (STRCMP(item_compare_func, "i") == 0)
18266 {
18267 item_compare_func = NULL;
18268 item_compare_ic = TRUE;
18269 }
18270 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018272
18273 if (argvars[2].v_type != VAR_UNKNOWN)
18274 {
18275 /* optional third argument: {dict} */
18276 if (argvars[2].v_type != VAR_DICT)
18277 {
18278 EMSG(_(e_dictreq));
18279 return;
18280 }
18281 item_compare_selfdict = argvars[2].vval.v_dict;
18282 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018286 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287 if (ptrs == NULL)
18288 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289
Bram Moolenaar327aa022014-03-25 18:24:23 +010018290 i = 0;
18291 if (sort)
18292 {
18293 /* sort(): ptrs will be the list to sort */
18294 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018295 {
18296 ptrs[i].item = li;
18297 ptrs[i].idx = i;
18298 ++i;
18299 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018300
18301 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018302 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018303 /* test the compare function */
18304 if (item_compare_func != NULL
18305 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018306 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018307 EMSG(_("E702: Sort compare function failed"));
18308 else
18309 {
18310 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018311 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018312 item_compare_func == NULL ? item_compare : item_compare2);
18313
18314 if (!item_compare_func_err)
18315 {
18316 /* Clear the List and append the items in sorted order. */
18317 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18318 l->lv_len = 0;
18319 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018320 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018321 }
18322 }
18323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018326 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18327
18328 /* f_uniq(): ptrs will be a stack of items to remove */
18329 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018330 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018331 item_compare_func_ptr = item_compare_func
18332 ? item_compare2 : item_compare;
18333
18334 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18335 li = li->li_next)
18336 {
18337 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18338 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018339 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018340 if (item_compare_func_err)
18341 {
18342 EMSG(_("E882: Uniq compare function failed"));
18343 break;
18344 }
18345 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018347 if (!item_compare_func_err)
18348 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018349 while (--i >= 0)
18350 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018351 li = ptrs[i].item->li_next;
18352 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018353 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018354 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018355 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018356 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018357 list_fix_watch(l, li);
18358 listitem_free(li);
18359 l->lv_len--;
18360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018361 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018362 }
18363
18364 vim_free(ptrs);
18365 }
18366}
18367
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018368/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018369 * "sort({list})" function
18370 */
18371 static void
18372f_sort(argvars, rettv)
18373 typval_T *argvars;
18374 typval_T *rettv;
18375{
18376 do_sort_uniq(argvars, rettv, TRUE);
18377}
18378
18379/*
18380 * "uniq({list})" function
18381 */
18382 static void
18383f_uniq(argvars, rettv)
18384 typval_T *argvars;
18385 typval_T *rettv;
18386{
18387 do_sort_uniq(argvars, rettv, FALSE);
18388}
18389
18390/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018391 * "soundfold({word})" function
18392 */
18393 static void
18394f_soundfold(argvars, rettv)
18395 typval_T *argvars;
18396 typval_T *rettv;
18397{
18398 char_u *s;
18399
18400 rettv->v_type = VAR_STRING;
18401 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018402#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018403 rettv->vval.v_string = eval_soundfold(s);
18404#else
18405 rettv->vval.v_string = vim_strsave(s);
18406#endif
18407}
18408
18409/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018410 * "spellbadword()" function
18411 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018412 static void
18413f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018414 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018415 typval_T *rettv;
18416{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018417 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018418 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018419 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018421 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018422 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018423
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018424#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018425 if (argvars[0].v_type == VAR_UNKNOWN)
18426 {
18427 /* Find the start and length of the badly spelled word. */
18428 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18429 if (len != 0)
18430 word = ml_get_cursor();
18431 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018432 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018433 {
18434 char_u *str = get_tv_string_chk(&argvars[0]);
18435 int capcol = -1;
18436
18437 if (str != NULL)
18438 {
18439 /* Check the argument for spelling. */
18440 while (*str != NUL)
18441 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018442 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018443 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018444 {
18445 word = str;
18446 break;
18447 }
18448 str += len;
18449 }
18450 }
18451 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018452#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018453
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018454 list_append_string(rettv->vval.v_list, word, len);
18455 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018456 attr == HLF_SPB ? "bad" :
18457 attr == HLF_SPR ? "rare" :
18458 attr == HLF_SPL ? "local" :
18459 attr == HLF_SPC ? "caps" :
18460 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018461}
18462
18463/*
18464 * "spellsuggest()" function
18465 */
18466 static void
18467f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018468 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018469 typval_T *rettv;
18470{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018471#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018472 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018473 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018474 int maxcount;
18475 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018476 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018477 listitem_T *li;
18478 int need_capital = FALSE;
18479#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018480
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018481 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018482 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018483
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018484#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018485 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018486 {
18487 str = get_tv_string(&argvars[0]);
18488 if (argvars[1].v_type != VAR_UNKNOWN)
18489 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018490 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018491 if (maxcount <= 0)
18492 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018493 if (argvars[2].v_type != VAR_UNKNOWN)
18494 {
18495 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18496 if (typeerr)
18497 return;
18498 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018499 }
18500 else
18501 maxcount = 25;
18502
Bram Moolenaar4770d092006-01-12 23:22:24 +000018503 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018504
18505 for (i = 0; i < ga.ga_len; ++i)
18506 {
18507 str = ((char_u **)ga.ga_data)[i];
18508
18509 li = listitem_alloc();
18510 if (li == NULL)
18511 vim_free(str);
18512 else
18513 {
18514 li->li_tv.v_type = VAR_STRING;
18515 li->li_tv.v_lock = 0;
18516 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018517 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018518 }
18519 }
18520 ga_clear(&ga);
18521 }
18522#endif
18523}
18524
Bram Moolenaar0d660222005-01-07 21:51:51 +000018525 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018526f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018527 typval_T *argvars;
18528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018529{
18530 char_u *str;
18531 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018532 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018533 regmatch_T regmatch;
18534 char_u patbuf[NUMBUFLEN];
18535 char_u *save_cpo;
18536 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018537 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018538 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018539 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018540
18541 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18542 save_cpo = p_cpo;
18543 p_cpo = (char_u *)"";
18544
18545 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018546 if (argvars[1].v_type != VAR_UNKNOWN)
18547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018548 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18549 if (pat == NULL)
18550 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018552 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018553 }
18554 if (pat == NULL || *pat == NUL)
18555 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018556
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018557 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018559 if (typeerr)
18560 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561
Bram Moolenaar0d660222005-01-07 21:51:51 +000018562 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18563 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018565 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018566 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018567 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018568 if (*str == NUL)
18569 match = FALSE; /* empty item at the end */
18570 else
18571 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018572 if (match)
18573 end = regmatch.startp[0];
18574 else
18575 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018576 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18577 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018578 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018579 if (list_append_string(rettv->vval.v_list, str,
18580 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018581 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582 }
18583 if (!match)
18584 break;
18585 /* Advance to just after the match. */
18586 if (regmatch.endp[0] > str)
18587 col = 0;
18588 else
18589 {
18590 /* Don't get stuck at the same match. */
18591#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018592 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018593#else
18594 col = 1;
18595#endif
18596 }
18597 str = regmatch.endp[0];
18598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599
Bram Moolenaar473de612013-06-08 18:19:48 +020018600 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602
Bram Moolenaar0d660222005-01-07 21:51:51 +000018603 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604}
18605
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018606#ifdef FEAT_FLOAT
18607/*
18608 * "sqrt()" function
18609 */
18610 static void
18611f_sqrt(argvars, rettv)
18612 typval_T *argvars;
18613 typval_T *rettv;
18614{
18615 float_T f;
18616
18617 rettv->v_type = VAR_FLOAT;
18618 if (get_float_arg(argvars, &f) == OK)
18619 rettv->vval.v_float = sqrt(f);
18620 else
18621 rettv->vval.v_float = 0.0;
18622}
18623
18624/*
18625 * "str2float()" function
18626 */
18627 static void
18628f_str2float(argvars, rettv)
18629 typval_T *argvars;
18630 typval_T *rettv;
18631{
18632 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18633
18634 if (*p == '+')
18635 p = skipwhite(p + 1);
18636 (void)string2float(p, &rettv->vval.v_float);
18637 rettv->v_type = VAR_FLOAT;
18638}
18639#endif
18640
Bram Moolenaar2c932302006-03-18 21:42:09 +000018641/*
18642 * "str2nr()" function
18643 */
18644 static void
18645f_str2nr(argvars, rettv)
18646 typval_T *argvars;
18647 typval_T *rettv;
18648{
18649 int base = 10;
18650 char_u *p;
18651 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018652 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018653
18654 if (argvars[1].v_type != VAR_UNKNOWN)
18655 {
18656 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018657 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018658 {
18659 EMSG(_(e_invarg));
18660 return;
18661 }
18662 }
18663
18664 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018665 if (*p == '+')
18666 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018667 switch (base)
18668 {
18669 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18670 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18671 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18672 default: what = 0;
18673 }
18674 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018675 rettv->vval.v_number = n;
18676}
18677
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678#ifdef HAVE_STRFTIME
18679/*
18680 * "strftime({format}[, {time}])" function
18681 */
18682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018684 typval_T *argvars;
18685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686{
18687 char_u result_buf[256];
18688 struct tm *curtime;
18689 time_t seconds;
18690 char_u *p;
18691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018695 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 seconds = time(NULL);
18697 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 curtime = localtime(&seconds);
18700 /* MSVC returns NULL for an invalid value of seconds. */
18701 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018702 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 else
18704 {
18705# ifdef FEAT_MBYTE
18706 vimconv_T conv;
18707 char_u *enc;
18708
18709 conv.vc_type = CONV_NONE;
18710 enc = enc_locale();
18711 convert_setup(&conv, p_enc, enc);
18712 if (conv.vc_type != CONV_NONE)
18713 p = string_convert(&conv, p, NULL);
18714# endif
18715 if (p != NULL)
18716 (void)strftime((char *)result_buf, sizeof(result_buf),
18717 (char *)p, curtime);
18718 else
18719 result_buf[0] = NUL;
18720
18721# ifdef FEAT_MBYTE
18722 if (conv.vc_type != CONV_NONE)
18723 vim_free(p);
18724 convert_setup(&conv, enc, p_enc);
18725 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 else
18728# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018729 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730
18731# ifdef FEAT_MBYTE
18732 /* Release conversion descriptors */
18733 convert_setup(&conv, NULL, NULL);
18734 vim_free(enc);
18735# endif
18736 }
18737}
18738#endif
18739
18740/*
18741 * "stridx()" function
18742 */
18743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018744f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018745 typval_T *argvars;
18746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747{
18748 char_u buf[NUMBUFLEN];
18749 char_u *needle;
18750 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018753 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018755 needle = get_tv_string_chk(&argvars[1]);
18756 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018757 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018758 if (needle == NULL || haystack == NULL)
18759 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 if (argvars[2].v_type != VAR_UNKNOWN)
18762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018763 int error = FALSE;
18764
18765 start_idx = get_tv_number_chk(&argvars[2], &error);
18766 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018767 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018768 if (start_idx >= 0)
18769 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018770 }
18771
18772 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18773 if (pos != NULL)
18774 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775}
18776
18777/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018778 * "string()" function
18779 */
18780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018781f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018782 typval_T *argvars;
18783 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018784{
18785 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018786 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018788 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018789 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018790 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018791 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018792 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793}
18794
18795/*
18796 * "strlen()" function
18797 */
18798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018799f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018800 typval_T *argvars;
18801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 rettv->vval.v_number = (varnumber_T)(STRLEN(
18804 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805}
18806
18807/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018808 * "strchars()" function
18809 */
18810 static void
18811f_strchars(argvars, rettv)
18812 typval_T *argvars;
18813 typval_T *rettv;
18814{
18815 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018816 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018817#ifdef FEAT_MBYTE
18818 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018819 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018820#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018821
18822 if (argvars[1].v_type != VAR_UNKNOWN)
18823 skipcc = get_tv_number_chk(&argvars[1], NULL);
18824 if (skipcc < 0 || skipcc > 1)
18825 EMSG(_(e_invarg));
18826 else
18827 {
18828#ifdef FEAT_MBYTE
18829 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18830 while (*s != NUL)
18831 {
18832 func_mb_ptr2char_adv(&s);
18833 ++len;
18834 }
18835 rettv->vval.v_number = len;
18836#else
18837 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18838#endif
18839 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018840}
18841
18842/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018843 * "strdisplaywidth()" function
18844 */
18845 static void
18846f_strdisplaywidth(argvars, rettv)
18847 typval_T *argvars;
18848 typval_T *rettv;
18849{
18850 char_u *s = get_tv_string(&argvars[0]);
18851 int col = 0;
18852
18853 if (argvars[1].v_type != VAR_UNKNOWN)
18854 col = get_tv_number(&argvars[1]);
18855
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018856 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018857}
18858
18859/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018860 * "strwidth()" function
18861 */
18862 static void
18863f_strwidth(argvars, rettv)
18864 typval_T *argvars;
18865 typval_T *rettv;
18866{
18867 char_u *s = get_tv_string(&argvars[0]);
18868
18869 rettv->vval.v_number = (varnumber_T)(
18870#ifdef FEAT_MBYTE
18871 mb_string2cells(s, -1)
18872#else
18873 STRLEN(s)
18874#endif
18875 );
18876}
18877
18878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 * "strpart()" function
18880 */
18881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018882f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018883 typval_T *argvars;
18884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885{
18886 char_u *p;
18887 int n;
18888 int len;
18889 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018890 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018892 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893 slen = (int)STRLEN(p);
18894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018895 n = get_tv_number_chk(&argvars[1], &error);
18896 if (error)
18897 len = 0;
18898 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018899 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 else
18901 len = slen - n; /* default len: all bytes that are available. */
18902
18903 /*
18904 * Only return the overlap between the specified part and the actual
18905 * string.
18906 */
18907 if (n < 0)
18908 {
18909 len += n;
18910 n = 0;
18911 }
18912 else if (n > slen)
18913 n = slen;
18914 if (len < 0)
18915 len = 0;
18916 else if (n + len > slen)
18917 len = slen - n;
18918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018919 rettv->v_type = VAR_STRING;
18920 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921}
18922
18923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018924 * "strridx()" function
18925 */
18926 static void
18927f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 typval_T *argvars;
18929 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018930{
18931 char_u buf[NUMBUFLEN];
18932 char_u *needle;
18933 char_u *haystack;
18934 char_u *rest;
18935 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018936 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018938 needle = get_tv_string_chk(&argvars[1]);
18939 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018940
18941 rettv->vval.v_number = -1;
18942 if (needle == NULL || haystack == NULL)
18943 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018944
18945 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018946 if (argvars[2].v_type != VAR_UNKNOWN)
18947 {
18948 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018949 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018950 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018951 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018952 }
18953 else
18954 end_idx = haystack_len;
18955
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018957 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018959 lastmatch = haystack + end_idx;
18960 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018961 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018962 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 for (rest = haystack; *rest != '\0'; ++rest)
18964 {
18965 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018966 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018967 break;
18968 lastmatch = rest;
18969 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018971
18972 if (lastmatch == NULL)
18973 rettv->vval.v_number = -1;
18974 else
18975 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18976}
18977
18978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 * "strtrans()" function
18980 */
18981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018982f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018983 typval_T *argvars;
18984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018986 rettv->v_type = VAR_STRING;
18987 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988}
18989
18990/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018991 * "submatch()" function
18992 */
18993 static void
18994f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018995 typval_T *argvars;
18996 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018997{
Bram Moolenaar41571762014-04-02 19:00:58 +020018998 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018999 int no;
19000 int retList = 0;
19001
19002 no = (int)get_tv_number_chk(&argvars[0], &error);
19003 if (error)
19004 return;
19005 error = FALSE;
19006 if (argvars[1].v_type != VAR_UNKNOWN)
19007 retList = get_tv_number_chk(&argvars[1], &error);
19008 if (error)
19009 return;
19010
19011 if (retList == 0)
19012 {
19013 rettv->v_type = VAR_STRING;
19014 rettv->vval.v_string = reg_submatch(no);
19015 }
19016 else
19017 {
19018 rettv->v_type = VAR_LIST;
19019 rettv->vval.v_list = reg_submatch_list(no);
19020 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021}
19022
19023/*
19024 * "substitute()" function
19025 */
19026 static void
19027f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019028 typval_T *argvars;
19029 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019030{
19031 char_u patbuf[NUMBUFLEN];
19032 char_u subbuf[NUMBUFLEN];
19033 char_u flagsbuf[NUMBUFLEN];
19034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019035 char_u *str = get_tv_string_chk(&argvars[0]);
19036 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19037 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19038 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19039
Bram Moolenaar0d660222005-01-07 21:51:51 +000019040 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019041 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19042 rettv->vval.v_string = NULL;
19043 else
19044 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045}
19046
19047/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019048 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019052 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054{
19055 int id = 0;
19056#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019057 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 long col;
19059 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019060 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019062 lnum = get_tv_lnum(argvars); /* -1 on type error */
19063 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19064 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019066 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019067 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019068 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069#endif
19070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019071 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072}
19073
19074/*
19075 * "synIDattr(id, what [, mode])" function
19076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019079 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081{
19082 char_u *p = NULL;
19083#ifdef FEAT_SYN_HL
19084 int id;
19085 char_u *what;
19086 char_u *mode;
19087 char_u modebuf[NUMBUFLEN];
19088 int modec;
19089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019090 id = get_tv_number(&argvars[0]);
19091 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019094 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019096 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 modec = 0; /* replace invalid with current */
19098 }
19099 else
19100 {
19101#ifdef FEAT_GUI
19102 if (gui.in_use)
19103 modec = 'g';
19104 else
19105#endif
19106 if (t_colors > 1)
19107 modec = 'c';
19108 else
19109 modec = 't';
19110 }
19111
19112
19113 switch (TOLOWER_ASC(what[0]))
19114 {
19115 case 'b':
19116 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19117 p = highlight_color(id, what, modec);
19118 else /* bold */
19119 p = highlight_has_attr(id, HL_BOLD, modec);
19120 break;
19121
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019122 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 p = highlight_color(id, what, modec);
19124 break;
19125
19126 case 'i':
19127 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19128 p = highlight_has_attr(id, HL_INVERSE, modec);
19129 else /* italic */
19130 p = highlight_has_attr(id, HL_ITALIC, modec);
19131 break;
19132
19133 case 'n': /* name */
19134 p = get_highlight_name(NULL, id - 1);
19135 break;
19136
19137 case 'r': /* reverse */
19138 p = highlight_has_attr(id, HL_INVERSE, modec);
19139 break;
19140
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019141 case 's':
19142 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19143 p = highlight_color(id, what, modec);
19144 else /* standout */
19145 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146 break;
19147
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019148 case 'u':
19149 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19150 /* underline */
19151 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19152 else
19153 /* undercurl */
19154 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 break;
19156 }
19157
19158 if (p != NULL)
19159 p = vim_strsave(p);
19160#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161 rettv->v_type = VAR_STRING;
19162 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163}
19164
19165/*
19166 * "synIDtrans(id)" function
19167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172{
19173 int id;
19174
19175#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019176 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177
19178 if (id > 0)
19179 id = syn_get_final_id(id);
19180 else
19181#endif
19182 id = 0;
19183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019184 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185}
19186
19187/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019188 * "synconcealed(lnum, col)" function
19189 */
19190 static void
19191f_synconcealed(argvars, rettv)
19192 typval_T *argvars UNUSED;
19193 typval_T *rettv;
19194{
19195#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19196 long lnum;
19197 long col;
19198 int syntax_flags = 0;
19199 int cchar;
19200 int matchid = 0;
19201 char_u str[NUMBUFLEN];
19202#endif
19203
19204 rettv->v_type = VAR_LIST;
19205 rettv->vval.v_list = NULL;
19206
19207#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19208 lnum = get_tv_lnum(argvars); /* -1 on type error */
19209 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19210
19211 vim_memset(str, NUL, sizeof(str));
19212
19213 if (rettv_list_alloc(rettv) != FAIL)
19214 {
19215 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19216 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19217 && curwin->w_p_cole > 0)
19218 {
19219 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19220 syntax_flags = get_syntax_info(&matchid);
19221
19222 /* get the conceal character */
19223 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19224 {
19225 cchar = syn_get_sub_char();
19226 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19227 cchar = lcs_conceal;
19228 if (cchar != NUL)
19229 {
19230# ifdef FEAT_MBYTE
19231 if (has_mbyte)
19232 (*mb_char2bytes)(cchar, str);
19233 else
19234# endif
19235 str[0] = cchar;
19236 }
19237 }
19238 }
19239
19240 list_append_number(rettv->vval.v_list,
19241 (syntax_flags & HL_CONCEAL) != 0);
19242 /* -1 to auto-determine strlen */
19243 list_append_string(rettv->vval.v_list, str, -1);
19244 list_append_number(rettv->vval.v_list, matchid);
19245 }
19246#endif
19247}
19248
19249/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019250 * "synstack(lnum, col)" function
19251 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019252 static void
19253f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019254 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019255 typval_T *rettv;
19256{
19257#ifdef FEAT_SYN_HL
19258 long lnum;
19259 long col;
19260 int i;
19261 int id;
19262#endif
19263
19264 rettv->v_type = VAR_LIST;
19265 rettv->vval.v_list = NULL;
19266
19267#ifdef FEAT_SYN_HL
19268 lnum = get_tv_lnum(argvars); /* -1 on type error */
19269 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19270
19271 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019272 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019273 && rettv_list_alloc(rettv) != FAIL)
19274 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019275 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019276 for (i = 0; ; ++i)
19277 {
19278 id = syn_get_stack_item(i);
19279 if (id < 0)
19280 break;
19281 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19282 break;
19283 }
19284 }
19285#endif
19286}
19287
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019289get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019290 typval_T *argvars;
19291 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019292 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019294 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019296 char_u *infile = NULL;
19297 char_u buf[NUMBUFLEN];
19298 int err = FALSE;
19299 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019300 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019301 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019303 rettv->v_type = VAR_STRING;
19304 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019305 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019306 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019308 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019309 {
19310 /*
19311 * Write the string to a temp file, to be used for input of the shell
19312 * command.
19313 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019314 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019315 {
19316 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019317 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019318 }
19319
19320 fd = mch_fopen((char *)infile, WRITEBIN);
19321 if (fd == NULL)
19322 {
19323 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019324 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019325 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019326 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019327 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019328 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19329 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019330 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019331 else
19332 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019333 size_t len;
19334
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019335 p = get_tv_string_buf_chk(&argvars[1], buf);
19336 if (p == NULL)
19337 {
19338 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019339 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019340 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019341 len = STRLEN(p);
19342 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019343 err = TRUE;
19344 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019345 if (fclose(fd) != 0)
19346 err = TRUE;
19347 if (err)
19348 {
19349 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019350 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019351 }
19352 }
19353
Bram Moolenaar52a72462014-08-29 15:53:52 +020019354 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19355 * echoes typeahead, that messes up the display. */
19356 if (!msg_silent)
19357 flags += SHELL_COOKED;
19358
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019359 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019361 int len;
19362 listitem_T *li;
19363 char_u *s = NULL;
19364 char_u *start;
19365 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019366 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367
Bram Moolenaar52a72462014-08-29 15:53:52 +020019368 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019369 if (res == NULL)
19370 goto errret;
19371
19372 list = list_alloc();
19373 if (list == NULL)
19374 goto errret;
19375
19376 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019378 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019379 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019380 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019381 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019382
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019383 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019384 if (s == NULL)
19385 goto errret;
19386
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019387 for (p = s; start < end; ++p, ++start)
19388 *p = *start == NUL ? NL : *start;
19389 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019390
19391 li = listitem_alloc();
19392 if (li == NULL)
19393 {
19394 vim_free(s);
19395 goto errret;
19396 }
19397 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019398 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019399 li->li_tv.vval.v_string = s;
19400 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019402
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019403 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019404 rettv->v_type = VAR_LIST;
19405 rettv->vval.v_list = list;
19406 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019408 else
19409 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019410 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019411#ifdef USE_CR
19412 /* translate <CR> into <NL> */
19413 if (res != NULL)
19414 {
19415 char_u *s;
19416
19417 for (s = res; *s; ++s)
19418 {
19419 if (*s == CAR)
19420 *s = NL;
19421 }
19422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423#else
19424# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019425 /* translate <CR><NL> into <NL> */
19426 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019428 char_u *s, *d;
19429
19430 d = res;
19431 for (s = res; *s; ++s)
19432 {
19433 if (s[0] == CAR && s[1] == NL)
19434 ++s;
19435 *d++ = *s;
19436 }
19437 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439# endif
19440#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019441 rettv->vval.v_string = res;
19442 res = NULL;
19443 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019444
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019445errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019446 if (infile != NULL)
19447 {
19448 mch_remove(infile);
19449 vim_free(infile);
19450 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019451 if (res != NULL)
19452 vim_free(res);
19453 if (list != NULL)
19454 list_free(list, TRUE);
19455}
19456
19457/*
19458 * "system()" function
19459 */
19460 static void
19461f_system(argvars, rettv)
19462 typval_T *argvars;
19463 typval_T *rettv;
19464{
19465 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19466}
19467
19468/*
19469 * "systemlist()" function
19470 */
19471 static void
19472f_systemlist(argvars, rettv)
19473 typval_T *argvars;
19474 typval_T *rettv;
19475{
19476 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477}
19478
19479/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019480 * "tabpagebuflist()" function
19481 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019482 static void
19483f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019484 typval_T *argvars UNUSED;
19485 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019486{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019487#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019488 tabpage_T *tp;
19489 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019490
19491 if (argvars[0].v_type == VAR_UNKNOWN)
19492 wp = firstwin;
19493 else
19494 {
19495 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19496 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019497 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019498 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019499 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019500 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019501 for (; wp != NULL; wp = wp->w_next)
19502 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019503 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019504 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019505 }
19506#endif
19507}
19508
19509
19510/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019511 * "tabpagenr()" function
19512 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019513 static void
19514f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019515 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019516 typval_T *rettv;
19517{
19518 int nr = 1;
19519#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019520 char_u *arg;
19521
19522 if (argvars[0].v_type != VAR_UNKNOWN)
19523 {
19524 arg = get_tv_string_chk(&argvars[0]);
19525 nr = 0;
19526 if (arg != NULL)
19527 {
19528 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019529 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019530 else
19531 EMSG2(_(e_invexpr2), arg);
19532 }
19533 }
19534 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019535 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019536#endif
19537 rettv->vval.v_number = nr;
19538}
19539
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019540
19541#ifdef FEAT_WINDOWS
19542static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19543
19544/*
19545 * Common code for tabpagewinnr() and winnr().
19546 */
19547 static int
19548get_winnr(tp, argvar)
19549 tabpage_T *tp;
19550 typval_T *argvar;
19551{
19552 win_T *twin;
19553 int nr = 1;
19554 win_T *wp;
19555 char_u *arg;
19556
19557 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19558 if (argvar->v_type != VAR_UNKNOWN)
19559 {
19560 arg = get_tv_string_chk(argvar);
19561 if (arg == NULL)
19562 nr = 0; /* type error; errmsg already given */
19563 else if (STRCMP(arg, "$") == 0)
19564 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19565 else if (STRCMP(arg, "#") == 0)
19566 {
19567 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19568 if (twin == NULL)
19569 nr = 0;
19570 }
19571 else
19572 {
19573 EMSG2(_(e_invexpr2), arg);
19574 nr = 0;
19575 }
19576 }
19577
19578 if (nr > 0)
19579 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19580 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019581 {
19582 if (wp == NULL)
19583 {
19584 /* didn't find it in this tabpage */
19585 nr = 0;
19586 break;
19587 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019588 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019589 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019590 return nr;
19591}
19592#endif
19593
19594/*
19595 * "tabpagewinnr()" function
19596 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019597 static void
19598f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019599 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019600 typval_T *rettv;
19601{
19602 int nr = 1;
19603#ifdef FEAT_WINDOWS
19604 tabpage_T *tp;
19605
19606 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19607 if (tp == NULL)
19608 nr = 0;
19609 else
19610 nr = get_winnr(tp, &argvars[1]);
19611#endif
19612 rettv->vval.v_number = nr;
19613}
19614
19615
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019616/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019617 * "tagfiles()" function
19618 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019619 static void
19620f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019621 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019622 typval_T *rettv;
19623{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019624 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019625 tagname_T tn;
19626 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019627
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019628 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019629 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019630 fname = alloc(MAXPATHL);
19631 if (fname == NULL)
19632 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019633
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019634 for (first = TRUE; ; first = FALSE)
19635 if (get_tagfname(&tn, first, fname) == FAIL
19636 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019637 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019638 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019639 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019640}
19641
19642/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019643 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019644 */
19645 static void
19646f_taglist(argvars, rettv)
19647 typval_T *argvars;
19648 typval_T *rettv;
19649{
19650 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019651
19652 tag_pattern = get_tv_string(&argvars[0]);
19653
19654 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019655 if (*tag_pattern == NUL)
19656 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019657
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019658 if (rettv_list_alloc(rettv) == OK)
19659 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019660}
19661
19662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 * "tempname()" function
19664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019666f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669{
19670 static int x = 'A';
19671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019673 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674
19675 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19676 * names. Skip 'I' and 'O', they are used for shell redirection. */
19677 do
19678 {
19679 if (x == 'Z')
19680 x = '0';
19681 else if (x == '9')
19682 x = 'A';
19683 else
19684 {
19685#ifdef EBCDIC
19686 if (x == 'I')
19687 x = 'J';
19688 else if (x == 'R')
19689 x = 'S';
19690 else
19691#endif
19692 ++x;
19693 }
19694 } while (x == 'I' || x == 'O');
19695}
19696
19697/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019698 * "test(list)" function: Just checking the walls...
19699 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019700 static void
19701f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019702 typval_T *argvars UNUSED;
19703 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019704{
19705 /* Used for unit testing. Change the code below to your liking. */
19706#if 0
19707 listitem_T *li;
19708 list_T *l;
19709 char_u *bad, *good;
19710
19711 if (argvars[0].v_type != VAR_LIST)
19712 return;
19713 l = argvars[0].vval.v_list;
19714 if (l == NULL)
19715 return;
19716 li = l->lv_first;
19717 if (li == NULL)
19718 return;
19719 bad = get_tv_string(&li->li_tv);
19720 li = li->li_next;
19721 if (li == NULL)
19722 return;
19723 good = get_tv_string(&li->li_tv);
19724 rettv->vval.v_number = test_edit_score(bad, good);
19725#endif
19726}
19727
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019728#ifdef FEAT_FLOAT
19729/*
19730 * "tan()" function
19731 */
19732 static void
19733f_tan(argvars, rettv)
19734 typval_T *argvars;
19735 typval_T *rettv;
19736{
19737 float_T f;
19738
19739 rettv->v_type = VAR_FLOAT;
19740 if (get_float_arg(argvars, &f) == OK)
19741 rettv->vval.v_float = tan(f);
19742 else
19743 rettv->vval.v_float = 0.0;
19744}
19745
19746/*
19747 * "tanh()" function
19748 */
19749 static void
19750f_tanh(argvars, rettv)
19751 typval_T *argvars;
19752 typval_T *rettv;
19753{
19754 float_T f;
19755
19756 rettv->v_type = VAR_FLOAT;
19757 if (get_float_arg(argvars, &f) == OK)
19758 rettv->vval.v_float = tanh(f);
19759 else
19760 rettv->vval.v_float = 0.0;
19761}
19762#endif
19763
Bram Moolenaard52d9742005-08-21 22:20:28 +000019764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 * "tolower(string)" function
19766 */
19767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019769 typval_T *argvars;
19770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771{
19772 char_u *p;
19773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774 p = vim_strsave(get_tv_string(&argvars[0]));
19775 rettv->v_type = VAR_STRING;
19776 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777
19778 if (p != NULL)
19779 while (*p != NUL)
19780 {
19781#ifdef FEAT_MBYTE
19782 int l;
19783
19784 if (enc_utf8)
19785 {
19786 int c, lc;
19787
19788 c = utf_ptr2char(p);
19789 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019790 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 /* TODO: reallocate string when byte count changes. */
19792 if (utf_char2len(lc) == l)
19793 utf_char2bytes(lc, p);
19794 p += l;
19795 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019796 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 p += l; /* skip multi-byte character */
19798 else
19799#endif
19800 {
19801 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19802 ++p;
19803 }
19804 }
19805}
19806
19807/*
19808 * "toupper(string)" function
19809 */
19810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019811f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019812 typval_T *argvars;
19813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019816 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817}
19818
19819/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019820 * "tr(string, fromstr, tostr)" function
19821 */
19822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 typval_T *argvars;
19825 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019826{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019827 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019828 char_u *fromstr;
19829 char_u *tostr;
19830 char_u *p;
19831#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019832 int inlen;
19833 int fromlen;
19834 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019835 int idx;
19836 char_u *cpstr;
19837 int cplen;
19838 int first = TRUE;
19839#endif
19840 char_u buf[NUMBUFLEN];
19841 char_u buf2[NUMBUFLEN];
19842 garray_T ga;
19843
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019844 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019845 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19846 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019847
19848 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019849 rettv->v_type = VAR_STRING;
19850 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019851 if (fromstr == NULL || tostr == NULL)
19852 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019853 ga_init2(&ga, (int)sizeof(char), 80);
19854
19855#ifdef FEAT_MBYTE
19856 if (!has_mbyte)
19857#endif
19858 /* not multi-byte: fromstr and tostr must be the same length */
19859 if (STRLEN(fromstr) != STRLEN(tostr))
19860 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019861#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019862error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019863#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019864 EMSG2(_(e_invarg2), fromstr);
19865 ga_clear(&ga);
19866 return;
19867 }
19868
19869 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019870 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019871 {
19872#ifdef FEAT_MBYTE
19873 if (has_mbyte)
19874 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019875 inlen = (*mb_ptr2len)(in_str);
19876 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019877 cplen = inlen;
19878 idx = 0;
19879 for (p = fromstr; *p != NUL; p += fromlen)
19880 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019881 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019882 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019883 {
19884 for (p = tostr; *p != NUL; p += tolen)
19885 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019886 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019887 if (idx-- == 0)
19888 {
19889 cplen = tolen;
19890 cpstr = p;
19891 break;
19892 }
19893 }
19894 if (*p == NUL) /* tostr is shorter than fromstr */
19895 goto error;
19896 break;
19897 }
19898 ++idx;
19899 }
19900
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019901 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019902 {
19903 /* Check that fromstr and tostr have the same number of
19904 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019905 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019906 first = FALSE;
19907 for (p = tostr; *p != NUL; p += tolen)
19908 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019909 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019910 --idx;
19911 }
19912 if (idx != 0)
19913 goto error;
19914 }
19915
Bram Moolenaarcde88542015-08-11 19:14:00 +020019916 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019917 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019918 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019919
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019920 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019921 }
19922 else
19923#endif
19924 {
19925 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019926 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019927 if (p != NULL)
19928 ga_append(&ga, tostr[p - fromstr]);
19929 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019930 ga_append(&ga, *in_str);
19931 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019932 }
19933 }
19934
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019935 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019936 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019937 ga_append(&ga, NUL);
19938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019939 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019940}
19941
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019942#ifdef FEAT_FLOAT
19943/*
19944 * "trunc({float})" function
19945 */
19946 static void
19947f_trunc(argvars, rettv)
19948 typval_T *argvars;
19949 typval_T *rettv;
19950{
19951 float_T f;
19952
19953 rettv->v_type = VAR_FLOAT;
19954 if (get_float_arg(argvars, &f) == OK)
19955 /* trunc() is not in C90, use floor() or ceil() instead. */
19956 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19957 else
19958 rettv->vval.v_float = 0.0;
19959}
19960#endif
19961
Bram Moolenaar8299df92004-07-10 09:47:34 +000019962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 * "type(expr)" function
19964 */
19965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019966f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019967 typval_T *argvars;
19968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019970 int n;
19971
19972 switch (argvars[0].v_type)
19973 {
19974 case VAR_NUMBER: n = 0; break;
19975 case VAR_STRING: n = 1; break;
19976 case VAR_FUNC: n = 2; break;
19977 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019978 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019979#ifdef FEAT_FLOAT
19980 case VAR_FLOAT: n = 5; break;
19981#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019982 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19983 }
19984 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985}
19986
19987/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019988 * "undofile(name)" function
19989 */
19990 static void
19991f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019992 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019993 typval_T *rettv;
19994{
19995 rettv->v_type = VAR_STRING;
19996#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019997 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019998 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019999
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020000 if (*fname == NUL)
20001 {
20002 /* If there is no file name there will be no undo file. */
20003 rettv->vval.v_string = NULL;
20004 }
20005 else
20006 {
20007 char_u *ffname = FullName_save(fname, FALSE);
20008
20009 if (ffname != NULL)
20010 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20011 vim_free(ffname);
20012 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020013 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020014#else
20015 rettv->vval.v_string = NULL;
20016#endif
20017}
20018
20019/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020020 * "undotree()" function
20021 */
20022 static void
20023f_undotree(argvars, rettv)
20024 typval_T *argvars UNUSED;
20025 typval_T *rettv;
20026{
20027 if (rettv_dict_alloc(rettv) == OK)
20028 {
20029 dict_T *dict = rettv->vval.v_dict;
20030 list_T *list;
20031
Bram Moolenaar730cde92010-06-27 05:18:54 +020020032 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020033 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020034 dict_add_nr_str(dict, "save_last",
20035 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020036 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20037 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020038 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020039
20040 list = list_alloc();
20041 if (list != NULL)
20042 {
20043 u_eval_tree(curbuf->b_u_oldhead, list);
20044 dict_add_list(dict, "entries", list);
20045 }
20046 }
20047}
20048
20049/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020050 * "values(dict)" function
20051 */
20052 static void
20053f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 typval_T *argvars;
20055 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020056{
20057 dict_list(argvars, rettv, 1);
20058}
20059
20060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 * "virtcol(string)" function
20062 */
20063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020064f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 typval_T *argvars;
20066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067{
20068 colnr_T vcol = 0;
20069 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020070 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020072 fp = var2fpos(&argvars[0], FALSE, &fnum);
20073 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20074 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 {
20076 getvvcol(curwin, fp, NULL, NULL, &vcol);
20077 ++vcol;
20078 }
20079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020080 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081}
20082
20083/*
20084 * "visualmode()" function
20085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020087f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020088 typval_T *argvars UNUSED;
20089 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 char_u str[2];
20092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020093 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 str[0] = curbuf->b_visual_mode_eval;
20095 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097
20098 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020099 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101}
20102
20103/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020104 * "wildmenumode()" function
20105 */
20106 static void
20107f_wildmenumode(argvars, rettv)
20108 typval_T *argvars UNUSED;
20109 typval_T *rettv UNUSED;
20110{
20111#ifdef FEAT_WILDMENU
20112 if (wild_menu_showing)
20113 rettv->vval.v_number = 1;
20114#endif
20115}
20116
20117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 * "winbufnr(nr)" function
20119 */
20120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020121f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 typval_T *argvars;
20123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124{
20125 win_T *wp;
20126
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020127 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020129 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020131 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132}
20133
20134/*
20135 * "wincol()" function
20136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020139 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141{
20142 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144}
20145
20146/*
20147 * "winheight(nr)" function
20148 */
20149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020150f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020151 typval_T *argvars;
20152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153{
20154 win_T *wp;
20155
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020156 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020160 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161}
20162
20163/*
20164 * "winline()" function
20165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020167f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170{
20171 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173}
20174
20175/*
20176 * "winnr()" function
20177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020179f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020180 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182{
20183 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020184
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020186 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020188 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189}
20190
20191/*
20192 * "winrestcmd()" function
20193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020195f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020196 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198{
20199#ifdef FEAT_WINDOWS
20200 win_T *wp;
20201 int winnr = 1;
20202 garray_T ga;
20203 char_u buf[50];
20204
20205 ga_init2(&ga, (int)sizeof(char), 70);
20206 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20207 {
20208 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20209 ga_concat(&ga, buf);
20210# ifdef FEAT_VERTSPLIT
20211 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20212 ga_concat(&ga, buf);
20213# endif
20214 ++winnr;
20215 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020216 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020220 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020222 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223}
20224
20225/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020226 * "winrestview()" function
20227 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020228 static void
20229f_winrestview(argvars, rettv)
20230 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020231 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020232{
20233 dict_T *dict;
20234
20235 if (argvars[0].v_type != VAR_DICT
20236 || (dict = argvars[0].vval.v_dict) == NULL)
20237 EMSG(_(e_invarg));
20238 else
20239 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020240 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20241 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20242 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20243 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020244#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020245 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20246 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020247#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020248 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20249 {
20250 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20251 curwin->w_set_curswant = FALSE;
20252 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020253
Bram Moolenaar82c25852014-05-28 16:47:16 +020020254 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20255 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020256#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020257 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20258 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020259#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020260 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20261 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20262 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20263 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020264
20265 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020266 win_new_height(curwin, curwin->w_height);
20267# ifdef FEAT_VERTSPLIT
20268 win_new_width(curwin, W_WIDTH(curwin));
20269# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020270 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020271
Bram Moolenaarb851a962014-10-31 15:45:52 +010020272 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020273 curwin->w_topline = 1;
20274 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20275 curwin->w_topline = curbuf->b_ml.ml_line_count;
20276#ifdef FEAT_DIFF
20277 check_topfill(curwin, TRUE);
20278#endif
20279 }
20280}
20281
20282/*
20283 * "winsaveview()" function
20284 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020285 static void
20286f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020287 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020288 typval_T *rettv;
20289{
20290 dict_T *dict;
20291
Bram Moolenaara800b422010-06-27 01:15:55 +020020292 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020293 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020294 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020295
20296 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20297 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20298#ifdef FEAT_VIRTUALEDIT
20299 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20300#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020301 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020302 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20303
20304 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20305#ifdef FEAT_DIFF
20306 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20307#endif
20308 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20309 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20310}
20311
20312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 * "winwidth(nr)" function
20314 */
20315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020316f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020317 typval_T *argvars;
20318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319{
20320 win_T *wp;
20321
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020322 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020324 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 else
20326#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020327 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020329 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330#endif
20331}
20332
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020334 * "wordcount()" function
20335 */
20336 static void
20337f_wordcount(argvars, rettv)
20338 typval_T *argvars UNUSED;
20339 typval_T *rettv;
20340{
20341 if (rettv_dict_alloc(rettv) == FAIL)
20342 return;
20343 cursor_pos_info(rettv->vval.v_dict);
20344}
20345
20346/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020347 * Write list of strings to file
20348 */
20349 static int
20350write_list(fd, list, binary)
20351 FILE *fd;
20352 list_T *list;
20353 int binary;
20354{
20355 listitem_T *li;
20356 int c;
20357 int ret = OK;
20358 char_u *s;
20359
20360 for (li = list->lv_first; li != NULL; li = li->li_next)
20361 {
20362 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20363 {
20364 if (*s == '\n')
20365 c = putc(NUL, fd);
20366 else
20367 c = putc(*s, fd);
20368 if (c == EOF)
20369 {
20370 ret = FAIL;
20371 break;
20372 }
20373 }
20374 if (!binary || li->li_next != NULL)
20375 if (putc('\n', fd) == EOF)
20376 {
20377 ret = FAIL;
20378 break;
20379 }
20380 if (ret == FAIL)
20381 {
20382 EMSG(_(e_write));
20383 break;
20384 }
20385 }
20386 return ret;
20387}
20388
20389/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020390 * "writefile()" function
20391 */
20392 static void
20393f_writefile(argvars, rettv)
20394 typval_T *argvars;
20395 typval_T *rettv;
20396{
20397 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020398 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020399 char_u *fname;
20400 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020401 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020402
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020403 if (check_restricted() || check_secure())
20404 return;
20405
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020406 if (argvars[0].v_type != VAR_LIST)
20407 {
20408 EMSG2(_(e_listarg), "writefile()");
20409 return;
20410 }
20411 if (argvars[0].vval.v_list == NULL)
20412 return;
20413
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020414 if (argvars[2].v_type != VAR_UNKNOWN)
20415 {
20416 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20417 binary = TRUE;
20418 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20419 append = TRUE;
20420 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020421
20422 /* Always open the file in binary mode, library functions have a mind of
20423 * their own about CR-LF conversion. */
20424 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020425 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20426 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020427 {
20428 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20429 ret = -1;
20430 }
20431 else
20432 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020433 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20434 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020435 fclose(fd);
20436 }
20437
20438 rettv->vval.v_number = ret;
20439}
20440
20441/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020442 * "xor(expr, expr)" function
20443 */
20444 static void
20445f_xor(argvars, rettv)
20446 typval_T *argvars;
20447 typval_T *rettv;
20448{
20449 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20450 ^ get_tv_number_chk(&argvars[1], NULL);
20451}
20452
20453
20454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020456 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 */
20458 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020459var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020460 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020461 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020462 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020464 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020466 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467
Bram Moolenaara5525202006-03-02 22:52:09 +000020468 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020469 if (varp->v_type == VAR_LIST)
20470 {
20471 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020472 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020473 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020474 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020475
20476 l = varp->vval.v_list;
20477 if (l == NULL)
20478 return NULL;
20479
20480 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020481 pos.lnum = list_find_nr(l, 0L, &error);
20482 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020483 return NULL; /* invalid line number */
20484
20485 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020486 pos.col = list_find_nr(l, 1L, &error);
20487 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020488 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020489 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020490
20491 /* We accept "$" for the column number: last column. */
20492 li = list_find(l, 1L);
20493 if (li != NULL && li->li_tv.v_type == VAR_STRING
20494 && li->li_tv.vval.v_string != NULL
20495 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20496 pos.col = len + 1;
20497
Bram Moolenaara5525202006-03-02 22:52:09 +000020498 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020499 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020500 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020501 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020502
Bram Moolenaara5525202006-03-02 22:52:09 +000020503#ifdef FEAT_VIRTUALEDIT
20504 /* Get the virtual offset. Defaults to zero. */
20505 pos.coladd = list_find_nr(l, 2L, &error);
20506 if (error)
20507 pos.coladd = 0;
20508#endif
20509
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020510 return &pos;
20511 }
20512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020513 name = get_tv_string_chk(varp);
20514 if (name == NULL)
20515 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020516 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020518 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20519 {
20520 if (VIsual_active)
20521 return &VIsual;
20522 return &curwin->w_cursor;
20523 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020524 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020526 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20528 return NULL;
20529 return pp;
20530 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020531
20532#ifdef FEAT_VIRTUALEDIT
20533 pos.coladd = 0;
20534#endif
20535
Bram Moolenaar477933c2007-07-17 14:32:23 +000020536 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020537 {
20538 pos.col = 0;
20539 if (name[1] == '0') /* "w0": first visible line */
20540 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020541 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020542 pos.lnum = curwin->w_topline;
20543 return &pos;
20544 }
20545 else if (name[1] == '$') /* "w$": last visible line */
20546 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020547 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020548 pos.lnum = curwin->w_botline - 1;
20549 return &pos;
20550 }
20551 }
20552 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020554 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 {
20556 pos.lnum = curbuf->b_ml.ml_line_count;
20557 pos.col = 0;
20558 }
20559 else
20560 {
20561 pos.lnum = curwin->w_cursor.lnum;
20562 pos.col = (colnr_T)STRLEN(ml_get_curline());
20563 }
20564 return &pos;
20565 }
20566 return NULL;
20567}
20568
20569/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020570 * Convert list in "arg" into a position and optional file number.
20571 * When "fnump" is NULL there is no file number, only 3 items.
20572 * Note that the column is passed on as-is, the caller may want to decrement
20573 * it to use 1 for the first column.
20574 * Return FAIL when conversion is not possible, doesn't check the position for
20575 * validity.
20576 */
20577 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020578list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020579 typval_T *arg;
20580 pos_T *posp;
20581 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020582 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020583{
20584 list_T *l = arg->vval.v_list;
20585 long i = 0;
20586 long n;
20587
Bram Moolenaar493c1782014-05-28 14:34:46 +020020588 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20589 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020590 if (arg->v_type != VAR_LIST
20591 || l == NULL
20592 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020593 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020594 return FAIL;
20595
20596 if (fnump != NULL)
20597 {
20598 n = list_find_nr(l, i++, NULL); /* fnum */
20599 if (n < 0)
20600 return FAIL;
20601 if (n == 0)
20602 n = curbuf->b_fnum; /* current buffer */
20603 *fnump = n;
20604 }
20605
20606 n = list_find_nr(l, i++, NULL); /* lnum */
20607 if (n < 0)
20608 return FAIL;
20609 posp->lnum = n;
20610
20611 n = list_find_nr(l, i++, NULL); /* col */
20612 if (n < 0)
20613 return FAIL;
20614 posp->col = n;
20615
20616#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020617 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020618 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020619 posp->coladd = 0;
20620 else
20621 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020622#endif
20623
Bram Moolenaar493c1782014-05-28 14:34:46 +020020624 if (curswantp != NULL)
20625 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20626
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020627 return OK;
20628}
20629
20630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 * Get the length of an environment variable name.
20632 * Advance "arg" to the first character after the name.
20633 * Return 0 for error.
20634 */
20635 static int
20636get_env_len(arg)
20637 char_u **arg;
20638{
20639 char_u *p;
20640 int len;
20641
20642 for (p = *arg; vim_isIDc(*p); ++p)
20643 ;
20644 if (p == *arg) /* no name found */
20645 return 0;
20646
20647 len = (int)(p - *arg);
20648 *arg = p;
20649 return len;
20650}
20651
20652/*
20653 * Get the length of the name of a function or internal variable.
20654 * "arg" is advanced to the first non-white character after the name.
20655 * Return 0 if something is wrong.
20656 */
20657 static int
20658get_id_len(arg)
20659 char_u **arg;
20660{
20661 char_u *p;
20662 int len;
20663
20664 /* Find the end of the name. */
20665 for (p = *arg; eval_isnamec(*p); ++p)
20666 ;
20667 if (p == *arg) /* no name found */
20668 return 0;
20669
20670 len = (int)(p - *arg);
20671 *arg = skipwhite(p);
20672
20673 return len;
20674}
20675
20676/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020677 * Get the length of the name of a variable or function.
20678 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020680 * Return -1 if curly braces expansion failed.
20681 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 * If the name contains 'magic' {}'s, expand them and return the
20683 * expanded name in an allocated string via 'alias' - caller must free.
20684 */
20685 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020686get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 char_u **arg;
20688 char_u **alias;
20689 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020690 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691{
20692 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 char_u *p;
20694 char_u *expr_start;
20695 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696
20697 *alias = NULL; /* default to no alias */
20698
20699 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20700 && (*arg)[2] == (int)KE_SNR)
20701 {
20702 /* hard coded <SNR>, already translated */
20703 *arg += 3;
20704 return get_id_len(arg) + 3;
20705 }
20706 len = eval_fname_script(*arg);
20707 if (len > 0)
20708 {
20709 /* literal "<SID>", "s:" or "<SNR>" */
20710 *arg += len;
20711 }
20712
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020714 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020716 p = find_name_end(*arg, &expr_start, &expr_end,
20717 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 if (expr_start != NULL)
20719 {
20720 char_u *temp_string;
20721
20722 if (!evaluate)
20723 {
20724 len += (int)(p - *arg);
20725 *arg = skipwhite(p);
20726 return len;
20727 }
20728
20729 /*
20730 * Include any <SID> etc in the expanded string:
20731 * Thus the -len here.
20732 */
20733 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20734 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020735 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 *alias = temp_string;
20737 *arg = skipwhite(p);
20738 return (int)STRLEN(temp_string);
20739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740
20741 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020742 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 EMSG2(_(e_invexpr2), *arg);
20744
20745 return len;
20746}
20747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020748/*
20749 * Find the end of a variable or function name, taking care of magic braces.
20750 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20751 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020752 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020753 * Return a pointer to just after the name. Equal to "arg" if there is no
20754 * valid name.
20755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020757find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 char_u *arg;
20759 char_u **expr_start;
20760 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020761 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020763 int mb_nest = 0;
20764 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 char_u *p;
20766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020767 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020769 *expr_start = NULL;
20770 *expr_end = NULL;
20771 }
20772
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020773 /* Quick check for valid starting character. */
20774 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20775 return arg;
20776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020777 for (p = arg; *p != NUL
20778 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020779 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020780 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020781 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020782 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020783 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020784 if (*p == '\'')
20785 {
20786 /* skip over 'string' to avoid counting [ and ] inside it. */
20787 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20788 ;
20789 if (*p == NUL)
20790 break;
20791 }
20792 else if (*p == '"')
20793 {
20794 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20795 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20796 if (*p == '\\' && p[1] != NUL)
20797 ++p;
20798 if (*p == NUL)
20799 break;
20800 }
20801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020802 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 if (*p == '[')
20805 ++br_nest;
20806 else if (*p == ']')
20807 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020810 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020812 if (*p == '{')
20813 {
20814 mb_nest++;
20815 if (expr_start != NULL && *expr_start == NULL)
20816 *expr_start = p;
20817 }
20818 else if (*p == '}')
20819 {
20820 mb_nest--;
20821 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20822 *expr_end = p;
20823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 }
20826
20827 return p;
20828}
20829
20830/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020831 * Expands out the 'magic' {}'s in a variable/function name.
20832 * Note that this can call itself recursively, to deal with
20833 * constructs like foo{bar}{baz}{bam}
20834 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20835 * "in_start" ^
20836 * "expr_start" ^
20837 * "expr_end" ^
20838 * "in_end" ^
20839 *
20840 * Returns a new allocated string, which the caller must free.
20841 * Returns NULL for failure.
20842 */
20843 static char_u *
20844make_expanded_name(in_start, expr_start, expr_end, in_end)
20845 char_u *in_start;
20846 char_u *expr_start;
20847 char_u *expr_end;
20848 char_u *in_end;
20849{
20850 char_u c1;
20851 char_u *retval = NULL;
20852 char_u *temp_result;
20853 char_u *nextcmd = NULL;
20854
20855 if (expr_end == NULL || in_end == NULL)
20856 return NULL;
20857 *expr_start = NUL;
20858 *expr_end = NUL;
20859 c1 = *in_end;
20860 *in_end = NUL;
20861
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020862 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020863 if (temp_result != NULL && nextcmd == NULL)
20864 {
20865 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20866 + (in_end - expr_end) + 1));
20867 if (retval != NULL)
20868 {
20869 STRCPY(retval, in_start);
20870 STRCAT(retval, temp_result);
20871 STRCAT(retval, expr_end + 1);
20872 }
20873 }
20874 vim_free(temp_result);
20875
20876 *in_end = c1; /* put char back for error messages */
20877 *expr_start = '{';
20878 *expr_end = '}';
20879
20880 if (retval != NULL)
20881 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020882 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020883 if (expr_start != NULL)
20884 {
20885 /* Further expansion! */
20886 temp_result = make_expanded_name(retval, expr_start,
20887 expr_end, temp_result);
20888 vim_free(retval);
20889 retval = temp_result;
20890 }
20891 }
20892
20893 return retval;
20894}
20895
20896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020898 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 */
20900 static int
20901eval_isnamec(c)
20902 int c;
20903{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020904 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20905}
20906
20907/*
20908 * Return TRUE if character "c" can be used as the first character in a
20909 * variable or function name (excluding '{' and '}').
20910 */
20911 static int
20912eval_isnamec1(c)
20913 int c;
20914{
20915 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916}
20917
20918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 * Set number v: variable to "val".
20920 */
20921 void
20922set_vim_var_nr(idx, val)
20923 int idx;
20924 long val;
20925{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020926 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927}
20928
20929/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020930 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 */
20932 long
20933get_vim_var_nr(idx)
20934 int idx;
20935{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020936 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937}
20938
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020939/*
20940 * Get string v: variable value. Uses a static buffer, can only be used once.
20941 */
20942 char_u *
20943get_vim_var_str(idx)
20944 int idx;
20945{
20946 return get_tv_string(&vimvars[idx].vv_tv);
20947}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020948
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020950 * Get List v: variable value. Caller must take care of reference count when
20951 * needed.
20952 */
20953 list_T *
20954get_vim_var_list(idx)
20955 int idx;
20956{
20957 return vimvars[idx].vv_list;
20958}
20959
20960/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020961 * Set v:char to character "c".
20962 */
20963 void
20964set_vim_var_char(c)
20965 int c;
20966{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020967 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020968
20969#ifdef FEAT_MBYTE
20970 if (has_mbyte)
20971 buf[(*mb_char2bytes)(c, buf)] = NUL;
20972 else
20973#endif
20974 {
20975 buf[0] = c;
20976 buf[1] = NUL;
20977 }
20978 set_vim_var_string(VV_CHAR, buf, -1);
20979}
20980
20981/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020982 * Set v:count to "count" and v:count1 to "count1".
20983 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 */
20985 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020986set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 long count;
20988 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020989 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020991 if (set_prevcount)
20992 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020993 vimvars[VV_COUNT].vv_nr = count;
20994 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995}
20996
20997/*
20998 * Set string v: variable to a copy of "val".
20999 */
21000 void
21001set_vim_var_string(idx, val, len)
21002 int idx;
21003 char_u *val;
21004 int len; /* length of "val" to use or -1 (whole string) */
21005{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021006 /* Need to do this (at least) once, since we can't initialize a union.
21007 * Will always be invoked when "v:progname" is set. */
21008 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21009
Bram Moolenaare9a41262005-01-15 22:18:47 +000021010 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021012 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021014 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021016 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017}
21018
21019/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021020 * Set List v: variable to "val".
21021 */
21022 void
21023set_vim_var_list(idx, val)
21024 int idx;
21025 list_T *val;
21026{
21027 list_unref(vimvars[idx].vv_list);
21028 vimvars[idx].vv_list = val;
21029 if (val != NULL)
21030 ++val->lv_refcount;
21031}
21032
21033/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021034 * Set Dictionary v: variable to "val".
21035 */
21036 void
21037set_vim_var_dict(idx, val)
21038 int idx;
21039 dict_T *val;
21040{
21041 int todo;
21042 hashitem_T *hi;
21043
21044 dict_unref(vimvars[idx].vv_dict);
21045 vimvars[idx].vv_dict = val;
21046 if (val != NULL)
21047 {
21048 ++val->dv_refcount;
21049
21050 /* Set readonly */
21051 todo = (int)val->dv_hashtab.ht_used;
21052 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21053 {
21054 if (HASHITEM_EMPTY(hi))
21055 continue;
21056 --todo;
21057 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21058 }
21059 }
21060}
21061
21062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 * Set v:register if needed.
21064 */
21065 void
21066set_reg_var(c)
21067 int c;
21068{
21069 char_u regname;
21070
21071 if (c == 0 || c == ' ')
21072 regname = '"';
21073 else
21074 regname = c;
21075 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021076 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 set_vim_var_string(VV_REG, &regname, 1);
21078}
21079
21080/*
21081 * Get or set v:exception. If "oldval" == NULL, return the current value.
21082 * Otherwise, restore the value to "oldval" and return NULL.
21083 * Must always be called in pairs to save and restore v:exception! Does not
21084 * take care of memory allocations.
21085 */
21086 char_u *
21087v_exception(oldval)
21088 char_u *oldval;
21089{
21090 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021091 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092
Bram Moolenaare9a41262005-01-15 22:18:47 +000021093 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 return NULL;
21095}
21096
21097/*
21098 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21099 * Otherwise, restore the value to "oldval" and return NULL.
21100 * Must always be called in pairs to save and restore v:throwpoint! Does not
21101 * take care of memory allocations.
21102 */
21103 char_u *
21104v_throwpoint(oldval)
21105 char_u *oldval;
21106{
21107 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021108 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109
Bram Moolenaare9a41262005-01-15 22:18:47 +000021110 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 return NULL;
21112}
21113
21114#if defined(FEAT_AUTOCMD) || defined(PROTO)
21115/*
21116 * Set v:cmdarg.
21117 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21118 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21119 * Must always be called in pairs!
21120 */
21121 char_u *
21122set_cmdarg(eap, oldarg)
21123 exarg_T *eap;
21124 char_u *oldarg;
21125{
21126 char_u *oldval;
21127 char_u *newval;
21128 unsigned len;
21129
Bram Moolenaare9a41262005-01-15 22:18:47 +000021130 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021131 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021133 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021134 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021135 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 }
21137
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021138 if (eap->force_bin == FORCE_BIN)
21139 len = 6;
21140 else if (eap->force_bin == FORCE_NOBIN)
21141 len = 8;
21142 else
21143 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021144
21145 if (eap->read_edit)
21146 len += 7;
21147
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021148 if (eap->force_ff != 0)
21149 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21150# ifdef FEAT_MBYTE
21151 if (eap->force_enc != 0)
21152 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021153 if (eap->bad_char != 0)
21154 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021155# endif
21156
21157 newval = alloc(len + 1);
21158 if (newval == NULL)
21159 return NULL;
21160
21161 if (eap->force_bin == FORCE_BIN)
21162 sprintf((char *)newval, " ++bin");
21163 else if (eap->force_bin == FORCE_NOBIN)
21164 sprintf((char *)newval, " ++nobin");
21165 else
21166 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021167
21168 if (eap->read_edit)
21169 STRCAT(newval, " ++edit");
21170
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021171 if (eap->force_ff != 0)
21172 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21173 eap->cmd + eap->force_ff);
21174# ifdef FEAT_MBYTE
21175 if (eap->force_enc != 0)
21176 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21177 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021178 if (eap->bad_char == BAD_KEEP)
21179 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21180 else if (eap->bad_char == BAD_DROP)
21181 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21182 else if (eap->bad_char != 0)
21183 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021184# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021185 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021186 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187}
21188#endif
21189
21190/*
21191 * Get the value of internal variable "name".
21192 * Return OK or FAIL.
21193 */
21194 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021195get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 char_u *name;
21197 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021198 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021199 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021200 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021201 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202{
21203 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021204 typval_T *tv = NULL;
21205 typval_T atv;
21206 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208
21209 /* truncate the name, so that we can use strcmp() */
21210 cc = name[len];
21211 name[len] = NUL;
21212
21213 /*
21214 * Check for "b:changedtick".
21215 */
21216 if (STRCMP(name, "b:changedtick") == 0)
21217 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021218 atv.v_type = VAR_NUMBER;
21219 atv.vval.v_number = curbuf->b_changedtick;
21220 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 }
21222
21223 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 * Check for user-defined variables.
21225 */
21226 else
21227 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021228 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021231 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021232 if (dip != NULL)
21233 *dip = v;
21234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 }
21236
Bram Moolenaare9a41262005-01-15 22:18:47 +000021237 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021239 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021240 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 ret = FAIL;
21242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021243 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021244 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245
21246 name[len] = cc;
21247
21248 return ret;
21249}
21250
21251/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021252 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21253 * Also handle function call with Funcref variable: func(expr)
21254 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21255 */
21256 static int
21257handle_subscript(arg, rettv, evaluate, verbose)
21258 char_u **arg;
21259 typval_T *rettv;
21260 int evaluate; /* do more than finding the end */
21261 int verbose; /* give error messages */
21262{
21263 int ret = OK;
21264 dict_T *selfdict = NULL;
21265 char_u *s;
21266 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021267 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021268
21269 while (ret == OK
21270 && (**arg == '['
21271 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021272 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021273 && !vim_iswhite(*(*arg - 1)))
21274 {
21275 if (**arg == '(')
21276 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021277 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021278 if (evaluate)
21279 {
21280 functv = *rettv;
21281 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021282
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021283 /* Invoke the function. Recursive! */
21284 s = functv.vval.v_string;
21285 }
21286 else
21287 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021288 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021289 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21290 &len, evaluate, selfdict);
21291
21292 /* Clear the funcref afterwards, so that deleting it while
21293 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021294 if (evaluate)
21295 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021296
21297 /* Stop the expression evaluation when immediately aborting on
21298 * error, or when an interrupt occurred or an exception was thrown
21299 * but not caught. */
21300 if (aborting())
21301 {
21302 if (ret == OK)
21303 clear_tv(rettv);
21304 ret = FAIL;
21305 }
21306 dict_unref(selfdict);
21307 selfdict = NULL;
21308 }
21309 else /* **arg == '[' || **arg == '.' */
21310 {
21311 dict_unref(selfdict);
21312 if (rettv->v_type == VAR_DICT)
21313 {
21314 selfdict = rettv->vval.v_dict;
21315 if (selfdict != NULL)
21316 ++selfdict->dv_refcount;
21317 }
21318 else
21319 selfdict = NULL;
21320 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21321 {
21322 clear_tv(rettv);
21323 ret = FAIL;
21324 }
21325 }
21326 }
21327 dict_unref(selfdict);
21328 return ret;
21329}
21330
21331/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021332 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021333 * value).
21334 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021335 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021336alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021337{
Bram Moolenaar33570922005-01-25 22:26:29 +000021338 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021339}
21340
21341/*
21342 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 * The string "s" must have been allocated, it is consumed.
21344 * Return NULL for out of memory, the variable otherwise.
21345 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021346 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021347alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 char_u *s;
21349{
Bram Moolenaar33570922005-01-25 22:26:29 +000021350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021352 rettv = alloc_tv();
21353 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021355 rettv->v_type = VAR_STRING;
21356 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 }
21358 else
21359 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021360 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361}
21362
21363/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021364 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021366 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021368 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369{
21370 if (varp != NULL)
21371 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021372 switch (varp->v_type)
21373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021374 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021375 func_unref(varp->vval.v_string);
21376 /*FALLTHROUGH*/
21377 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021378 vim_free(varp->vval.v_string);
21379 break;
21380 case VAR_LIST:
21381 list_unref(varp->vval.v_list);
21382 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021383 case VAR_DICT:
21384 dict_unref(varp->vval.v_dict);
21385 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021386 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021387#ifdef FEAT_FLOAT
21388 case VAR_FLOAT:
21389#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021390 case VAR_UNKNOWN:
21391 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021392 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021393 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394 break;
21395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 vim_free(varp);
21397 }
21398}
21399
21400/*
21401 * Free the memory for a variable value and set the value to NULL or 0.
21402 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021403 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021405 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406{
21407 if (varp != NULL)
21408 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021409 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021411 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021412 func_unref(varp->vval.v_string);
21413 /*FALLTHROUGH*/
21414 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021415 vim_free(varp->vval.v_string);
21416 varp->vval.v_string = NULL;
21417 break;
21418 case VAR_LIST:
21419 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021420 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021421 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021422 case VAR_DICT:
21423 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021424 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021425 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021426 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021427 varp->vval.v_number = 0;
21428 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021429#ifdef FEAT_FLOAT
21430 case VAR_FLOAT:
21431 varp->vval.v_float = 0.0;
21432 break;
21433#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 case VAR_UNKNOWN:
21435 break;
21436 default:
21437 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021439 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 }
21441}
21442
21443/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021444 * Set the value of a variable to NULL without freeing items.
21445 */
21446 static void
21447init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021449{
21450 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021451 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021452}
21453
21454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 * Get the number value of a variable.
21456 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021457 * For incompatible types, return 0.
21458 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21459 * caller of incompatible types: it sets *denote to TRUE if "denote"
21460 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 */
21462 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021463get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021464 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021466 int error = FALSE;
21467
21468 return get_tv_number_chk(varp, &error); /* return 0L on error */
21469}
21470
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021471 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021472get_tv_number_chk(varp, denote)
21473 typval_T *varp;
21474 int *denote;
21475{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021476 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021478 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021480 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021481 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021482#ifdef FEAT_FLOAT
21483 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021484 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021485 break;
21486#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021487 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021488 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021489 break;
21490 case VAR_STRING:
21491 if (varp->vval.v_string != NULL)
21492 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021493 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021494 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021495 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021496 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021497 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021498 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021499 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021500 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021501 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021505 if (denote == NULL) /* useful for values that must be unsigned */
21506 n = -1;
21507 else
21508 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021509 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510}
21511
21512/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021513 * Get the lnum from the first argument.
21514 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021515 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 */
21517 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021518get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021519 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520{
Bram Moolenaar33570922005-01-25 22:26:29 +000021521 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 linenr_T lnum;
21523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021524 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 if (lnum == 0) /* no valid number, try using line() */
21526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021527 rettv.v_type = VAR_NUMBER;
21528 f_line(argvars, &rettv);
21529 lnum = rettv.vval.v_number;
21530 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 }
21532 return lnum;
21533}
21534
21535/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021536 * Get the lnum from the first argument.
21537 * Also accepts "$", then "buf" is used.
21538 * Returns 0 on error.
21539 */
21540 static linenr_T
21541get_tv_lnum_buf(argvars, buf)
21542 typval_T *argvars;
21543 buf_T *buf;
21544{
21545 if (argvars[0].v_type == VAR_STRING
21546 && argvars[0].vval.v_string != NULL
21547 && argvars[0].vval.v_string[0] == '$'
21548 && buf != NULL)
21549 return buf->b_ml.ml_line_count;
21550 return get_tv_number_chk(&argvars[0], NULL);
21551}
21552
21553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 * Get the string value of a variable.
21555 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021556 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21557 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 * If the String variable has never been set, return an empty string.
21559 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021560 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21561 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 */
21563 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021564get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021565 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566{
21567 static char_u mybuf[NUMBUFLEN];
21568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021569 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570}
21571
21572 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021573get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021574 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021575 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021577 char_u *res = get_tv_string_buf_chk(varp, buf);
21578
21579 return res != NULL ? res : (char_u *)"";
21580}
21581
Bram Moolenaar7d647822014-04-05 21:28:56 +020021582/*
21583 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21584 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021585 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021586get_tv_string_chk(varp)
21587 typval_T *varp;
21588{
21589 static char_u mybuf[NUMBUFLEN];
21590
21591 return get_tv_string_buf_chk(varp, mybuf);
21592}
21593
21594 static char_u *
21595get_tv_string_buf_chk(varp, buf)
21596 typval_T *varp;
21597 char_u *buf;
21598{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021599 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021601 case VAR_NUMBER:
21602 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21603 return buf;
21604 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021605 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021606 break;
21607 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021608 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021609 break;
21610 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021611 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021612 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021613#ifdef FEAT_FLOAT
21614 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021615 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021616 break;
21617#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021618 case VAR_STRING:
21619 if (varp->vval.v_string != NULL)
21620 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021621 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021622 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021623 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021626 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627}
21628
21629/*
21630 * Find variable "name" in the list of variables.
21631 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021632 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021633 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021634 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021637find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021639 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021640 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021643 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644
Bram Moolenaara7043832005-01-21 11:56:39 +000021645 ht = find_var_ht(name, &varname);
21646 if (htp != NULL)
21647 *htp = ht;
21648 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021650 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651}
21652
21653/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021654 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021655 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021657 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021658find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021660 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021661 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021662 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021663{
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 hashitem_T *hi;
21665
21666 if (*varname == NUL)
21667 {
21668 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021669 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021670 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021671 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021672 case 'g': return &globvars_var;
21673 case 'v': return &vimvars_var;
21674 case 'b': return &curbuf->b_bufvar;
21675 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021676#ifdef FEAT_WINDOWS
21677 case 't': return &curtab->tp_winvar;
21678#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021679 case 'l': return current_funccal == NULL
21680 ? NULL : &current_funccal->l_vars_var;
21681 case 'a': return current_funccal == NULL
21682 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 }
21684 return NULL;
21685 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021686
21687 hi = hash_find(ht, varname);
21688 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021689 {
21690 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021691 * worked find the variable again. Don't auto-load a script if it was
21692 * loaded already, otherwise it would be loaded every time when
21693 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021694 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021695 {
21696 /* Note: script_autoload() may make "hi" invalid. It must either
21697 * be obtained again or not used. */
21698 if (!script_autoload(varname, FALSE) || aborting())
21699 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021700 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021701 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021702 if (HASHITEM_EMPTY(hi))
21703 return NULL;
21704 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021705 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021706}
21707
21708/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021709 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021710 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021711 * Set "varname" to the start of name without ':'.
21712 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021713 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021714find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715 char_u *name;
21716 char_u **varname;
21717{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021718 hashitem_T *hi;
21719
Bram Moolenaar73627d02015-08-11 15:46:09 +020021720 if (name[0] == NUL)
21721 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 if (name[1] != ':')
21723 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021724 /* The name must not start with a colon or #. */
21725 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 return NULL;
21727 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021728
21729 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021730 hi = hash_find(&compat_hashtab, name);
21731 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021732 return &compat_hashtab;
21733
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021735 return &globvarht; /* global variable */
21736 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 }
21738 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021739 if (*name == 'g') /* global variable */
21740 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021741 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21742 */
21743 if (vim_strchr(name + 2, ':') != NULL
21744 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021745 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021747 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021749 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021750#ifdef FEAT_WINDOWS
21751 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021752 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021754 if (*name == 'v') /* v: variable */
21755 return &vimvarht;
21756 if (*name == 'a' && current_funccal != NULL) /* function argument */
21757 return &current_funccal->l_avars.dv_hashtab;
21758 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21759 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 if (*name == 's' /* script variable */
21761 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21762 return &SCRIPT_VARS(current_SID);
21763 return NULL;
21764}
21765
21766/*
21767 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021768 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 * Returns NULL when it doesn't exist.
21770 */
21771 char_u *
21772get_var_value(name)
21773 char_u *name;
21774{
Bram Moolenaar33570922005-01-25 22:26:29 +000021775 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021777 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 if (v == NULL)
21779 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021780 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781}
21782
21783/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021784 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 * sourcing this script and when executing functions defined in the script.
21786 */
21787 void
21788new_script_vars(id)
21789 scid_T id;
21790{
Bram Moolenaara7043832005-01-21 11:56:39 +000021791 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 hashtab_T *ht;
21793 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021794
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21796 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021797 /* Re-allocating ga_data means that an ht_array pointing to
21798 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021799 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021800 for (i = 1; i <= ga_scripts.ga_len; ++i)
21801 {
21802 ht = &SCRIPT_VARS(i);
21803 if (ht->ht_mask == HT_INIT_SIZE - 1)
21804 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021805 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021807 }
21808
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 while (ga_scripts.ga_len < id)
21810 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021811 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021812 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021813 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815 }
21816 }
21817}
21818
21819/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021820 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21821 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 */
21823 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021824init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 dict_T *dict;
21826 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021827 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828{
Bram Moolenaar33570922005-01-25 22:26:29 +000021829 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021830 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021831 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021832 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021833 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021834 dict_var->di_tv.vval.v_dict = dict;
21835 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021836 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021837 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21838 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839}
21840
21841/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021842 * Unreference a dictionary initialized by init_var_dict().
21843 */
21844 void
21845unref_var_dict(dict)
21846 dict_T *dict;
21847{
21848 /* Now the dict needs to be freed if no one else is using it, go back to
21849 * normal reference counting. */
21850 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21851 dict_unref(dict);
21852}
21853
21854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021856 * Frees all allocated variables and the value they contain.
21857 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 */
21859 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021860vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021861 hashtab_T *ht;
21862{
21863 vars_clear_ext(ht, TRUE);
21864}
21865
21866/*
21867 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21868 */
21869 static void
21870vars_clear_ext(ht, free_val)
21871 hashtab_T *ht;
21872 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873{
Bram Moolenaara7043832005-01-21 11:56:39 +000021874 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 hashitem_T *hi;
21876 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877
Bram Moolenaar33570922005-01-25 22:26:29 +000021878 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021879 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021880 for (hi = ht->ht_array; todo > 0; ++hi)
21881 {
21882 if (!HASHITEM_EMPTY(hi))
21883 {
21884 --todo;
21885
Bram Moolenaar33570922005-01-25 22:26:29 +000021886 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021887 * ht_array might change then. hash_clear() takes care of it
21888 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021889 v = HI2DI(hi);
21890 if (free_val)
21891 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021892 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021893 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021894 }
21895 }
21896 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021897 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898}
21899
Bram Moolenaara7043832005-01-21 11:56:39 +000021900/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 * Delete a variable from hashtab "ht" at item "hi".
21902 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021905delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 hashtab_T *ht;
21907 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908{
Bram Moolenaar33570922005-01-25 22:26:29 +000021909 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021910
21911 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 clear_tv(&di->di_tv);
21913 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914}
21915
21916/*
21917 * List the value of one internal variable.
21918 */
21919 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021920list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021921 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021923 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 char_u *tofree;
21926 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021927 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021928
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021929 current_copyID += COPYID_INC;
21930 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021931 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021932 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021933 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934}
21935
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021937list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 char_u *prefix;
21939 char_u *name;
21940 int type;
21941 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021942 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943{
Bram Moolenaar31859182007-08-14 20:41:13 +000021944 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21945 msg_start();
21946 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 if (name != NULL) /* "a:" vars don't have a name stored */
21948 msg_puts(name);
21949 msg_putchar(' ');
21950 msg_advance(22);
21951 if (type == VAR_NUMBER)
21952 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021953 else if (type == VAR_FUNC)
21954 msg_putchar('*');
21955 else if (type == VAR_LIST)
21956 {
21957 msg_putchar('[');
21958 if (*string == '[')
21959 ++string;
21960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021961 else if (type == VAR_DICT)
21962 {
21963 msg_putchar('{');
21964 if (*string == '{')
21965 ++string;
21966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 else
21968 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021969
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021971
21972 if (type == VAR_FUNC)
21973 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021974 if (*first)
21975 {
21976 msg_clr_eos();
21977 *first = FALSE;
21978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979}
21980
21981/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021982 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 * If the variable already exists, the value is updated.
21984 * Otherwise the variable is created.
21985 */
21986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021987set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021989 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021990 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991{
Bram Moolenaar33570922005-01-25 22:26:29 +000021992 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021996 ht = find_var_ht(name, &varname);
21997 if (ht == NULL || *varname == NUL)
21998 {
21999 EMSG2(_(e_illvar), name);
22000 return;
22001 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022002 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022003
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022004 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22005 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022006
Bram Moolenaar33570922005-01-25 22:26:29 +000022007 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022010 if (var_check_ro(v->di_flags, name, FALSE)
22011 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022012 return;
22013 if (v->di_tv.v_type != tv->v_type
22014 && !((v->di_tv.v_type == VAR_STRING
22015 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022016 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022017 || tv->v_type == VAR_NUMBER))
22018#ifdef FEAT_FLOAT
22019 && !((v->di_tv.v_type == VAR_NUMBER
22020 || v->di_tv.v_type == VAR_FLOAT)
22021 && (tv->v_type == VAR_NUMBER
22022 || tv->v_type == VAR_FLOAT))
22023#endif
22024 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022025 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022026 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022027 return;
22028 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022029
22030 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022031 * Handle setting internal v: variables separately where needed to
22032 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 */
22034 if (ht == &vimvarht)
22035 {
22036 if (v->di_tv.v_type == VAR_STRING)
22037 {
22038 vim_free(v->di_tv.vval.v_string);
22039 if (copy || tv->v_type != VAR_STRING)
22040 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22041 else
22042 {
22043 /* Take over the string to avoid an extra alloc/free. */
22044 v->di_tv.vval.v_string = tv->vval.v_string;
22045 tv->vval.v_string = NULL;
22046 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022047 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022048 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022049 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022050 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022051 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022052 if (STRCMP(varname, "searchforward") == 0)
22053 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022054#ifdef FEAT_SEARCH_EXTRA
22055 else if (STRCMP(varname, "hlsearch") == 0)
22056 {
22057 no_hlsearch = !v->di_tv.vval.v_number;
22058 redraw_all_later(SOME_VALID);
22059 }
22060#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022061 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022062 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022063 else if (v->di_tv.v_type != tv->v_type)
22064 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022065 }
22066
22067 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 }
22069 else /* add a new variable */
22070 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022071 /* Can't add "v:" variable. */
22072 if (ht == &vimvarht)
22073 {
22074 EMSG2(_(e_illvar), name);
22075 return;
22076 }
22077
Bram Moolenaar92124a32005-06-17 22:03:40 +000022078 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022079 if (!valid_varname(varname))
22080 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022081
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022082 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22083 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022084 if (v == NULL)
22085 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022086 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022087 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022089 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 return;
22091 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022092 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022094
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022095 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022096 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022097 else
22098 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022099 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022100 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022101 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103}
22104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022105/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022106 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022107 * Also give an error message.
22108 */
22109 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022110var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022111 int flags;
22112 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022113 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022114{
22115 if (flags & DI_FLAGS_RO)
22116 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022117 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022118 return TRUE;
22119 }
22120 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22121 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022122 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022123 return TRUE;
22124 }
22125 return FALSE;
22126}
22127
22128/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022129 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22130 * Also give an error message.
22131 */
22132 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022133var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022134 int flags;
22135 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022136 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022137{
22138 if (flags & DI_FLAGS_FIX)
22139 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022140 EMSG2(_("E795: Cannot delete variable %s"),
22141 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022142 return TRUE;
22143 }
22144 return FALSE;
22145}
22146
22147/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022148 * Check if a funcref is assigned to a valid variable name.
22149 * Return TRUE and give an error if not.
22150 */
22151 static int
22152var_check_func_name(name, new_var)
22153 char_u *name; /* points to start of variable name */
22154 int new_var; /* TRUE when creating the variable */
22155{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022156 /* Allow for w: b: s: and t:. */
22157 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022158 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22159 ? name[2] : name[0]))
22160 {
22161 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22162 name);
22163 return TRUE;
22164 }
22165 /* Don't allow hiding a function. When "v" is not NULL we might be
22166 * assigning another function to the same var, the type is checked
22167 * below. */
22168 if (new_var && function_exists(name))
22169 {
22170 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22171 name);
22172 return TRUE;
22173 }
22174 return FALSE;
22175}
22176
22177/*
22178 * Check if a variable name is valid.
22179 * Return FALSE and give an error if not.
22180 */
22181 static int
22182valid_varname(varname)
22183 char_u *varname;
22184{
22185 char_u *p;
22186
22187 for (p = varname; *p != NUL; ++p)
22188 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22189 && *p != AUTOLOAD_CHAR)
22190 {
22191 EMSG2(_(e_illvar), varname);
22192 return FALSE;
22193 }
22194 return TRUE;
22195}
22196
22197/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022198 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022199 * Also give an error message, using "name" or _("name") when use_gettext is
22200 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022201 */
22202 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022203tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022204 int lock;
22205 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022206 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022207{
22208 if (lock & VAR_LOCKED)
22209 {
22210 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022211 name == NULL ? (char_u *)_("Unknown")
22212 : use_gettext ? (char_u *)_(name)
22213 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022214 return TRUE;
22215 }
22216 if (lock & VAR_FIXED)
22217 {
22218 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022219 name == NULL ? (char_u *)_("Unknown")
22220 : use_gettext ? (char_u *)_(name)
22221 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022222 return TRUE;
22223 }
22224 return FALSE;
22225}
22226
22227/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022228 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022229 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022230 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022231 * It is OK for "from" and "to" to point to the same item. This is used to
22232 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022233 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022234 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022235copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022236 typval_T *from;
22237 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022239 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022240 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022241 switch (from->v_type)
22242 {
22243 case VAR_NUMBER:
22244 to->vval.v_number = from->vval.v_number;
22245 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022246#ifdef FEAT_FLOAT
22247 case VAR_FLOAT:
22248 to->vval.v_float = from->vval.v_float;
22249 break;
22250#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022251 case VAR_STRING:
22252 case VAR_FUNC:
22253 if (from->vval.v_string == NULL)
22254 to->vval.v_string = NULL;
22255 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022256 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022257 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022258 if (from->v_type == VAR_FUNC)
22259 func_ref(to->vval.v_string);
22260 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022261 break;
22262 case VAR_LIST:
22263 if (from->vval.v_list == NULL)
22264 to->vval.v_list = NULL;
22265 else
22266 {
22267 to->vval.v_list = from->vval.v_list;
22268 ++to->vval.v_list->lv_refcount;
22269 }
22270 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022271 case VAR_DICT:
22272 if (from->vval.v_dict == NULL)
22273 to->vval.v_dict = NULL;
22274 else
22275 {
22276 to->vval.v_dict = from->vval.v_dict;
22277 ++to->vval.v_dict->dv_refcount;
22278 }
22279 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022280 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022281 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022282 break;
22283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284}
22285
22286/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022287 * Make a copy of an item.
22288 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022289 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22290 * reference to an already copied list/dict can be used.
22291 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022292 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022293 static int
22294item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022295 typval_T *from;
22296 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022297 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022298 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022299{
22300 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022301 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022302
Bram Moolenaar33570922005-01-25 22:26:29 +000022303 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022304 {
22305 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022306 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022307 }
22308 ++recurse;
22309
22310 switch (from->v_type)
22311 {
22312 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022313#ifdef FEAT_FLOAT
22314 case VAR_FLOAT:
22315#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022316 case VAR_STRING:
22317 case VAR_FUNC:
22318 copy_tv(from, to);
22319 break;
22320 case VAR_LIST:
22321 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022322 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022323 if (from->vval.v_list == NULL)
22324 to->vval.v_list = NULL;
22325 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22326 {
22327 /* use the copy made earlier */
22328 to->vval.v_list = from->vval.v_list->lv_copylist;
22329 ++to->vval.v_list->lv_refcount;
22330 }
22331 else
22332 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22333 if (to->vval.v_list == NULL)
22334 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022335 break;
22336 case VAR_DICT:
22337 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022338 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022339 if (from->vval.v_dict == NULL)
22340 to->vval.v_dict = NULL;
22341 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22342 {
22343 /* use the copy made earlier */
22344 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22345 ++to->vval.v_dict->dv_refcount;
22346 }
22347 else
22348 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22349 if (to->vval.v_dict == NULL)
22350 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022351 break;
22352 default:
22353 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022354 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022355 }
22356 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022357 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022358}
22359
22360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 * ":echo expr1 ..." print each argument separated with a space, add a
22362 * newline at the end.
22363 * ":echon expr1 ..." print each argument plain.
22364 */
22365 void
22366ex_echo(eap)
22367 exarg_T *eap;
22368{
22369 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022370 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022371 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 char_u *p;
22373 int needclr = TRUE;
22374 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022375 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376
22377 if (eap->skip)
22378 ++emsg_skip;
22379 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22380 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022381 /* If eval1() causes an error message the text from the command may
22382 * still need to be cleared. E.g., "echo 22,44". */
22383 need_clr_eos = needclr;
22384
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022386 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 {
22388 /*
22389 * Report the invalid expression unless the expression evaluation
22390 * has been cancelled due to an aborting error, an interrupt, or an
22391 * exception.
22392 */
22393 if (!aborting())
22394 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022395 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022396 break;
22397 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022398 need_clr_eos = FALSE;
22399
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 if (!eap->skip)
22401 {
22402 if (atstart)
22403 {
22404 atstart = FALSE;
22405 /* Call msg_start() after eval1(), evaluating the expression
22406 * may cause a message to appear. */
22407 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022408 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022409 /* Mark the saved text as finishing the line, so that what
22410 * follows is displayed on a new line when scrolling back
22411 * at the more prompt. */
22412 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 }
22416 else if (eap->cmdidx == CMD_echo)
22417 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022418 current_copyID += COPYID_INC;
22419 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022420 if (p != NULL)
22421 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022423 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022425 if (*p != TAB && needclr)
22426 {
22427 /* remove any text still there from the command */
22428 msg_clr_eos();
22429 needclr = FALSE;
22430 }
22431 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 }
22433 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022434 {
22435#ifdef FEAT_MBYTE
22436 if (has_mbyte)
22437 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022438 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022439
22440 (void)msg_outtrans_len_attr(p, i, echo_attr);
22441 p += i - 1;
22442 }
22443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022445 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022448 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022450 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 arg = skipwhite(arg);
22452 }
22453 eap->nextcmd = check_nextcmd(arg);
22454
22455 if (eap->skip)
22456 --emsg_skip;
22457 else
22458 {
22459 /* remove text that may still be there from the command */
22460 if (needclr)
22461 msg_clr_eos();
22462 if (eap->cmdidx == CMD_echo)
22463 msg_end();
22464 }
22465}
22466
22467/*
22468 * ":echohl {name}".
22469 */
22470 void
22471ex_echohl(eap)
22472 exarg_T *eap;
22473{
22474 int id;
22475
22476 id = syn_name2id(eap->arg);
22477 if (id == 0)
22478 echo_attr = 0;
22479 else
22480 echo_attr = syn_id2attr(id);
22481}
22482
22483/*
22484 * ":execute expr1 ..." execute the result of an expression.
22485 * ":echomsg expr1 ..." Print a message
22486 * ":echoerr expr1 ..." Print an error
22487 * Each gets spaces around each argument and a newline at the end for
22488 * echo commands
22489 */
22490 void
22491ex_execute(eap)
22492 exarg_T *eap;
22493{
22494 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022495 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 int ret = OK;
22497 char_u *p;
22498 garray_T ga;
22499 int len;
22500 int save_did_emsg;
22501
22502 ga_init2(&ga, 1, 80);
22503
22504 if (eap->skip)
22505 ++emsg_skip;
22506 while (*arg != NUL && *arg != '|' && *arg != '\n')
22507 {
22508 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022509 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 {
22511 /*
22512 * Report the invalid expression unless the expression evaluation
22513 * has been cancelled due to an aborting error, an interrupt, or an
22514 * exception.
22515 */
22516 if (!aborting())
22517 EMSG2(_(e_invexpr2), p);
22518 ret = FAIL;
22519 break;
22520 }
22521
22522 if (!eap->skip)
22523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022524 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 len = (int)STRLEN(p);
22526 if (ga_grow(&ga, len + 2) == FAIL)
22527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022528 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 ret = FAIL;
22530 break;
22531 }
22532 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 ga.ga_len += len;
22536 }
22537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 arg = skipwhite(arg);
22540 }
22541
22542 if (ret != FAIL && ga.ga_data != NULL)
22543 {
22544 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022547 out_flush();
22548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 else if (eap->cmdidx == CMD_echoerr)
22550 {
22551 /* We don't want to abort following commands, restore did_emsg. */
22552 save_did_emsg = did_emsg;
22553 EMSG((char_u *)ga.ga_data);
22554 if (!force_abort)
22555 did_emsg = save_did_emsg;
22556 }
22557 else if (eap->cmdidx == CMD_execute)
22558 do_cmdline((char_u *)ga.ga_data,
22559 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22560 }
22561
22562 ga_clear(&ga);
22563
22564 if (eap->skip)
22565 --emsg_skip;
22566
22567 eap->nextcmd = check_nextcmd(arg);
22568}
22569
22570/*
22571 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22572 * "arg" points to the "&" or '+' when called, to "option" when returning.
22573 * Returns NULL when no option name found. Otherwise pointer to the char
22574 * after the option name.
22575 */
22576 static char_u *
22577find_option_end(arg, opt_flags)
22578 char_u **arg;
22579 int *opt_flags;
22580{
22581 char_u *p = *arg;
22582
22583 ++p;
22584 if (*p == 'g' && p[1] == ':')
22585 {
22586 *opt_flags = OPT_GLOBAL;
22587 p += 2;
22588 }
22589 else if (*p == 'l' && p[1] == ':')
22590 {
22591 *opt_flags = OPT_LOCAL;
22592 p += 2;
22593 }
22594 else
22595 *opt_flags = 0;
22596
22597 if (!ASCII_ISALPHA(*p))
22598 return NULL;
22599 *arg = p;
22600
22601 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22602 p += 4; /* termcap option */
22603 else
22604 while (ASCII_ISALPHA(*p))
22605 ++p;
22606 return p;
22607}
22608
22609/*
22610 * ":function"
22611 */
22612 void
22613ex_function(eap)
22614 exarg_T *eap;
22615{
22616 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022617 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 int j;
22619 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022621 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622 char_u *name = NULL;
22623 char_u *p;
22624 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022625 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 garray_T newargs;
22627 garray_T newlines;
22628 int varargs = FALSE;
22629 int mustend = FALSE;
22630 int flags = 0;
22631 ufunc_T *fp;
22632 int indent;
22633 int nesting;
22634 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022635 dictitem_T *v;
22636 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022637 static int func_nr = 0; /* number for nameless function */
22638 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022639 hashtab_T *ht;
22640 int todo;
22641 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022642 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643
22644 /*
22645 * ":function" without argument: list functions.
22646 */
22647 if (ends_excmd(*eap->arg))
22648 {
22649 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022650 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022651 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022652 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022653 {
22654 if (!HASHITEM_EMPTY(hi))
22655 {
22656 --todo;
22657 fp = HI2UF(hi);
22658 if (!isdigit(*fp->uf_name))
22659 list_func_head(fp, FALSE);
22660 }
22661 }
22662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 eap->nextcmd = check_nextcmd(eap->arg);
22664 return;
22665 }
22666
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022667 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022668 * ":function /pat": list functions matching pattern.
22669 */
22670 if (*eap->arg == '/')
22671 {
22672 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22673 if (!eap->skip)
22674 {
22675 regmatch_T regmatch;
22676
22677 c = *p;
22678 *p = NUL;
22679 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22680 *p = c;
22681 if (regmatch.regprog != NULL)
22682 {
22683 regmatch.rm_ic = p_ic;
22684
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022685 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022686 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22687 {
22688 if (!HASHITEM_EMPTY(hi))
22689 {
22690 --todo;
22691 fp = HI2UF(hi);
22692 if (!isdigit(*fp->uf_name)
22693 && vim_regexec(&regmatch, fp->uf_name, 0))
22694 list_func_head(fp, FALSE);
22695 }
22696 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022697 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022698 }
22699 }
22700 if (*p == '/')
22701 ++p;
22702 eap->nextcmd = check_nextcmd(p);
22703 return;
22704 }
22705
22706 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022707 * Get the function name. There are these situations:
22708 * func normal function name
22709 * "name" == func, "fudi.fd_dict" == NULL
22710 * dict.func new dictionary entry
22711 * "name" == NULL, "fudi.fd_dict" set,
22712 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22713 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022714 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022715 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22716 * dict.func existing dict entry that's not a Funcref
22717 * "name" == NULL, "fudi.fd_dict" set,
22718 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022719 * s:func script-local function name
22720 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022723 name = trans_function_name(&p, eap->skip, 0, &fudi);
22724 paren = (vim_strchr(p, '(') != NULL);
22725 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 {
22727 /*
22728 * Return on an invalid expression in braces, unless the expression
22729 * evaluation has been cancelled due to an aborting error, an
22730 * interrupt, or an exception.
22731 */
22732 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022733 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022734 if (!eap->skip && fudi.fd_newkey != NULL)
22735 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022736 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 else
22740 eap->skip = TRUE;
22741 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022742
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 /* An error in a function call during evaluation of an expression in magic
22744 * braces should not cause the function not to be defined. */
22745 saved_did_emsg = did_emsg;
22746 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747
22748 /*
22749 * ":function func" with only function name: list function.
22750 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022751 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 {
22753 if (!ends_excmd(*skipwhite(p)))
22754 {
22755 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022756 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 }
22758 eap->nextcmd = check_nextcmd(p);
22759 if (eap->nextcmd != NULL)
22760 *p = NUL;
22761 if (!eap->skip && !got_int)
22762 {
22763 fp = find_func(name);
22764 if (fp != NULL)
22765 {
22766 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022767 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022769 if (FUNCLINE(fp, j) == NULL)
22770 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 msg_putchar('\n');
22772 msg_outnum((long)(j + 1));
22773 if (j < 9)
22774 msg_putchar(' ');
22775 if (j < 99)
22776 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022777 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 out_flush(); /* show a line at a time */
22779 ui_breakcheck();
22780 }
22781 if (!got_int)
22782 {
22783 msg_putchar('\n');
22784 msg_puts((char_u *)" endfunction");
22785 }
22786 }
22787 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022788 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022790 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 }
22792
22793 /*
22794 * ":function name(arg1, arg2)" Define function.
22795 */
22796 p = skipwhite(p);
22797 if (*p != '(')
22798 {
22799 if (!eap->skip)
22800 {
22801 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022802 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 }
22804 /* attempt to continue by skipping some text */
22805 if (vim_strchr(p, '(') != NULL)
22806 p = vim_strchr(p, '(');
22807 }
22808 p = skipwhite(p + 1);
22809
22810 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22811 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22812
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022813 if (!eap->skip)
22814 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022815 /* Check the name of the function. Unless it's a dictionary function
22816 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022817 if (name != NULL)
22818 arg = name;
22819 else
22820 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022821 if (arg != NULL && (fudi.fd_di == NULL
22822 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022823 {
22824 if (*arg == K_SPECIAL)
22825 j = 3;
22826 else
22827 j = 0;
22828 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22829 : eval_isnamec(arg[j])))
22830 ++j;
22831 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022832 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022833 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022834 /* Disallow using the g: dict. */
22835 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22836 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022837 }
22838
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 /*
22840 * Isolate the arguments: "arg1, arg2, ...)"
22841 */
22842 while (*p != ')')
22843 {
22844 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22845 {
22846 varargs = TRUE;
22847 p += 3;
22848 mustend = TRUE;
22849 }
22850 else
22851 {
22852 arg = p;
22853 while (ASCII_ISALNUM(*p) || *p == '_')
22854 ++p;
22855 if (arg == p || isdigit(*arg)
22856 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22857 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22858 {
22859 if (!eap->skip)
22860 EMSG2(_("E125: Illegal argument: %s"), arg);
22861 break;
22862 }
22863 if (ga_grow(&newargs, 1) == FAIL)
22864 goto erret;
22865 c = *p;
22866 *p = NUL;
22867 arg = vim_strsave(arg);
22868 if (arg == NULL)
22869 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022870
22871 /* Check for duplicate argument name. */
22872 for (i = 0; i < newargs.ga_len; ++i)
22873 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22874 {
22875 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022876 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022877 goto erret;
22878 }
22879
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22881 *p = c;
22882 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883 if (*p == ',')
22884 ++p;
22885 else
22886 mustend = TRUE;
22887 }
22888 p = skipwhite(p);
22889 if (mustend && *p != ')')
22890 {
22891 if (!eap->skip)
22892 EMSG2(_(e_invarg2), eap->arg);
22893 break;
22894 }
22895 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022896 if (*p != ')')
22897 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 ++p; /* skip the ')' */
22899
Bram Moolenaare9a41262005-01-15 22:18:47 +000022900 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 for (;;)
22902 {
22903 p = skipwhite(p);
22904 if (STRNCMP(p, "range", 5) == 0)
22905 {
22906 flags |= FC_RANGE;
22907 p += 5;
22908 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022909 else if (STRNCMP(p, "dict", 4) == 0)
22910 {
22911 flags |= FC_DICT;
22912 p += 4;
22913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 else if (STRNCMP(p, "abort", 5) == 0)
22915 {
22916 flags |= FC_ABORT;
22917 p += 5;
22918 }
22919 else
22920 break;
22921 }
22922
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022923 /* When there is a line break use what follows for the function body.
22924 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22925 if (*p == '\n')
22926 line_arg = p + 1;
22927 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 EMSG(_(e_trailing));
22929
22930 /*
22931 * Read the body of the function, until ":endfunction" is found.
22932 */
22933 if (KeyTyped)
22934 {
22935 /* Check if the function already exists, don't let the user type the
22936 * whole function before telling him it doesn't work! For a script we
22937 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022938 if (!eap->skip && !eap->forceit)
22939 {
22940 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22941 EMSG(_(e_funcdict));
22942 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022943 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022946 if (!eap->skip && did_emsg)
22947 goto erret;
22948
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 msg_putchar('\n'); /* don't overwrite the function name */
22950 cmdline_row = msg_row;
22951 }
22952
22953 indent = 2;
22954 nesting = 0;
22955 for (;;)
22956 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022957 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022958 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022959 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022960 saved_wait_return = FALSE;
22961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022963 sourcing_lnum_off = sourcing_lnum;
22964
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022965 if (line_arg != NULL)
22966 {
22967 /* Use eap->arg, split up in parts by line breaks. */
22968 theline = line_arg;
22969 p = vim_strchr(theline, '\n');
22970 if (p == NULL)
22971 line_arg += STRLEN(line_arg);
22972 else
22973 {
22974 *p = NUL;
22975 line_arg = p + 1;
22976 }
22977 }
22978 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 theline = getcmdline(':', 0L, indent);
22980 else
22981 theline = eap->getline(':', eap->cookie, indent);
22982 if (KeyTyped)
22983 lines_left = Rows - 1;
22984 if (theline == NULL)
22985 {
22986 EMSG(_("E126: Missing :endfunction"));
22987 goto erret;
22988 }
22989
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022990 /* Detect line continuation: sourcing_lnum increased more than one. */
22991 if (sourcing_lnum > sourcing_lnum_off + 1)
22992 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22993 else
22994 sourcing_lnum_off = 0;
22995
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 if (skip_until != NULL)
22997 {
22998 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22999 * don't check for ":endfunc". */
23000 if (STRCMP(theline, skip_until) == 0)
23001 {
23002 vim_free(skip_until);
23003 skip_until = NULL;
23004 }
23005 }
23006 else
23007 {
23008 /* skip ':' and blanks*/
23009 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23010 ;
23011
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023012 /* Check for "endfunction". */
23013 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023015 if (line_arg == NULL)
23016 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 break;
23018 }
23019
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023020 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 * at "end". */
23022 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23023 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023024 else if (STRNCMP(p, "if", 2) == 0
23025 || STRNCMP(p, "wh", 2) == 0
23026 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 || STRNCMP(p, "try", 3) == 0)
23028 indent += 2;
23029
23030 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023031 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023033 if (*p == '!')
23034 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023036 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23037 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023039 ++nesting;
23040 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 }
23042 }
23043
23044 /* Check for ":append" or ":insert". */
23045 p = skip_range(p, NULL);
23046 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23047 || (p[0] == 'i'
23048 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23049 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23050 skip_until = vim_strsave((char_u *)".");
23051
23052 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23053 arg = skipwhite(skiptowhite(p));
23054 if (arg[0] == '<' && arg[1] =='<'
23055 && ((p[0] == 'p' && p[1] == 'y'
23056 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23057 || (p[0] == 'p' && p[1] == 'e'
23058 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23059 || (p[0] == 't' && p[1] == 'c'
23060 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023061 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23062 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23064 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023065 || (p[0] == 'm' && p[1] == 'z'
23066 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 ))
23068 {
23069 /* ":python <<" continues until a dot, like ":append" */
23070 p = skipwhite(arg + 2);
23071 if (*p == NUL)
23072 skip_until = vim_strsave((char_u *)".");
23073 else
23074 skip_until = vim_strsave(p);
23075 }
23076 }
23077
23078 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023079 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023080 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023081 if (line_arg == NULL)
23082 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023084 }
23085
23086 /* Copy the line to newly allocated memory. get_one_sourceline()
23087 * allocates 250 bytes per line, this saves 80% on average. The cost
23088 * is an extra alloc/free. */
23089 p = vim_strsave(theline);
23090 if (p != NULL)
23091 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023092 if (line_arg == NULL)
23093 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023094 theline = p;
23095 }
23096
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023097 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23098
23099 /* Add NULL lines for continuation lines, so that the line count is
23100 * equal to the index in the growarray. */
23101 while (sourcing_lnum_off-- > 0)
23102 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023103
23104 /* Check for end of eap->arg. */
23105 if (line_arg != NULL && *line_arg == NUL)
23106 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023107 }
23108
23109 /* Don't define the function when skipping commands or when an error was
23110 * detected. */
23111 if (eap->skip || did_emsg)
23112 goto erret;
23113
23114 /*
23115 * If there are no errors, add the function
23116 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023117 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023118 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023119 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023120 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023121 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023122 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023123 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023124 goto erret;
23125 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023126
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023127 fp = find_func(name);
23128 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023130 if (!eap->forceit)
23131 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023132 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023133 goto erret;
23134 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023135 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023136 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023137 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023138 name);
23139 goto erret;
23140 }
23141 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023142 ga_clear_strings(&(fp->uf_args));
23143 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023144 vim_free(name);
23145 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 }
23148 else
23149 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023150 char numbuf[20];
23151
23152 fp = NULL;
23153 if (fudi.fd_newkey == NULL && !eap->forceit)
23154 {
23155 EMSG(_(e_funcdict));
23156 goto erret;
23157 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023158 if (fudi.fd_di == NULL)
23159 {
23160 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023161 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023162 goto erret;
23163 }
23164 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023165 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023166 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023167
23168 /* Give the function a sequential number. Can only be used with a
23169 * Funcref! */
23170 vim_free(name);
23171 sprintf(numbuf, "%d", ++func_nr);
23172 name = vim_strsave((char_u *)numbuf);
23173 if (name == NULL)
23174 goto erret;
23175 }
23176
23177 if (fp == NULL)
23178 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023179 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023180 {
23181 int slen, plen;
23182 char_u *scriptname;
23183
23184 /* Check that the autoload name matches the script name. */
23185 j = FAIL;
23186 if (sourcing_name != NULL)
23187 {
23188 scriptname = autoload_name(name);
23189 if (scriptname != NULL)
23190 {
23191 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023192 plen = (int)STRLEN(p);
23193 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023194 if (slen > plen && fnamecmp(p,
23195 sourcing_name + slen - plen) == 0)
23196 j = OK;
23197 vim_free(scriptname);
23198 }
23199 }
23200 if (j == FAIL)
23201 {
23202 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23203 goto erret;
23204 }
23205 }
23206
23207 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 if (fp == NULL)
23209 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023210
23211 if (fudi.fd_dict != NULL)
23212 {
23213 if (fudi.fd_di == NULL)
23214 {
23215 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023216 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023217 if (fudi.fd_di == NULL)
23218 {
23219 vim_free(fp);
23220 goto erret;
23221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023222 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23223 {
23224 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023225 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023226 goto erret;
23227 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023228 }
23229 else
23230 /* overwrite existing dict entry */
23231 clear_tv(&fudi.fd_di->di_tv);
23232 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023233 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023234 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023235 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023236
23237 /* behave like "dict" was used */
23238 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023239 }
23240
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023242 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023243 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23244 {
23245 vim_free(fp);
23246 goto erret;
23247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023249 fp->uf_args = newargs;
23250 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023251#ifdef FEAT_PROFILE
23252 fp->uf_tml_count = NULL;
23253 fp->uf_tml_total = NULL;
23254 fp->uf_tml_self = NULL;
23255 fp->uf_profiling = FALSE;
23256 if (prof_def_func())
23257 func_do_profile(fp);
23258#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023259 fp->uf_varargs = varargs;
23260 fp->uf_flags = flags;
23261 fp->uf_calls = 0;
23262 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023263 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264
23265erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 ga_clear_strings(&newargs);
23267 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023268ret_free:
23269 vim_free(skip_until);
23270 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023273 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274}
23275
23276/*
23277 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023278 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023280 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023281 * TFN_INT: internal function name OK
23282 * TFN_QUIET: be quiet
23283 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284 * Advances "pp" to just after the function name (if no error).
23285 */
23286 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023287trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 char_u **pp;
23289 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023290 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023291 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023293 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 char_u *start;
23295 char_u *end;
23296 int lead;
23297 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023299 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023300
23301 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023302 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023304
23305 /* Check for hard coded <SNR>: already translated function ID (from a user
23306 * command). */
23307 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23308 && (*pp)[2] == (int)KE_SNR)
23309 {
23310 *pp += 3;
23311 len = get_id_len(pp) + 3;
23312 return vim_strnsave(start, len);
23313 }
23314
23315 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23316 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023318 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023320
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023321 /* Note that TFN_ flags use the same values as GLV_ flags. */
23322 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023323 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023324 if (end == start)
23325 {
23326 if (!skip)
23327 EMSG(_("E129: Function name required"));
23328 goto theend;
23329 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023330 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023331 {
23332 /*
23333 * Report an invalid expression in braces, unless the expression
23334 * evaluation has been cancelled due to an aborting error, an
23335 * interrupt, or an exception.
23336 */
23337 if (!aborting())
23338 {
23339 if (end != NULL)
23340 EMSG2(_(e_invarg2), start);
23341 }
23342 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023343 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023344 goto theend;
23345 }
23346
23347 if (lv.ll_tv != NULL)
23348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023349 if (fdp != NULL)
23350 {
23351 fdp->fd_dict = lv.ll_dict;
23352 fdp->fd_newkey = lv.ll_newkey;
23353 lv.ll_newkey = NULL;
23354 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023355 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023356 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23357 {
23358 name = vim_strsave(lv.ll_tv->vval.v_string);
23359 *pp = end;
23360 }
23361 else
23362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023363 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23364 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023365 EMSG(_(e_funcref));
23366 else
23367 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023368 name = NULL;
23369 }
23370 goto theend;
23371 }
23372
23373 if (lv.ll_name == NULL)
23374 {
23375 /* Error found, but continue after the function name. */
23376 *pp = end;
23377 goto theend;
23378 }
23379
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023380 /* Check if the name is a Funcref. If so, use the value. */
23381 if (lv.ll_exp_name != NULL)
23382 {
23383 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023384 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023385 if (name == lv.ll_exp_name)
23386 name = NULL;
23387 }
23388 else
23389 {
23390 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023391 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023392 if (name == *pp)
23393 name = NULL;
23394 }
23395 if (name != NULL)
23396 {
23397 name = vim_strsave(name);
23398 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023399 if (STRNCMP(name, "<SNR>", 5) == 0)
23400 {
23401 /* Change "<SNR>" to the byte sequence. */
23402 name[0] = K_SPECIAL;
23403 name[1] = KS_EXTRA;
23404 name[2] = (int)KE_SNR;
23405 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23406 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023407 goto theend;
23408 }
23409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023410 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023411 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023412 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023413 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23414 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23415 {
23416 /* When there was "s:" already or the name expanded to get a
23417 * leading "s:" then remove it. */
23418 lv.ll_name += 2;
23419 len -= 2;
23420 lead = 2;
23421 }
23422 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023423 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023424 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023425 /* skip over "s:" and "g:" */
23426 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023427 lv.ll_name += 2;
23428 len = (int)(end - lv.ll_name);
23429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023430
23431 /*
23432 * Copy the function name to allocated memory.
23433 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23434 * Accept <SNR>123_name() outside a script.
23435 */
23436 if (skip)
23437 lead = 0; /* do nothing */
23438 else if (lead > 0)
23439 {
23440 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023441 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23442 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023443 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023444 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023445 if (current_SID <= 0)
23446 {
23447 EMSG(_(e_usingsid));
23448 goto theend;
23449 }
23450 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23451 lead += (int)STRLEN(sid_buf);
23452 }
23453 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023454 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023455 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023456 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023457 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023458 goto theend;
23459 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023460 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023461 {
23462 char_u *cp = vim_strchr(lv.ll_name, ':');
23463
23464 if (cp != NULL && cp < end)
23465 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023466 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023467 goto theend;
23468 }
23469 }
23470
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023471 name = alloc((unsigned)(len + lead + 1));
23472 if (name != NULL)
23473 {
23474 if (lead > 0)
23475 {
23476 name[0] = K_SPECIAL;
23477 name[1] = KS_EXTRA;
23478 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023479 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023480 STRCPY(name + 3, sid_buf);
23481 }
23482 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023483 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023484 }
23485 *pp = end;
23486
23487theend:
23488 clear_lval(&lv);
23489 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490}
23491
23492/*
23493 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23494 * Return 2 if "p" starts with "s:".
23495 * Return 0 otherwise.
23496 */
23497 static int
23498eval_fname_script(p)
23499 char_u *p;
23500{
23501 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23502 || STRNICMP(p + 1, "SNR>", 4) == 0))
23503 return 5;
23504 if (p[0] == 's' && p[1] == ':')
23505 return 2;
23506 return 0;
23507}
23508
23509/*
23510 * Return TRUE if "p" starts with "<SID>" or "s:".
23511 * Only works if eval_fname_script() returned non-zero for "p"!
23512 */
23513 static int
23514eval_fname_sid(p)
23515 char_u *p;
23516{
23517 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23518}
23519
23520/*
23521 * List the head of the function: "name(arg1, arg2)".
23522 */
23523 static void
23524list_func_head(fp, indent)
23525 ufunc_T *fp;
23526 int indent;
23527{
23528 int j;
23529
23530 msg_start();
23531 if (indent)
23532 MSG_PUTS(" ");
23533 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023534 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 {
23536 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023537 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 }
23539 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023540 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023542 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 {
23544 if (j)
23545 MSG_PUTS(", ");
23546 msg_puts(FUNCARG(fp, j));
23547 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023548 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 {
23550 if (j)
23551 MSG_PUTS(", ");
23552 MSG_PUTS("...");
23553 }
23554 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023555 if (fp->uf_flags & FC_ABORT)
23556 MSG_PUTS(" abort");
23557 if (fp->uf_flags & FC_RANGE)
23558 MSG_PUTS(" range");
23559 if (fp->uf_flags & FC_DICT)
23560 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023561 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023562 if (p_verbose > 0)
23563 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564}
23565
23566/*
23567 * Find a function by name, return pointer to it in ufuncs.
23568 * Return NULL for unknown function.
23569 */
23570 static ufunc_T *
23571find_func(name)
23572 char_u *name;
23573{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023574 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023576 hi = hash_find(&func_hashtab, name);
23577 if (!HASHITEM_EMPTY(hi))
23578 return HI2UF(hi);
23579 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580}
23581
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023582#if defined(EXITFREE) || defined(PROTO)
23583 void
23584free_all_functions()
23585{
23586 hashitem_T *hi;
23587
23588 /* Need to start all over every time, because func_free() may change the
23589 * hash table. */
23590 while (func_hashtab.ht_used > 0)
23591 for (hi = func_hashtab.ht_array; ; ++hi)
23592 if (!HASHITEM_EMPTY(hi))
23593 {
23594 func_free(HI2UF(hi));
23595 break;
23596 }
23597}
23598#endif
23599
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023600 int
23601translated_function_exists(name)
23602 char_u *name;
23603{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023604 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023605 return find_internal_func(name) >= 0;
23606 return find_func(name) != NULL;
23607}
23608
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023609/*
23610 * Return TRUE if a function "name" exists.
23611 */
23612 static int
23613function_exists(name)
23614 char_u *name;
23615{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023616 char_u *nm = name;
23617 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023618 int n = FALSE;
23619
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023620 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23621 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023622 nm = skipwhite(nm);
23623
23624 /* Only accept "funcname", "funcname ", "funcname (..." and
23625 * "funcname(...", not "funcname!...". */
23626 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023627 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023628 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023629 return n;
23630}
23631
Bram Moolenaara1544c02013-05-30 12:35:52 +020023632 char_u *
23633get_expanded_name(name, check)
23634 char_u *name;
23635 int check;
23636{
23637 char_u *nm = name;
23638 char_u *p;
23639
23640 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23641
23642 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023643 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023644 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023645
Bram Moolenaara1544c02013-05-30 12:35:52 +020023646 vim_free(p);
23647 return NULL;
23648}
23649
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023650/*
23651 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023652 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23653 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023654 */
23655 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023656builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023657 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023658 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023659{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023660 char_u *p;
23661
23662 if (!ASCII_ISLOWER(name[0]))
23663 return FALSE;
23664 p = vim_strchr(name, AUTOLOAD_CHAR);
23665 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023666}
23667
Bram Moolenaar05159a02005-02-26 23:04:13 +000023668#if defined(FEAT_PROFILE) || defined(PROTO)
23669/*
23670 * Start profiling function "fp".
23671 */
23672 static void
23673func_do_profile(fp)
23674 ufunc_T *fp;
23675{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023676 int len = fp->uf_lines.ga_len;
23677
23678 if (len == 0)
23679 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023680 fp->uf_tm_count = 0;
23681 profile_zero(&fp->uf_tm_self);
23682 profile_zero(&fp->uf_tm_total);
23683 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023684 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023685 if (fp->uf_tml_total == NULL)
23686 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023687 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023688 if (fp->uf_tml_self == NULL)
23689 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023690 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023691 fp->uf_tml_idx = -1;
23692 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23693 || fp->uf_tml_self == NULL)
23694 return; /* out of memory */
23695
23696 fp->uf_profiling = TRUE;
23697}
23698
23699/*
23700 * Dump the profiling results for all functions in file "fd".
23701 */
23702 void
23703func_dump_profile(fd)
23704 FILE *fd;
23705{
23706 hashitem_T *hi;
23707 int todo;
23708 ufunc_T *fp;
23709 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023710 ufunc_T **sorttab;
23711 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023712
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023713 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023714 if (todo == 0)
23715 return; /* nothing to dump */
23716
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023717 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023718
Bram Moolenaar05159a02005-02-26 23:04:13 +000023719 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23720 {
23721 if (!HASHITEM_EMPTY(hi))
23722 {
23723 --todo;
23724 fp = HI2UF(hi);
23725 if (fp->uf_profiling)
23726 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023727 if (sorttab != NULL)
23728 sorttab[st_len++] = fp;
23729
Bram Moolenaar05159a02005-02-26 23:04:13 +000023730 if (fp->uf_name[0] == K_SPECIAL)
23731 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23732 else
23733 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23734 if (fp->uf_tm_count == 1)
23735 fprintf(fd, "Called 1 time\n");
23736 else
23737 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23738 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23739 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23740 fprintf(fd, "\n");
23741 fprintf(fd, "count total (s) self (s)\n");
23742
23743 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23744 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023745 if (FUNCLINE(fp, i) == NULL)
23746 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023747 prof_func_line(fd, fp->uf_tml_count[i],
23748 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023749 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23750 }
23751 fprintf(fd, "\n");
23752 }
23753 }
23754 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023755
23756 if (sorttab != NULL && st_len > 0)
23757 {
23758 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23759 prof_total_cmp);
23760 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23761 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23762 prof_self_cmp);
23763 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23764 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023765
23766 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023767}
Bram Moolenaar73830342005-02-28 22:48:19 +000023768
23769 static void
23770prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23771 FILE *fd;
23772 ufunc_T **sorttab;
23773 int st_len;
23774 char *title;
23775 int prefer_self; /* when equal print only self time */
23776{
23777 int i;
23778 ufunc_T *fp;
23779
23780 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23781 fprintf(fd, "count total (s) self (s) function\n");
23782 for (i = 0; i < 20 && i < st_len; ++i)
23783 {
23784 fp = sorttab[i];
23785 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23786 prefer_self);
23787 if (fp->uf_name[0] == K_SPECIAL)
23788 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23789 else
23790 fprintf(fd, " %s()\n", fp->uf_name);
23791 }
23792 fprintf(fd, "\n");
23793}
23794
23795/*
23796 * Print the count and times for one function or function line.
23797 */
23798 static void
23799prof_func_line(fd, count, total, self, prefer_self)
23800 FILE *fd;
23801 int count;
23802 proftime_T *total;
23803 proftime_T *self;
23804 int prefer_self; /* when equal print only self time */
23805{
23806 if (count > 0)
23807 {
23808 fprintf(fd, "%5d ", count);
23809 if (prefer_self && profile_equal(total, self))
23810 fprintf(fd, " ");
23811 else
23812 fprintf(fd, "%s ", profile_msg(total));
23813 if (!prefer_self && profile_equal(total, self))
23814 fprintf(fd, " ");
23815 else
23816 fprintf(fd, "%s ", profile_msg(self));
23817 }
23818 else
23819 fprintf(fd, " ");
23820}
23821
23822/*
23823 * Compare function for total time sorting.
23824 */
23825 static int
23826#ifdef __BORLANDC__
23827_RTLENTRYF
23828#endif
23829prof_total_cmp(s1, s2)
23830 const void *s1;
23831 const void *s2;
23832{
23833 ufunc_T *p1, *p2;
23834
23835 p1 = *(ufunc_T **)s1;
23836 p2 = *(ufunc_T **)s2;
23837 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23838}
23839
23840/*
23841 * Compare function for self time sorting.
23842 */
23843 static int
23844#ifdef __BORLANDC__
23845_RTLENTRYF
23846#endif
23847prof_self_cmp(s1, s2)
23848 const void *s1;
23849 const void *s2;
23850{
23851 ufunc_T *p1, *p2;
23852
23853 p1 = *(ufunc_T **)s1;
23854 p2 = *(ufunc_T **)s2;
23855 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23856}
23857
Bram Moolenaar05159a02005-02-26 23:04:13 +000023858#endif
23859
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023860/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023861 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023862 * Return TRUE if a package was loaded.
23863 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023864 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023865script_autoload(name, reload)
23866 char_u *name;
23867 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023868{
23869 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023870 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023871 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023872 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023873
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023874 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023875 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023876 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023877 return FALSE;
23878
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023879 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023880
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023881 /* Find the name in the list of previously loaded package names. Skip
23882 * "autoload/", it's always the same. */
23883 for (i = 0; i < ga_loaded.ga_len; ++i)
23884 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23885 break;
23886 if (!reload && i < ga_loaded.ga_len)
23887 ret = FALSE; /* was loaded already */
23888 else
23889 {
23890 /* Remember the name if it wasn't loaded already. */
23891 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23892 {
23893 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23894 tofree = NULL;
23895 }
23896
23897 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023898 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023899 ret = TRUE;
23900 }
23901
23902 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023903 return ret;
23904}
23905
23906/*
23907 * Return the autoload script name for a function or variable name.
23908 * Returns NULL when out of memory.
23909 */
23910 static char_u *
23911autoload_name(name)
23912 char_u *name;
23913{
23914 char_u *p;
23915 char_u *scriptname;
23916
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023917 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023918 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23919 if (scriptname == NULL)
23920 return FALSE;
23921 STRCPY(scriptname, "autoload/");
23922 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023923 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023924 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023925 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023926 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023927 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023928}
23929
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23931
23932/*
23933 * Function given to ExpandGeneric() to obtain the list of user defined
23934 * function names.
23935 */
23936 char_u *
23937get_user_func_name(xp, idx)
23938 expand_T *xp;
23939 int idx;
23940{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023941 static long_u done;
23942 static hashitem_T *hi;
23943 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944
23945 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023947 done = 0;
23948 hi = func_hashtab.ht_array;
23949 }
23950 if (done < func_hashtab.ht_used)
23951 {
23952 if (done++ > 0)
23953 ++hi;
23954 while (HASHITEM_EMPTY(hi))
23955 ++hi;
23956 fp = HI2UF(hi);
23957
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023958 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023959 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023960
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023961 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23962 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963
23964 cat_func_name(IObuff, fp);
23965 if (xp->xp_context != EXPAND_USER_FUNC)
23966 {
23967 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023968 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 STRCAT(IObuff, ")");
23970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971 return IObuff;
23972 }
23973 return NULL;
23974}
23975
23976#endif /* FEAT_CMDL_COMPL */
23977
23978/*
23979 * Copy the function name of "fp" to buffer "buf".
23980 * "buf" must be able to hold the function name plus three bytes.
23981 * Takes care of script-local function names.
23982 */
23983 static void
23984cat_func_name(buf, fp)
23985 char_u *buf;
23986 ufunc_T *fp;
23987{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023988 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023989 {
23990 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023991 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 }
23993 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023994 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995}
23996
23997/*
23998 * ":delfunction {name}"
23999 */
24000 void
24001ex_delfunction(eap)
24002 exarg_T *eap;
24003{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024004 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 char_u *p;
24006 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024007 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008
24009 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024010 name = trans_function_name(&p, eap->skip, 0, &fudi);
24011 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024013 {
24014 if (fudi.fd_dict != NULL && !eap->skip)
24015 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 if (!ends_excmd(*skipwhite(p)))
24019 {
24020 vim_free(name);
24021 EMSG(_(e_trailing));
24022 return;
24023 }
24024 eap->nextcmd = check_nextcmd(p);
24025 if (eap->nextcmd != NULL)
24026 *p = NUL;
24027
24028 if (!eap->skip)
24029 fp = find_func(name);
24030 vim_free(name);
24031
24032 if (!eap->skip)
24033 {
24034 if (fp == NULL)
24035 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024036 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 return;
24038 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024039 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 {
24041 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24042 return;
24043 }
24044
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024045 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024047 /* Delete the dict item that refers to the function, it will
24048 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024049 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024051 else
24052 func_free(fp);
24053 }
24054}
24055
24056/*
24057 * Free a function and remove it from the list of functions.
24058 */
24059 static void
24060func_free(fp)
24061 ufunc_T *fp;
24062{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024063 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024064
24065 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024066 ga_clear_strings(&(fp->uf_args));
24067 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024068#ifdef FEAT_PROFILE
24069 vim_free(fp->uf_tml_count);
24070 vim_free(fp->uf_tml_total);
24071 vim_free(fp->uf_tml_self);
24072#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024073
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024074 /* remove the function from the function hashtable */
24075 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24076 if (HASHITEM_EMPTY(hi))
24077 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024078 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024079 hash_remove(&func_hashtab, hi);
24080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024081 vim_free(fp);
24082}
24083
24084/*
24085 * Unreference a Function: decrement the reference count and free it when it
24086 * becomes zero. Only for numbered functions.
24087 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024088 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024089func_unref(name)
24090 char_u *name;
24091{
24092 ufunc_T *fp;
24093
24094 if (name != NULL && isdigit(*name))
24095 {
24096 fp = find_func(name);
24097 if (fp == NULL)
24098 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024099 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024100 {
24101 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024102 * when "uf_calls" becomes zero. */
24103 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024104 func_free(fp);
24105 }
24106 }
24107}
24108
24109/*
24110 * Count a reference to a Function.
24111 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024112 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024113func_ref(name)
24114 char_u *name;
24115{
24116 ufunc_T *fp;
24117
24118 if (name != NULL && isdigit(*name))
24119 {
24120 fp = find_func(name);
24121 if (fp == NULL)
24122 EMSG2(_(e_intern2), "func_ref()");
24123 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024124 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 }
24126}
24127
24128/*
24129 * Call a user function.
24130 */
24131 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024132call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 ufunc_T *fp; /* pointer to function */
24134 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024135 typval_T *argvars; /* arguments */
24136 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137 linenr_T firstline; /* first line of range */
24138 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024139 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140{
Bram Moolenaar33570922005-01-25 22:26:29 +000024141 char_u *save_sourcing_name;
24142 linenr_T save_sourcing_lnum;
24143 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024144 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024145 int save_did_emsg;
24146 static int depth = 0;
24147 dictitem_T *v;
24148 int fixvar_idx = 0; /* index in fixvar[] */
24149 int i;
24150 int ai;
24151 char_u numbuf[NUMBUFLEN];
24152 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024153 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024154#ifdef FEAT_PROFILE
24155 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024156 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158
24159 /* If depth of calling is getting too high, don't execute the function */
24160 if (depth >= p_mfd)
24161 {
24162 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024163 rettv->v_type = VAR_NUMBER;
24164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 return;
24166 }
24167 ++depth;
24168
24169 line_breakcheck(); /* check for CTRL-C hit */
24170
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024171 fc = (funccall_T *)alloc(sizeof(funccall_T));
24172 fc->caller = current_funccal;
24173 current_funccal = fc;
24174 fc->func = fp;
24175 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024176 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024177 fc->linenr = 0;
24178 fc->returned = FALSE;
24179 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024181 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24182 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183
Bram Moolenaar33570922005-01-25 22:26:29 +000024184 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024185 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024186 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24187 * each argument variable and saves a lot of time.
24188 */
24189 /*
24190 * Init l: variables.
24191 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024192 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024193 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024194 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024195 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24196 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024197 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024198 name = v->di_key;
24199 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024200 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024201 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024202 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024203 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024204 v->di_tv.vval.v_dict = selfdict;
24205 ++selfdict->dv_refcount;
24206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024207
Bram Moolenaar33570922005-01-25 22:26:29 +000024208 /*
24209 * Init a: variables.
24210 * Set a:0 to "argcount".
24211 * Set a:000 to a list with room for the "..." arguments.
24212 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024213 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024214 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024215 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024216 /* Use "name" to avoid a warning from some compiler that checks the
24217 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024218 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024219 name = v->di_key;
24220 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024221 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024222 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024223 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024224 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024225 v->di_tv.vval.v_list = &fc->l_varlist;
24226 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24227 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24228 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024229
24230 /*
24231 * Set a:firstline to "firstline" and a:lastline to "lastline".
24232 * Set a:name to named arguments.
24233 * Set a:N to the "..." arguments.
24234 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024235 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024236 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024237 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024238 (varnumber_T)lastline);
24239 for (i = 0; i < argcount; ++i)
24240 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024241 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024242 if (ai < 0)
24243 /* named argument a:name */
24244 name = FUNCARG(fp, i);
24245 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024246 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024247 /* "..." argument a:1, a:2, etc. */
24248 sprintf((char *)numbuf, "%d", ai + 1);
24249 name = numbuf;
24250 }
24251 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24252 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024253 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024254 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24255 }
24256 else
24257 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024258 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24259 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024260 if (v == NULL)
24261 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024262 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024263 }
24264 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024265 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024266
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024267 /* Note: the values are copied directly to avoid alloc/free.
24268 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024269 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024270 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024271
24272 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24273 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024274 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24275 fc->l_listitems[ai].li_tv = argvars[i];
24276 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024277 }
24278 }
24279
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280 /* Don't redraw while executing the function. */
24281 ++RedrawingDisabled;
24282 save_sourcing_name = sourcing_name;
24283 save_sourcing_lnum = sourcing_lnum;
24284 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024285 /* need space for function name + ("function " + 3) or "[number]" */
24286 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24287 + STRLEN(fp->uf_name) + 20;
24288 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 if (sourcing_name != NULL)
24290 {
24291 if (save_sourcing_name != NULL
24292 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024293 sprintf((char *)sourcing_name, "%s[%d]..",
24294 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 else
24296 STRCPY(sourcing_name, "function ");
24297 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24298
24299 if (p_verbose >= 12)
24300 {
24301 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024302 verbose_enter_scroll();
24303
Bram Moolenaar555b2802005-05-19 21:08:39 +000024304 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305 if (p_verbose >= 14)
24306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024308 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024309 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024310 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311
24312 msg_puts((char_u *)"(");
24313 for (i = 0; i < argcount; ++i)
24314 {
24315 if (i > 0)
24316 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024317 if (argvars[i].v_type == VAR_NUMBER)
24318 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319 else
24320 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024321 /* Do not want errors such as E724 here. */
24322 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024323 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024324 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024325 if (s != NULL)
24326 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024327 if (vim_strsize(s) > MSG_BUF_CLEN)
24328 {
24329 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24330 s = buf;
24331 }
24332 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024333 vim_free(tofree);
24334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 }
24336 }
24337 msg_puts((char_u *)")");
24338 }
24339 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024340
24341 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342 --no_wait_return;
24343 }
24344 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024345#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024346 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024347 {
24348 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24349 func_do_profile(fp);
24350 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024351 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024352 {
24353 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024354 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024355 profile_zero(&fp->uf_tm_children);
24356 }
24357 script_prof_save(&wait_start);
24358 }
24359#endif
24360
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024362 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 save_did_emsg = did_emsg;
24364 did_emsg = FALSE;
24365
24366 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024367 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24369
24370 --RedrawingDisabled;
24371
24372 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024373 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024375 clear_tv(rettv);
24376 rettv->v_type = VAR_NUMBER;
24377 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 }
24379
Bram Moolenaar05159a02005-02-26 23:04:13 +000024380#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024381 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024382 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024383 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024384 profile_end(&call_start);
24385 profile_sub_wait(&wait_start, &call_start);
24386 profile_add(&fp->uf_tm_total, &call_start);
24387 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024388 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024389 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024390 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24391 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024392 }
24393 }
24394#endif
24395
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396 /* when being verbose, mention the return value */
24397 if (p_verbose >= 12)
24398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024400 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024403 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024404 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024405 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024406 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024407 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024408 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024409 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024410 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024411 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024412 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024413
Bram Moolenaar555b2802005-05-19 21:08:39 +000024414 /* The value may be very long. Skip the middle part, so that we
24415 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024416 * truncate it at the end. Don't want errors such as E724 here. */
24417 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024418 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024419 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024420 if (s != NULL)
24421 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024422 if (vim_strsize(s) > MSG_BUF_CLEN)
24423 {
24424 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24425 s = buf;
24426 }
24427 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024428 vim_free(tofree);
24429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024430 }
24431 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024432
24433 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434 --no_wait_return;
24435 }
24436
24437 vim_free(sourcing_name);
24438 sourcing_name = save_sourcing_name;
24439 sourcing_lnum = save_sourcing_lnum;
24440 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024441#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024442 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443 script_prof_restore(&wait_start);
24444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445
24446 if (p_verbose >= 12 && sourcing_name != NULL)
24447 {
24448 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024449 verbose_enter_scroll();
24450
Bram Moolenaar555b2802005-05-19 21:08:39 +000024451 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024453
24454 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455 --no_wait_return;
24456 }
24457
24458 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024459 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024461
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024462 /* If the a:000 list and the l: and a: dicts are not referenced we can
24463 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024464 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24465 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24466 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24467 {
24468 free_funccal(fc, FALSE);
24469 }
24470 else
24471 {
24472 hashitem_T *hi;
24473 listitem_T *li;
24474 int todo;
24475
24476 /* "fc" is still in use. This can happen when returning "a:000" or
24477 * assigning "l:" to a global variable.
24478 * Link "fc" in the list for garbage collection later. */
24479 fc->caller = previous_funccal;
24480 previous_funccal = fc;
24481
24482 /* Make a copy of the a: variables, since we didn't do that above. */
24483 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24484 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24485 {
24486 if (!HASHITEM_EMPTY(hi))
24487 {
24488 --todo;
24489 v = HI2DI(hi);
24490 copy_tv(&v->di_tv, &v->di_tv);
24491 }
24492 }
24493
24494 /* Make a copy of the a:000 items, since we didn't do that above. */
24495 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24496 copy_tv(&li->li_tv, &li->li_tv);
24497 }
24498}
24499
24500/*
24501 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024502 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024503 */
24504 static int
24505can_free_funccal(fc, copyID)
24506 funccall_T *fc;
24507 int copyID;
24508{
24509 return (fc->l_varlist.lv_copyID != copyID
24510 && fc->l_vars.dv_copyID != copyID
24511 && fc->l_avars.dv_copyID != copyID);
24512}
24513
24514/*
24515 * Free "fc" and what it contains.
24516 */
24517 static void
24518free_funccal(fc, free_val)
24519 funccall_T *fc;
24520 int free_val; /* a: vars were allocated */
24521{
24522 listitem_T *li;
24523
24524 /* The a: variables typevals may not have been allocated, only free the
24525 * allocated variables. */
24526 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24527
24528 /* free all l: variables */
24529 vars_clear(&fc->l_vars.dv_hashtab);
24530
24531 /* Free the a:000 variables if they were allocated. */
24532 if (free_val)
24533 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24534 clear_tv(&li->li_tv);
24535
24536 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537}
24538
24539/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024540 * Add a number variable "name" to dict "dp" with value "nr".
24541 */
24542 static void
24543add_nr_var(dp, v, name, nr)
24544 dict_T *dp;
24545 dictitem_T *v;
24546 char *name;
24547 varnumber_T nr;
24548{
24549 STRCPY(v->di_key, name);
24550 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24551 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24552 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024553 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024554 v->di_tv.vval.v_number = nr;
24555}
24556
24557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 * ":return [expr]"
24559 */
24560 void
24561ex_return(eap)
24562 exarg_T *eap;
24563{
24564 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024565 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 int returning = FALSE;
24567
24568 if (current_funccal == NULL)
24569 {
24570 EMSG(_("E133: :return not inside a function"));
24571 return;
24572 }
24573
24574 if (eap->skip)
24575 ++emsg_skip;
24576
24577 eap->nextcmd = NULL;
24578 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024579 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580 {
24581 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024582 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024584 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 }
24586 /* It's safer to return also on error. */
24587 else if (!eap->skip)
24588 {
24589 /*
24590 * Return unless the expression evaluation has been cancelled due to an
24591 * aborting error, an interrupt, or an exception.
24592 */
24593 if (!aborting())
24594 returning = do_return(eap, FALSE, TRUE, NULL);
24595 }
24596
24597 /* When skipping or the return gets pending, advance to the next command
24598 * in this line (!returning). Otherwise, ignore the rest of the line.
24599 * Following lines will be ignored by get_func_line(). */
24600 if (returning)
24601 eap->nextcmd = NULL;
24602 else if (eap->nextcmd == NULL) /* no argument */
24603 eap->nextcmd = check_nextcmd(arg);
24604
24605 if (eap->skip)
24606 --emsg_skip;
24607}
24608
24609/*
24610 * Return from a function. Possibly makes the return pending. Also called
24611 * for a pending return at the ":endtry" or after returning from an extra
24612 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024613 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024614 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615 * FALSE when the return gets pending.
24616 */
24617 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024618do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619 exarg_T *eap;
24620 int reanimate;
24621 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024622 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623{
24624 int idx;
24625 struct condstack *cstack = eap->cstack;
24626
24627 if (reanimate)
24628 /* Undo the return. */
24629 current_funccal->returned = FALSE;
24630
24631 /*
24632 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24633 * not in its finally clause (which then is to be executed next) is found.
24634 * In this case, make the ":return" pending for execution at the ":endtry".
24635 * Otherwise, return normally.
24636 */
24637 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24638 if (idx >= 0)
24639 {
24640 cstack->cs_pending[idx] = CSTP_RETURN;
24641
24642 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024643 /* A pending return again gets pending. "rettv" points to an
24644 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024646 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 else
24648 {
24649 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024650 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024652 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024654 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 {
24656 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024657 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024658 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 else
24660 EMSG(_(e_outofmem));
24661 }
24662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024663 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664
24665 if (reanimate)
24666 {
24667 /* The pending return value could be overwritten by a ":return"
24668 * without argument in a finally clause; reset the default
24669 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024670 current_funccal->rettv->v_type = VAR_NUMBER;
24671 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672 }
24673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024674 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675 }
24676 else
24677 {
24678 current_funccal->returned = TRUE;
24679
24680 /* If the return is carried out now, store the return value. For
24681 * a return immediately after reanimation, the value is already
24682 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024683 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024685 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024686 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024688 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 }
24690 }
24691
24692 return idx < 0;
24693}
24694
24695/*
24696 * Free the variable with a pending return value.
24697 */
24698 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024699discard_pending_return(rettv)
24700 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024701{
Bram Moolenaar33570922005-01-25 22:26:29 +000024702 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703}
24704
24705/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024706 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 * is an allocated string. Used by report_pending() for verbose messages.
24708 */
24709 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024710get_return_cmd(rettv)
24711 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024712{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024713 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024714 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024715 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024717 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024718 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024719 if (s == NULL)
24720 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024721
24722 STRCPY(IObuff, ":return ");
24723 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24724 if (STRLEN(s) + 8 >= IOSIZE)
24725 STRCPY(IObuff + IOSIZE - 4, "...");
24726 vim_free(tofree);
24727 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728}
24729
24730/*
24731 * Get next function line.
24732 * Called by do_cmdline() to get the next line.
24733 * Returns allocated string, or NULL for end of function.
24734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024735 char_u *
24736get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024737 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024739 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740{
Bram Moolenaar33570922005-01-25 22:26:29 +000024741 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024742 ufunc_T *fp = fcp->func;
24743 char_u *retval;
24744 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745
24746 /* If breakpoints have been added/deleted need to check for it. */
24747 if (fcp->dbg_tick != debug_tick)
24748 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024749 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024750 sourcing_lnum);
24751 fcp->dbg_tick = debug_tick;
24752 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024753#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024754 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024755 func_line_end(cookie);
24756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757
Bram Moolenaar05159a02005-02-26 23:04:13 +000024758 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024759 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24760 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024761 retval = NULL;
24762 else
24763 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024764 /* Skip NULL lines (continuation lines). */
24765 while (fcp->linenr < gap->ga_len
24766 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24767 ++fcp->linenr;
24768 if (fcp->linenr >= gap->ga_len)
24769 retval = NULL;
24770 else
24771 {
24772 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24773 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024774#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024775 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024776 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024777#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 }
24780
24781 /* Did we encounter a breakpoint? */
24782 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24783 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024784 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024786 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 sourcing_lnum);
24788 fcp->dbg_tick = debug_tick;
24789 }
24790
24791 return retval;
24792}
24793
Bram Moolenaar05159a02005-02-26 23:04:13 +000024794#if defined(FEAT_PROFILE) || defined(PROTO)
24795/*
24796 * Called when starting to read a function line.
24797 * "sourcing_lnum" must be correct!
24798 * When skipping lines it may not actually be executed, but we won't find out
24799 * until later and we need to store the time now.
24800 */
24801 void
24802func_line_start(cookie)
24803 void *cookie;
24804{
24805 funccall_T *fcp = (funccall_T *)cookie;
24806 ufunc_T *fp = fcp->func;
24807
24808 if (fp->uf_profiling && sourcing_lnum >= 1
24809 && sourcing_lnum <= fp->uf_lines.ga_len)
24810 {
24811 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024812 /* Skip continuation lines. */
24813 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24814 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024815 fp->uf_tml_execed = FALSE;
24816 profile_start(&fp->uf_tml_start);
24817 profile_zero(&fp->uf_tml_children);
24818 profile_get_wait(&fp->uf_tml_wait);
24819 }
24820}
24821
24822/*
24823 * Called when actually executing a function line.
24824 */
24825 void
24826func_line_exec(cookie)
24827 void *cookie;
24828{
24829 funccall_T *fcp = (funccall_T *)cookie;
24830 ufunc_T *fp = fcp->func;
24831
24832 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24833 fp->uf_tml_execed = TRUE;
24834}
24835
24836/*
24837 * Called when done with a function line.
24838 */
24839 void
24840func_line_end(cookie)
24841 void *cookie;
24842{
24843 funccall_T *fcp = (funccall_T *)cookie;
24844 ufunc_T *fp = fcp->func;
24845
24846 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24847 {
24848 if (fp->uf_tml_execed)
24849 {
24850 ++fp->uf_tml_count[fp->uf_tml_idx];
24851 profile_end(&fp->uf_tml_start);
24852 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024853 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024854 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24855 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024856 }
24857 fp->uf_tml_idx = -1;
24858 }
24859}
24860#endif
24861
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862/*
24863 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024864 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865 */
24866 int
24867func_has_ended(cookie)
24868 void *cookie;
24869{
Bram Moolenaar33570922005-01-25 22:26:29 +000024870 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871
24872 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24873 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024874 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875 || fcp->returned);
24876}
24877
24878/*
24879 * return TRUE if cookie indicates a function which "abort"s on errors.
24880 */
24881 int
24882func_has_abort(cookie)
24883 void *cookie;
24884{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024885 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886}
24887
24888#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24889typedef enum
24890{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024891 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24892 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24893 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894} var_flavour_T;
24895
24896static var_flavour_T var_flavour __ARGS((char_u *varname));
24897
24898 static var_flavour_T
24899var_flavour(varname)
24900 char_u *varname;
24901{
24902 char_u *p = varname;
24903
24904 if (ASCII_ISUPPER(*p))
24905 {
24906 while (*(++p))
24907 if (ASCII_ISLOWER(*p))
24908 return VAR_FLAVOUR_SESSION;
24909 return VAR_FLAVOUR_VIMINFO;
24910 }
24911 else
24912 return VAR_FLAVOUR_DEFAULT;
24913}
24914#endif
24915
24916#if defined(FEAT_VIMINFO) || defined(PROTO)
24917/*
24918 * Restore global vars that start with a capital from the viminfo file
24919 */
24920 int
24921read_viminfo_varlist(virp, writing)
24922 vir_T *virp;
24923 int writing;
24924{
24925 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024926 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024927 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928
24929 if (!writing && (find_viminfo_parameter('!') != NULL))
24930 {
24931 tab = vim_strchr(virp->vir_line + 1, '\t');
24932 if (tab != NULL)
24933 {
24934 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024935 switch (*tab)
24936 {
24937 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024938#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024939 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024940#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024941 case 'D': type = VAR_DICT; break;
24942 case 'L': type = VAR_LIST; break;
24943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944
24945 tab = vim_strchr(tab, '\t');
24946 if (tab != NULL)
24947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024948 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024949 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024950 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024952#ifdef FEAT_FLOAT
24953 else if (type == VAR_FLOAT)
24954 (void)string2float(tab + 1, &tv.vval.v_float);
24955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024957 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024958 if (type == VAR_DICT || type == VAR_LIST)
24959 {
24960 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24961
24962 if (etv == NULL)
24963 /* Failed to parse back the dict or list, use it as a
24964 * string. */
24965 tv.v_type = VAR_STRING;
24966 else
24967 {
24968 vim_free(tv.vval.v_string);
24969 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024970 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024971 }
24972 }
24973
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024974 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024975
24976 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024977 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024978 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24979 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980 }
24981 }
24982 }
24983
24984 return viminfo_readline(virp);
24985}
24986
24987/*
24988 * Write global vars that start with a capital to the viminfo file
24989 */
24990 void
24991write_viminfo_varlist(fp)
24992 FILE *fp;
24993{
Bram Moolenaar33570922005-01-25 22:26:29 +000024994 hashitem_T *hi;
24995 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024996 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024997 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024998 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024999 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025000 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001
25002 if (find_viminfo_parameter('!') == NULL)
25003 return;
25004
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025005 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025006
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025007 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025008 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025010 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025011 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025012 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025013 this_var = HI2DI(hi);
25014 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025015 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025016 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025017 {
25018 case VAR_STRING: s = "STR"; break;
25019 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025020#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025021 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025022#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025023 case VAR_DICT: s = "DIC"; break;
25024 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025025 default: continue;
25026 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025027 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025028 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025029 if (p != NULL)
25030 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025031 vim_free(tofree);
25032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025033 }
25034 }
25035}
25036#endif
25037
25038#if defined(FEAT_SESSION) || defined(PROTO)
25039 int
25040store_session_globals(fd)
25041 FILE *fd;
25042{
Bram Moolenaar33570922005-01-25 22:26:29 +000025043 hashitem_T *hi;
25044 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025045 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 char_u *p, *t;
25047
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025048 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025049 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025051 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025053 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025054 this_var = HI2DI(hi);
25055 if ((this_var->di_tv.v_type == VAR_NUMBER
25056 || this_var->di_tv.v_type == VAR_STRING)
25057 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025058 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025059 /* Escape special characters with a backslash. Turn a LF and
25060 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025061 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025062 (char_u *)"\\\"\n\r");
25063 if (p == NULL) /* out of memory */
25064 break;
25065 for (t = p; *t != NUL; ++t)
25066 if (*t == '\n')
25067 *t = 'n';
25068 else if (*t == '\r')
25069 *t = 'r';
25070 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025071 this_var->di_key,
25072 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25073 : ' ',
25074 p,
25075 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25076 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025077 || put_eol(fd) == FAIL)
25078 {
25079 vim_free(p);
25080 return FAIL;
25081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082 vim_free(p);
25083 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025084#ifdef FEAT_FLOAT
25085 else if (this_var->di_tv.v_type == VAR_FLOAT
25086 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25087 {
25088 float_T f = this_var->di_tv.vval.v_float;
25089 int sign = ' ';
25090
25091 if (f < 0)
25092 {
25093 f = -f;
25094 sign = '-';
25095 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025096 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025097 this_var->di_key, sign, f) < 0)
25098 || put_eol(fd) == FAIL)
25099 return FAIL;
25100 }
25101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025102 }
25103 }
25104 return OK;
25105}
25106#endif
25107
Bram Moolenaar661b1822005-07-28 22:36:45 +000025108/*
25109 * Display script name where an item was last set.
25110 * Should only be invoked when 'verbose' is non-zero.
25111 */
25112 void
25113last_set_msg(scriptID)
25114 scid_T scriptID;
25115{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025116 char_u *p;
25117
Bram Moolenaar661b1822005-07-28 22:36:45 +000025118 if (scriptID != 0)
25119 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025120 p = home_replace_save(NULL, get_scriptname(scriptID));
25121 if (p != NULL)
25122 {
25123 verbose_enter();
25124 MSG_PUTS(_("\n\tLast set from "));
25125 MSG_PUTS(p);
25126 vim_free(p);
25127 verbose_leave();
25128 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025129 }
25130}
25131
Bram Moolenaard812df62008-11-09 12:46:09 +000025132/*
25133 * List v:oldfiles in a nice way.
25134 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025135 void
25136ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025137 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025138{
25139 list_T *l = vimvars[VV_OLDFILES].vv_list;
25140 listitem_T *li;
25141 int nr = 0;
25142
25143 if (l == NULL)
25144 msg((char_u *)_("No old files"));
25145 else
25146 {
25147 msg_start();
25148 msg_scroll = TRUE;
25149 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25150 {
25151 msg_outnum((long)++nr);
25152 MSG_PUTS(": ");
25153 msg_outtrans(get_tv_string(&li->li_tv));
25154 msg_putchar('\n');
25155 out_flush(); /* output one line at a time */
25156 ui_breakcheck();
25157 }
25158 /* Assume "got_int" was set to truncate the listing. */
25159 got_int = FALSE;
25160
25161#ifdef FEAT_BROWSE_CMD
25162 if (cmdmod.browse)
25163 {
25164 quit_more = FALSE;
25165 nr = prompt_for_number(FALSE);
25166 msg_starthere();
25167 if (nr > 0)
25168 {
25169 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25170 (long)nr);
25171
25172 if (p != NULL)
25173 {
25174 p = expand_env_save(p);
25175 eap->arg = p;
25176 eap->cmdidx = CMD_edit;
25177 cmdmod.browse = FALSE;
25178 do_exedit(eap, NULL);
25179 vim_free(p);
25180 }
25181 }
25182 }
25183#endif
25184 }
25185}
25186
Bram Moolenaar53744302015-07-17 17:38:22 +020025187/* reset v:option_new, v:option_old and v:option_type */
25188 void
25189reset_v_option_vars()
25190{
25191 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25192 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25193 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25194}
25195
25196
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197#endif /* FEAT_EVAL */
25198
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025200#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201
25202#ifdef WIN3264
25203/*
25204 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25205 */
25206static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25207static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25208static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25209
25210/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025211 * Get the short path (8.3) for the filename in "fnamep".
25212 * Only works for a valid file name.
25213 * When the path gets longer "fnamep" is changed and the allocated buffer
25214 * is put in "bufp".
25215 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25216 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217 */
25218 static int
25219get_short_pathname(fnamep, bufp, fnamelen)
25220 char_u **fnamep;
25221 char_u **bufp;
25222 int *fnamelen;
25223{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025224 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 char_u *newbuf;
25226
25227 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228 l = GetShortPathName(*fnamep, *fnamep, len);
25229 if (l > len - 1)
25230 {
25231 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025232 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 newbuf = vim_strnsave(*fnamep, l);
25234 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236
25237 vim_free(*bufp);
25238 *fnamep = *bufp = newbuf;
25239
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025240 /* Really should always succeed, as the buffer is big enough. */
25241 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242 }
25243
25244 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025245 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246}
25247
25248/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025249 * Get the short path (8.3) for the filename in "fname". The converted
25250 * path is returned in "bufp".
25251 *
25252 * Some of the directories specified in "fname" may not exist. This function
25253 * will shorten the existing directories at the beginning of the path and then
25254 * append the remaining non-existing path.
25255 *
25256 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025257 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025258 * bufp - Pointer to an allocated buffer for the filename.
25259 * fnamelen - Length of the filename pointed to by fname
25260 *
25261 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 */
25263 static int
25264shortpath_for_invalid_fname(fname, bufp, fnamelen)
25265 char_u **fname;
25266 char_u **bufp;
25267 int *fnamelen;
25268{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025269 char_u *short_fname, *save_fname, *pbuf_unused;
25270 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025272 int old_len, len;
25273 int new_len, sfx_len;
25274 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275
25276 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025277 old_len = *fnamelen;
25278 save_fname = vim_strnsave(*fname, old_len);
25279 pbuf_unused = NULL;
25280 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025282 endp = save_fname + old_len - 1; /* Find the end of the copy */
25283 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025285 /*
25286 * Try shortening the supplied path till it succeeds by removing one
25287 * directory at a time from the tail of the path.
25288 */
25289 len = 0;
25290 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025292 /* go back one path-separator */
25293 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25294 --endp;
25295 if (endp <= save_fname)
25296 break; /* processed the complete path */
25297
25298 /*
25299 * Replace the path separator with a NUL and try to shorten the
25300 * resulting path.
25301 */
25302 ch = *endp;
25303 *endp = 0;
25304 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025305 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025306 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25307 {
25308 retval = FAIL;
25309 goto theend;
25310 }
25311 *endp = ch; /* preserve the string */
25312
25313 if (len > 0)
25314 break; /* successfully shortened the path */
25315
25316 /* failed to shorten the path. Skip the path separator */
25317 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 }
25319
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025320 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025322 /*
25323 * Succeeded in shortening the path. Now concatenate the shortened
25324 * path with the remaining path at the tail.
25325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025327 /* Compute the length of the new path. */
25328 sfx_len = (int)(save_endp - endp) + 1;
25329 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025331 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025333 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025335 /* There is not enough space in the currently allocated string,
25336 * copy it to a buffer big enough. */
25337 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025339 {
25340 retval = FAIL;
25341 goto theend;
25342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 }
25344 else
25345 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025346 /* Transfer short_fname to the main buffer (it's big enough),
25347 * unless get_short_pathname() did its work in-place. */
25348 *fname = *bufp = save_fname;
25349 if (short_fname != save_fname)
25350 vim_strncpy(save_fname, short_fname, len);
25351 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025353
25354 /* concat the not-shortened part of the path */
25355 vim_strncpy(*fname + len, endp, sfx_len);
25356 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025358
25359theend:
25360 vim_free(pbuf_unused);
25361 vim_free(save_fname);
25362
25363 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364}
25365
25366/*
25367 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025368 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 */
25370 static int
25371shortpath_for_partial(fnamep, bufp, fnamelen)
25372 char_u **fnamep;
25373 char_u **bufp;
25374 int *fnamelen;
25375{
25376 int sepcount, len, tflen;
25377 char_u *p;
25378 char_u *pbuf, *tfname;
25379 int hasTilde;
25380
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025381 /* Count up the path separators from the RHS.. so we know which part
25382 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025384 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385 if (vim_ispathsep(*p))
25386 ++sepcount;
25387
25388 /* Need full path first (use expand_env() to remove a "~/") */
25389 hasTilde = (**fnamep == '~');
25390 if (hasTilde)
25391 pbuf = tfname = expand_env_save(*fnamep);
25392 else
25393 pbuf = tfname = FullName_save(*fnamep, FALSE);
25394
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025395 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025396
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025397 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399
25400 if (len == 0)
25401 {
25402 /* Don't have a valid filename, so shorten the rest of the
25403 * path if we can. This CAN give us invalid 8.3 filenames, but
25404 * there's not a lot of point in guessing what it might be.
25405 */
25406 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025407 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409 }
25410
25411 /* Count the paths backward to find the beginning of the desired string. */
25412 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025413 {
25414#ifdef FEAT_MBYTE
25415 if (has_mbyte)
25416 p -= mb_head_off(tfname, p);
25417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025418 if (vim_ispathsep(*p))
25419 {
25420 if (sepcount == 0 || (hasTilde && sepcount == 1))
25421 break;
25422 else
25423 sepcount --;
25424 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426 if (hasTilde)
25427 {
25428 --p;
25429 if (p >= tfname)
25430 *p = '~';
25431 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 }
25434 else
25435 ++p;
25436
25437 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25438 vim_free(*bufp);
25439 *fnamelen = (int)STRLEN(p);
25440 *bufp = pbuf;
25441 *fnamep = p;
25442
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025443 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444}
25445#endif /* WIN3264 */
25446
25447/*
25448 * Adjust a filename, according to a string of modifiers.
25449 * *fnamep must be NUL terminated when called. When returning, the length is
25450 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025451 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452 * When there is an error, *fnamep is set to NULL.
25453 */
25454 int
25455modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25456 char_u *src; /* string with modifiers */
25457 int *usedlen; /* characters after src that are used */
25458 char_u **fnamep; /* file name so far */
25459 char_u **bufp; /* buffer for allocated file name or NULL */
25460 int *fnamelen; /* length of fnamep */
25461{
25462 int valid = 0;
25463 char_u *tail;
25464 char_u *s, *p, *pbuf;
25465 char_u dirname[MAXPATHL];
25466 int c;
25467 int has_fullname = 0;
25468#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025469 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470 int has_shortname = 0;
25471#endif
25472
25473repeat:
25474 /* ":p" - full path/file_name */
25475 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25476 {
25477 has_fullname = 1;
25478
25479 valid |= VALID_PATH;
25480 *usedlen += 2;
25481
25482 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25483 if ((*fnamep)[0] == '~'
25484#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25485 && ((*fnamep)[1] == '/'
25486# ifdef BACKSLASH_IN_FILENAME
25487 || (*fnamep)[1] == '\\'
25488# endif
25489 || (*fnamep)[1] == NUL)
25490
25491#endif
25492 )
25493 {
25494 *fnamep = expand_env_save(*fnamep);
25495 vim_free(*bufp); /* free any allocated file name */
25496 *bufp = *fnamep;
25497 if (*fnamep == NULL)
25498 return -1;
25499 }
25500
25501 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025502 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 {
25504 if (vim_ispathsep(*p)
25505 && p[1] == '.'
25506 && (p[2] == NUL
25507 || vim_ispathsep(p[2])
25508 || (p[2] == '.'
25509 && (p[3] == NUL || vim_ispathsep(p[3])))))
25510 break;
25511 }
25512
25513 /* FullName_save() is slow, don't use it when not needed. */
25514 if (*p != NUL || !vim_isAbsName(*fnamep))
25515 {
25516 *fnamep = FullName_save(*fnamep, *p != NUL);
25517 vim_free(*bufp); /* free any allocated file name */
25518 *bufp = *fnamep;
25519 if (*fnamep == NULL)
25520 return -1;
25521 }
25522
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025523#ifdef WIN3264
25524# if _WIN32_WINNT >= 0x0500
25525 if (vim_strchr(*fnamep, '~') != NULL)
25526 {
25527 /* Expand 8.3 filename to full path. Needed to make sure the same
25528 * file does not have two different names.
25529 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25530 p = alloc(_MAX_PATH + 1);
25531 if (p != NULL)
25532 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025533 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025534 {
25535 vim_free(*bufp);
25536 *bufp = *fnamep = p;
25537 }
25538 else
25539 vim_free(p);
25540 }
25541 }
25542# endif
25543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 /* Append a path separator to a directory. */
25545 if (mch_isdir(*fnamep))
25546 {
25547 /* Make room for one or two extra characters. */
25548 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25549 vim_free(*bufp); /* free any allocated file name */
25550 *bufp = *fnamep;
25551 if (*fnamep == NULL)
25552 return -1;
25553 add_pathsep(*fnamep);
25554 }
25555 }
25556
25557 /* ":." - path relative to the current directory */
25558 /* ":~" - path relative to the home directory */
25559 /* ":8" - shortname path - postponed till after */
25560 while (src[*usedlen] == ':'
25561 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25562 {
25563 *usedlen += 2;
25564 if (c == '8')
25565 {
25566#ifdef WIN3264
25567 has_shortname = 1; /* Postpone this. */
25568#endif
25569 continue;
25570 }
25571 pbuf = NULL;
25572 /* Need full path first (use expand_env() to remove a "~/") */
25573 if (!has_fullname)
25574 {
25575 if (c == '.' && **fnamep == '~')
25576 p = pbuf = expand_env_save(*fnamep);
25577 else
25578 p = pbuf = FullName_save(*fnamep, FALSE);
25579 }
25580 else
25581 p = *fnamep;
25582
25583 has_fullname = 0;
25584
25585 if (p != NULL)
25586 {
25587 if (c == '.')
25588 {
25589 mch_dirname(dirname, MAXPATHL);
25590 s = shorten_fname(p, dirname);
25591 if (s != NULL)
25592 {
25593 *fnamep = s;
25594 if (pbuf != NULL)
25595 {
25596 vim_free(*bufp); /* free any allocated file name */
25597 *bufp = pbuf;
25598 pbuf = NULL;
25599 }
25600 }
25601 }
25602 else
25603 {
25604 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25605 /* Only replace it when it starts with '~' */
25606 if (*dirname == '~')
25607 {
25608 s = vim_strsave(dirname);
25609 if (s != NULL)
25610 {
25611 *fnamep = s;
25612 vim_free(*bufp);
25613 *bufp = s;
25614 }
25615 }
25616 }
25617 vim_free(pbuf);
25618 }
25619 }
25620
25621 tail = gettail(*fnamep);
25622 *fnamelen = (int)STRLEN(*fnamep);
25623
25624 /* ":h" - head, remove "/file_name", can be repeated */
25625 /* Don't remove the first "/" or "c:\" */
25626 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25627 {
25628 valid |= VALID_HEAD;
25629 *usedlen += 2;
25630 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025631 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025632 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633 *fnamelen = (int)(tail - *fnamep);
25634#ifdef VMS
25635 if (*fnamelen > 0)
25636 *fnamelen += 1; /* the path separator is part of the path */
25637#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025638 if (*fnamelen == 0)
25639 {
25640 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25641 p = vim_strsave((char_u *)".");
25642 if (p == NULL)
25643 return -1;
25644 vim_free(*bufp);
25645 *bufp = *fnamep = tail = p;
25646 *fnamelen = 1;
25647 }
25648 else
25649 {
25650 while (tail > s && !after_pathsep(s, tail))
25651 mb_ptr_back(*fnamep, tail);
25652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653 }
25654
25655 /* ":8" - shortname */
25656 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25657 {
25658 *usedlen += 2;
25659#ifdef WIN3264
25660 has_shortname = 1;
25661#endif
25662 }
25663
25664#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025665 /*
25666 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 */
25668 if (has_shortname)
25669 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025670 /* Copy the string if it is shortened by :h and when it wasn't copied
25671 * yet, because we are going to change it in place. Avoids changing
25672 * the buffer name for "%:8". */
25673 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674 {
25675 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025676 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677 return -1;
25678 vim_free(*bufp);
25679 *bufp = *fnamep = p;
25680 }
25681
25682 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025683 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684 if (!has_fullname && !vim_isAbsName(*fnamep))
25685 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025686 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025687 return -1;
25688 }
25689 else
25690 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025691 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692
Bram Moolenaardc935552011-08-17 15:23:23 +020025693 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025695 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025696 return -1;
25697
25698 if (l == 0)
25699 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025700 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025702 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 return -1;
25704 }
25705 *fnamelen = l;
25706 }
25707 }
25708#endif /* WIN3264 */
25709
25710 /* ":t" - tail, just the basename */
25711 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25712 {
25713 *usedlen += 2;
25714 *fnamelen -= (int)(tail - *fnamep);
25715 *fnamep = tail;
25716 }
25717
25718 /* ":e" - extension, can be repeated */
25719 /* ":r" - root, without extension, can be repeated */
25720 while (src[*usedlen] == ':'
25721 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25722 {
25723 /* find a '.' in the tail:
25724 * - for second :e: before the current fname
25725 * - otherwise: The last '.'
25726 */
25727 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25728 s = *fnamep - 2;
25729 else
25730 s = *fnamep + *fnamelen - 1;
25731 for ( ; s > tail; --s)
25732 if (s[0] == '.')
25733 break;
25734 if (src[*usedlen + 1] == 'e') /* :e */
25735 {
25736 if (s > tail)
25737 {
25738 *fnamelen += (int)(*fnamep - (s + 1));
25739 *fnamep = s + 1;
25740#ifdef VMS
25741 /* cut version from the extension */
25742 s = *fnamep + *fnamelen - 1;
25743 for ( ; s > *fnamep; --s)
25744 if (s[0] == ';')
25745 break;
25746 if (s > *fnamep)
25747 *fnamelen = s - *fnamep;
25748#endif
25749 }
25750 else if (*fnamep <= tail)
25751 *fnamelen = 0;
25752 }
25753 else /* :r */
25754 {
25755 if (s > tail) /* remove one extension */
25756 *fnamelen = (int)(s - *fnamep);
25757 }
25758 *usedlen += 2;
25759 }
25760
25761 /* ":s?pat?foo?" - substitute */
25762 /* ":gs?pat?foo?" - global substitute */
25763 if (src[*usedlen] == ':'
25764 && (src[*usedlen + 1] == 's'
25765 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25766 {
25767 char_u *str;
25768 char_u *pat;
25769 char_u *sub;
25770 int sep;
25771 char_u *flags;
25772 int didit = FALSE;
25773
25774 flags = (char_u *)"";
25775 s = src + *usedlen + 2;
25776 if (src[*usedlen + 1] == 'g')
25777 {
25778 flags = (char_u *)"g";
25779 ++s;
25780 }
25781
25782 sep = *s++;
25783 if (sep)
25784 {
25785 /* find end of pattern */
25786 p = vim_strchr(s, sep);
25787 if (p != NULL)
25788 {
25789 pat = vim_strnsave(s, (int)(p - s));
25790 if (pat != NULL)
25791 {
25792 s = p + 1;
25793 /* find end of substitution */
25794 p = vim_strchr(s, sep);
25795 if (p != NULL)
25796 {
25797 sub = vim_strnsave(s, (int)(p - s));
25798 str = vim_strnsave(*fnamep, *fnamelen);
25799 if (sub != NULL && str != NULL)
25800 {
25801 *usedlen = (int)(p + 1 - src);
25802 s = do_string_sub(str, pat, sub, flags);
25803 if (s != NULL)
25804 {
25805 *fnamep = s;
25806 *fnamelen = (int)STRLEN(s);
25807 vim_free(*bufp);
25808 *bufp = s;
25809 didit = TRUE;
25810 }
25811 }
25812 vim_free(sub);
25813 vim_free(str);
25814 }
25815 vim_free(pat);
25816 }
25817 }
25818 /* after using ":s", repeat all the modifiers */
25819 if (didit)
25820 goto repeat;
25821 }
25822 }
25823
Bram Moolenaar26df0922014-02-23 23:39:13 +010025824 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25825 {
25826 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25827 if (p == NULL)
25828 return -1;
25829 vim_free(*bufp);
25830 *bufp = *fnamep = p;
25831 *fnamelen = (int)STRLEN(p);
25832 *usedlen += 2;
25833 }
25834
Bram Moolenaar071d4272004-06-13 20:20:40 +000025835 return valid;
25836}
25837
25838/*
25839 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25840 * "flags" can be "g" to do a global substitute.
25841 * Returns an allocated string, NULL for error.
25842 */
25843 char_u *
25844do_string_sub(str, pat, sub, flags)
25845 char_u *str;
25846 char_u *pat;
25847 char_u *sub;
25848 char_u *flags;
25849{
25850 int sublen;
25851 regmatch_T regmatch;
25852 int i;
25853 int do_all;
25854 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025855 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856 garray_T ga;
25857 char_u *ret;
25858 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025859 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860
25861 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25862 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025863 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864
25865 ga_init2(&ga, 1, 200);
25866
25867 do_all = (flags[0] == 'g');
25868
25869 regmatch.rm_ic = p_ic;
25870 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25871 if (regmatch.regprog != NULL)
25872 {
25873 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025874 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25876 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025877 /* Skip empty match except for first match. */
25878 if (regmatch.startp[0] == regmatch.endp[0])
25879 {
25880 if (zero_width == regmatch.startp[0])
25881 {
25882 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025883 i = MB_PTR2LEN(tail);
25884 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25885 (size_t)i);
25886 ga.ga_len += i;
25887 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025888 continue;
25889 }
25890 zero_width = regmatch.startp[0];
25891 }
25892
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893 /*
25894 * Get some space for a temporary buffer to do the substitution
25895 * into. It will contain:
25896 * - The text up to where the match is.
25897 * - The substituted text.
25898 * - The text after the match.
25899 */
25900 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025901 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25903 {
25904 ga_clear(&ga);
25905 break;
25906 }
25907
25908 /* copy the text up to where the match is */
25909 i = (int)(regmatch.startp[0] - tail);
25910 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25911 /* add the substituted text */
25912 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25913 + ga.ga_len + i, TRUE, TRUE, FALSE);
25914 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025915 tail = regmatch.endp[0];
25916 if (*tail == NUL)
25917 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025918 if (!do_all)
25919 break;
25920 }
25921
25922 if (ga.ga_data != NULL)
25923 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25924
Bram Moolenaar473de612013-06-08 18:19:48 +020025925 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 }
25927
25928 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25929 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025930 if (p_cpo == empty_option)
25931 p_cpo = save_cpo;
25932 else
25933 /* Darn, evaluating {sub} expression changed the value. */
25934 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025935
25936 return ret;
25937}
25938
25939#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */