blob: 5b06a30a52a3a5269d9c0e7fc824d031bb854ecc [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 Moolenaar33570922005-01-25 22:26:29 +0000367};
368
369/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000370#define vv_type vv_di.di_tv.v_type
371#define vv_nr vv_di.di_tv.vval.v_number
372#define vv_float vv_di.di_tv.vval.v_float
373#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000374#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000376
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200377static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000378#define vimvarht vimvardict.dv_hashtab
379
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
381static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
383static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
384static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000385static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
386static void list_glob_vars __ARGS((int *first));
387static void list_buf_vars __ARGS((int *first));
388static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_vim_vars __ARGS((int *first));
393static void list_script_vars __ARGS((int *first));
394static void list_func_vars __ARGS((int *first));
395static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
397static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100398static 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 +0000399static void clear_lval __ARGS((lval_T *lp));
400static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
401static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
403static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
404static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
405static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
406static void item_lock __ARGS((typval_T *tv, int deep, int lock));
407static int tv_islocked __ARGS((typval_T *tv));
408
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
410static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000415static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
416static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000417
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000418static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000423static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100425static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
426static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
427static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000428static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000430static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
432static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000433static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100435static 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 +0000436static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000437static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200438static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
440static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
446static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000448#ifdef FEAT_FLOAT
449static int string2float __ARGS((char_u *text, float_T *value));
450#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
452static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100453static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static 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 +0200455static 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 +0000456static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000457static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#ifdef FEAT_FLOAT
460static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100464static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200468static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200471static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100484static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100486static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
489static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
490#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000491static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000494static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000496#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000497static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200505static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000506#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
510static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200520static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200522#ifdef FEAT_FLOAT
523static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
524#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000527static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#ifdef FEAT_FLOAT
534static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000538static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000547static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000549static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000555static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200556static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200566static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000567static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000568static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200571static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100578static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000581static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000595static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100600static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200615static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
617#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200618#ifdef FEAT_LUA
619static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200626static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100643static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000644static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000649static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000650static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200651#ifdef FEAT_PYTHON3
652static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
654#ifdef FEAT_PYTHON
655static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200674static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100676static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000679static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000681static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000688static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000689static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000690static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000691static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200693static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000694static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100696#ifdef FEAT_CRYPT
697static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
698#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000699static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200700static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200704static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000707static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000715static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717#ifdef HAVE_STRFTIME
718static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
720static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200726static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000733static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200734static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200736static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000738static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000740static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000741static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000743static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200744#ifdef FEAT_FLOAT
745static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
752static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200755static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200756static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100757static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100761static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000768static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100772static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773
Bram Moolenaar493c1782014-05-28 14:34:46 +0200774static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000775static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int get_env_len __ARGS((char_u **arg));
777static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
780#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
781#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
782 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000783static 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 +0000784static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000785static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000787static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static typval_T *alloc_tv __ARGS((void));
789static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void init_tv __ARGS((typval_T *varp));
791static long get_tv_number __ARGS((typval_T *varp));
792static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000793static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *get_tv_string __ARGS((typval_T *varp));
795static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100797static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
798static 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 +0000799static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
800static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
801static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000802static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
803static 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 +0000804static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
805static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000806static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200807static int var_check_func_name __ARGS((char_u *name, int new_var));
808static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000809static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000810static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
812static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
813static int eval_fname_script __ARGS((char_u *p));
814static int eval_fname_sid __ARGS((char_u *p));
815static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static ufunc_T *find_func __ARGS((char_u *name));
817static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200818static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
820static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000821static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
822static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
823static int
824# ifdef __BORLANDC__
825 _RTLENTRYF
826# endif
827 prof_total_cmp __ARGS((const void *s1, const void *s2));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200834static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838static 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 +0000839static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
840static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000841static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000842static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
843static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000844static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000846static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200847static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200848static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200850
851#ifdef EBCDIC
852static int compare_func_name __ARGS((const void *s1, const void *s2));
853static void sortFunctions __ARGS(());
854#endif
855
Bram Moolenaar33570922005-01-25 22:26:29 +0000856/*
857 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 */
859 void
860eval_init()
861{
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 int i;
863 struct vimvar *p;
864
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
866 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200867 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870
871 for (i = 0; i < VV_LEN; ++i)
872 {
873 p = &vimvars[i];
874 STRCPY(p->vv_di.di_key, p->vv_name);
875 if (p->vv_flags & VV_RO)
876 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
877 else if (p->vv_flags & VV_RO_SBX)
878 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
879 else
880 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000881
882 /* add to v: scope dict, unless the value is not always available */
883 if (p->vv_type != VAR_UNKNOWN)
884 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 /* add to compat scope dict */
887 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000889 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100890 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200891 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
894 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100895 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896 */
897 sortFunctions();
898#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000899}
900
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901#if defined(EXITFREE) || defined(PROTO)
902 void
903eval_clear()
904{
905 int i;
906 struct vimvar *p;
907
908 for (i = 0; i < VV_LEN; ++i)
909 {
910 p = &vimvars[i];
911 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000913 vim_free(p->vv_str);
914 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000915 }
916 else if (p->vv_di.di_tv.v_type == VAR_LIST)
917 {
918 list_unref(p->vv_list);
919 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921 }
922 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000923 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 hash_clear(&compat_hashtab);
925
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200928 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100929# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930
931 /* global variables */
932 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000934 /* autoloaded script names */
935 ga_clear_strings(&ga_loaded);
936
Bram Moolenaarcca74132013-09-25 21:00:28 +0200937 /* Script-local variables. First clear all the variables and in a second
938 * loop free the scriptvar_T, because a variable in one script might hold
939 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200942 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 ga_clear(&ga_scripts);
945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000946 /* unreferenced lists and dicts */
947 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000948
949 /* functions */
950 free_all_functions();
951 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952}
953#endif
954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Return the name of the executed function.
957 */
958 char_u *
959func_name(cookie)
960 void *cookie;
961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the next breakpoint line for a funccall cookie.
967 */
968 linenr_T *
969func_breakpoint(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the address holding the debug tick for a funccall cookie.
977 */
978 int *
979func_dbg_tick(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Return the nesting level for a funccall cookie.
987 */
988 int
989func_level(cookie)
990 void *cookie;
991{
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993}
994
995/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000996funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000998/* pointer to list of previously used funccal, still around because some
999 * item in it is still being used. */
1000funccall_T *previous_funccal = NULL;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Return TRUE when a function was ended by a ":return" command.
1004 */
1005 int
1006current_func_returned()
1007{
1008 return current_funccal->returned;
1009}
1010
1011
1012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * Set an internal variable to a string value. Creates the variable if it does
1014 * not already exist.
1015 */
1016 void
1017set_internal_string_var(name, value)
1018 char_u *name;
1019 char_u *value;
1020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 val = vim_strsave(value);
1025 if (val != NULL)
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 }
1034}
1035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001037static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038static char_u *redir_endp = NULL;
1039static char_u *redir_varname = NULL;
1040
1041/*
1042 * Start recording command output to a variable
1043 * Returns OK if successfully completed the setup. FAIL otherwise.
1044 */
1045 int
1046var_redir_start(name, append)
1047 char_u *name;
1048 int append; /* append to an existing variable */
1049{
1050 int save_emsg;
1051 int err;
1052 typval_T tv;
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 {
1057 EMSG(_(e_invarg));
1058 return FAIL;
1059 }
1060
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 redir_varname = vim_strsave(name);
1063 if (redir_varname == NULL)
1064 return FAIL;
1065
1066 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1067 if (redir_lval == NULL)
1068 {
1069 var_redir_stop();
1070 return FAIL;
1071 }
1072
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 /* The output is stored in growarray "redir_ga" until redirection ends. */
1074 ga_init2(&redir_ga, (int)sizeof(char), 500);
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001077 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001078 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1080 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp != NULL && *redir_endp != NUL)
1083 /* Trailing characters are present after the variable name */
1084 EMSG(_(e_trailing));
1085 else
1086 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
1091
1092 /* check if we can write to the variable: set it to or append an empty
1093 * string */
1094 save_emsg = did_emsg;
1095 did_emsg = FALSE;
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = (char_u *)"";
1098 if (append)
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1100 else
1101 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001104 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (err)
1106 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 var_redir_stop();
1109 return FAIL;
1110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 * Append "value[value_len]" to the variable set by var_redir_start().
1117 * The actual appending is postponed until redirection ends, because the value
1118 * appended may in fact be the string we write to, changing it may cause freed
1119 * memory to be used:
1120 * :redir => foo
1121 * :let foo
1122 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 */
1124 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130
1131 if (redir_lval == NULL)
1132 return;
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001139 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 {
1141 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 }
1144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146}
1147
1148/*
1149 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 */
1152 void
1153var_redir_stop()
1154{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 typval_T tv;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_lval != NULL)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* If there was no error: assign the text to the variable. */
1160 if (redir_endp != NULL)
1161 {
1162 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1163 tv.v_type = VAR_STRING;
1164 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 /* Call get_lval() again, if it's inside a Dict or List it may
1166 * have changed. */
1167 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001168 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001169 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1170 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1171 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* free the collected output */
1175 vim_free(redir_ga.ga_data);
1176 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 vim_free(redir_lval);
1179 redir_lval = NULL;
1180 }
1181 vim_free(redir_varname);
1182 redir_varname = NULL;
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# if defined(FEAT_MBYTE) || defined(PROTO)
1186 int
1187eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1188 char_u *enc_from;
1189 char_u *enc_to;
1190 char_u *fname_from;
1191 char_u *fname_to;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1196 set_vim_var_string(VV_CC_TO, enc_to, -1);
1197 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1198 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1199 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_CC_FROM, NULL, -1);
1202 set_vim_var_string(VV_CC_TO, NULL, -1);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205
1206 if (err)
1207 return FAIL;
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1213 int
1214eval_printexpr(fname, args)
1215 char_u *fname;
1216 char_u *args;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, fname, -1);
1221 set_vim_var_string(VV_CMDARG, args, -1);
1222 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1223 err = TRUE;
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_CMDARG, NULL, -1);
1226
1227 if (err)
1228 {
1229 mch_remove(fname);
1230 return FAIL;
1231 }
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_DIFF) || defined(PROTO)
1237 void
1238eval_diff(origfile, newfile, outfile)
1239 char_u *origfile;
1240 char_u *newfile;
1241 char_u *outfile;
1242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253
1254 void
1255eval_patch(origfile, difffile, outfile)
1256 char_u *origfile;
1257 char_u *difffile;
1258 char_u *outfile;
1259{
1260 int err;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270# endif
1271
1272/*
1273 * Top level evaluation function, returning a boolean.
1274 * Sets "error" to TRUE if there was an error.
1275 * Return TRUE or FALSE.
1276 */
1277 int
1278eval_to_bool(arg, error, nextcmd, skip)
1279 char_u *arg;
1280 int *error;
1281 char_u **nextcmd;
1282 int skip; /* only parse, don't execute */
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval = FALSE;
1286
1287 if (skip)
1288 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
1292 {
1293 *error = FALSE;
1294 if (!skip)
1295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 if (skip)
1301 --emsg_skip;
1302
1303 return retval;
1304}
1305
1306/*
1307 * Top level evaluation function, returning a string. If "skip" is TRUE,
1308 * only parsing to "nextcmd" is done, without reporting errors. Return
1309 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1310 */
1311 char_u *
1312eval_to_string_skip(arg, nextcmd, skip)
1313 char_u *arg;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *retval;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 retval = vim_strsave(get_tv_string(&tv));
1327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (skip)
1330 --emsg_skip;
1331
1332 return retval;
1333}
1334
1335/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336 * Skip over an expression at "*pp".
1337 * Return FAIL for an error, OK otherwise.
1338 */
1339 int
1340skip_expr(pp)
1341 char_u **pp;
1342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344
1345 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347}
1348
1349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 * When "convert" is TRUE convert a List into a sequence of lines and convert
1352 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Return pointer to allocated memory, or NULL for failure.
1354 */
1355 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *arg;
1358 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 retval = NULL;
1370 else
1371 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001373 {
1374 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001377 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001378 if (tv.vval.v_list->lv_len > 0)
1379 ga_append(&ga, NL);
1380 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 ga_append(&ga, NUL);
1382 retval = (char_u *)ga.ga_data;
1383 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384#ifdef FEAT_FLOAT
1385 else if (convert && tv.v_type == VAR_FLOAT)
1386 {
1387 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1388 retval = vim_strsave(numbuf);
1389 }
1390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 else
1392 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 return retval;
1397}
1398
1399/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 * Call eval_to_string() without using current local variables and using
1401 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *arg;
1406 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *retval;
1410 void *save_funccalp;
1411
1412 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 if (use_sandbox)
1414 ++sandbox;
1415 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 if (use_sandbox)
1418 --sandbox;
1419 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 restore_funccal(save_funccalp);
1421 return retval;
1422}
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Top level evaluation function, returning a number.
1426 * Evaluates "expr" silently.
1427 * Returns -1 for an error.
1428 */
1429 int
1430eval_to_number(expr)
1431 char_u *expr;
1432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001435 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 ++emsg_off;
1438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 retval = -1;
1441 else
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 --emsg_off;
1447
1448 return retval;
1449}
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451/*
1452 * Prepare v: variable "idx" to be used.
1453 * Save the current typeval in "save_tv".
1454 * When not used yet add the variable to the v: hashtable.
1455 */
1456 static void
1457prepare_vimvar(idx, save_tv)
1458 int idx;
1459 typval_T *save_tv;
1460{
1461 *save_tv = vimvars[idx].vv_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1464}
1465
1466/*
1467 * Restore v: variable "idx" to typeval "save_tv".
1468 * When no longer defined, remove the variable from the v: hashtable.
1469 */
1470 static void
1471restore_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 hashitem_T *hi;
1476
Bram Moolenaara40058a2005-07-11 22:42:07 +00001477 vimvars[idx].vv_tv = *save_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 {
1480 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1481 if (HASHITEM_EMPTY(hi))
1482 EMSG2(_(e_intern2), "restore_vimvar()");
1483 else
1484 hash_remove(&vimvarht, hi);
1485 }
1486}
1487
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001488#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001489/*
1490 * Evaluate an expression to a list with suggestions.
1491 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001492 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 */
1494 list_T *
1495eval_spell_expr(badword, expr)
1496 char_u *badword;
1497 char_u *expr;
1498{
1499 typval_T save_val;
1500 typval_T rettv;
1501 list_T *list = NULL;
1502 char_u *p = skipwhite(expr);
1503
1504 /* Set "v:val" to the bad word. */
1505 prepare_vimvar(VV_VAL, &save_val);
1506 vimvars[VV_VAL].vv_type = VAR_STRING;
1507 vimvars[VV_VAL].vv_str = badword;
1508 if (p_verbose == 0)
1509 ++emsg_off;
1510
1511 if (eval1(&p, &rettv, TRUE) == OK)
1512 {
1513 if (rettv.v_type != VAR_LIST)
1514 clear_tv(&rettv);
1515 else
1516 list = rettv.vval.v_list;
1517 }
1518
1519 if (p_verbose == 0)
1520 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521 restore_vimvar(VV_VAL, &save_val);
1522
1523 return list;
1524}
1525
1526/*
1527 * "list" is supposed to contain two items: a word and a number. Return the
1528 * word in "pp" and the number as the return value.
1529 * Return -1 if anything isn't right.
1530 * Used to get the good word and score from the eval_spell_expr() result.
1531 */
1532 int
1533get_spellword(list, pp)
1534 list_T *list;
1535 char_u **pp;
1536{
1537 listitem_T *li;
1538
1539 li = list->lv_first;
1540 if (li == NULL)
1541 return -1;
1542 *pp = get_tv_string(&li->li_tv);
1543
1544 li = li->li_next;
1545 if (li == NULL)
1546 return -1;
1547 return get_tv_number(&li->li_tv);
1548}
1549#endif
1550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 * Top level evaluation function.
1553 * Returns an allocated typval_T with the result.
1554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 */
1556 typval_T *
1557eval_expr(arg, nextcmd)
1558 char_u *arg;
1559 char_u **nextcmd;
1560{
1561 typval_T *tv;
1562
1563 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565 {
1566 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 }
1569
1570 return tv;
1571}
1572
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 * Uses argv[argc] for the function arguments. Only Number and String
1577 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001580 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001581call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 char_u *func;
1583 int argc;
1584 char_u **argv;
1585 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long n;
1591 int len;
1592 int i;
1593 int doesrange;
1594 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001597 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 for (i = 0; i < argc; i++)
1602 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001603 /* Pass a NULL or empty argument as an empty string */
1604 if (argv[i] == NULL || *argv[i] == NUL)
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001608 continue;
1609 }
1610
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001611 if (str_arg_only)
1612 len = 0;
1613 else
1614 /* Recognize a number argument, the others must be strings. */
1615 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (len != 0 && len == (int)STRLEN(argv[i]))
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_NUMBER;
1619 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627
1628 if (safe)
1629 {
1630 save_funccalp = save_funccal();
1631 ++sandbox;
1632 }
1633
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1635 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (safe)
1639 {
1640 --sandbox;
1641 restore_funccal(save_funccalp);
1642 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 vim_free(argvars);
1644
1645 if (ret == FAIL)
1646 clear_tv(rettv);
1647
1648 return ret;
1649}
1650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001651/*
1652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 /* All arguments are passed as strings, no conversion to number. */
1667 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1668 return -1;
1669
1670 retval = get_tv_number_chk(&rettv, NULL);
1671 clear_tv(&rettv);
1672 return retval;
1673}
1674
1675#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1676 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1677
Bram Moolenaar4f688582007-07-24 12:34:30 +00001678# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001680 * Call vimL function "func" and return the result as a string.
1681 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 * Uses argv[argc] for the function arguments.
1683 */
1684 void *
1685call_func_retstr(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 return NULL;
1697
1698 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001707 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 */
1709 void *
1710call_func_retlist(func, argc, argv, safe)
1711 char_u *func;
1712 int argc;
1713 char_u **argv;
1714 int safe; /* use the sandbox */
1715{
1716 typval_T rettv;
1717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 if (rettv.v_type != VAR_LIST)
1723 {
1724 clear_tv(&rettv);
1725 return NULL;
1726 }
1727
1728 return rettv.vval.v_list;
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
1733 * Save the current function call pointer, and set it to NULL.
1734 * Used when executing autocommands and for ":source".
1735 */
1736 void *
1737save_funccal()
1738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 current_funccal = NULL;
1742 return (void *)fc;
1743}
1744
1745 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746restore_funccal(vfc)
1747 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749 funccall_T *fc = (funccall_T *)vfc;
1750
1751 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754#if defined(FEAT_PROFILE) || defined(PROTO)
1755/*
1756 * Prepare profiling for entering a child or something else that is not
1757 * counted for the script/function itself.
1758 * Should always be called in pair with prof_child_exit().
1759 */
1760 void
1761prof_child_enter(tm)
1762 proftime_T *tm; /* place to store waittime */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 profile_start(&fc->prof_child);
1768 script_prof_save(tm);
1769}
1770
1771/*
1772 * Take care of time spent in a child.
1773 * Should always be called after prof_child_enter().
1774 */
1775 void
1776prof_child_exit(tm)
1777 proftime_T *tm; /* where waittime was stored */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 {
1783 profile_end(&fc->prof_child);
1784 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1785 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1786 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1787 }
1788 script_prof_restore(tm);
1789}
1790#endif
1791
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_FOLDING
1794/*
1795 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1796 * it in "*cp". Doesn't give error messages.
1797 */
1798 int
1799eval_foldexpr(arg, cp)
1800 char_u *arg;
1801 int *cp;
1802{
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int retval;
1805 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001806 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1807 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 ++sandbox;
1812 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 if (tv.v_type == VAR_NUMBER)
1820 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001821 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 retval = 0;
1823 else
1824 {
1825 /* If the result is a string, check if there is a non-digit before
1826 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!VIM_ISDIGIT(*s) && *s != '-')
1829 *cp = *s++;
1830 retval = atol((char *)s);
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 --sandbox;
1837 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let" list all variable values
1845 * ":let var1 var2" list variable values
1846 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 * ":let var += expr" assignment command.
1848 * ":let var -= expr" assignment command.
1849 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 void
1853ex_let(eap)
1854 exarg_T *eap;
1855{
1856 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 int var_count = 0;
1861 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 argend = skip_var_list(arg, &var_count, &semicolon);
1867 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001869 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1870 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001871 expr = skipwhite(argend);
1872 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1873 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001875 /*
1876 * ":let" without "=": list variables
1877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 if (*arg == '[')
1879 EMSG(_(e_invarg));
1880 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_glob_vars(&first);
1887 list_buf_vars(&first);
1888 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001891#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_script_vars(&first);
1893 list_func_vars(&first);
1894 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 eap->nextcmd = check_nextcmd(arg);
1897 }
1898 else
1899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op[0] = '=';
1901 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1905 op[0] = *expr; /* +=, -= or .= */
1906 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 else
1909 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (eap->skip)
1912 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 {
1916 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 --emsg_skip;
1919 }
1920 else if (i != FAIL)
1921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927}
1928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929/*
1930 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1931 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 * When "nextchars" is not NULL it points to a string with characters that
1933 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1934 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Returns OK or FAIL;
1936 */
1937 static int
1938ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1939 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int copy; /* copy values from "tv", don't move */
1942 int semicolon; /* from skip_var_list() */
1943 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
1946 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 listitem_T *item;
1950 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
1952 if (*arg != '[')
1953 {
1954 /*
1955 * ":let var = expr" or ":for var in list"
1956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 return OK;
1960 }
1961
1962 /*
1963 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1964 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001965 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
1967 EMSG(_(e_listreq));
1968 return FAIL;
1969 }
1970
1971 i = list_len(l);
1972 if (semicolon == 0 && var_count < i)
1973 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001974 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 }
1977 if (var_count - semicolon > i)
1978 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001979 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 return FAIL;
1981 }
1982
1983 item = l->lv_first;
1984 while (*arg != ']')
1985 {
1986 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 item = item->li_next;
1989 if (arg == NULL)
1990 return FAIL;
1991
1992 arg = skipwhite(arg);
1993 if (*arg == ';')
1994 {
1995 /* Put the rest of the list (may be empty) in the var after ';'.
1996 * Create a new list for this. */
1997 l = list_alloc();
1998 if (l == NULL)
1999 return FAIL;
2000 while (item != NULL)
2001 {
2002 list_append_tv(l, &item->li_tv);
2003 item = item->li_next;
2004 }
2005
2006 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002007 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 ltv.vval.v_list = l;
2009 l->lv_refcount = 1;
2010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2012 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 clear_tv(&ltv);
2014 if (arg == NULL)
2015 return FAIL;
2016 break;
2017 }
2018 else if (*arg != ',' && *arg != ']')
2019 {
2020 EMSG2(_(e_intern2), "ex_let_vars()");
2021 return FAIL;
2022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Skip over assignable variable "var" or list of variables "[var, var]".
2030 * Used for ":let varvar = expr" and ":for varvar in expr".
2031 * For "[var, var]" increment "*var_count" for each variable.
2032 * for "[var, var; var]" set "semicolon".
2033 * Return NULL for an error.
2034 */
2035 static char_u *
2036skip_var_list(arg, var_count, semicolon)
2037 char_u *arg;
2038 int *var_count;
2039 int *semicolon;
2040{
2041 char_u *p, *s;
2042
2043 if (*arg == '[')
2044 {
2045 /* "[var, var]": find the matching ']'. */
2046 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002047 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 {
2049 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2050 s = skip_var_one(p);
2051 if (s == p)
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 ++*var_count;
2057
2058 p = skipwhite(s);
2059 if (*p == ']')
2060 break;
2061 else if (*p == ';')
2062 {
2063 if (*semicolon == 1)
2064 {
2065 EMSG(_("Double ; in list of variables"));
2066 return NULL;
2067 }
2068 *semicolon = 1;
2069 }
2070 else if (*p != ',')
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 }
2076 return p + 1;
2077 }
2078 else
2079 return skip_var_one(arg);
2080}
2081
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 static char_u *
2087skip_var_one(arg)
2088 char_u *arg;
2089{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 if (*arg == '@' && arg[1] != NUL)
2091 return arg + 2;
2092 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2093 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094}
2095
Bram Moolenaara7043832005-01-21 11:56:39 +00002096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 * List variables for hashtab "ht" with prefix "prefix".
2098 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 hashitem_T *hi;
2108 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 int todo;
2110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
2116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 di = HI2DI(hi);
2118 if (empty || di->di_tv.v_type != VAR_STRING
2119 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 }
2123}
2124
2125/*
2126 * List global variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_glob_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List buffer variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_buf_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 char_u numbuf[NUMBUFLEN];
2143
Bram Moolenaar429fa852013-04-15 12:27:36 +02002144 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2149 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List window variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_win_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#ifdef FEAT_WINDOWS
2164/*
2165 * List tab page variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_tab_vars(first)
2169 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173}
2174#endif
2175
Bram Moolenaara7043832005-01-21 11:56:39 +00002176/*
2177 * List Vim variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_vim_vars(first)
2181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184}
2185
2186/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187 * List script-local variables, if there is a script.
2188 */
2189 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_script_vars(first)
2191 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192{
2193 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2195 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196}
2197
2198/*
2199 * List function variables, if there is a function.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_func_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_funccal != NULL)
2206 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * List variables in "arg".
2212 */
2213 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 exarg_T *eap;
2216 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002260 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
2519check_changedtick(arg)
2520 char_u *arg;
2521{
2522 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2523 {
2524 EMSG2(_(e_readonlyvar), arg);
2525 return TRUE;
2526 }
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Get an lval: variable, Dict item or List item that can be assigned a value
2532 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2533 * "name.key", "name.key[expr]" etc.
2534 * Indexing only works if "name" is an existing List or Dictionary.
2535 * "name" points to the start of the name.
2536 * If "rettv" is not NULL it points to the value to be assigned.
2537 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2538 * wrong; must end in space or cmd separator.
2539 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540 * flags:
2541 * GLV_QUIET: do not give error messages
2542 * GLV_NO_AUTOLOAD: do not use script autoloading
2543 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002545 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 */
2548 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
2552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int unlet;
2554 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 char_u *expr_start, *expr_end;
2560 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 dictitem_T *v;
2562 typval_T var1;
2563 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (skip)
2575 {
2576 /* When skipping just find the end of the name. */
2577 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002578 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
2580
2581 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (expr_start != NULL)
2584 {
2585 /* Don't expand the name when we already know there is an error. */
2586 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2587 && *p != '[' && *p != '.')
2588 {
2589 EMSG(_(e_trailing));
2590 return NULL;
2591 }
2592
2593 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2594 if (lp->ll_exp_name == NULL)
2595 {
2596 /* Report an invalid expression in braces, unless the
2597 * expression evaluation has been cancelled due to an
2598 * aborting error, an interrupt, or an exception. */
2599 if (!aborting() && !quiet)
2600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002601 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 EMSG2(_(e_invarg2), name);
2603 return NULL;
2604 }
2605 }
2606 lp->ll_name = lp->ll_exp_name;
2607 }
2608 else
2609 lp->ll_name = name;
2610
2611 /* Without [idx] or .key we are done. */
2612 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2613 return p;
2614
2615 cc = *p;
2616 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (v == NULL && !quiet)
2619 EMSG2(_(e_undefvar), lp->ll_name);
2620 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 if (v == NULL)
2622 return NULL;
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /*
2625 * Loop until no more [idx] or .key is following.
2626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2631 && !(lp->ll_tv->v_type == VAR_DICT
2632 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E689: Can only index a List or Dictionary"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E708: [:] must come last"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 len = -1;
2646 if (*p == '.')
2647 {
2648 key = p + 1;
2649 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2650 ;
2651 if (len == 0)
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_(e_emptykey));
2655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = key + len;
2658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (*p == ':')
2664 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 empty1 = FALSE;
2668 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var1) == NULL)
2671 {
2672 /* not a number or string */
2673 clear_tv(&var1);
2674 return NULL;
2675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677
2678 /* Optionally get the second index [ :expr]. */
2679 if (*p == ':')
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (rettv != NULL && (rettv->v_type != VAR_LIST
2690 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = skipwhite(p + 1);
2699 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (get_tv_string_chk(&var2) == NULL)
2711 {
2712 /* not a number or string */
2713 if (!empty1)
2714 clear_tv(&var1);
2715 clear_tv(&var2);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734
2735 /* Skip to past ']'. */
2736 ++p;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
2741 if (len == -1)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*key == NUL)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_list = NULL;
2754 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 /* When assigning to a scope dictionary check that a function and
2758 * variable name is valid (only variable name unless it is l: or
2759 * g: dictionary). Disallow overwriting a builtin function. */
2760 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 int prevval;
2763 int wrong;
2764
2765 if (len != -1)
2766 {
2767 prevval = key[len];
2768 key[len] = NUL;
2769 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002770 else
2771 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2773 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 || !valid_varname(key);
2776 if (len != -1)
2777 key[len] = prevval;
2778 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 return NULL;
2780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 /* Can't add "v:" variable. */
2785 if (lp->ll_dict == &vimvardict)
2786 {
2787 EMSG2(_(e_illvar), name);
2788 return NULL;
2789 }
2790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002791 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 p = NULL;
2808 break;
2809 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* existing variable, need to check if it can be changed */
2811 else if (var_check_ro(lp->ll_di->di_flags, name))
2812 return NULL;
2813
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 if (len == -1)
2815 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818 else
2819 {
2820 /*
2821 * Get the number and item for the only or first index of the List.
2822 */
2823 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
2826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var1);
2829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_list = lp->ll_tv->vval.v_list;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (lp->ll_n1 < 0)
2836 {
2837 lp->ll_n1 = 0;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 }
2840 }
2841 if (lp->ll_li == NULL)
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849
2850 /*
2851 * May need to find the item or absolute index for the second
2852 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 * When no index given: "lp->ll_empty2" is TRUE.
2854 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 {
2865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2873 if (lp->ll_n1 < 0)
2874 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2875 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return p;
2888}
2889
2890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 */
2893 static void
2894clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 vim_free(lp->ll_exp_name);
2898 vim_free(lp->ll_newkey);
2899}
2900
2901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 listitem_T *ri;
2916 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917
2918 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 cc = *endp;
2923 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002929 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002930 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 {
2932 if (tv_op(&tv, rettv, op) == OK)
2933 set_var(lp->ll_name, &tv, FALSE);
2934 clear_tv(&tv);
2935 }
2936 }
2937 else
2938 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 else if (tv_check_lock(lp->ll_newkey == NULL
2943 ? lp->ll_tv->v_lock
2944 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2945 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 else if (lp->ll_range)
2947 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002948 listitem_T *ll_li = lp->ll_li;
2949 int ll_n1 = lp->ll_n1;
2950
2951 /*
2952 * Check whether any of the list items is locked
2953 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002954 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002955 {
2956 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name))
2957 return;
2958 ri = ri->li_next;
2959 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2960 break;
2961 ll_li = ll_li->li_next;
2962 ++ll_n1;
2963 }
2964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 /*
2966 * Assign the List values to the list items.
2967 */
2968 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002969 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 if (op != NULL && *op != '=')
2971 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2972 else
2973 {
2974 clear_tv(&lp->ll_li->li_tv);
2975 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 ri = ri->li_next;
2978 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2979 break;
2980 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002983 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 ri = NULL;
2986 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 lp->ll_li = lp->ll_li->li_next;
2990 ++lp->ll_n1;
2991 }
2992 if (ri != NULL)
2993 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 else if (lp->ll_empty2
2995 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 : lp->ll_n1 != lp->ll_n2)
2997 EMSG(_("E711: List value has not enough items"));
2998 }
2999 else
3000 {
3001 /*
3002 * Assign to a List or Dictionary item.
3003 */
3004 if (lp->ll_newkey != NULL)
3005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 if (op != NULL && *op != '=')
3007 {
3008 EMSG2(_(e_letwrong), op);
3009 return;
3010 }
3011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003013 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 if (di == NULL)
3015 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003016 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3017 {
3018 vim_free(di);
3019 return;
3020 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else if (op != NULL && *op != '=')
3024 {
3025 tv_op(lp->ll_tv, rettv, op);
3026 return;
3027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003028 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003030
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 /*
3032 * Assign the value to the variable or list item.
3033 */
3034 if (copy)
3035 copy_tv(rettv, lp->ll_tv);
3036 else
3037 {
3038 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003039 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 }
3042 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003043}
3044
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3047 * Returns OK or FAIL.
3048 */
3049 static int
3050tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003051 typval_T *tv1;
3052 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 char_u *op;
3054{
3055 long n;
3056 char_u numbuf[NUMBUFLEN];
3057 char_u *s;
3058
3059 /* Can't do anything with a Funcref or a Dict on the right. */
3060 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3061 {
3062 switch (tv1->v_type)
3063 {
3064 case VAR_DICT:
3065 case VAR_FUNC:
3066 break;
3067
3068 case VAR_LIST:
3069 if (*op != '+' || tv2->v_type != VAR_LIST)
3070 break;
3071 /* List += List */
3072 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3073 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3074 return OK;
3075
3076 case VAR_NUMBER:
3077 case VAR_STRING:
3078 if (tv2->v_type == VAR_LIST)
3079 break;
3080 if (*op == '+' || *op == '-')
3081 {
3082 /* nr += nr or nr -= nr*/
3083 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084#ifdef FEAT_FLOAT
3085 if (tv2->v_type == VAR_FLOAT)
3086 {
3087 float_T f = n;
3088
3089 if (*op == '+')
3090 f += tv2->vval.v_float;
3091 else
3092 f -= tv2->vval.v_float;
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_FLOAT;
3095 tv1->vval.v_float = f;
3096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098#endif
3099 {
3100 if (*op == '+')
3101 n += get_tv_number(tv2);
3102 else
3103 n -= get_tv_number(tv2);
3104 clear_tv(tv1);
3105 tv1->v_type = VAR_NUMBER;
3106 tv1->vval.v_number = n;
3107 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003108 }
3109 else
3110 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 if (tv2->v_type == VAR_FLOAT)
3112 break;
3113
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 /* str .= str */
3115 s = get_tv_string(tv1);
3116 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_STRING;
3119 tv1->vval.v_string = s;
3120 }
3121 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122
3123#ifdef FEAT_FLOAT
3124 case VAR_FLOAT:
3125 {
3126 float_T f;
3127
3128 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3129 && tv2->v_type != VAR_NUMBER
3130 && tv2->v_type != VAR_STRING))
3131 break;
3132 if (tv2->v_type == VAR_FLOAT)
3133 f = tv2->vval.v_float;
3134 else
3135 f = get_tv_number(tv2);
3136 if (*op == '+')
3137 tv1->vval.v_float += f;
3138 else
3139 tv1->vval.v_float -= f;
3140 }
3141 return OK;
3142#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 }
3144 }
3145
3146 EMSG2(_(e_letwrong), op);
3147 return FAIL;
3148}
3149
3150/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 * Add a watcher to a list.
3152 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003153 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003155 list_T *l;
3156 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157{
3158 lw->lw_next = l->lv_watch;
3159 l->lv_watch = lw;
3160}
3161
3162/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003163 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * No warning when it isn't found...
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172
3173 lwp = &l->lv_watch;
3174 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3175 {
3176 if (lw == lwrem)
3177 {
3178 *lwp = lw->lw_next;
3179 break;
3180 }
3181 lwp = &lw->lw_next;
3182 }
3183}
3184
3185/*
3186 * Just before removing an item from a list: advance watchers to the next
3187 * item.
3188 */
3189 static void
3190list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 list_T *l;
3192 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3197 if (lw->lw_item == item)
3198 lw->lw_item = item->li_next;
3199}
3200
3201/*
3202 * Evaluate the expression used in a ":for var in expr" command.
3203 * "arg" points to "var".
3204 * Set "*errp" to TRUE for an error, FALSE otherwise;
3205 * Return a pointer that holds the info. Null when there is an error.
3206 */
3207 void *
3208eval_for_line(arg, errp, nextcmdp, skip)
3209 char_u *arg;
3210 int *errp;
3211 char_u **nextcmdp;
3212 int skip;
3213{
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 typval_T tv;
3217 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218
3219 *errp = TRUE; /* default: there is an error */
3220
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 if (fi == NULL)
3223 return NULL;
3224
3225 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3226 if (expr == NULL)
3227 return fi;
3228
3229 expr = skipwhite(expr);
3230 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3231 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003232 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 return fi;
3234 }
3235
3236 if (skip)
3237 ++emsg_skip;
3238 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3239 {
3240 *errp = FALSE;
3241 if (!skip)
3242 {
3243 l = tv.vval.v_list;
3244 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003247 clear_tv(&tv);
3248 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 else
3250 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003251 /* No need to increment the refcount, it's already set for the
3252 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 fi->fi_list = l;
3254 list_add_watch(l, &fi->fi_lw);
3255 fi->fi_lw.lw_item = l->lv_first;
3256 }
3257 }
3258 }
3259 if (skip)
3260 --emsg_skip;
3261
3262 return fi;
3263}
3264
3265/*
3266 * Use the first item in a ":for" list. Advance to the next.
3267 * Assign the values to the variable (list). "arg" points to the first one.
3268 * Return TRUE when a valid item was found, FALSE when at end of list or
3269 * something wrong.
3270 */
3271 int
3272next_for_item(fi_void, arg)
3273 void *fi_void;
3274 char_u *arg;
3275{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003276 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279
3280 item = fi->fi_lw.lw_item;
3281 if (item == NULL)
3282 result = FALSE;
3283 else
3284 {
3285 fi->fi_lw.lw_item = item->li_next;
3286 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3287 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3288 }
3289 return result;
3290}
3291
3292/*
3293 * Free the structure used to store info used by ":for".
3294 */
3295 void
3296free_for_info(fi_void)
3297 void *fi_void;
3298{
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003301 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003302 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003304 list_unref(fi->fi_list);
3305 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 vim_free(fi);
3307}
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3310
3311 void
3312set_context_for_expression(xp, arg, cmdidx)
3313 expand_T *xp;
3314 char_u *arg;
3315 cmdidx_T cmdidx;
3316{
3317 int got_eq = FALSE;
3318 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 if (cmdidx == CMD_let)
3322 {
3323 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003324 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 {
3326 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003327 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 {
3329 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 if (vim_iswhite(*p))
3332 break;
3333 }
3334 return;
3335 }
3336 }
3337 else
3338 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3339 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 while ((xp->xp_pattern = vim_strpbrk(arg,
3341 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3342 {
3343 c = *xp->xp_pattern;
3344 if (c == '&')
3345 {
3346 c = xp->xp_pattern[1];
3347 if (c == '&')
3348 {
3349 ++xp->xp_pattern;
3350 xp->xp_context = cmdidx != CMD_let || got_eq
3351 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3352 }
3353 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003356 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3357 xp->xp_pattern += 2;
3358
3359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 else if (c == '$')
3362 {
3363 /* environment variable */
3364 xp->xp_context = EXPAND_ENV_VARS;
3365 }
3366 else if (c == '=')
3367 {
3368 got_eq = TRUE;
3369 xp->xp_context = EXPAND_EXPRESSION;
3370 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003371 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 && xp->xp_context == EXPAND_FUNCTIONS
3373 && vim_strchr(xp->xp_pattern, '(') == NULL)
3374 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003375 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 break;
3377 }
3378 else if (cmdidx != CMD_let || got_eq)
3379 {
3380 if (c == '"') /* string */
3381 {
3382 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3383 if (c == '\\' && xp->xp_pattern[1] != NUL)
3384 ++xp->xp_pattern;
3385 xp->xp_context = EXPAND_NOTHING;
3386 }
3387 else if (c == '\'') /* literal string */
3388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003389 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3391 /* skip */ ;
3392 xp->xp_context = EXPAND_NOTHING;
3393 }
3394 else if (c == '|')
3395 {
3396 if (xp->xp_pattern[1] == '|')
3397 {
3398 ++xp->xp_pattern;
3399 xp->xp_context = EXPAND_EXPRESSION;
3400 }
3401 else
3402 xp->xp_context = EXPAND_COMMANDS;
3403 }
3404 else
3405 xp->xp_context = EXPAND_EXPRESSION;
3406 }
3407 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003408 /* Doesn't look like something valid, expand as an expression
3409 * anyway. */
3410 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 arg = xp->xp_pattern;
3412 if (*arg != NUL)
3413 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3414 /* skip */ ;
3415 }
3416 xp->xp_pattern = arg;
3417}
3418
3419#endif /* FEAT_CMDL_COMPL */
3420
3421/*
3422 * ":1,25call func(arg1, arg2)" function call.
3423 */
3424 void
3425ex_call(eap)
3426 exarg_T *eap;
3427{
3428 char_u *arg = eap->arg;
3429 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003431 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 linenr_T lnum;
3435 int doesrange;
3436 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003439 if (eap->skip)
3440 {
3441 /* trans_function_name() doesn't work well when skipping, use eval0()
3442 * instead to skip to any following command, e.g. for:
3443 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003444 ++emsg_skip;
3445 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3446 clear_tv(&rettv);
3447 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003448 return;
3449 }
3450
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003451 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003452 if (fudi.fd_newkey != NULL)
3453 {
3454 /* Still need to give an error message for missing key. */
3455 EMSG2(_(e_dictkey), fudi.fd_newkey);
3456 vim_free(fudi.fd_newkey);
3457 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 if (tofree == NULL)
3459 return;
3460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003461 /* Increase refcount on dictionary, it could get deleted when evaluating
3462 * the arguments. */
3463 if (fudi.fd_dict != NULL)
3464 ++fudi.fd_dict->dv_refcount;
3465
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003466 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003468 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
Bram Moolenaar532c7802005-01-27 14:44:31 +00003470 /* Skip white space to allow ":call func ()". Not good, but required for
3471 * backward compatibility. */
3472 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003473 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 if (*startarg != '(')
3476 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003477 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 goto end;
3479 }
3480
3481 /*
3482 * When skipping, evaluate the function once, to find the end of the
3483 * arguments.
3484 * When the function takes a range, this is discovered after the first
3485 * call, and the loop is broken.
3486 */
3487 if (eap->skip)
3488 {
3489 ++emsg_skip;
3490 lnum = eap->line2; /* do it once, also with an invalid range */
3491 }
3492 else
3493 lnum = eap->line1;
3494 for ( ; lnum <= eap->line2; ++lnum)
3495 {
3496 if (!eap->skip && eap->addr_count > 0)
3497 {
3498 curwin->w_cursor.lnum = lnum;
3499 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003500#ifdef FEAT_VIRTUALEDIT
3501 curwin->w_cursor.coladd = 0;
3502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003505 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003506 eap->line1, eap->line2, &doesrange,
3507 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 failed = TRUE;
3510 break;
3511 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003512
3513 /* Handle a function returning a Funcref, Dictionary or List. */
3514 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3515 {
3516 failed = TRUE;
3517 break;
3518 }
3519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (doesrange || eap->skip)
3522 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003525 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003527 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (aborting())
3529 break;
3530 }
3531 if (eap->skip)
3532 --emsg_skip;
3533
3534 if (!failed)
3535 {
3536 /* Check for trailing illegal characters and a following command. */
3537 if (!ends_excmd(*arg))
3538 {
3539 emsg_severe = TRUE;
3540 EMSG(_(e_trailing));
3541 }
3542 else
3543 eap->nextcmd = check_nextcmd(arg);
3544 }
3545
3546end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003547 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003548 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
3551/*
3552 * ":unlet[!] var1 ... " command.
3553 */
3554 void
3555ex_unlet(eap)
3556 exarg_T *eap;
3557{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003558 ex_unletlock(eap, eap->arg, 0);
3559}
3560
3561/*
3562 * ":lockvar" and ":unlockvar" commands
3563 */
3564 void
3565ex_lockvar(eap)
3566 exarg_T *eap;
3567{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 int deep = 2;
3570
3571 if (eap->forceit)
3572 deep = -1;
3573 else if (vim_isdigit(*arg))
3574 {
3575 deep = getdigits(&arg);
3576 arg = skipwhite(arg);
3577 }
3578
3579 ex_unletlock(eap, arg, deep);
3580}
3581
3582/*
3583 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3584 */
3585 static void
3586ex_unletlock(eap, argstart, deep)
3587 exarg_T *eap;
3588 char_u *argstart;
3589 int deep;
3590{
3591 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 do
3597 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003599 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003600 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 if (lv.ll_name == NULL)
3602 error = TRUE; /* error but continue parsing */
3603 if (name_end == NULL || (!vim_iswhite(*name_end)
3604 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (name_end != NULL)
3607 {
3608 emsg_severe = TRUE;
3609 EMSG(_(e_trailing));
3610 }
3611 if (!(eap->skip || error))
3612 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
3614 }
3615
3616 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 {
3618 if (eap->cmdidx == CMD_unlet)
3619 {
3620 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3621 error = TRUE;
3622 }
3623 else
3624 {
3625 if (do_lock_var(&lv, name_end, deep,
3626 eap->cmdidx == CMD_lockvar) == FAIL)
3627 error = TRUE;
3628 }
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 if (!eap->skip)
3632 clear_lval(&lv);
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 arg = skipwhite(name_end);
3635 } while (!ends_excmd(*arg));
3636
3637 eap->nextcmd = check_nextcmd(arg);
3638}
3639
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 int forceit;
3645{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 int ret = OK;
3647 int cc;
3648
3649 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 cc = *name_end;
3652 *name_end = NUL;
3653
3654 /* Normal name or expanded name. */
3655 if (check_changedtick(lp->ll_name))
3656 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3662 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 else if (lp->ll_range)
3664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003666 listitem_T *ll_li = lp->ll_li;
3667 int ll_n1 = lp->ll_n1;
3668
3669 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3670 {
3671 li = ll_li->li_next;
3672 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name))
3673 return FAIL;
3674 ll_li = li;
3675 ++ll_n1;
3676 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677
3678 /* Delete a range of List items. */
3679 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3680 {
3681 li = lp->ll_li->li_next;
3682 listitem_remove(lp->ll_list, lp->ll_li);
3683 lp->ll_li = li;
3684 ++lp->ll_n1;
3685 }
3686 }
3687 else
3688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 /* unlet a List item. */
3691 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003694 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 }
3696
3697 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003698}
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
3701 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003702 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 */
3704 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003707 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
Bram Moolenaar33570922005-01-25 22:26:29 +00003709 hashtab_T *ht;
3710 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003711 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003712 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
Bram Moolenaar33570922005-01-25 22:26:29 +00003714 ht = find_var_ht(name, &varname);
3715 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 hi = hash_find(ht, varname);
3718 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003719 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003720 di = HI2DI(hi);
3721 if (var_check_fixed(di->di_flags, name)
3722 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 return FAIL;
3724 delete_var(ht, hi);
3725 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003728 if (forceit)
3729 return OK;
3730 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 return FAIL;
3732}
3733
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734/*
3735 * Lock or unlock variable indicated by "lp".
3736 * "deep" is the levels to go (-1 for unlimited);
3737 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3738 */
3739 static int
3740do_lock_var(lp, name_end, deep, lock)
3741 lval_T *lp;
3742 char_u *name_end;
3743 int deep;
3744 int lock;
3745{
3746 int ret = OK;
3747 int cc;
3748 dictitem_T *di;
3749
3750 if (deep == 0) /* nothing to do */
3751 return OK;
3752
3753 if (lp->ll_tv == NULL)
3754 {
3755 cc = *name_end;
3756 *name_end = NUL;
3757
3758 /* Normal name or expanded name. */
3759 if (check_changedtick(lp->ll_name))
3760 ret = FAIL;
3761 else
3762 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003763 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764 if (di == NULL)
3765 ret = FAIL;
3766 else
3767 {
3768 if (lock)
3769 di->di_flags |= DI_FLAGS_LOCK;
3770 else
3771 di->di_flags &= ~DI_FLAGS_LOCK;
3772 item_lock(&di->di_tv, deep, lock);
3773 }
3774 }
3775 *name_end = cc;
3776 }
3777 else if (lp->ll_range)
3778 {
3779 listitem_T *li = lp->ll_li;
3780
3781 /* (un)lock a range of List items. */
3782 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3783 {
3784 item_lock(&li->li_tv, deep, lock);
3785 li = li->li_next;
3786 ++lp->ll_n1;
3787 }
3788 }
3789 else if (lp->ll_list != NULL)
3790 /* (un)lock a List item. */
3791 item_lock(&lp->ll_li->li_tv, deep, lock);
3792 else
3793 /* un(lock) a Dictionary item. */
3794 item_lock(&lp->ll_di->di_tv, deep, lock);
3795
3796 return ret;
3797}
3798
3799/*
3800 * Lock or unlock an item. "deep" is nr of levels to go.
3801 */
3802 static void
3803item_lock(tv, deep, lock)
3804 typval_T *tv;
3805 int deep;
3806 int lock;
3807{
3808 static int recurse = 0;
3809 list_T *l;
3810 listitem_T *li;
3811 dict_T *d;
3812 hashitem_T *hi;
3813 int todo;
3814
3815 if (recurse >= DICT_MAXNEST)
3816 {
3817 EMSG(_("E743: variable nested too deep for (un)lock"));
3818 return;
3819 }
3820 if (deep == 0)
3821 return;
3822 ++recurse;
3823
3824 /* lock/unlock the item itself */
3825 if (lock)
3826 tv->v_lock |= VAR_LOCKED;
3827 else
3828 tv->v_lock &= ~VAR_LOCKED;
3829
3830 switch (tv->v_type)
3831 {
3832 case VAR_LIST:
3833 if ((l = tv->vval.v_list) != NULL)
3834 {
3835 if (lock)
3836 l->lv_lock |= VAR_LOCKED;
3837 else
3838 l->lv_lock &= ~VAR_LOCKED;
3839 if (deep < 0 || deep > 1)
3840 /* recursive: lock/unlock the items the List contains */
3841 for (li = l->lv_first; li != NULL; li = li->li_next)
3842 item_lock(&li->li_tv, deep - 1, lock);
3843 }
3844 break;
3845 case VAR_DICT:
3846 if ((d = tv->vval.v_dict) != NULL)
3847 {
3848 if (lock)
3849 d->dv_lock |= VAR_LOCKED;
3850 else
3851 d->dv_lock &= ~VAR_LOCKED;
3852 if (deep < 0 || deep > 1)
3853 {
3854 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003855 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003856 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3857 {
3858 if (!HASHITEM_EMPTY(hi))
3859 {
3860 --todo;
3861 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3862 }
3863 }
3864 }
3865 }
3866 }
3867 --recurse;
3868}
3869
Bram Moolenaara40058a2005-07-11 22:42:07 +00003870/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003871 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3872 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003873 */
3874 static int
3875tv_islocked(tv)
3876 typval_T *tv;
3877{
3878 return (tv->v_lock & VAR_LOCKED)
3879 || (tv->v_type == VAR_LIST
3880 && tv->vval.v_list != NULL
3881 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3882 || (tv->v_type == VAR_DICT
3883 && tv->vval.v_dict != NULL
3884 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3885}
3886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3888/*
3889 * Delete all "menutrans_" variables.
3890 */
3891 void
3892del_menutrans_vars()
3893{
Bram Moolenaar33570922005-01-25 22:26:29 +00003894 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003895 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896
Bram Moolenaar33570922005-01-25 22:26:29 +00003897 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003899 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003900 {
3901 if (!HASHITEM_EMPTY(hi))
3902 {
3903 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3905 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003906 }
3907 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003908 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909}
3910#endif
3911
3912#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3913
3914/*
3915 * Local string buffer for the next two functions to store a variable name
3916 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3917 * get_user_var_name().
3918 */
3919
3920static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3921
3922static char_u *varnamebuf = NULL;
3923static int varnamebuflen = 0;
3924
3925/*
3926 * Function to concatenate a prefix and a variable name.
3927 */
3928 static char_u *
3929cat_prefix_varname(prefix, name)
3930 int prefix;
3931 char_u *name;
3932{
3933 int len;
3934
3935 len = (int)STRLEN(name) + 3;
3936 if (len > varnamebuflen)
3937 {
3938 vim_free(varnamebuf);
3939 len += 10; /* some additional space */
3940 varnamebuf = alloc(len);
3941 if (varnamebuf == NULL)
3942 {
3943 varnamebuflen = 0;
3944 return NULL;
3945 }
3946 varnamebuflen = len;
3947 }
3948 *varnamebuf = prefix;
3949 varnamebuf[1] = ':';
3950 STRCPY(varnamebuf + 2, name);
3951 return varnamebuf;
3952}
3953
3954/*
3955 * Function given to ExpandGeneric() to obtain the list of user defined
3956 * (global/buffer/window/built-in) variable names.
3957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 char_u *
3959get_user_var_name(xp, idx)
3960 expand_T *xp;
3961 int idx;
3962{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 static long_u gdone;
3964 static long_u bdone;
3965 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003966#ifdef FEAT_WINDOWS
3967 static long_u tdone;
3968#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003969 static int vidx;
3970 static hashitem_T *hi;
3971 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972
3973 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003975 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003976#ifdef FEAT_WINDOWS
3977 tdone = 0;
3978#endif
3979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980
3981 /* Global variables */
3982 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003984 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003986 else
3987 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003988 while (HASHITEM_EMPTY(hi))
3989 ++hi;
3990 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3991 return cat_prefix_varname('g', hi->hi_key);
3992 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003994
3995 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003996 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003999 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 else
4002 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 while (HASHITEM_EMPTY(hi))
4004 ++hi;
4005 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004009 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return (char_u *)"b:changedtick";
4011 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004012
4013 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004014 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004017 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004019 else
4020 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 while (HASHITEM_EMPTY(hi))
4022 ++hi;
4023 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004025
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026#ifdef FEAT_WINDOWS
4027 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004028 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029 if (tdone < ht->ht_used)
4030 {
4031 if (tdone++ == 0)
4032 hi = ht->ht_array;
4033 else
4034 ++hi;
4035 while (HASHITEM_EMPTY(hi))
4036 ++hi;
4037 return cat_prefix_varname('t', hi->hi_key);
4038 }
4039#endif
4040
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 /* v: variables */
4042 if (vidx < VV_LEN)
4043 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 vim_free(varnamebuf);
4046 varnamebuf = NULL;
4047 varnamebuflen = 0;
4048 return NULL;
4049}
4050
4051#endif /* FEAT_CMDL_COMPL */
4052
4053/*
4054 * types for expressions.
4055 */
4056typedef enum
4057{
4058 TYPE_UNKNOWN = 0
4059 , TYPE_EQUAL /* == */
4060 , TYPE_NEQUAL /* != */
4061 , TYPE_GREATER /* > */
4062 , TYPE_GEQUAL /* >= */
4063 , TYPE_SMALLER /* < */
4064 , TYPE_SEQUAL /* <= */
4065 , TYPE_MATCH /* =~ */
4066 , TYPE_NOMATCH /* !~ */
4067} exptype_T;
4068
4069/*
4070 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004071 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4073 */
4074
4075/*
4076 * Handle zero level expression.
4077 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004079 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 * Return OK or FAIL.
4081 */
4082 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 char_u **nextcmd;
4087 int evaluate;
4088{
4089 int ret;
4090 char_u *p;
4091
4092 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (ret == FAIL || !ends_excmd(*p))
4095 {
4096 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 /*
4099 * Report the invalid expression unless the expression evaluation has
4100 * been cancelled due to an aborting error, an interrupt, or an
4101 * exception.
4102 */
4103 if (!aborting())
4104 EMSG2(_(e_invexpr2), arg);
4105 ret = FAIL;
4106 }
4107 if (nextcmd != NULL)
4108 *nextcmd = check_nextcmd(p);
4109
4110 return ret;
4111}
4112
4113/*
4114 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004115 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 *
4117 * "arg" must point to the first non-white of the expression.
4118 * "arg" is advanced to the next non-white after the recognized expression.
4119 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004120 * Note: "rettv.v_lock" is not set.
4121 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Return OK or FAIL.
4123 */
4124 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 int evaluate;
4129{
4130 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004131 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 /*
4134 * Get the first variable.
4135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 return FAIL;
4138
4139 if ((*arg)[0] == '?')
4140 {
4141 result = FALSE;
4142 if (evaluate)
4143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 int error = FALSE;
4145
4146 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 if (error)
4150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152
4153 /*
4154 * Get the second variable.
4155 */
4156 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Check for the ":".
4162 */
4163 if ((*arg)[0] != ':')
4164 {
4165 EMSG(_("E109: Missing ':' after '?'"));
4166 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169 }
4170
4171 /*
4172 * Get the third variable.
4173 */
4174 *arg = skipwhite(*arg + 1);
4175 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4176 {
4177 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180 }
4181 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * Handle first level expression:
4190 * expr2 || expr2 || expr2 logical OR
4191 *
4192 * "arg" must point to the first non-white of the expression.
4193 * "arg" is advanced to the next non-white after the recognized expression.
4194 *
4195 * Return OK or FAIL.
4196 */
4197 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 int evaluate;
4202{
Bram Moolenaar33570922005-01-25 22:26:29 +00004203 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 long result;
4205 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208 /*
4209 * Get the first variable.
4210 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213
4214 /*
4215 * Repeat until there is no following "||".
4216 */
4217 first = TRUE;
4218 result = FALSE;
4219 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4220 {
4221 if (evaluate && first)
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 if (error)
4227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 first = FALSE;
4229 }
4230
4231 /*
4232 * Get the second variable.
4233 */
4234 *arg = skipwhite(*arg + 2);
4235 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4236 return FAIL;
4237
4238 /*
4239 * Compute the result.
4240 */
4241 if (evaluate && !result)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 if (evaluate)
4250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 rettv->v_type = VAR_NUMBER;
4252 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * Handle second level expression:
4261 * expr3 && expr3 && expr3 logical AND
4262 *
4263 * "arg" must point to the first non-white of the expression.
4264 * "arg" is advanced to the next non-white after the recognized expression.
4265 *
4266 * Return OK or FAIL.
4267 */
4268 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 int evaluate;
4273{
Bram Moolenaar33570922005-01-25 22:26:29 +00004274 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 long result;
4276 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 /*
4280 * Get the first variable.
4281 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return FAIL;
4284
4285 /*
4286 * Repeat until there is no following "&&".
4287 */
4288 first = TRUE;
4289 result = TRUE;
4290 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4291 {
4292 if (evaluate && first)
4293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 if (error)
4298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 first = FALSE;
4300 }
4301
4302 /*
4303 * Get the second variable.
4304 */
4305 *arg = skipwhite(*arg + 2);
4306 if (eval4(arg, &var2, evaluate && result) == FAIL)
4307 return FAIL;
4308
4309 /*
4310 * Compute the result.
4311 */
4312 if (evaluate && result)
4313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (error)
4318 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 if (evaluate)
4321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 rettv->v_type = VAR_NUMBER;
4323 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325 }
4326
4327 return OK;
4328}
4329
4330/*
4331 * Handle third level expression:
4332 * var1 == var2
4333 * var1 =~ var2
4334 * var1 != var2
4335 * var1 !~ var2
4336 * var1 > var2
4337 * var1 >= var2
4338 * var1 < var2
4339 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004340 * var1 is var2
4341 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 *
4343 * "arg" must point to the first non-white of the expression.
4344 * "arg" is advanced to the next non-white after the recognized expression.
4345 *
4346 * Return OK or FAIL.
4347 */
4348 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 int evaluate;
4353{
Bram Moolenaar33570922005-01-25 22:26:29 +00004354 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 char_u *p;
4356 int i;
4357 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 int len = 2;
4360 long n1, n2;
4361 char_u *s1, *s2;
4362 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4363 regmatch_T regmatch;
4364 int ic;
4365 char_u *save_cpo;
4366
4367 /*
4368 * Get the first variable.
4369 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 return FAIL;
4372
4373 p = *arg;
4374 switch (p[0])
4375 {
4376 case '=': if (p[1] == '=')
4377 type = TYPE_EQUAL;
4378 else if (p[1] == '~')
4379 type = TYPE_MATCH;
4380 break;
4381 case '!': if (p[1] == '=')
4382 type = TYPE_NEQUAL;
4383 else if (p[1] == '~')
4384 type = TYPE_NOMATCH;
4385 break;
4386 case '>': if (p[1] != '=')
4387 {
4388 type = TYPE_GREATER;
4389 len = 1;
4390 }
4391 else
4392 type = TYPE_GEQUAL;
4393 break;
4394 case '<': if (p[1] != '=')
4395 {
4396 type = TYPE_SMALLER;
4397 len = 1;
4398 }
4399 else
4400 type = TYPE_SEQUAL;
4401 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 case 'i': if (p[1] == 's')
4403 {
4404 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4405 len = 5;
4406 if (!vim_isIDc(p[len]))
4407 {
4408 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4409 type_is = TRUE;
4410 }
4411 }
4412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414
4415 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004416 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 */
4418 if (type != TYPE_UNKNOWN)
4419 {
4420 /* extra question mark appended: ignore case */
4421 if (p[len] == '?')
4422 {
4423 ic = TRUE;
4424 ++len;
4425 }
4426 /* extra '#' appended: match case */
4427 else if (p[len] == '#')
4428 {
4429 ic = FALSE;
4430 ++len;
4431 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004432 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 else
4434 ic = p_ic;
4435
4436 /*
4437 * Get the second variable.
4438 */
4439 *arg = skipwhite(p + len);
4440 if (eval5(arg, &var2, evaluate) == FAIL)
4441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 return FAIL;
4444 }
4445
4446 if (evaluate)
4447 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 if (type_is && rettv->v_type != var2.v_type)
4449 {
4450 /* For "is" a different type always means FALSE, for "notis"
4451 * it means TRUE. */
4452 n1 = (type == TYPE_NEQUAL);
4453 }
4454 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4455 {
4456 if (type_is)
4457 {
4458 n1 = (rettv->v_type == var2.v_type
4459 && rettv->vval.v_list == var2.vval.v_list);
4460 if (type == TYPE_NEQUAL)
4461 n1 = !n1;
4462 }
4463 else if (rettv->v_type != var2.v_type
4464 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4465 {
4466 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004467 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004469 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 clear_tv(rettv);
4471 clear_tv(&var2);
4472 return FAIL;
4473 }
4474 else
4475 {
4476 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004477 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4478 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 if (type == TYPE_NEQUAL)
4480 n1 = !n1;
4481 }
4482 }
4483
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004484 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4485 {
4486 if (type_is)
4487 {
4488 n1 = (rettv->v_type == var2.v_type
4489 && rettv->vval.v_dict == var2.vval.v_dict);
4490 if (type == TYPE_NEQUAL)
4491 n1 = !n1;
4492 }
4493 else if (rettv->v_type != var2.v_type
4494 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4495 {
4496 if (rettv->v_type != var2.v_type)
4497 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4498 else
4499 EMSG(_("E736: Invalid operation for Dictionary"));
4500 clear_tv(rettv);
4501 clear_tv(&var2);
4502 return FAIL;
4503 }
4504 else
4505 {
4506 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004507 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4508 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004509 if (type == TYPE_NEQUAL)
4510 n1 = !n1;
4511 }
4512 }
4513
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4515 {
4516 if (rettv->v_type != var2.v_type
4517 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4518 {
4519 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004520 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004521 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004522 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 clear_tv(rettv);
4524 clear_tv(&var2);
4525 return FAIL;
4526 }
4527 else
4528 {
4529 /* Compare two Funcrefs for being equal or unequal. */
4530 if (rettv->vval.v_string == NULL
4531 || var2.vval.v_string == NULL)
4532 n1 = FALSE;
4533 else
4534 n1 = STRCMP(rettv->vval.v_string,
4535 var2.vval.v_string) == 0;
4536 if (type == TYPE_NEQUAL)
4537 n1 = !n1;
4538 }
4539 }
4540
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004541#ifdef FEAT_FLOAT
4542 /*
4543 * If one of the two variables is a float, compare as a float.
4544 * When using "=~" or "!~", always compare as string.
4545 */
4546 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4547 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4548 {
4549 float_T f1, f2;
4550
4551 if (rettv->v_type == VAR_FLOAT)
4552 f1 = rettv->vval.v_float;
4553 else
4554 f1 = get_tv_number(rettv);
4555 if (var2.v_type == VAR_FLOAT)
4556 f2 = var2.vval.v_float;
4557 else
4558 f2 = get_tv_number(&var2);
4559 n1 = FALSE;
4560 switch (type)
4561 {
4562 case TYPE_EQUAL: n1 = (f1 == f2); break;
4563 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4564 case TYPE_GREATER: n1 = (f1 > f2); break;
4565 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4566 case TYPE_SMALLER: n1 = (f1 < f2); break;
4567 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4568 case TYPE_UNKNOWN:
4569 case TYPE_MATCH:
4570 case TYPE_NOMATCH: break; /* avoid gcc warning */
4571 }
4572 }
4573#endif
4574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 /*
4576 * If one of the two variables is a number, compare as a number.
4577 * When using "=~" or "!~", always compare as string.
4578 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004579 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 n1 = get_tv_number(rettv);
4583 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 switch (type)
4585 {
4586 case TYPE_EQUAL: n1 = (n1 == n2); break;
4587 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4588 case TYPE_GREATER: n1 = (n1 > n2); break;
4589 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4590 case TYPE_SMALLER: n1 = (n1 < n2); break;
4591 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4592 case TYPE_UNKNOWN:
4593 case TYPE_MATCH:
4594 case TYPE_NOMATCH: break; /* avoid gcc warning */
4595 }
4596 }
4597 else
4598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 s1 = get_tv_string_buf(rettv, buf1);
4600 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4602 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4603 else
4604 i = 0;
4605 n1 = FALSE;
4606 switch (type)
4607 {
4608 case TYPE_EQUAL: n1 = (i == 0); break;
4609 case TYPE_NEQUAL: n1 = (i != 0); break;
4610 case TYPE_GREATER: n1 = (i > 0); break;
4611 case TYPE_GEQUAL: n1 = (i >= 0); break;
4612 case TYPE_SMALLER: n1 = (i < 0); break;
4613 case TYPE_SEQUAL: n1 = (i <= 0); break;
4614
4615 case TYPE_MATCH:
4616 case TYPE_NOMATCH:
4617 /* avoid 'l' flag in 'cpoptions' */
4618 save_cpo = p_cpo;
4619 p_cpo = (char_u *)"";
4620 regmatch.regprog = vim_regcomp(s2,
4621 RE_MAGIC + RE_STRING);
4622 regmatch.rm_ic = ic;
4623 if (regmatch.regprog != NULL)
4624 {
4625 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004626 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (type == TYPE_NOMATCH)
4628 n1 = !n1;
4629 }
4630 p_cpo = save_cpo;
4631 break;
4632
4633 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4634 }
4635 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 clear_tv(rettv);
4637 clear_tv(&var2);
4638 rettv->v_type = VAR_NUMBER;
4639 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 }
4642
4643 return OK;
4644}
4645
4646/*
4647 * Handle fourth level expression:
4648 * + number addition
4649 * - number subtraction
4650 * . string concatenation
4651 *
4652 * "arg" must point to the first non-white of the expression.
4653 * "arg" is advanced to the next non-white after the recognized expression.
4654 *
4655 * Return OK or FAIL.
4656 */
4657 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 int evaluate;
4662{
Bram Moolenaar33570922005-01-25 22:26:29 +00004663 typval_T var2;
4664 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 int op;
4666 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004667#ifdef FEAT_FLOAT
4668 float_T f1 = 0, f2 = 0;
4669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 char_u *s1, *s2;
4671 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4672 char_u *p;
4673
4674 /*
4675 * Get the first variable.
4676 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004677 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 return FAIL;
4679
4680 /*
4681 * Repeat computing, until no '+', '-' or '.' is following.
4682 */
4683 for (;;)
4684 {
4685 op = **arg;
4686 if (op != '+' && op != '-' && op != '.')
4687 break;
4688
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689 if ((op != '+' || rettv->v_type != VAR_LIST)
4690#ifdef FEAT_FLOAT
4691 && (op == '.' || rettv->v_type != VAR_FLOAT)
4692#endif
4693 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004694 {
4695 /* For "list + ...", an illegal use of the first operand as
4696 * a number cannot be determined before evaluating the 2nd
4697 * operand: if this is also a list, all is ok.
4698 * For "something . ...", "something - ..." or "non-list + ...",
4699 * we know that the first operand needs to be a string or number
4700 * without evaluating the 2nd operand. So check before to avoid
4701 * side effects after an error. */
4702 if (evaluate && get_tv_string_chk(rettv) == NULL)
4703 {
4704 clear_tv(rettv);
4705 return FAIL;
4706 }
4707 }
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 /*
4710 * Get the second variable.
4711 */
4712 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004713 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return FAIL;
4717 }
4718
4719 if (evaluate)
4720 {
4721 /*
4722 * Compute the result.
4723 */
4724 if (op == '.')
4725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004726 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4727 s2 = get_tv_string_buf_chk(&var2, buf2);
4728 if (s2 == NULL) /* type error ? */
4729 {
4730 clear_tv(rettv);
4731 clear_tv(&var2);
4732 return FAIL;
4733 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004734 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 clear_tv(rettv);
4736 rettv->v_type = VAR_STRING;
4737 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004739 else if (op == '+' && rettv->v_type == VAR_LIST
4740 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004741 {
4742 /* concatenate Lists */
4743 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4744 &var3) == FAIL)
4745 {
4746 clear_tv(rettv);
4747 clear_tv(&var2);
4748 return FAIL;
4749 }
4750 clear_tv(rettv);
4751 *rettv = var3;
4752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 else
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 int error = FALSE;
4756
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757#ifdef FEAT_FLOAT
4758 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760 f1 = rettv->vval.v_float;
4761 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004763 else
4764#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004766 n1 = get_tv_number_chk(rettv, &error);
4767 if (error)
4768 {
4769 /* This can only happen for "list + non-list". For
4770 * "non-list + ..." or "something - ...", we returned
4771 * before evaluating the 2nd operand. */
4772 clear_tv(rettv);
4773 return FAIL;
4774 }
4775#ifdef FEAT_FLOAT
4776 if (var2.v_type == VAR_FLOAT)
4777 f1 = n1;
4778#endif
4779 }
4780#ifdef FEAT_FLOAT
4781 if (var2.v_type == VAR_FLOAT)
4782 {
4783 f2 = var2.vval.v_float;
4784 n2 = 0;
4785 }
4786 else
4787#endif
4788 {
4789 n2 = get_tv_number_chk(&var2, &error);
4790 if (error)
4791 {
4792 clear_tv(rettv);
4793 clear_tv(&var2);
4794 return FAIL;
4795 }
4796#ifdef FEAT_FLOAT
4797 if (rettv->v_type == VAR_FLOAT)
4798 f2 = n2;
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802
4803#ifdef FEAT_FLOAT
4804 /* If there is a float on either side the result is a float. */
4805 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4806 {
4807 if (op == '+')
4808 f1 = f1 + f2;
4809 else
4810 f1 = f1 - f2;
4811 rettv->v_type = VAR_FLOAT;
4812 rettv->vval.v_float = f1;
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815#endif
4816 {
4817 if (op == '+')
4818 n1 = n1 + n2;
4819 else
4820 n1 = n1 - n2;
4821 rettv->v_type = VAR_NUMBER;
4822 rettv->vval.v_number = n1;
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827 }
4828 return OK;
4829}
4830
4831/*
4832 * Handle fifth level expression:
4833 * * number multiplication
4834 * / number division
4835 * % number modulo
4836 *
4837 * "arg" must point to the first non-white of the expression.
4838 * "arg" is advanced to the next non-white after the recognized expression.
4839 *
4840 * Return OK or FAIL.
4841 */
4842 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004843eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004847 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
Bram Moolenaar33570922005-01-25 22:26:29 +00004849 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 int op;
4851 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004852#ifdef FEAT_FLOAT
4853 int use_float = FALSE;
4854 float_T f1 = 0, f2;
4855#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004856 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
4858 /*
4859 * Get the first variable.
4860 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004861 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return FAIL;
4863
4864 /*
4865 * Repeat computing, until no '*', '/' or '%' is following.
4866 */
4867 for (;;)
4868 {
4869 op = **arg;
4870 if (op != '*' && op != '/' && op != '%')
4871 break;
4872
4873 if (evaluate)
4874 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004875#ifdef FEAT_FLOAT
4876 if (rettv->v_type == VAR_FLOAT)
4877 {
4878 f1 = rettv->vval.v_float;
4879 use_float = TRUE;
4880 n1 = 0;
4881 }
4882 else
4883#endif
4884 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886 if (error)
4887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 else
4890 n1 = 0;
4891
4892 /*
4893 * Get the second variable.
4894 */
4895 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004896 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return FAIL;
4898
4899 if (evaluate)
4900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (var2.v_type == VAR_FLOAT)
4903 {
4904 if (!use_float)
4905 {
4906 f1 = n1;
4907 use_float = TRUE;
4908 }
4909 f2 = var2.vval.v_float;
4910 n2 = 0;
4911 }
4912 else
4913#endif
4914 {
4915 n2 = get_tv_number_chk(&var2, &error);
4916 clear_tv(&var2);
4917 if (error)
4918 return FAIL;
4919#ifdef FEAT_FLOAT
4920 if (use_float)
4921 f2 = n2;
4922#endif
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
4925 /*
4926 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932 if (op == '*')
4933 f1 = f1 * f2;
4934 else if (op == '/')
4935 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004936# ifdef VMS
4937 /* VMS crashes on divide by zero, work around it */
4938 if (f2 == 0.0)
4939 {
4940 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004941 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004942 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004943 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004944 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004945 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004946 }
4947 else
4948 f1 = f1 / f2;
4949# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004950 /* We rely on the floating point library to handle divide
4951 * by zero to result in "inf" and not a crash. */
4952 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004953# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004957 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 return FAIL;
4959 }
4960 rettv->v_type = VAR_FLOAT;
4961 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 if (op == '*')
4967 n1 = n1 * n2;
4968 else if (op == '/')
4969 {
4970 if (n2 == 0) /* give an error message? */
4971 {
4972 if (n1 == 0)
4973 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4974 else if (n1 < 0)
4975 n1 = -0x7fffffffL;
4976 else
4977 n1 = 0x7fffffffL;
4978 }
4979 else
4980 n1 = n1 / n2;
4981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 {
4984 if (n2 == 0) /* give an error message? */
4985 n1 = 0;
4986 else
4987 n1 = n1 % n2;
4988 }
4989 rettv->v_type = VAR_NUMBER;
4990 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 }
4994
4995 return OK;
4996}
4997
4998/*
4999 * Handle sixth level expression:
5000 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005001 * "string" string constant
5002 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 * &option-name option value
5004 * @r register contents
5005 * identifier variable value
5006 * function() function call
5007 * $VAR environment variable
5008 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005009 * [expr, expr] List
5010 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 *
5012 * Also handle:
5013 * ! in front logical NOT
5014 * - in front unary minus
5015 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 * trailing [] subscript in String or List
5017 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 *
5019 * "arg" must point to the first non-white of the expression.
5020 * "arg" is advanced to the next non-white after the recognized expression.
5021 *
5022 * Return OK or FAIL.
5023 */
5024 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005025eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005029 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 long n;
5032 int len;
5033 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 char_u *start_leader, *end_leader;
5035 int ret = OK;
5036 char_u *alias;
5037
5038 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005039 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005040 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005042 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044 /*
5045 * Skip '!' and '-' characters. They are handled later.
5046 */
5047 start_leader = *arg;
5048 while (**arg == '!' || **arg == '-' || **arg == '+')
5049 *arg = skipwhite(*arg + 1);
5050 end_leader = *arg;
5051
5052 switch (**arg)
5053 {
5054 /*
5055 * Number constant.
5056 */
5057 case '0':
5058 case '1':
5059 case '2':
5060 case '3':
5061 case '4':
5062 case '5':
5063 case '6':
5064 case '7':
5065 case '8':
5066 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 {
5068#ifdef FEAT_FLOAT
5069 char_u *p = skipdigits(*arg + 1);
5070 int get_float = FALSE;
5071
5072 /* We accept a float when the format matches
5073 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005074 * strict to avoid backwards compatibility problems.
5075 * Don't look for a float after the "." operator, so that
5076 * ":let vers = 1.2.3" doesn't fail. */
5077 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 get_float = TRUE;
5080 p = skipdigits(p + 2);
5081 if (*p == 'e' || *p == 'E')
5082 {
5083 ++p;
5084 if (*p == '-' || *p == '+')
5085 ++p;
5086 if (!vim_isdigit(*p))
5087 get_float = FALSE;
5088 else
5089 p = skipdigits(p + 1);
5090 }
5091 if (ASCII_ISALPHA(*p) || *p == '.')
5092 get_float = FALSE;
5093 }
5094 if (get_float)
5095 {
5096 float_T f;
5097
5098 *arg += string2float(*arg, &f);
5099 if (evaluate)
5100 {
5101 rettv->v_type = VAR_FLOAT;
5102 rettv->vval.v_float = f;
5103 }
5104 }
5105 else
5106#endif
5107 {
5108 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5109 *arg += len;
5110 if (evaluate)
5111 {
5112 rettv->v_type = VAR_NUMBER;
5113 rettv->vval.v_number = n;
5114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
5119 /*
5120 * String constant: "string".
5121 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 break;
5124
5125 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 break;
5130
5131 /*
5132 * List: [expr, expr]
5133 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136
5137 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 * Dictionary: {key: val, key: val}
5139 */
5140 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5141 break;
5142
5143 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005144 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005146 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148
5149 /*
5150 * Environment variable: $VAR.
5151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154
5155 /*
5156 * Register contents: @r.
5157 */
5158 case '@': ++*arg;
5159 if (evaluate)
5160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005162 rettv->vval.v_string = get_reg_contents(**arg,
5163 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 if (**arg != NUL)
5166 ++*arg;
5167 break;
5168
5169 /*
5170 * nested expression: (expression).
5171 */
5172 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (**arg == ')')
5175 ++*arg;
5176 else if (ret == OK)
5177 {
5178 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 ret = FAIL;
5181 }
5182 break;
5183
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 break;
5186 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187
5188 if (ret == NOTDONE)
5189 {
5190 /*
5191 * Must be a variable or function name.
5192 * Can also be a curly-braces kind of name: {expr}.
5193 */
5194 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005195 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 if (alias != NULL)
5197 s = alias;
5198
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005199 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 ret = FAIL;
5201 else
5202 {
5203 if (**arg == '(') /* recursive! */
5204 {
5205 /* If "s" is the name of a variable of type VAR_FUNC
5206 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005207 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208
5209 /* Invoke the function. */
5210 ret = get_func_tv(s, len, rettv, arg,
5211 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005212 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005213
5214 /* If evaluate is FALSE rettv->v_type was not set in
5215 * get_func_tv, but it's needed in handle_subscript() to parse
5216 * what follows. So set it here. */
5217 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5218 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005219 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005220 rettv->v_type = VAR_FUNC;
5221 }
5222
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 /* Stop the expression evaluation when immediately
5224 * aborting on error, or when an interrupt occurred or
5225 * an exception was thrown but not caught. */
5226 if (aborting())
5227 {
5228 if (ret == OK)
5229 clear_tv(rettv);
5230 ret = FAIL;
5231 }
5232 }
5233 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005234 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005235 else
5236 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005238 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 }
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 *arg = skipwhite(*arg);
5242
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005243 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5244 * expr(expr). */
5245 if (ret == OK)
5246 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248 /*
5249 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5250 */
5251 if (ret == OK && evaluate && end_leader > start_leader)
5252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005253 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005254 int val = 0;
5255#ifdef FEAT_FLOAT
5256 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005258 if (rettv->v_type == VAR_FLOAT)
5259 f = rettv->vval.v_float;
5260 else
5261#endif
5262 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005265 clear_tv(rettv);
5266 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005268 else
5269 {
5270 while (end_leader > start_leader)
5271 {
5272 --end_leader;
5273 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005274 {
5275#ifdef FEAT_FLOAT
5276 if (rettv->v_type == VAR_FLOAT)
5277 f = !f;
5278 else
5279#endif
5280 val = !val;
5281 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 {
5284#ifdef FEAT_FLOAT
5285 if (rettv->v_type == VAR_FLOAT)
5286 f = -f;
5287 else
5288#endif
5289 val = -val;
5290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005292#ifdef FEAT_FLOAT
5293 if (rettv->v_type == VAR_FLOAT)
5294 {
5295 clear_tv(rettv);
5296 rettv->vval.v_float = f;
5297 }
5298 else
5299#endif
5300 {
5301 clear_tv(rettv);
5302 rettv->v_type = VAR_NUMBER;
5303 rettv->vval.v_number = val;
5304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
5307
5308 return ret;
5309}
5310
5311/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005312 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5313 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5315 */
5316 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005317eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005319 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322{
5323 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 long len = -1;
5327 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005329 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005331 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005333 if (verbose)
5334 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 return FAIL;
5336 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005337#ifdef FEAT_FLOAT
5338 else if (rettv->v_type == VAR_FLOAT)
5339 {
5340 if (verbose)
5341 EMSG(_(e_float_as_string));
5342 return FAIL;
5343 }
5344#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 /*
5349 * dict.name
5350 */
5351 key = *arg + 1;
5352 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5353 ;
5354 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 /*
5361 * something[idx]
5362 *
5363 * Get the (first) variable from inside the [].
5364 */
5365 *arg = skipwhite(*arg + 1);
5366 if (**arg == ':')
5367 empty1 = TRUE;
5368 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5369 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005370 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5371 {
5372 /* not a number or string */
5373 clear_tv(&var1);
5374 return FAIL;
5375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376
5377 /*
5378 * Get the second variable from inside the [:].
5379 */
5380 if (**arg == ':')
5381 {
5382 range = TRUE;
5383 *arg = skipwhite(*arg + 1);
5384 if (**arg == ']')
5385 empty2 = TRUE;
5386 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005388 if (!empty1)
5389 clear_tv(&var1);
5390 return FAIL;
5391 }
5392 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5393 {
5394 /* not a number or string */
5395 if (!empty1)
5396 clear_tv(&var1);
5397 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 return FAIL;
5399 }
5400 }
5401
5402 /* Check for the ']'. */
5403 if (**arg != ']')
5404 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005405 if (verbose)
5406 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 clear_tv(&var1);
5408 if (range)
5409 clear_tv(&var2);
5410 return FAIL;
5411 }
5412 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 }
5414
5415 if (evaluate)
5416 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417 n1 = 0;
5418 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 n1 = get_tv_number(&var1);
5421 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 }
5423 if (range)
5424 {
5425 if (empty2)
5426 n2 = -1;
5427 else
5428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 n2 = get_tv_number(&var2);
5430 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 }
5432 }
5433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 {
5436 case VAR_NUMBER:
5437 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 len = (long)STRLEN(s);
5440 if (range)
5441 {
5442 /* The resulting variable is a substring. If the indexes
5443 * are out of range the result is empty. */
5444 if (n1 < 0)
5445 {
5446 n1 = len + n1;
5447 if (n1 < 0)
5448 n1 = 0;
5449 }
5450 if (n2 < 0)
5451 n2 = len + n2;
5452 else if (n2 >= len)
5453 n2 = len;
5454 if (n1 >= len || n2 < 0 || n1 > n2)
5455 s = NULL;
5456 else
5457 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5458 }
5459 else
5460 {
5461 /* The resulting variable is a string of a single
5462 * character. If the index is too big or negative the
5463 * result is empty. */
5464 if (n1 >= len || n1 < 0)
5465 s = NULL;
5466 else
5467 s = vim_strnsave(s + n1, 1);
5468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(rettv);
5470 rettv->v_type = VAR_STRING;
5471 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 break;
5473
5474 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 if (n1 < 0)
5477 n1 = len + n1;
5478 if (!empty1 && (n1 < 0 || n1 >= len))
5479 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005480 /* For a range we allow invalid values and return an empty
5481 * list. A list index out of range is an error. */
5482 if (!range)
5483 {
5484 if (verbose)
5485 EMSGN(_(e_listidx), n1);
5486 return FAIL;
5487 }
5488 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 if (range)
5491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 list_T *l;
5493 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494
5495 if (n2 < 0)
5496 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005497 else if (n2 >= len)
5498 n2 = len - 1;
5499 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005500 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 l = list_alloc();
5502 if (l == NULL)
5503 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 n1 <= n2; ++n1)
5506 {
5507 if (list_append_tv(l, &item->li_tv) == FAIL)
5508 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005509 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 return FAIL;
5511 }
5512 item = item->li_next;
5513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514 clear_tv(rettv);
5515 rettv->v_type = VAR_LIST;
5516 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005517 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 }
5519 else
5520 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005521 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 clear_tv(rettv);
5523 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 }
5525 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526
5527 case VAR_DICT:
5528 if (range)
5529 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005530 if (verbose)
5531 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 if (len == -1)
5533 clear_tv(&var1);
5534 return FAIL;
5535 }
5536 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005538
5539 if (len == -1)
5540 {
5541 key = get_tv_string(&var1);
5542 if (*key == NUL)
5543 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005544 if (verbose)
5545 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 clear_tv(&var1);
5547 return FAIL;
5548 }
5549 }
5550
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005551 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005553 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005554 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 if (len == -1)
5556 clear_tv(&var1);
5557 if (item == NULL)
5558 return FAIL;
5559
5560 copy_tv(&item->di_tv, &var1);
5561 clear_tv(rettv);
5562 *rettv = var1;
5563 }
5564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 }
5567
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 return OK;
5569}
5570
5571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 * Get an option value.
5573 * "arg" points to the '&' or '+' before the option name.
5574 * "arg" is advanced to character after the option name.
5575 * Return OK or FAIL.
5576 */
5577 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 int evaluate;
5582{
5583 char_u *option_end;
5584 long numval;
5585 char_u *stringval;
5586 int opt_type;
5587 int c;
5588 int working = (**arg == '+'); /* has("+option") */
5589 int ret = OK;
5590 int opt_flags;
5591
5592 /*
5593 * Isolate the option name and find its value.
5594 */
5595 option_end = find_option_end(arg, &opt_flags);
5596 if (option_end == NULL)
5597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 EMSG2(_("E112: Option name missing: %s"), *arg);
5600 return FAIL;
5601 }
5602
5603 if (!evaluate)
5604 {
5605 *arg = option_end;
5606 return OK;
5607 }
5608
5609 c = *option_end;
5610 *option_end = NUL;
5611 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 if (opt_type == -3) /* invalid name */
5615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 EMSG2(_("E113: Unknown option: %s"), *arg);
5618 ret = FAIL;
5619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 if (opt_type == -2) /* hidden string option */
5623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 rettv->v_type = VAR_STRING;
5625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else if (opt_type == -1) /* hidden number option */
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 rettv->v_type = VAR_NUMBER;
5630 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
5632 else if (opt_type == 1) /* number option */
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 rettv->v_type = VAR_NUMBER;
5635 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 else /* string option */
5638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 rettv->v_type = VAR_STRING;
5640 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 }
5643 else if (working && (opt_type == -2 || opt_type == -1))
5644 ret = FAIL;
5645
5646 *option_end = c; /* put back for error messages */
5647 *arg = option_end;
5648
5649 return ret;
5650}
5651
5652/*
5653 * Allocate a variable for a string constant.
5654 * Return OK or FAIL.
5655 */
5656 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 int evaluate;
5661{
5662 char_u *p;
5663 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 int extra = 0;
5665
5666 /*
5667 * Find the end of the string, skipping backslashed characters.
5668 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (*p == '\\' && p[1] != NUL)
5672 {
5673 ++p;
5674 /* A "\<x>" form occupies at least 4 characters, and produces up
5675 * to 6 characters: reserve space for 2 extra */
5676 if (*p == '<')
5677 extra += 2;
5678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680
5681 if (*p != '"')
5682 {
5683 EMSG2(_("E114: Missing quote: %s"), *arg);
5684 return FAIL;
5685 }
5686
5687 /* If only parsing, set *arg and return here */
5688 if (!evaluate)
5689 {
5690 *arg = p + 1;
5691 return OK;
5692 }
5693
5694 /*
5695 * Copy the string into allocated memory, handling backslashed
5696 * characters.
5697 */
5698 name = alloc((unsigned)(p - *arg + extra));
5699 if (name == NULL)
5700 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 rettv->v_type = VAR_STRING;
5702 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
5706 if (*p == '\\')
5707 {
5708 switch (*++p)
5709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 case 'b': *name++ = BS; ++p; break;
5711 case 'e': *name++ = ESC; ++p; break;
5712 case 'f': *name++ = FF; ++p; break;
5713 case 'n': *name++ = NL; ++p; break;
5714 case 'r': *name++ = CAR; ++p; break;
5715 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 case 'X': /* hex: "\x1", "\x12" */
5718 case 'x':
5719 case 'u': /* Unicode: "\u0023" */
5720 case 'U':
5721 if (vim_isxdigit(p[1]))
5722 {
5723 int n, nr;
5724 int c = toupper(*p);
5725
5726 if (c == 'X')
5727 n = 2;
5728 else
5729 n = 4;
5730 nr = 0;
5731 while (--n >= 0 && vim_isxdigit(p[1]))
5732 {
5733 ++p;
5734 nr = (nr << 4) + hex2nr(*p);
5735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737#ifdef FEAT_MBYTE
5738 /* For "\u" store the number according to
5739 * 'encoding'. */
5740 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 else
5743#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 break;
5747
5748 /* octal: "\1", "\12", "\123" */
5749 case '0':
5750 case '1':
5751 case '2':
5752 case '3':
5753 case '4':
5754 case '5':
5755 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 case '7': *name = *p++ - '0';
5757 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 *name = (*name << 3) + *p++ - '0';
5760 if (*p >= '0' && *p <= '7')
5761 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 break;
5765
5766 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 if (extra != 0)
5769 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 break;
5772 }
5773 /* FALLTHROUGH */
5774
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 break;
5777 }
5778 }
5779 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 *arg = p + 1;
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 return OK;
5787}
5788
5789/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 * Return OK or FAIL.
5792 */
5793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005794get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 int evaluate;
5798{
5799 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005801 int reduce = 0;
5802
5803 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 */
5806 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 break;
5812 ++reduce;
5813 ++p;
5814 }
5815 }
5816
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 return FAIL;
5821 }
5822
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 if (!evaluate)
5825 {
5826 *arg = p + 1;
5827 return OK;
5828 }
5829
5830 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 */
5833 str = alloc((unsigned)((p - *arg) - reduce));
5834 if (str == NULL)
5835 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 rettv->v_type = VAR_STRING;
5837 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 break;
5845 ++p;
5846 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 *arg = p + 1;
5851
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 return OK;
5853}
5854
5855/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 * Allocate a variable for a List and fill it from "*arg".
5857 * Return OK or FAIL.
5858 */
5859 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863 int evaluate;
5864{
Bram Moolenaar33570922005-01-25 22:26:29 +00005865 list_T *l = NULL;
5866 typval_T tv;
5867 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868
5869 if (evaluate)
5870 {
5871 l = list_alloc();
5872 if (l == NULL)
5873 return FAIL;
5874 }
5875
5876 *arg = skipwhite(*arg + 1);
5877 while (**arg != ']' && **arg != NUL)
5878 {
5879 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5880 goto failret;
5881 if (evaluate)
5882 {
5883 item = listitem_alloc();
5884 if (item != NULL)
5885 {
5886 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005887 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 list_append(l, item);
5889 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005890 else
5891 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 }
5893
5894 if (**arg == ']')
5895 break;
5896 if (**arg != ',')
5897 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 goto failret;
5900 }
5901 *arg = skipwhite(*arg + 1);
5902 }
5903
5904 if (**arg != ']')
5905 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907failret:
5908 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005909 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 return FAIL;
5911 }
5912
5913 *arg = skipwhite(*arg + 1);
5914 if (evaluate)
5915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005916 rettv->v_type = VAR_LIST;
5917 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 ++l->lv_refcount;
5919 }
5920
5921 return OK;
5922}
5923
5924/*
5925 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005926 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005928 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929list_alloc()
5930{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931 list_T *l;
5932
5933 l = (list_T *)alloc_clear(sizeof(list_T));
5934 if (l != NULL)
5935 {
5936 /* Prepend the list to the list of lists for garbage collection. */
5937 if (first_list != NULL)
5938 first_list->lv_used_prev = l;
5939 l->lv_used_prev = NULL;
5940 l->lv_used_next = first_list;
5941 first_list = l;
5942 }
5943 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944}
5945
5946/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005947 * Allocate an empty list for a return value.
5948 * Returns OK or FAIL.
5949 */
5950 static int
5951rettv_list_alloc(rettv)
5952 typval_T *rettv;
5953{
5954 list_T *l = list_alloc();
5955
5956 if (l == NULL)
5957 return FAIL;
5958
5959 rettv->vval.v_list = l;
5960 rettv->v_type = VAR_LIST;
5961 ++l->lv_refcount;
5962 return OK;
5963}
5964
5965/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 * Unreference a list: decrement the reference count and free it when it
5967 * becomes zero.
5968 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005969 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005973 if (l != NULL && --l->lv_refcount <= 0)
5974 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975}
5976
5977/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005978 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 * Ignores the reference count.
5980 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005981 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005982list_free(l, recurse)
5983 list_T *l;
5984 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985{
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005988 /* Remove the list from the list of lists for garbage collection. */
5989 if (l->lv_used_prev == NULL)
5990 first_list = l->lv_used_next;
5991 else
5992 l->lv_used_prev->lv_used_next = l->lv_used_next;
5993 if (l->lv_used_next != NULL)
5994 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5995
Bram Moolenaard9fba312005-06-26 22:34:35 +00005996 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005998 /* Remove the item before deleting it. */
5999 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006000 if (recurse || (item->li_tv.v_type != VAR_LIST
6001 && item->li_tv.v_type != VAR_DICT))
6002 clear_tv(&item->li_tv);
6003 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004 }
6005 vim_free(l);
6006}
6007
6008/*
6009 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006010 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006012 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013listitem_alloc()
6014{
Bram Moolenaar33570922005-01-25 22:26:29 +00006015 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016}
6017
6018/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006019 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006021 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006025 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 vim_free(item);
6027}
6028
6029/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006030 * Remove a list item from a List and free it. Also clears the value.
6031 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006032 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006033listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 list_T *l;
6035 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006036{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006037 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006038 listitem_free(item);
6039}
6040
6041/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 * Get the number of items in a list.
6043 */
6044 static long
6045list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 if (l == NULL)
6049 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006050 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051}
6052
6053/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006054 * Return TRUE when two lists have exactly the same values.
6055 */
6056 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 list_T *l1;
6059 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006060 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006062{
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006064
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006065 if (l1 == NULL || l2 == NULL)
6066 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006067 if (l1 == l2)
6068 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006069 if (list_len(l1) != list_len(l2))
6070 return FALSE;
6071
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006072 for (item1 = l1->lv_first, item2 = l2->lv_first;
6073 item1 != NULL && item2 != NULL;
6074 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006075 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076 return FALSE;
6077 return item1 == NULL && item2 == NULL;
6078}
6079
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006080#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6081 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006082/*
6083 * Return the dictitem that an entry in a hashtable points to.
6084 */
6085 dictitem_T *
6086dict_lookup(hi)
6087 hashitem_T *hi;
6088{
6089 return HI2DI(hi);
6090}
6091#endif
6092
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006094 * Return TRUE when two dictionaries have exactly the same key/values.
6095 */
6096 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006097dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 dict_T *d1;
6099 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006100 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006101 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006102{
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 hashitem_T *hi;
6104 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006105 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006106
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006107 if (d1 == NULL || d2 == NULL)
6108 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (d1 == d2)
6110 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006111 if (dict_len(d1) != dict_len(d2))
6112 return FALSE;
6113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006114 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006116 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006117 if (!HASHITEM_EMPTY(hi))
6118 {
6119 item2 = dict_find(d2, hi->hi_key, -1);
6120 if (item2 == NULL)
6121 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 return FALSE;
6124 --todo;
6125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126 }
6127 return TRUE;
6128}
6129
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006130static int tv_equal_recurse_limit;
6131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006134 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006135 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136 */
6137 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006138tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 typval_T *tv1;
6140 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006141 int ic; /* ignore case */
6142 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143{
6144 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006145 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006147 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006149 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006150 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006151
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006152 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153 * recursiveness to a limit. We guess they are equal then.
6154 * A fixed limit has the problem of still taking an awful long time.
6155 * Reduce the limit every time running into it. That should work fine for
6156 * deeply linked structures that are not recursively linked and catch
6157 * recursiveness quickly. */
6158 if (!recursive)
6159 tv_equal_recurse_limit = 1000;
6160 if (recursive_cnt >= tv_equal_recurse_limit)
6161 {
6162 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006163 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006164 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006165
6166 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006168 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006169 ++recursive_cnt;
6170 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6171 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006172 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006173
6174 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006175 ++recursive_cnt;
6176 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6177 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006178 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179
6180 case VAR_FUNC:
6181 return (tv1->vval.v_string != NULL
6182 && tv2->vval.v_string != NULL
6183 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6184
6185 case VAR_NUMBER:
6186 return tv1->vval.v_number == tv2->vval.v_number;
6187
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006188#ifdef FEAT_FLOAT
6189 case VAR_FLOAT:
6190 return tv1->vval.v_float == tv2->vval.v_float;
6191#endif
6192
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193 case VAR_STRING:
6194 s1 = get_tv_string_buf(tv1, buf1);
6195 s2 = get_tv_string_buf(tv2, buf2);
6196 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198
6199 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200 return TRUE;
6201}
6202
6203/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204 * Locate item with index "n" in list "l" and return it.
6205 * A negative index is counted from the end; -1 is the last item.
6206 * Returns NULL when "n" is out of range.
6207 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006208 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006209list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006211 long n;
6212{
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006214 long idx;
6215
6216 if (l == NULL)
6217 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006218
6219 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006221 n = l->lv_len + n;
6222
6223 /* Check for index out of range. */
6224 if (n < 0 || n >= l->lv_len)
6225 return NULL;
6226
6227 /* When there is a cached index may start search from there. */
6228 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006230 if (n < l->lv_idx / 2)
6231 {
6232 /* closest to the start of the list */
6233 item = l->lv_first;
6234 idx = 0;
6235 }
6236 else if (n > (l->lv_idx + l->lv_len) / 2)
6237 {
6238 /* closest to the end of the list */
6239 item = l->lv_last;
6240 idx = l->lv_len - 1;
6241 }
6242 else
6243 {
6244 /* closest to the cached index */
6245 item = l->lv_idx_item;
6246 idx = l->lv_idx;
6247 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 }
6249 else
6250 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006251 if (n < l->lv_len / 2)
6252 {
6253 /* closest to the start of the list */
6254 item = l->lv_first;
6255 idx = 0;
6256 }
6257 else
6258 {
6259 /* closest to the end of the list */
6260 item = l->lv_last;
6261 idx = l->lv_len - 1;
6262 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264
6265 while (n > idx)
6266 {
6267 /* search forward */
6268 item = item->li_next;
6269 ++idx;
6270 }
6271 while (n < idx)
6272 {
6273 /* search backward */
6274 item = item->li_prev;
6275 --idx;
6276 }
6277
6278 /* cache the used index */
6279 l->lv_idx = idx;
6280 l->lv_idx_item = item;
6281
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282 return item;
6283}
6284
6285/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006286 * Get list item "l[idx]" as a number.
6287 */
6288 static long
6289list_find_nr(l, idx, errorp)
6290 list_T *l;
6291 long idx;
6292 int *errorp; /* set to TRUE when something wrong */
6293{
6294 listitem_T *li;
6295
6296 li = list_find(l, idx);
6297 if (li == NULL)
6298 {
6299 if (errorp != NULL)
6300 *errorp = TRUE;
6301 return -1L;
6302 }
6303 return get_tv_number_chk(&li->li_tv, errorp);
6304}
6305
6306/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006307 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6308 */
6309 char_u *
6310list_find_str(l, idx)
6311 list_T *l;
6312 long idx;
6313{
6314 listitem_T *li;
6315
6316 li = list_find(l, idx - 1);
6317 if (li == NULL)
6318 {
6319 EMSGN(_(e_listidx), idx);
6320 return NULL;
6321 }
6322 return get_tv_string(&li->li_tv);
6323}
6324
6325/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006326 * Locate "item" list "l" and return its index.
6327 * Returns -1 when "item" is not in the list.
6328 */
6329 static long
6330list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 list_T *l;
6332 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006333{
6334 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006336
6337 if (l == NULL)
6338 return -1;
6339 idx = 0;
6340 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6341 ++idx;
6342 if (li == NULL)
6343 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006344 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006345}
6346
6347/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 * Append item "item" to the end of list "l".
6349 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006350 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 list_T *l;
6353 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354{
6355 if (l->lv_last == NULL)
6356 {
6357 /* empty list */
6358 l->lv_first = item;
6359 l->lv_last = item;
6360 item->li_prev = NULL;
6361 }
6362 else
6363 {
6364 l->lv_last->li_next = item;
6365 item->li_prev = l->lv_last;
6366 l->lv_last = item;
6367 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006368 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369 item->li_next = NULL;
6370}
6371
6372/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006376 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 list_T *l;
6379 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006381 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382
Bram Moolenaar05159a02005-02-26 23:04:13 +00006383 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006385 copy_tv(tv, &li->li_tv);
6386 list_append(l, li);
6387 return OK;
6388}
6389
6390/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006391 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006392 * Return FAIL when out of memory.
6393 */
6394 int
6395list_append_dict(list, dict)
6396 list_T *list;
6397 dict_T *dict;
6398{
6399 listitem_T *li = listitem_alloc();
6400
6401 if (li == NULL)
6402 return FAIL;
6403 li->li_tv.v_type = VAR_DICT;
6404 li->li_tv.v_lock = 0;
6405 li->li_tv.vval.v_dict = dict;
6406 list_append(list, li);
6407 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408 return OK;
6409}
6410
6411/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006412 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006413 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006414 * Returns FAIL when out of memory.
6415 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006416 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006417list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006418 list_T *l;
6419 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006420 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006421{
6422 listitem_T *li = listitem_alloc();
6423
6424 if (li == NULL)
6425 return FAIL;
6426 list_append(l, li);
6427 li->li_tv.v_type = VAR_STRING;
6428 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006429 if (str == NULL)
6430 li->li_tv.vval.v_string = NULL;
6431 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006432 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006433 return FAIL;
6434 return OK;
6435}
6436
6437/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006438 * Append "n" to list "l".
6439 * Returns FAIL when out of memory.
6440 */
6441 static int
6442list_append_number(l, n)
6443 list_T *l;
6444 varnumber_T n;
6445{
6446 listitem_T *li;
6447
6448 li = listitem_alloc();
6449 if (li == NULL)
6450 return FAIL;
6451 li->li_tv.v_type = VAR_NUMBER;
6452 li->li_tv.v_lock = 0;
6453 li->li_tv.vval.v_number = n;
6454 list_append(l, li);
6455 return OK;
6456}
6457
6458/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 * If "item" is NULL append at the end.
6461 * Return FAIL when out of memory.
6462 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006463 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 list_T *l;
6466 typval_T *tv;
6467 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468{
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470
6471 if (ni == NULL)
6472 return FAIL;
6473 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006474 list_insert(l, ni, item);
6475 return OK;
6476}
6477
6478 void
6479list_insert(l, ni, item)
6480 list_T *l;
6481 listitem_T *ni;
6482 listitem_T *item;
6483{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 if (item == NULL)
6485 /* Append new item at end of list. */
6486 list_append(l, ni);
6487 else
6488 {
6489 /* Insert new item before existing item. */
6490 ni->li_prev = item->li_prev;
6491 ni->li_next = item;
6492 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006493 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006495 ++l->lv_idx;
6496 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006498 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006500 l->lv_idx_item = NULL;
6501 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006503 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505}
6506
6507/*
6508 * Extend "l1" with "l2".
6509 * If "bef" is NULL append at the end, otherwise insert before this item.
6510 * Returns FAIL when out of memory.
6511 */
6512 static int
6513list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 list_T *l1;
6515 list_T *l2;
6516 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006519 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006521 /* We also quit the loop when we have inserted the original item count of
6522 * the list, avoid a hang when we extend a list with itself. */
6523 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6525 return FAIL;
6526 return OK;
6527}
6528
6529/*
6530 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6531 * Return FAIL when out of memory.
6532 */
6533 static int
6534list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006535 list_T *l1;
6536 list_T *l2;
6537 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538{
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006541 if (l1 == NULL || l2 == NULL)
6542 return FAIL;
6543
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (l == NULL)
6547 return FAIL;
6548 tv->v_type = VAR_LIST;
6549 tv->vval.v_list = l;
6550
6551 /* append all items from the second list */
6552 return list_extend(l, l2, NULL);
6553}
6554
6555/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 * Returns NULL when out of memory.
6560 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 list_T *copy;
6568 listitem_T *item;
6569 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570
6571 if (orig == NULL)
6572 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573
6574 copy = list_alloc();
6575 if (copy != NULL)
6576 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577 if (copyID != 0)
6578 {
6579 /* Do this before adding the items, because one of the items may
6580 * refer back to this list. */
6581 orig->lv_copyID = copyID;
6582 orig->lv_copylist = copy;
6583 }
6584 for (item = orig->lv_first; item != NULL && !got_int;
6585 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 {
6587 ni = listitem_alloc();
6588 if (ni == NULL)
6589 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006590 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 {
6592 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6593 {
6594 vim_free(ni);
6595 break;
6596 }
6597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006599 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 list_append(copy, ni);
6601 }
6602 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 if (item != NULL)
6604 {
6605 list_unref(copy);
6606 copy = NULL;
6607 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 }
6609
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 return copy;
6611}
6612
6613/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006614 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006615 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006616 * This used to be called list_remove, but that conflicts with a Sun header
6617 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006619 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006620vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006621 list_T *l;
6622 listitem_T *item;
6623 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624{
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006627 /* notify watchers */
6628 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006630 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 list_fix_watch(l, ip);
6632 if (ip == item2)
6633 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635
6636 if (item2->li_next == NULL)
6637 l->lv_last = item->li_prev;
6638 else
6639 item2->li_next->li_prev = item->li_prev;
6640 if (item->li_prev == NULL)
6641 l->lv_first = item2->li_next;
6642 else
6643 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006644 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645}
6646
6647/*
6648 * Return an allocated string with the string representation of a list.
6649 * May return NULL.
6650 */
6651 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006652list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006653 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006654 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655{
6656 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657
6658 if (tv->vval.v_list == NULL)
6659 return NULL;
6660 ga_init2(&ga, (int)sizeof(char), 80);
6661 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006662 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006663 {
6664 vim_free(ga.ga_data);
6665 return NULL;
6666 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 ga_append(&ga, ']');
6668 ga_append(&ga, NUL);
6669 return (char_u *)ga.ga_data;
6670}
6671
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006672typedef struct join_S {
6673 char_u *s;
6674 char_u *tofree;
6675} join_T;
6676
6677 static int
6678list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6679 garray_T *gap; /* to store the result in */
6680 list_T *l;
6681 char_u *sep;
6682 int echo_style;
6683 int copyID;
6684 garray_T *join_gap; /* to keep each list item string */
6685{
6686 int i;
6687 join_T *p;
6688 int len;
6689 int sumlen = 0;
6690 int first = TRUE;
6691 char_u *tofree;
6692 char_u numbuf[NUMBUFLEN];
6693 listitem_T *item;
6694 char_u *s;
6695
6696 /* Stringify each item in the list. */
6697 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6698 {
6699 if (echo_style)
6700 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6701 else
6702 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6703 if (s == NULL)
6704 return FAIL;
6705
6706 len = (int)STRLEN(s);
6707 sumlen += len;
6708
6709 ga_grow(join_gap, 1);
6710 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6711 if (tofree != NULL || s != numbuf)
6712 {
6713 p->s = s;
6714 p->tofree = tofree;
6715 }
6716 else
6717 {
6718 p->s = vim_strnsave(s, len);
6719 p->tofree = p->s;
6720 }
6721
6722 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006723 if (did_echo_string_emsg) /* recursion error, bail out */
6724 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 }
6726
6727 /* Allocate result buffer with its total size, avoid re-allocation and
6728 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6729 if (join_gap->ga_len >= 2)
6730 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6731 if (ga_grow(gap, sumlen + 2) == FAIL)
6732 return FAIL;
6733
6734 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6735 {
6736 if (first)
6737 first = FALSE;
6738 else
6739 ga_concat(gap, sep);
6740 p = ((join_T *)join_gap->ga_data) + i;
6741
6742 if (p->s != NULL)
6743 ga_concat(gap, p->s);
6744 line_breakcheck();
6745 }
6746
6747 return OK;
6748}
6749
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006751 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006752 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006753 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006754 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006755 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006756list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006757 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006758 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006760 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006761 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006763 garray_T join_ga;
6764 int retval;
6765 join_T *p;
6766 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006767
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006768 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6769 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6770
6771 /* Dispose each item in join_ga. */
6772 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 p = (join_T *)join_ga.ga_data;
6775 for (i = 0; i < join_ga.ga_len; ++i)
6776 {
6777 vim_free(p->tofree);
6778 ++p;
6779 }
6780 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006782
6783 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784}
6785
6786/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787 * Garbage collection for lists and dictionaries.
6788 *
6789 * We use reference counts to be able to free most items right away when they
6790 * are no longer used. But for composite items it's possible that it becomes
6791 * unused while the reference count is > 0: When there is a recursive
6792 * reference. Example:
6793 * :let l = [1, 2, 3]
6794 * :let d = {9: l}
6795 * :let l[1] = d
6796 *
6797 * Since this is quite unusual we handle this with garbage collection: every
6798 * once in a while find out which lists and dicts are not referenced from any
6799 * variable.
6800 *
6801 * Here is a good reference text about garbage collection (refers to Python
6802 * but it applies to all reference-counting mechanisms):
6803 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006804 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006805
6806/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 * Do garbage collection for lists and dicts.
6808 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006809 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810 int
6811garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006812{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006813 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006814 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815 buf_T *buf;
6816 win_T *wp;
6817 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006818 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006819 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006820 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006821#ifdef FEAT_WINDOWS
6822 tabpage_T *tp;
6823#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006824
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006825 /* Only do this once. */
6826 want_garbage_collect = FALSE;
6827 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006828 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006829
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006830 /* We advance by two because we add one for items referenced through
6831 * previous_funccal. */
6832 current_copyID += COPYID_INC;
6833 copyID = current_copyID;
6834
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 /*
6836 * 1. Go through all accessible variables and mark all lists and dicts
6837 * with copyID.
6838 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006839
6840 /* Don't free variables in the previous_funccal list unless they are only
6841 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006842 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006843 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6844 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006845 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6846 NULL);
6847 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6848 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006849 }
6850
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 /* script-local variables */
6852 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006853 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854
6855 /* buffer-local variables */
6856 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006857 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6858 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859
6860 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006861 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6863 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006864#ifdef FEAT_AUTOCMD
6865 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006866 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6867 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006868#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006870#ifdef FEAT_WINDOWS
6871 /* tabpage-local variables */
6872 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006873 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6874 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006875#endif
6876
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006878 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879
6880 /* function-local variables */
6881 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6882 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6884 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 }
6886
Bram Moolenaard812df62008-11-09 12:46:09 +00006887 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006889
Bram Moolenaar1dced572012-04-05 16:54:08 +02006890#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006891 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006892#endif
6893
Bram Moolenaardb913952012-06-29 12:54:53 +02006894#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006896#endif
6897
6898#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006900#endif
6901
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006903 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 /*
6905 * 2. Free lists and dictionaries that are not referenced.
6906 */
6907 did_free = free_unref_items(copyID);
6908
6909 /*
6910 * 3. Check if any funccal can be freed now.
6911 */
6912 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006913 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 if (can_free_funccal(*pfc, copyID))
6915 {
6916 fc = *pfc;
6917 *pfc = fc->caller;
6918 free_funccal(fc, TRUE);
6919 did_free = TRUE;
6920 did_free_funccal = TRUE;
6921 }
6922 else
6923 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006924 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 if (did_free_funccal)
6926 /* When a funccal was freed some more items might be garbage
6927 * collected, so run again. */
6928 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006929 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 else if (p_verbose > 0)
6931 {
6932 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6933 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934
6935 return did_free;
6936}
6937
6938/*
6939 * Free lists and dictionaries that are no longer referenced.
6940 */
6941 static int
6942free_unref_items(copyID)
6943 int copyID;
6944{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006945 dict_T *dd, *dd_next;
6946 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 int did_free = FALSE;
6948
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 */
6952 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006953 {
6954 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006957 /* Free the Dictionary and ordinary items it contains, but don't
6958 * recurse into Lists and Dictionaries, they will be in the list
6959 * of dicts or list of lists. */
6960 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006963 dd = dd_next;
6964 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965
6966 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 * Go through the list of lists and free items without the copyID.
6968 * But don't free a list that has a watcher (used in a for loop), these
6969 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970 */
6971 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006972 {
6973 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6975 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006977 /* Free the List and ordinary items it contains, but don't recurse
6978 * into Lists and Dictionaries, they will be in the list of dicts
6979 * or list of lists. */
6980 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006983 ll = ll_next;
6984 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 return did_free;
6986}
6987
6988/*
6989 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006990 * "list_stack" is used to add lists to be marked. Can be NULL.
6991 *
6992 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 int
6995set_ref_in_ht(ht, copyID, list_stack)
6996 hashtab_T *ht;
6997 int copyID;
6998 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999{
7000 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007001 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 hashtab_T *cur_ht;
7004 ht_stack_T *ht_stack = NULL;
7005 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 cur_ht = ht;
7008 for (;;)
7009 {
7010 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 /* Mark each item in the hashtab. If the item contains a hashtab
7013 * it is added to ht_stack, if it contains a list it is added to
7014 * list_stack. */
7015 todo = (int)cur_ht->ht_used;
7016 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7017 if (!HASHITEM_EMPTY(hi))
7018 {
7019 --todo;
7020 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7021 &ht_stack, list_stack);
7022 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024
7025 if (ht_stack == NULL)
7026 break;
7027
7028 /* take an item from the stack */
7029 cur_ht = ht_stack->ht;
7030 tempitem = ht_stack;
7031 ht_stack = ht_stack->prev;
7032 free(tempitem);
7033 }
7034
7035 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036}
7037
7038/*
7039 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7041 *
7042 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 int
7045set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 list_T *l;
7047 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 listitem_T *li;
7051 int abort = FALSE;
7052 list_T *cur_l;
7053 list_stack_T *list_stack = NULL;
7054 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 cur_l = l;
7057 for (;;)
7058 {
7059 if (!abort)
7060 /* Mark each item in the list. If the item contains a hashtab
7061 * it is added to ht_stack, if it contains a list it is added to
7062 * list_stack. */
7063 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7064 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7065 ht_stack, &list_stack);
7066 if (list_stack == NULL)
7067 break;
7068
7069 /* take an item from the stack */
7070 cur_l = list_stack->list;
7071 tempitem = list_stack;
7072 list_stack = list_stack->prev;
7073 free(tempitem);
7074 }
7075
7076 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077}
7078
7079/*
7080 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 * "list_stack" is used to add lists to be marked. Can be NULL.
7082 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7083 *
7084 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007086 int
7087set_ref_in_item(tv, copyID, ht_stack, list_stack)
7088 typval_T *tv;
7089 int copyID;
7090 ht_stack_T **ht_stack;
7091 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092{
7093 dict_T *dd;
7094 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007096
7097 switch (tv->v_type)
7098 {
7099 case VAR_DICT:
7100 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007101 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007102 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103 /* Didn't see this dict yet. */
7104 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 if (ht_stack == NULL)
7106 {
7107 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7108 }
7109 else
7110 {
7111 ht_stack_T *newitem = (ht_stack_T*)malloc(
7112 sizeof(ht_stack_T));
7113 if (newitem == NULL)
7114 abort = TRUE;
7115 else
7116 {
7117 newitem->ht = &dd->dv_hashtab;
7118 newitem->prev = *ht_stack;
7119 *ht_stack = newitem;
7120 }
7121 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007124
7125 case VAR_LIST:
7126 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007127 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007129 /* Didn't see this list yet. */
7130 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 if (list_stack == NULL)
7132 {
7133 abort = set_ref_in_list(ll, copyID, ht_stack);
7134 }
7135 else
7136 {
7137 list_stack_T *newitem = (list_stack_T*)malloc(
7138 sizeof(list_stack_T));
7139 if (newitem == NULL)
7140 abort = TRUE;
7141 else
7142 {
7143 newitem->list = ll;
7144 newitem->prev = *list_stack;
7145 *list_stack = newitem;
7146 }
7147 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007148 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007149 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007150 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007151 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152}
7153
7154/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155 * Allocate an empty header for a dictionary.
7156 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007157 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158dict_alloc()
7159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007164 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007165 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007166 if (first_dict != NULL)
7167 first_dict->dv_used_prev = d;
7168 d->dv_used_next = first_dict;
7169 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007170 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007171
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007173 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007174 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007175 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007177 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007178 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179}
7180
7181/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007182 * Allocate an empty dict for a return value.
7183 * Returns OK or FAIL.
7184 */
7185 static int
7186rettv_dict_alloc(rettv)
7187 typval_T *rettv;
7188{
7189 dict_T *d = dict_alloc();
7190
7191 if (d == NULL)
7192 return FAIL;
7193
7194 rettv->vval.v_dict = d;
7195 rettv->v_type = VAR_DICT;
7196 ++d->dv_refcount;
7197 return OK;
7198}
7199
7200
7201/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 * Unreference a Dictionary: decrement the reference count and free it when it
7203 * becomes zero.
7204 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007205 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007209 if (d != NULL && --d->dv_refcount <= 0)
7210 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211}
7212
7213/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007214 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 * Ignores the reference count.
7216 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007217 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007218dict_free(d, recurse)
7219 dict_T *d;
7220 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007224 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007226 /* Remove the dict from the list of dicts for garbage collection. */
7227 if (d->dv_used_prev == NULL)
7228 first_dict = d->dv_used_next;
7229 else
7230 d->dv_used_prev->dv_used_next = d->dv_used_next;
7231 if (d->dv_used_next != NULL)
7232 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7233
7234 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007235 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007236 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 if (!HASHITEM_EMPTY(hi))
7240 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007241 /* Remove the item before deleting it, just in case there is
7242 * something recursive causing trouble. */
7243 di = HI2DI(hi);
7244 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007245 if (recurse || (di->di_tv.v_type != VAR_LIST
7246 && di->di_tv.v_type != VAR_DICT))
7247 clear_tv(&di->di_tv);
7248 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 --todo;
7250 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 vim_free(d);
7254}
7255
7256/*
7257 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 * The "key" is copied to the new item.
7259 * Note that the value of the item "di_tv" still needs to be initialized!
7260 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007262 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263dictitem_alloc(key)
7264 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265{
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007268 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007272 di->di_flags = 0;
7273 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275}
7276
7277/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 * Make a copy of a Dictionary item.
7279 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283{
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7287 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007288 if (di != NULL)
7289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007291 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 copy_tv(&org->di_tv, &di->di_tv);
7293 }
7294 return di;
7295}
7296
7297/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 * Remove item "item" from Dictionary "dict" and free it.
7299 */
7300 static void
7301dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 dict_T *dict;
7303 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304{
Bram Moolenaar33570922005-01-25 22:26:29 +00007305 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 if (HASHITEM_EMPTY(hi))
7309 EMSG2(_(e_intern2), "dictitem_remove()");
7310 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 dictitem_free(item);
7313}
7314
7315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316 * Free a dict item. Also clears the value.
7317 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007318 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 clear_tv(&item->di_tv);
7323 vim_free(item);
7324}
7325
7326/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7328 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007329 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330 * Returns NULL when out of memory.
7331 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007333dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007336 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337{
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 dict_T *copy;
7339 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342
7343 if (orig == NULL)
7344 return NULL;
7345
7346 copy = dict_alloc();
7347 if (copy != NULL)
7348 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007349 if (copyID != 0)
7350 {
7351 orig->dv_copyID = copyID;
7352 orig->dv_copydict = copy;
7353 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007354 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007355 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007356 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 --todo;
7360
7361 di = dictitem_alloc(hi->hi_key);
7362 if (di == NULL)
7363 break;
7364 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007365 {
7366 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7367 copyID) == FAIL)
7368 {
7369 vim_free(di);
7370 break;
7371 }
7372 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 else
7374 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7375 if (dict_add(copy, di) == FAIL)
7376 {
7377 dictitem_free(di);
7378 break;
7379 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007384 if (todo > 0)
7385 {
7386 dict_unref(copy);
7387 copy = NULL;
7388 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 }
7390
7391 return copy;
7392}
7393
7394/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007396 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007398 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 dict_T *d;
7401 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402{
Bram Moolenaar33570922005-01-25 22:26:29 +00007403 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404}
7405
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007407 * Add a number or string entry to dictionary "d".
7408 * When "str" is NULL use number "nr", otherwise use "str".
7409 * Returns FAIL when out of memory and when key already exists.
7410 */
7411 int
7412dict_add_nr_str(d, key, nr, str)
7413 dict_T *d;
7414 char *key;
7415 long nr;
7416 char_u *str;
7417{
7418 dictitem_T *item;
7419
7420 item = dictitem_alloc((char_u *)key);
7421 if (item == NULL)
7422 return FAIL;
7423 item->di_tv.v_lock = 0;
7424 if (str == NULL)
7425 {
7426 item->di_tv.v_type = VAR_NUMBER;
7427 item->di_tv.vval.v_number = nr;
7428 }
7429 else
7430 {
7431 item->di_tv.v_type = VAR_STRING;
7432 item->di_tv.vval.v_string = vim_strsave(str);
7433 }
7434 if (dict_add(d, item) == FAIL)
7435 {
7436 dictitem_free(item);
7437 return FAIL;
7438 }
7439 return OK;
7440}
7441
7442/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007443 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007444 * Returns FAIL when out of memory and when key already exists.
7445 */
7446 int
7447dict_add_list(d, key, list)
7448 dict_T *d;
7449 char *key;
7450 list_T *list;
7451{
7452 dictitem_T *item;
7453
7454 item = dictitem_alloc((char_u *)key);
7455 if (item == NULL)
7456 return FAIL;
7457 item->di_tv.v_lock = 0;
7458 item->di_tv.v_type = VAR_LIST;
7459 item->di_tv.vval.v_list = list;
7460 if (dict_add(d, item) == FAIL)
7461 {
7462 dictitem_free(item);
7463 return FAIL;
7464 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007465 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007466 return OK;
7467}
7468
7469/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 * Get the number of items in a Dictionary.
7471 */
7472 static long
7473dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 if (d == NULL)
7477 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007478 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479}
7480
7481/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 * Find item "key[len]" in Dictionary "d".
7483 * If "len" is negative use strlen(key).
7484 * Returns NULL when not found.
7485 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007486 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007487dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 char_u *key;
7490 int len;
7491{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492#define AKEYLEN 200
7493 char_u buf[AKEYLEN];
7494 char_u *akey;
7495 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 if (len < 0)
7499 akey = key;
7500 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007501 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 tofree = akey = vim_strnsave(key, len);
7503 if (akey == NULL)
7504 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007505 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 else
7507 {
7508 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007509 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 akey = buf;
7511 }
7512
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 vim_free(tofree);
7515 if (HASHITEM_EMPTY(hi))
7516 return NULL;
7517 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518}
7519
7520/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007521 * Get a string item from a dictionary.
7522 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007523 * Returns NULL if the entry doesn't exist or out of memory.
7524 */
7525 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007526get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527 dict_T *d;
7528 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007529 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007530{
7531 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007533
7534 di = dict_find(d, key, -1);
7535 if (di == NULL)
7536 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007537 s = get_tv_string(&di->di_tv);
7538 if (save && s != NULL)
7539 s = vim_strsave(s);
7540 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541}
7542
7543/*
7544 * Get a number item from a dictionary.
7545 * Returns 0 if the entry doesn't exist or out of memory.
7546 */
7547 long
7548get_dict_number(d, key)
7549 dict_T *d;
7550 char_u *key;
7551{
7552 dictitem_T *di;
7553
7554 di = dict_find(d, key, -1);
7555 if (di == NULL)
7556 return 0;
7557 return get_tv_number(&di->di_tv);
7558}
7559
7560/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 * Return an allocated string with the string representation of a Dictionary.
7562 * May return NULL.
7563 */
7564 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007565dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568{
7569 garray_T ga;
7570 int first = TRUE;
7571 char_u *tofree;
7572 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007576 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 return NULL;
7580 ga_init2(&ga, (int)sizeof(char), 80);
7581 ga_append(&ga, '{');
7582
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007583 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007584 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 --todo;
7589
7590 if (first)
7591 first = FALSE;
7592 else
7593 ga_concat(&ga, (char_u *)", ");
7594
7595 tofree = string_quote(hi->hi_key, FALSE);
7596 if (tofree != NULL)
7597 {
7598 ga_concat(&ga, tofree);
7599 vim_free(tofree);
7600 }
7601 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007602 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007603 if (s != NULL)
7604 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007606 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007607 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007608 line_breakcheck();
7609
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007612 if (todo > 0)
7613 {
7614 vim_free(ga.ga_data);
7615 return NULL;
7616 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007617
7618 ga_append(&ga, '}');
7619 ga_append(&ga, NUL);
7620 return (char_u *)ga.ga_data;
7621}
7622
7623/*
7624 * Allocate a variable for a Dictionary and fill it from "*arg".
7625 * Return OK or FAIL. Returns NOTDONE for {expr}.
7626 */
7627 static int
7628get_dict_tv(arg, rettv, evaluate)
7629 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 int evaluate;
7632{
Bram Moolenaar33570922005-01-25 22:26:29 +00007633 dict_T *d = NULL;
7634 typval_T tvkey;
7635 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007636 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007637 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640
7641 /*
7642 * First check if it's not a curly-braces thing: {expr}.
7643 * Must do this without evaluating, otherwise a function may be called
7644 * twice. Unfortunately this means we need to call eval1() twice for the
7645 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007646 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 if (*start != '}')
7649 {
7650 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7651 return FAIL;
7652 if (*start == '}')
7653 return NOTDONE;
7654 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655
7656 if (evaluate)
7657 {
7658 d = dict_alloc();
7659 if (d == NULL)
7660 return FAIL;
7661 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 tvkey.v_type = VAR_UNKNOWN;
7663 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664
7665 *arg = skipwhite(*arg + 1);
7666 while (**arg != '}' && **arg != NUL)
7667 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007668 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 goto failret;
7670 if (**arg != ':')
7671 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007672 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007673 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 goto failret;
7675 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007676 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007678 key = get_tv_string_buf_chk(&tvkey, buf);
7679 if (key == NULL || *key == NUL)
7680 {
7681 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7682 if (key != NULL)
7683 EMSG(_(e_emptykey));
7684 clear_tv(&tvkey);
7685 goto failret;
7686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688
7689 *arg = skipwhite(*arg + 1);
7690 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7691 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007692 if (evaluate)
7693 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 goto failret;
7695 }
7696 if (evaluate)
7697 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 if (item != NULL)
7700 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007701 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007702 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 clear_tv(&tv);
7704 goto failret;
7705 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007706 item = dictitem_alloc(key);
7707 clear_tv(&tvkey);
7708 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007711 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007712 if (dict_add(d, item) == FAIL)
7713 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 }
7715 }
7716
7717 if (**arg == '}')
7718 break;
7719 if (**arg != ',')
7720 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007721 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 goto failret;
7723 }
7724 *arg = skipwhite(*arg + 1);
7725 }
7726
7727 if (**arg != '}')
7728 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007729 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730failret:
7731 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007732 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 return FAIL;
7734 }
7735
7736 *arg = skipwhite(*arg + 1);
7737 if (evaluate)
7738 {
7739 rettv->v_type = VAR_DICT;
7740 rettv->vval.v_dict = d;
7741 ++d->dv_refcount;
7742 }
7743
7744 return OK;
7745}
7746
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007748 * Return a string with the string representation of a variable.
7749 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007750 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007751 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007752 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007753 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007754 */
7755 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007756echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007758 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007759 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007760 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007762 static int recurse = 0;
7763 char_u *r = NULL;
7764
Bram Moolenaar33570922005-01-25 22:26:29 +00007765 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007766 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007767 if (!did_echo_string_emsg)
7768 {
7769 /* Only give this message once for a recursive call to avoid
7770 * flooding the user with errors. And stop iterating over lists
7771 * and dicts. */
7772 did_echo_string_emsg = TRUE;
7773 EMSG(_("E724: variable nested too deep for displaying"));
7774 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007775 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007776 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007777 }
7778 ++recurse;
7779
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007780 switch (tv->v_type)
7781 {
7782 case VAR_FUNC:
7783 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 r = tv->vval.v_string;
7785 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007786
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007787 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007788 if (tv->vval.v_list == NULL)
7789 {
7790 *tofree = NULL;
7791 r = NULL;
7792 }
7793 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7794 {
7795 *tofree = NULL;
7796 r = (char_u *)"[...]";
7797 }
7798 else
7799 {
7800 tv->vval.v_list->lv_copyID = copyID;
7801 *tofree = list2string(tv, copyID);
7802 r = *tofree;
7803 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007804 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007805
Bram Moolenaar8c711452005-01-14 21:53:12 +00007806 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007807 if (tv->vval.v_dict == NULL)
7808 {
7809 *tofree = NULL;
7810 r = NULL;
7811 }
7812 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7813 {
7814 *tofree = NULL;
7815 r = (char_u *)"{...}";
7816 }
7817 else
7818 {
7819 tv->vval.v_dict->dv_copyID = copyID;
7820 *tofree = dict2string(tv, copyID);
7821 r = *tofree;
7822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825 case VAR_STRING:
7826 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 *tofree = NULL;
7828 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007830
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007831#ifdef FEAT_FLOAT
7832 case VAR_FLOAT:
7833 *tofree = NULL;
7834 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7835 r = numbuf;
7836 break;
7837#endif
7838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007840 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843
Bram Moolenaar8502c702014-06-17 12:51:16 +02007844 if (--recurse == 0)
7845 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007846 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007847}
7848
7849/*
7850 * Return a string with the string representation of a variable.
7851 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7852 * "numbuf" is used for a number.
7853 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007854 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007855 */
7856 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007857tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007859 char_u **tofree;
7860 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007861 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007862{
7863 switch (tv->v_type)
7864 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007865 case VAR_FUNC:
7866 *tofree = string_quote(tv->vval.v_string, TRUE);
7867 return *tofree;
7868 case VAR_STRING:
7869 *tofree = string_quote(tv->vval.v_string, FALSE);
7870 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 case VAR_FLOAT:
7873 *tofree = NULL;
7874 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7875 return numbuf;
7876#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007879 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007880 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007881 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007883 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007884 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007885}
7886
7887/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 * Return string "str" in ' quotes, doubling ' characters.
7889 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007890 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 */
7892 static char_u *
7893string_quote(str, function)
7894 char_u *str;
7895 int function;
7896{
Bram Moolenaar33570922005-01-25 22:26:29 +00007897 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898 char_u *p, *r, *s;
7899
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 len = (function ? 13 : 3);
7901 if (str != NULL)
7902 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007903 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 for (p = str; *p != NUL; mb_ptr_adv(p))
7905 if (*p == '\'')
7906 ++len;
7907 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007908 s = r = alloc(len);
7909 if (r != NULL)
7910 {
7911 if (function)
7912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914 r += 10;
7915 }
7916 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 if (str != NULL)
7919 for (p = str; *p != NUL; )
7920 {
7921 if (*p == '\'')
7922 *r++ = '\'';
7923 MB_COPY_CHAR(p, r);
7924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 if (function)
7927 *r++ = ')';
7928 *r++ = NUL;
7929 }
7930 return s;
7931}
7932
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007933#ifdef FEAT_FLOAT
7934/*
7935 * Convert the string "text" to a floating point number.
7936 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7937 * this always uses a decimal point.
7938 * Returns the length of the text that was consumed.
7939 */
7940 static int
7941string2float(text, value)
7942 char_u *text;
7943 float_T *value; /* result stored here */
7944{
7945 char *s = (char *)text;
7946 float_T f;
7947
7948 f = strtod(s, &s);
7949 *value = f;
7950 return (int)((char_u *)s - text);
7951}
7952#endif
7953
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 * Get the value of an environment variable.
7956 * "arg" is pointing to the '$'. It is advanced to after the name.
7957 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007958 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 */
7960 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 int evaluate;
7965{
7966 char_u *string = NULL;
7967 int len;
7968 int cc;
7969 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007970 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971
7972 ++*arg;
7973 name = *arg;
7974 len = get_env_len(arg);
7975 if (evaluate)
7976 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007977 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007978 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007979
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007980 cc = name[len];
7981 name[len] = NUL;
7982 /* first try vim_getenv(), fast for normal environment vars */
7983 string = vim_getenv(name, &mustfree);
7984 if (string != NULL && *string != NUL)
7985 {
7986 if (!mustfree)
7987 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007989 else
7990 {
7991 if (mustfree)
7992 vim_free(string);
7993
7994 /* next try expanding things like $VIM and ${HOME} */
7995 string = expand_env_save(name - 1);
7996 if (string != NULL && *string == '$')
7997 {
7998 vim_free(string);
7999 string = NULL;
8000 }
8001 }
8002 name[len] = cc;
8003
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 rettv->v_type = VAR_STRING;
8005 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 }
8007
8008 return OK;
8009}
8010
8011/*
8012 * Array with names and number of arguments of all internal functions
8013 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8014 */
8015static struct fst
8016{
8017 char *f_name; /* function name */
8018 char f_min_argc; /* minimal number of arguments */
8019 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008021 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022} functions[] =
8023{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#ifdef FEAT_FLOAT
8025 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008026 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008027#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008028 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008029 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {"append", 2, 2, f_append},
8031 {"argc", 0, 0, f_argc},
8032 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008033 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008034 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008035#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008036 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008037 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008038 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008041 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"bufexists", 1, 1, f_bufexists},
8043 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8044 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8045 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8046 {"buflisted", 1, 1, f_buflisted},
8047 {"bufloaded", 1, 1, f_bufloaded},
8048 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008049 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"bufwinnr", 1, 1, f_bufwinnr},
8051 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008052 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008053 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008054 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#ifdef FEAT_FLOAT
8056 {"ceil", 1, 1, f_ceil},
8057#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008058 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008059 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008061 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008063#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008064 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008065 {"complete_add", 1, 1, f_complete_add},
8066 {"complete_check", 0, 0, f_complete_check},
8067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#ifdef FEAT_FLOAT
8071 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008072 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008074 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008076 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008077 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"delete", 1, 1, f_delete},
8079 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008080 {"diff_filler", 1, 1, f_diff_filler},
8081 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008082 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008084 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"eventhandler", 0, 0, f_eventhandler},
8086 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008087 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008089#ifdef FEAT_FLOAT
8090 {"exp", 1, 1, f_exp},
8091#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008092 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008093 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008094 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8096 {"filereadable", 1, 1, f_filereadable},
8097 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008098 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008099 {"finddir", 1, 3, f_finddir},
8100 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#ifdef FEAT_FLOAT
8102 {"float2nr", 1, 1, f_float2nr},
8103 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008104 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008106 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"fnamemodify", 2, 2, f_fnamemodify},
8108 {"foldclosed", 1, 1, f_foldclosed},
8109 {"foldclosedend", 1, 1, f_foldclosedend},
8110 {"foldlevel", 1, 1, f_foldlevel},
8111 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008112 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008114 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008115 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008116 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008117 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008118 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"getchar", 0, 1, f_getchar},
8120 {"getcharmod", 0, 0, f_getcharmod},
8121 {"getcmdline", 0, 0, f_getcmdline},
8122 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008123 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008124 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008125 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008127 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008128 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"getfsize", 1, 1, f_getfsize},
8130 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008131 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008132 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008133 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008134 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008135 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008136 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008137 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008138 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008140 {"gettabvar", 2, 3, f_gettabvar},
8141 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"getwinposx", 0, 0, f_getwinposx},
8143 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008144 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008145 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008146 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008147 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008149 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008150 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008151 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8153 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8154 {"histadd", 2, 2, f_histadd},
8155 {"histdel", 1, 2, f_histdel},
8156 {"histget", 1, 2, f_histget},
8157 {"histnr", 1, 1, f_histnr},
8158 {"hlID", 1, 1, f_hlID},
8159 {"hlexists", 1, 1, f_hlexists},
8160 {"hostname", 0, 0, f_hostname},
8161 {"iconv", 3, 3, f_iconv},
8162 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008163 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008164 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008166 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"inputrestore", 0, 0, f_inputrestore},
8168 {"inputsave", 0, 0, f_inputsave},
8169 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008170 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008173 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008174 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008176 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008178 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"libcall", 3, 3, f_libcall},
8180 {"libcallnr", 3, 3, f_libcallnr},
8181 {"line", 1, 1, f_line},
8182 {"line2byte", 1, 1, f_line2byte},
8183 {"lispindent", 1, 1, f_lispindent},
8184 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008186 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008187 {"log10", 1, 1, f_log10},
8188#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008189#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008190 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008191#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008192 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008193 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008194 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008195 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008196 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008197 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008198 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008199 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008200 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008201 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008202 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008203 {"max", 1, 1, f_max},
8204 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008205#ifdef vim_mkdir
8206 {"mkdir", 1, 3, f_mkdir},
8207#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008208 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008209#ifdef FEAT_MZSCHEME
8210 {"mzeval", 1, 1, f_mzeval},
8211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008213 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008214 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008215 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008216#ifdef FEAT_FLOAT
8217 {"pow", 2, 2, f_pow},
8218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008220 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008221 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008222#ifdef FEAT_PYTHON3
8223 {"py3eval", 1, 1, f_py3eval},
8224#endif
8225#ifdef FEAT_PYTHON
8226 {"pyeval", 1, 1, f_pyeval},
8227#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008228 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008229 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008230 {"reltime", 0, 2, f_reltime},
8231 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"remote_expr", 2, 3, f_remote_expr},
8233 {"remote_foreground", 1, 1, f_remote_foreground},
8234 {"remote_peek", 1, 2, f_remote_peek},
8235 {"remote_read", 1, 1, f_remote_read},
8236 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008237 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008239 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008241 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008242#ifdef FEAT_FLOAT
8243 {"round", 1, 1, f_round},
8244#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008245 {"screenattr", 2, 2, f_screenattr},
8246 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008247 {"screencol", 0, 0, f_screencol},
8248 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008249 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008250 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008251 {"searchpair", 3, 7, f_searchpair},
8252 {"searchpairpos", 3, 7, f_searchpairpos},
8253 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"server2client", 2, 2, f_server2client},
8255 {"serverlist", 0, 0, f_serverlist},
8256 {"setbufvar", 3, 3, f_setbufvar},
8257 {"setcmdpos", 1, 1, f_setcmdpos},
8258 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008259 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008260 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008261 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008262 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008264 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008265 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008267#ifdef FEAT_CRYPT
8268 {"sha256", 1, 1, f_sha256},
8269#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008270 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008271 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#ifdef FEAT_FLOAT
8274 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008275 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008276#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008277 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008278 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008279 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008280 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008281 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008282#ifdef FEAT_FLOAT
8283 {"sqrt", 1, 1, f_sqrt},
8284 {"str2float", 1, 1, f_str2float},
8285#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008286 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008287 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008288 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289#ifdef HAVE_STRFTIME
8290 {"strftime", 1, 2, f_strftime},
8291#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008293 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"strlen", 1, 1, f_strlen},
8295 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008296 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008298 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008299 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"substitute", 4, 4, f_substitute},
8301 {"synID", 3, 3, f_synID},
8302 {"synIDattr", 2, 3, f_synIDattr},
8303 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008304 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008305 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008306 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008307 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008308 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008309 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008310 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008311 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008312 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008313#ifdef FEAT_FLOAT
8314 {"tan", 1, 1, f_tan},
8315 {"tanh", 1, 1, f_tanh},
8316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008318 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"tolower", 1, 1, f_tolower},
8320 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008321 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008322#ifdef FEAT_FLOAT
8323 {"trunc", 1, 1, f_trunc},
8324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008326 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008327 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008328 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008329 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"virtcol", 1, 1, f_virtcol},
8331 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008332 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"winbufnr", 1, 1, f_winbufnr},
8334 {"wincol", 0, 0, f_wincol},
8335 {"winheight", 1, 1, f_winheight},
8336 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008337 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008339 {"winrestview", 1, 1, f_winrestview},
8340 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008342 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008343 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344};
8345
8346#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8347
8348/*
8349 * Function given to ExpandGeneric() to obtain the list of internal
8350 * or user defined function names.
8351 */
8352 char_u *
8353get_function_name(xp, idx)
8354 expand_T *xp;
8355 int idx;
8356{
8357 static int intidx = -1;
8358 char_u *name;
8359
8360 if (idx == 0)
8361 intidx = -1;
8362 if (intidx < 0)
8363 {
8364 name = get_user_func_name(xp, idx);
8365 if (name != NULL)
8366 return name;
8367 }
8368 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8369 {
8370 STRCPY(IObuff, functions[intidx].f_name);
8371 STRCAT(IObuff, "(");
8372 if (functions[intidx].f_max_argc == 0)
8373 STRCAT(IObuff, ")");
8374 return IObuff;
8375 }
8376
8377 return NULL;
8378}
8379
8380/*
8381 * Function given to ExpandGeneric() to obtain the list of internal or
8382 * user defined variable or function names.
8383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 char_u *
8385get_expr_name(xp, idx)
8386 expand_T *xp;
8387 int idx;
8388{
8389 static int intidx = -1;
8390 char_u *name;
8391
8392 if (idx == 0)
8393 intidx = -1;
8394 if (intidx < 0)
8395 {
8396 name = get_function_name(xp, idx);
8397 if (name != NULL)
8398 return name;
8399 }
8400 return get_user_var_name(xp, ++intidx);
8401}
8402
8403#endif /* FEAT_CMDL_COMPL */
8404
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008405#if defined(EBCDIC) || defined(PROTO)
8406/*
8407 * Compare struct fst by function name.
8408 */
8409 static int
8410compare_func_name(s1, s2)
8411 const void *s1;
8412 const void *s2;
8413{
8414 struct fst *p1 = (struct fst *)s1;
8415 struct fst *p2 = (struct fst *)s2;
8416
8417 return STRCMP(p1->f_name, p2->f_name);
8418}
8419
8420/*
8421 * Sort the function table by function name.
8422 * The sorting of the table above is ASCII dependant.
8423 * On machines using EBCDIC we have to sort it.
8424 */
8425 static void
8426sortFunctions()
8427{
8428 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8429
8430 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8431}
8432#endif
8433
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435/*
8436 * Find internal function in table above.
8437 * Return index, or -1 if not found
8438 */
8439 static int
8440find_internal_func(name)
8441 char_u *name; /* name of the function */
8442{
8443 int first = 0;
8444 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8445 int cmp;
8446 int x;
8447
8448 /*
8449 * Find the function name in the table. Binary search.
8450 */
8451 while (first <= last)
8452 {
8453 x = first + ((unsigned)(last - first) >> 1);
8454 cmp = STRCMP(name, functions[x].f_name);
8455 if (cmp < 0)
8456 last = x - 1;
8457 else if (cmp > 0)
8458 first = x + 1;
8459 else
8460 return x;
8461 }
8462 return -1;
8463}
8464
8465/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008466 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8467 * name it contains, otherwise return "name".
8468 */
8469 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008470deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008471 char_u *name;
8472 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008473 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008474{
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008476 int cc;
8477
8478 cc = name[*lenp];
8479 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008480 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008483 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008484 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008485 {
8486 *lenp = 0;
8487 return (char_u *)""; /* just in case */
8488 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008489 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008490 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008491 }
8492
8493 return name;
8494}
8495
8496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 * Allocate a variable for the result of a function.
8498 * Return OK or FAIL.
8499 */
8500 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8502 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 char_u *name; /* name of the function */
8504 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 char_u **arg; /* argument, pointing to the '(' */
8507 linenr_T firstline; /* first line of range */
8508 linenr_T lastline; /* last line of range */
8509 int *doesrange; /* return: function handled range */
8510 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008511 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
8513 char_u *argp;
8514 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008515 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 int argcount = 0; /* number of arguments found */
8517
8518 /*
8519 * Get the arguments.
8520 */
8521 argp = *arg;
8522 while (argcount < MAX_FUNC_ARGS)
8523 {
8524 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8525 if (*argp == ')' || *argp == ',' || *argp == NUL)
8526 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8528 {
8529 ret = FAIL;
8530 break;
8531 }
8532 ++argcount;
8533 if (*argp != ',')
8534 break;
8535 }
8536 if (*argp == ')')
8537 ++argp;
8538 else
8539 ret = FAIL;
8540
8541 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008542 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008543 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008545 {
8546 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008547 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008549 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551
8552 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
8555 *arg = skipwhite(argp);
8556 return ret;
8557}
8558
8559
8560/*
8561 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008562 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008563 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 */
8565 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008566call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008568 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008570 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008572 typval_T *argvars; /* vars for arguments, must have "argcount"
8573 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 linenr_T firstline; /* first line of range */
8575 linenr_T lastline; /* last line of range */
8576 int *doesrange; /* return: function handled range */
8577 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#define ERROR_UNKNOWN 0
8582#define ERROR_TOOMANY 1
8583#define ERROR_TOOFEW 2
8584#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008585#define ERROR_DICT 4
8586#define ERROR_NONE 5
8587#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 int error = ERROR_NONE;
8589 int i;
8590 int llen;
8591 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592#define FLEN_FIXED 40
8593 char_u fname_buf[FLEN_FIXED + 1];
8594 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008595 char_u *name;
8596
8597 /* Make a copy of the name, if it comes from a funcref variable it could
8598 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008599 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008600 if (name == NULL)
8601 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
8603 /*
8604 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8605 * Change <SNR>123_name() to K_SNR 123_name().
8606 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 llen = eval_fname_script(name);
8609 if (llen > 0)
8610 {
8611 fname_buf[0] = K_SPECIAL;
8612 fname_buf[1] = KS_EXTRA;
8613 fname_buf[2] = (int)KE_SNR;
8614 i = 3;
8615 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8616 {
8617 if (current_SID <= 0)
8618 error = ERROR_SCRIPT;
8619 else
8620 {
8621 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8622 i = (int)STRLEN(fname_buf);
8623 }
8624 }
8625 if (i + STRLEN(name + llen) < FLEN_FIXED)
8626 {
8627 STRCPY(fname_buf + i, name + llen);
8628 fname = fname_buf;
8629 }
8630 else
8631 {
8632 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8633 if (fname == NULL)
8634 error = ERROR_OTHER;
8635 else
8636 {
8637 mch_memmove(fname, fname_buf, (size_t)i);
8638 STRCPY(fname + i, name + llen);
8639 }
8640 }
8641 }
8642 else
8643 fname = name;
8644
8645 *doesrange = FALSE;
8646
8647
8648 /* execute the function if no errors detected and executing */
8649 if (evaluate && error == ERROR_NONE)
8650 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008651 char_u *rfname = fname;
8652
8653 /* Ignore "g:" before a function name. */
8654 if (fname[0] == 'g' && fname[1] == ':')
8655 rfname = fname + 2;
8656
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008657 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8658 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 error = ERROR_UNKNOWN;
8660
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008661 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 {
8663 /*
8664 * User defined function.
8665 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008666 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008667
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008669 /* Trigger FuncUndefined event, may load the function. */
8670 if (fp == NULL
8671 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008672 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008673 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008675 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008676 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 }
8678#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008679 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008680 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008681 {
8682 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008683 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008684 }
8685
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 if (fp != NULL)
8687 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008688 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008690 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008692 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008694 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008695 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 else
8697 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008698 int did_save_redo = FALSE;
8699
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 /*
8701 * Call the user function.
8702 * Save and restore search patterns, script variables and
8703 * redo buffer.
8704 */
8705 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008706 if (!ins_compl_active())
8707 {
8708 saveRedobuff();
8709 did_save_redo = TRUE;
8710 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008711 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008714 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8715 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8716 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008717 /* Function was unreferenced while being used, free it
8718 * now. */
8719 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008720 if (did_save_redo)
8721 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 restore_search_patterns();
8723 error = ERROR_NONE;
8724 }
8725 }
8726 }
8727 else
8728 {
8729 /*
8730 * Find the function name in the table, call its implementation.
8731 */
8732 i = find_internal_func(fname);
8733 if (i >= 0)
8734 {
8735 if (argcount < functions[i].f_min_argc)
8736 error = ERROR_TOOFEW;
8737 else if (argcount > functions[i].f_max_argc)
8738 error = ERROR_TOOMANY;
8739 else
8740 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008741 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 error = ERROR_NONE;
8744 }
8745 }
8746 }
8747 /*
8748 * The function call (or "FuncUndefined" autocommand sequence) might
8749 * have been aborted by an error, an interrupt, or an explicitly thrown
8750 * exception that has not been caught so far. This situation can be
8751 * tested for by calling aborting(). For an error in an internal
8752 * function or for the "E132" error in call_user_func(), however, the
8753 * throw point at which the "force_abort" flag (temporarily reset by
8754 * emsg()) is normally updated has not been reached yet. We need to
8755 * update that flag first to make aborting() reliable.
8756 */
8757 update_force_abort();
8758 }
8759 if (error == ERROR_NONE)
8760 ret = OK;
8761
8762 /*
8763 * Report an error unless the argument evaluation or function call has been
8764 * cancelled due to an aborting error, an interrupt, or an exception.
8765 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008766 if (!aborting())
8767 {
8768 switch (error)
8769 {
8770 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008771 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008772 break;
8773 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008774 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008775 break;
8776 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008777 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008778 name);
8779 break;
8780 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008781 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008782 name);
8783 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008785 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008786 name);
8787 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008788 }
8789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 if (fname != name && fname != fname_buf)
8792 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008793 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
8795 return ret;
8796}
8797
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008798/*
8799 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008800 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008801 */
8802 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008803emsg_funcname(ermsg, name)
8804 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008805 char_u *name;
8806{
8807 char_u *p;
8808
8809 if (*name == K_SPECIAL)
8810 p = concat_str((char_u *)"<SNR>", name + 3);
8811 else
8812 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008813 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008814 if (p != name)
8815 vim_free(p);
8816}
8817
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008818/*
8819 * Return TRUE for a non-zero Number and a non-empty String.
8820 */
8821 static int
8822non_zero_arg(argvars)
8823 typval_T *argvars;
8824{
8825 return ((argvars[0].v_type == VAR_NUMBER
8826 && argvars[0].vval.v_number != 0)
8827 || (argvars[0].v_type == VAR_STRING
8828 && argvars[0].vval.v_string != NULL
8829 && *argvars[0].vval.v_string != NUL));
8830}
8831
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832/*********************************************
8833 * Implementation of the built-in functions
8834 */
8835
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008836#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008837static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8838
8839/*
8840 * Get the float value of "argvars[0]" into "f".
8841 * Returns FAIL when the argument is not a Number or Float.
8842 */
8843 static int
8844get_float_arg(argvars, f)
8845 typval_T *argvars;
8846 float_T *f;
8847{
8848 if (argvars[0].v_type == VAR_FLOAT)
8849 {
8850 *f = argvars[0].vval.v_float;
8851 return OK;
8852 }
8853 if (argvars[0].v_type == VAR_NUMBER)
8854 {
8855 *f = (float_T)argvars[0].vval.v_number;
8856 return OK;
8857 }
8858 EMSG(_("E808: Number or Float required"));
8859 return FAIL;
8860}
8861
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008862/*
8863 * "abs(expr)" function
8864 */
8865 static void
8866f_abs(argvars, rettv)
8867 typval_T *argvars;
8868 typval_T *rettv;
8869{
8870 if (argvars[0].v_type == VAR_FLOAT)
8871 {
8872 rettv->v_type = VAR_FLOAT;
8873 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8874 }
8875 else
8876 {
8877 varnumber_T n;
8878 int error = FALSE;
8879
8880 n = get_tv_number_chk(&argvars[0], &error);
8881 if (error)
8882 rettv->vval.v_number = -1;
8883 else if (n > 0)
8884 rettv->vval.v_number = n;
8885 else
8886 rettv->vval.v_number = -n;
8887 }
8888}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008889
8890/*
8891 * "acos()" function
8892 */
8893 static void
8894f_acos(argvars, rettv)
8895 typval_T *argvars;
8896 typval_T *rettv;
8897{
8898 float_T f;
8899
8900 rettv->v_type = VAR_FLOAT;
8901 if (get_float_arg(argvars, &f) == OK)
8902 rettv->vval.v_float = acos(f);
8903 else
8904 rettv->vval.v_float = 0.0;
8905}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008906#endif
8907
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008909 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 */
8911 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008912f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008913 typval_T *argvars;
8914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008919 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008921 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008922 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008923 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008925 }
8926 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008927 EMSG(_(e_listreq));
8928}
8929
8930/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008931 * "and(expr, expr)" function
8932 */
8933 static void
8934f_and(argvars, rettv)
8935 typval_T *argvars;
8936 typval_T *rettv;
8937{
8938 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8939 & get_tv_number_chk(&argvars[1], NULL);
8940}
8941
8942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008943 * "append(lnum, string/list)" function
8944 */
8945 static void
8946f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008947 typval_T *argvars;
8948 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008949{
8950 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008951 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008952 list_T *l = NULL;
8953 listitem_T *li = NULL;
8954 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008955 long added = 0;
8956
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008957 /* When coming here from Insert mode, sync undo, so that this can be
8958 * undone separately from what was previously inserted. */
8959 if (u_sync_once == 2)
8960 {
8961 u_sync_once = 1; /* notify that u_sync() was called */
8962 u_sync(TRUE);
8963 }
8964
Bram Moolenaar0d660222005-01-07 21:51:51 +00008965 lnum = get_tv_lnum(argvars);
8966 if (lnum >= 0
8967 && lnum <= curbuf->b_ml.ml_line_count
8968 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008969 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008970 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008971 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008972 l = argvars[1].vval.v_list;
8973 if (l == NULL)
8974 return;
8975 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008977 for (;;)
8978 {
8979 if (l == NULL)
8980 tv = &argvars[1]; /* append a string */
8981 else if (li == NULL)
8982 break; /* end of list */
8983 else
8984 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 line = get_tv_string_chk(tv);
8986 if (line == NULL) /* type error */
8987 {
8988 rettv->vval.v_number = 1; /* Failed */
8989 break;
8990 }
8991 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008992 ++added;
8993 if (l == NULL)
8994 break;
8995 li = li->li_next;
8996 }
8997
8998 appended_lines_mark(lnum, added);
8999 if (curwin->w_cursor.lnum > lnum)
9000 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 else
9003 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004}
9005
9006/*
9007 * "argc()" function
9008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009011 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015}
9016
9017/*
9018 * "argidx()" function
9019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009022 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026}
9027
9028/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009029 * "arglistid()" function
9030 */
9031 static void
9032f_arglistid(argvars, rettv)
9033 typval_T *argvars UNUSED;
9034 typval_T *rettv;
9035{
9036 win_T *wp;
9037 tabpage_T *tp = NULL;
9038 long n;
9039
9040 rettv->vval.v_number = -1;
9041 if (argvars[0].v_type != VAR_UNKNOWN)
9042 {
9043 if (argvars[1].v_type != VAR_UNKNOWN)
9044 {
9045 n = get_tv_number(&argvars[1]);
9046 if (n >= 0)
9047 tp = find_tabpage(n);
9048 }
9049 else
9050 tp = curtab;
9051
9052 if (tp != NULL)
9053 {
9054 wp = find_win_by_nr(&argvars[0], tp);
9055 if (wp != NULL)
9056 rettv->vval.v_number = wp->w_alist->id;
9057 }
9058 }
9059 else
9060 rettv->vval.v_number = curwin->w_alist->id;
9061}
9062
9063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 * "argv(nr)" function
9065 */
9066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 typval_T *argvars;
9069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
9071 int idx;
9072
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009073 if (argvars[0].v_type != VAR_UNKNOWN)
9074 {
9075 idx = get_tv_number_chk(&argvars[0], NULL);
9076 if (idx >= 0 && idx < ARGCOUNT)
9077 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9078 else
9079 rettv->vval.v_string = NULL;
9080 rettv->v_type = VAR_STRING;
9081 }
9082 else if (rettv_list_alloc(rettv) == OK)
9083 for (idx = 0; idx < ARGCOUNT; ++idx)
9084 list_append_string(rettv->vval.v_list,
9085 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086}
9087
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009088#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009089/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009090 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009091 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009092 static void
9093f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009094 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009095 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009096{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009097 float_T f;
9098
9099 rettv->v_type = VAR_FLOAT;
9100 if (get_float_arg(argvars, &f) == OK)
9101 rettv->vval.v_float = asin(f);
9102 else
9103 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009104}
9105
9106/*
9107 * "atan()" function
9108 */
9109 static void
9110f_atan(argvars, rettv)
9111 typval_T *argvars;
9112 typval_T *rettv;
9113{
9114 float_T f;
9115
9116 rettv->v_type = VAR_FLOAT;
9117 if (get_float_arg(argvars, &f) == OK)
9118 rettv->vval.v_float = atan(f);
9119 else
9120 rettv->vval.v_float = 0.0;
9121}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009122
9123/*
9124 * "atan2()" function
9125 */
9126 static void
9127f_atan2(argvars, rettv)
9128 typval_T *argvars;
9129 typval_T *rettv;
9130{
9131 float_T fx, fy;
9132
9133 rettv->v_type = VAR_FLOAT;
9134 if (get_float_arg(argvars, &fx) == OK
9135 && get_float_arg(&argvars[1], &fy) == OK)
9136 rettv->vval.v_float = atan2(fx, fy);
9137 else
9138 rettv->vval.v_float = 0.0;
9139}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009140#endif
9141
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142/*
9143 * "browse(save, title, initdir, default)" function
9144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150#ifdef FEAT_BROWSE
9151 int save;
9152 char_u *title;
9153 char_u *initdir;
9154 char_u *defname;
9155 char_u buf[NUMBUFLEN];
9156 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009159 save = get_tv_number_chk(&argvars[0], &error);
9160 title = get_tv_string_chk(&argvars[1]);
9161 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9162 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009164 if (error || title == NULL || initdir == NULL || defname == NULL)
9165 rettv->vval.v_string = NULL;
9166 else
9167 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009168 do_browse(save ? BROWSE_SAVE : 0,
9169 title, defname, NULL, initdir, NULL, curbuf);
9170#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009172#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009174}
9175
9176/*
9177 * "browsedir(title, initdir)" function
9178 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009181 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009182 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009183{
9184#ifdef FEAT_BROWSE
9185 char_u *title;
9186 char_u *initdir;
9187 char_u buf[NUMBUFLEN];
9188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189 title = get_tv_string_chk(&argvars[0]);
9190 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 if (title == NULL || initdir == NULL)
9193 rettv->vval.v_string = NULL;
9194 else
9195 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009196 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
Bram Moolenaar33570922005-01-25 22:26:29 +00009203static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009204
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205/*
9206 * Find a buffer by number or exact name.
9207 */
9208 static buf_T *
9209find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009214 if (avar->v_type == VAR_NUMBER)
9215 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009216 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009218 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009219 if (buf == NULL)
9220 {
9221 /* No full path name match, try a match with a URL or a "nofile"
9222 * buffer, these don't use the full path. */
9223 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9224 if (buf->b_fname != NULL
9225 && (path_with_url(buf->b_fname)
9226#ifdef FEAT_QUICKFIX
9227 || bt_nofile(buf)
9228#endif
9229 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009230 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009231 break;
9232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 }
9234 return buf;
9235}
9236
9237/*
9238 * "bufexists(expr)" function
9239 */
9240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009241f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009242 typval_T *argvars;
9243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009245 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246}
9247
9248/*
9249 * "buflisted(expr)" function
9250 */
9251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009252f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009253 typval_T *argvars;
9254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255{
9256 buf_T *buf;
9257
9258 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009259 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260}
9261
9262/*
9263 * "bufloaded(expr)" function
9264 */
9265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009266f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009267 typval_T *argvars;
9268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269{
9270 buf_T *buf;
9271
9272 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009273 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274}
9275
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009276static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009277
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278/*
9279 * Get buffer by number or pattern.
9280 */
9281 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009282get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009284 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009286 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 int save_magic;
9288 char_u *save_cpo;
9289 buf_T *buf;
9290
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291 if (tv->v_type == VAR_NUMBER)
9292 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009293 if (tv->v_type != VAR_STRING)
9294 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 if (name == NULL || *name == NUL)
9296 return curbuf;
9297 if (name[0] == '$' && name[1] == NUL)
9298 return lastbuf;
9299
9300 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9301 save_magic = p_magic;
9302 p_magic = TRUE;
9303 save_cpo = p_cpo;
9304 p_cpo = (char_u *)"";
9305
9306 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009307 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308
9309 p_magic = save_magic;
9310 p_cpo = save_cpo;
9311
9312 /* If not found, try expanding the name, like done for bufexists(). */
9313 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315
9316 return buf;
9317}
9318
9319/*
9320 * "bufname(expr)" function
9321 */
9322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009324 typval_T *argvars;
9325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326{
9327 buf_T *buf;
9328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009331 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 --emsg_off;
9338}
9339
9340/*
9341 * "bufnr(expr)" function
9342 */
9343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009345 typval_T *argvars;
9346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347{
9348 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009349 int error = FALSE;
9350 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009352 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009354 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009355 --emsg_off;
9356
9357 /* If the buffer isn't found and the second argument is not zero create a
9358 * new buffer. */
9359 if (buf == NULL
9360 && argvars[1].v_type != VAR_UNKNOWN
9361 && get_tv_number_chk(&argvars[1], &error) != 0
9362 && !error
9363 && (name = get_tv_string_chk(&argvars[0])) != NULL
9364 && !error)
9365 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9366
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371}
9372
9373/*
9374 * "bufwinnr(nr)" function
9375 */
9376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009378 typval_T *argvars;
9379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381#ifdef FEAT_WINDOWS
9382 win_T *wp;
9383 int winnr = 0;
9384#endif
9385 buf_T *buf;
9386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009387 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009389 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390#ifdef FEAT_WINDOWS
9391 for (wp = firstwin; wp; wp = wp->w_next)
9392 {
9393 ++winnr;
9394 if (wp->w_buffer == buf)
9395 break;
9396 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400#endif
9401 --emsg_off;
9402}
9403
9404/*
9405 * "byte2line(byte)" function
9406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
9412#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414#else
9415 long boff = 0;
9416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009417 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 (linenr_T)0, &boff);
9423#endif
9424}
9425
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009426 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009427byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009428 typval_T *argvars;
9429 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009430 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009431{
9432#ifdef FEAT_MBYTE
9433 char_u *t;
9434#endif
9435 char_u *str;
9436 long idx;
9437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009438 str = get_tv_string_chk(&argvars[0]);
9439 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009442 return;
9443
9444#ifdef FEAT_MBYTE
9445 t = str;
9446 for ( ; idx > 0; idx--)
9447 {
9448 if (*t == NUL) /* EOL reached */
9449 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009450 if (enc_utf8 && comp)
9451 t += utf_ptr2len(t);
9452 else
9453 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009454 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009455 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009456#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009457 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009459#endif
9460}
9461
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009462/*
9463 * "byteidx()" function
9464 */
9465 static void
9466f_byteidx(argvars, rettv)
9467 typval_T *argvars;
9468 typval_T *rettv;
9469{
9470 byteidx(argvars, rettv, FALSE);
9471}
9472
9473/*
9474 * "byteidxcomp()" function
9475 */
9476 static void
9477f_byteidxcomp(argvars, rettv)
9478 typval_T *argvars;
9479 typval_T *rettv;
9480{
9481 byteidx(argvars, rettv, TRUE);
9482}
9483
Bram Moolenaardb913952012-06-29 12:54:53 +02009484 int
9485func_call(name, args, selfdict, rettv)
9486 char_u *name;
9487 typval_T *args;
9488 dict_T *selfdict;
9489 typval_T *rettv;
9490{
9491 listitem_T *item;
9492 typval_T argv[MAX_FUNC_ARGS + 1];
9493 int argc = 0;
9494 int dummy;
9495 int r = 0;
9496
9497 for (item = args->vval.v_list->lv_first; item != NULL;
9498 item = item->li_next)
9499 {
9500 if (argc == MAX_FUNC_ARGS)
9501 {
9502 EMSG(_("E699: Too many arguments"));
9503 break;
9504 }
9505 /* Make a copy of each argument. This is needed to be able to set
9506 * v_lock to VAR_FIXED in the copy without changing the original list.
9507 */
9508 copy_tv(&item->li_tv, &argv[argc++]);
9509 }
9510
9511 if (item == NULL)
9512 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9513 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9514 &dummy, TRUE, selfdict);
9515
9516 /* Free the arguments. */
9517 while (argc > 0)
9518 clear_tv(&argv[--argc]);
9519
9520 return r;
9521}
9522
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009523/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009524 * "call(func, arglist)" function
9525 */
9526 static void
9527f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009528 typval_T *argvars;
9529 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009530{
9531 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009532 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009533
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009534 if (argvars[1].v_type != VAR_LIST)
9535 {
9536 EMSG(_(e_listreq));
9537 return;
9538 }
9539 if (argvars[1].vval.v_list == NULL)
9540 return;
9541
9542 if (argvars[0].v_type == VAR_FUNC)
9543 func = argvars[0].vval.v_string;
9544 else
9545 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 if (*func == NUL)
9547 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009548
Bram Moolenaare9a41262005-01-15 22:18:47 +00009549 if (argvars[2].v_type != VAR_UNKNOWN)
9550 {
9551 if (argvars[2].v_type != VAR_DICT)
9552 {
9553 EMSG(_(e_dictreq));
9554 return;
9555 }
9556 selfdict = argvars[2].vval.v_dict;
9557 }
9558
Bram Moolenaardb913952012-06-29 12:54:53 +02009559 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009560}
9561
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009562#ifdef FEAT_FLOAT
9563/*
9564 * "ceil({float})" function
9565 */
9566 static void
9567f_ceil(argvars, rettv)
9568 typval_T *argvars;
9569 typval_T *rettv;
9570{
9571 float_T f;
9572
9573 rettv->v_type = VAR_FLOAT;
9574 if (get_float_arg(argvars, &f) == OK)
9575 rettv->vval.v_float = ceil(f);
9576 else
9577 rettv->vval.v_float = 0.0;
9578}
9579#endif
9580
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009581/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009582 * "changenr()" function
9583 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009584 static void
9585f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009586 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009587 typval_T *rettv;
9588{
9589 rettv->vval.v_number = curbuf->b_u_seq_cur;
9590}
9591
9592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 * "char2nr(string)" function
9594 */
9595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *argvars;
9598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
9600#ifdef FEAT_MBYTE
9601 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009602 {
9603 int utf8 = 0;
9604
9605 if (argvars[1].v_type != VAR_UNKNOWN)
9606 utf8 = get_tv_number_chk(&argvars[1], NULL);
9607
9608 if (utf8)
9609 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9610 else
9611 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 else
9614#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616}
9617
9618/*
9619 * "cindent(lnum)" function
9620 */
9621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009623 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
9626#ifdef FEAT_CINDENT
9627 pos_T pos;
9628 linenr_T lnum;
9629
9630 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9633 {
9634 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 curwin->w_cursor = pos;
9637 }
9638 else
9639#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641}
9642
9643/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009644 * "clearmatches()" function
9645 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009646 static void
9647f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009648 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009649 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009650{
9651#ifdef FEAT_SEARCH_EXTRA
9652 clear_matches(curwin);
9653#endif
9654}
9655
9656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 * "col(string)" function
9658 */
9659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 typval_T *argvars;
9662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664 colnr_T col = 0;
9665 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009666 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009668 fp = var2fpos(&argvars[0], FALSE, &fnum);
9669 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 {
9671 if (fp->col == MAXCOL)
9672 {
9673 /* '> can be MAXCOL, get the length of the line then */
9674 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009675 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 else
9677 col = MAXCOL;
9678 }
9679 else
9680 {
9681 col = fp->col + 1;
9682#ifdef FEAT_VIRTUALEDIT
9683 /* col(".") when the cursor is on the NUL at the end of the line
9684 * because of "coladd" can be seen as an extra column. */
9685 if (virtual_active() && fp == &curwin->w_cursor)
9686 {
9687 char_u *p = ml_get_cursor();
9688
9689 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9690 curwin->w_virtcol - curwin->w_cursor.coladd))
9691 {
9692# ifdef FEAT_MBYTE
9693 int l;
9694
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009695 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 col += l;
9697# else
9698 if (*p != NUL && p[1] == NUL)
9699 ++col;
9700# endif
9701 }
9702 }
9703#endif
9704 }
9705 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707}
9708
Bram Moolenaar572cb562005-08-05 21:35:02 +00009709#if defined(FEAT_INS_EXPAND)
9710/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009711 * "complete()" function
9712 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009713 static void
9714f_complete(argvars, rettv)
9715 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009716 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009717{
9718 int startcol;
9719
9720 if ((State & INSERT) == 0)
9721 {
9722 EMSG(_("E785: complete() can only be used in Insert mode"));
9723 return;
9724 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009725
9726 /* Check for undo allowed here, because if something was already inserted
9727 * the line was already saved for undo and this check isn't done. */
9728 if (!undo_allowed())
9729 return;
9730
Bram Moolenaarade00832006-03-10 21:46:58 +00009731 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9732 {
9733 EMSG(_(e_invarg));
9734 return;
9735 }
9736
9737 startcol = get_tv_number_chk(&argvars[0], NULL);
9738 if (startcol <= 0)
9739 return;
9740
9741 set_completion(startcol - 1, argvars[1].vval.v_list);
9742}
9743
9744/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009745 * "complete_add()" function
9746 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009747 static void
9748f_complete_add(argvars, rettv)
9749 typval_T *argvars;
9750 typval_T *rettv;
9751{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009752 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009753}
9754
9755/*
9756 * "complete_check()" function
9757 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009758 static void
9759f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009760 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009761 typval_T *rettv;
9762{
9763 int saved = RedrawingDisabled;
9764
9765 RedrawingDisabled = 0;
9766 ins_compl_check_keys(0);
9767 rettv->vval.v_number = compl_interrupted;
9768 RedrawingDisabled = saved;
9769}
9770#endif
9771
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772/*
9773 * "confirm(message, buttons[, default [, type]])" function
9774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009777 typval_T *argvars UNUSED;
9778 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
9780#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9781 char_u *message;
9782 char_u *buttons = NULL;
9783 char_u buf[NUMBUFLEN];
9784 char_u buf2[NUMBUFLEN];
9785 int def = 1;
9786 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009787 char_u *typestr;
9788 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 message = get_tv_string_chk(&argvars[0]);
9791 if (message == NULL)
9792 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009793 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009795 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9796 if (buttons == NULL)
9797 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009800 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009801 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9804 if (typestr == NULL)
9805 error = TRUE;
9806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009808 switch (TOUPPER_ASC(*typestr))
9809 {
9810 case 'E': type = VIM_ERROR; break;
9811 case 'Q': type = VIM_QUESTION; break;
9812 case 'I': type = VIM_INFO; break;
9813 case 'W': type = VIM_WARNING; break;
9814 case 'G': type = VIM_GENERIC; break;
9815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 }
9817 }
9818 }
9819 }
9820
9821 if (buttons == NULL || *buttons == NUL)
9822 buttons = (char_u *)_("&Ok");
9823
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009824 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009825 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009826 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827#endif
9828}
9829
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009830/*
9831 * "copy()" function
9832 */
9833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009835 typval_T *argvars;
9836 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009837{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009838 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009841#ifdef FEAT_FLOAT
9842/*
9843 * "cos()" function
9844 */
9845 static void
9846f_cos(argvars, rettv)
9847 typval_T *argvars;
9848 typval_T *rettv;
9849{
9850 float_T f;
9851
9852 rettv->v_type = VAR_FLOAT;
9853 if (get_float_arg(argvars, &f) == OK)
9854 rettv->vval.v_float = cos(f);
9855 else
9856 rettv->vval.v_float = 0.0;
9857}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009858
9859/*
9860 * "cosh()" function
9861 */
9862 static void
9863f_cosh(argvars, rettv)
9864 typval_T *argvars;
9865 typval_T *rettv;
9866{
9867 float_T f;
9868
9869 rettv->v_type = VAR_FLOAT;
9870 if (get_float_arg(argvars, &f) == OK)
9871 rettv->vval.v_float = cosh(f);
9872 else
9873 rettv->vval.v_float = 0.0;
9874}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009875#endif
9876
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009878 * "count()" function
9879 */
9880 static void
9881f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009882 typval_T *argvars;
9883 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009884{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009885 long n = 0;
9886 int ic = FALSE;
9887
Bram Moolenaare9a41262005-01-15 22:18:47 +00009888 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009889 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 listitem_T *li;
9891 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009892 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009893
Bram Moolenaare9a41262005-01-15 22:18:47 +00009894 if ((l = argvars[0].vval.v_list) != NULL)
9895 {
9896 li = l->lv_first;
9897 if (argvars[2].v_type != VAR_UNKNOWN)
9898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009899 int error = FALSE;
9900
9901 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 if (argvars[3].v_type != VAR_UNKNOWN)
9903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 idx = get_tv_number_chk(&argvars[3], &error);
9905 if (!error)
9906 {
9907 li = list_find(l, idx);
9908 if (li == NULL)
9909 EMSGN(_(e_listidx), idx);
9910 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009912 if (error)
9913 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 }
9915
9916 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009917 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 ++n;
9919 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009920 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 else if (argvars[0].v_type == VAR_DICT)
9922 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009923 int todo;
9924 dict_T *d;
9925 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009926
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009927 if ((d = argvars[0].vval.v_dict) != NULL)
9928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 int error = FALSE;
9930
Bram Moolenaare9a41262005-01-15 22:18:47 +00009931 if (argvars[2].v_type != VAR_UNKNOWN)
9932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009934 if (argvars[3].v_type != VAR_UNKNOWN)
9935 EMSG(_(e_invarg));
9936 }
9937
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009938 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009939 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009940 {
9941 if (!HASHITEM_EMPTY(hi))
9942 {
9943 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009944 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009945 ++n;
9946 }
9947 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009948 }
9949 }
9950 else
9951 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009952 rettv->vval.v_number = n;
9953}
9954
9955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9957 *
9958 * Checks the existence of a cscope connection.
9959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009962 typval_T *argvars UNUSED;
9963 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965#ifdef FEAT_CSCOPE
9966 int num = 0;
9967 char_u *dbpath = NULL;
9968 char_u *prepend = NULL;
9969 char_u buf[NUMBUFLEN];
9970
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009971 if (argvars[0].v_type != VAR_UNKNOWN
9972 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974 num = (int)get_tv_number(&argvars[0]);
9975 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009976 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 }
9979
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981#endif
9982}
9983
9984/*
9985 * "cursor(lnum, col)" function
9986 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009987 * Moves the cursor to the specified line and column.
9988 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009992 typval_T *argvars;
9993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994{
9995 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009996#ifdef FEAT_VIRTUALEDIT
9997 long coladd = 0;
9998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010000 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010001 if (argvars[1].v_type == VAR_UNKNOWN)
10002 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010003 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010004 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010005
Bram Moolenaar493c1782014-05-28 14:34:46 +020010006 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010007 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010008 line = pos.lnum;
10009 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010010#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010011 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010012#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010013 if (curswant >= 0)
10014 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010015 }
10016 else
10017 {
10018 line = get_tv_lnum(argvars);
10019 col = get_tv_number_chk(&argvars[1], NULL);
10020#ifdef FEAT_VIRTUALEDIT
10021 if (argvars[2].v_type != VAR_UNKNOWN)
10022 coladd = get_tv_number_chk(&argvars[2], NULL);
10023#endif
10024 }
10025 if (line < 0 || col < 0
10026#ifdef FEAT_VIRTUALEDIT
10027 || coladd < 0
10028#endif
10029 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010030 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 if (line > 0)
10032 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 if (col > 0)
10034 curwin->w_cursor.col = col - 1;
10035#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010036 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037#endif
10038
10039 /* Make sure the cursor is in a valid position. */
10040 check_cursor();
10041#ifdef FEAT_MBYTE
10042 /* Correct cursor for multi-byte character. */
10043 if (has_mbyte)
10044 mb_adjust_cursor();
10045#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010046
10047 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010048 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049}
10050
10051/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010052 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 */
10054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010056 typval_T *argvars;
10057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010059 int noref = 0;
10060
10061 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010062 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010063 if (noref < 0 || noref > 1)
10064 EMSG(_(e_invarg));
10065 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010066 {
10067 current_copyID += COPYID_INC;
10068 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070}
10071
10072/*
10073 * "delete()" function
10074 */
10075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010076f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010077 typval_T *argvars;
10078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079{
10080 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
10086/*
10087 * "did_filetype()" function
10088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010091 typval_T *argvars UNUSED;
10092 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
10094#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096#endif
10097}
10098
10099/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010100 * "diff_filler()" function
10101 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010104 typval_T *argvars UNUSED;
10105 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010106{
10107#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010109#endif
10110}
10111
10112/*
10113 * "diff_hlID()" function
10114 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010117 typval_T *argvars UNUSED;
10118 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010119{
10120#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010122 static linenr_T prev_lnum = 0;
10123 static int changedtick = 0;
10124 static int fnum = 0;
10125 static int change_start = 0;
10126 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010127 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010128 int filler_lines;
10129 int col;
10130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 if (lnum < 0) /* ignore type error in {lnum} arg */
10132 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010133 if (lnum != prev_lnum
10134 || changedtick != curbuf->b_changedtick
10135 || fnum != curbuf->b_fnum)
10136 {
10137 /* New line, buffer, change: need to get the values. */
10138 filler_lines = diff_check(curwin, lnum);
10139 if (filler_lines < 0)
10140 {
10141 if (filler_lines == -1)
10142 {
10143 change_start = MAXCOL;
10144 change_end = -1;
10145 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10146 hlID = HLF_ADD; /* added line */
10147 else
10148 hlID = HLF_CHD; /* changed line */
10149 }
10150 else
10151 hlID = HLF_ADD; /* added line */
10152 }
10153 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010154 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010155 prev_lnum = lnum;
10156 changedtick = curbuf->b_changedtick;
10157 fnum = curbuf->b_fnum;
10158 }
10159
10160 if (hlID == HLF_CHD || hlID == HLF_TXD)
10161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010163 if (col >= change_start && col <= change_end)
10164 hlID = HLF_TXD; /* changed text */
10165 else
10166 hlID = HLF_CHD; /* changed line */
10167 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010168 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010169#endif
10170}
10171
10172/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010173 * "empty({expr})" function
10174 */
10175 static void
10176f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010177 typval_T *argvars;
10178 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010179{
10180 int n;
10181
10182 switch (argvars[0].v_type)
10183 {
10184 case VAR_STRING:
10185 case VAR_FUNC:
10186 n = argvars[0].vval.v_string == NULL
10187 || *argvars[0].vval.v_string == NUL;
10188 break;
10189 case VAR_NUMBER:
10190 n = argvars[0].vval.v_number == 0;
10191 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010192#ifdef FEAT_FLOAT
10193 case VAR_FLOAT:
10194 n = argvars[0].vval.v_float == 0.0;
10195 break;
10196#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010197 case VAR_LIST:
10198 n = argvars[0].vval.v_list == NULL
10199 || argvars[0].vval.v_list->lv_first == NULL;
10200 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 case VAR_DICT:
10202 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010203 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010204 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010205 default:
10206 EMSG2(_(e_intern2), "f_empty()");
10207 n = 0;
10208 }
10209
10210 rettv->vval.v_number = n;
10211}
10212
10213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 * "escape({string}, {chars})" function
10215 */
10216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 typval_T *argvars;
10219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
10221 char_u buf[NUMBUFLEN];
10222
Bram Moolenaar758711c2005-02-02 23:11:38 +000010223 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10224 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010225 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226}
10227
10228/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010229 * "eval()" function
10230 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010231 static void
10232f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010233 typval_T *argvars;
10234 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010235{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010236 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 s = get_tv_string_chk(&argvars[0]);
10239 if (s != NULL)
10240 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010241
Bram Moolenaar615b9972015-01-14 17:15:05 +010010242 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010243 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10244 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010245 if (p != NULL && !aborting())
10246 EMSG2(_(e_invexpr2), p);
10247 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010248 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010249 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010251 else if (*s != NUL)
10252 EMSG(_(e_trailing));
10253}
10254
10255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 * "eventhandler()" function
10257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010259f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010260 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264}
10265
10266/*
10267 * "executable()" function
10268 */
10269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010270f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010271 typval_T *argvars;
10272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010274 char_u *name = get_tv_string(&argvars[0]);
10275
10276 /* Check in $PATH and also check directly if there is a directory name. */
10277 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10278 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010279}
10280
10281/*
10282 * "exepath()" function
10283 */
10284 static void
10285f_exepath(argvars, rettv)
10286 typval_T *argvars;
10287 typval_T *rettv;
10288{
10289 char_u *p = NULL;
10290
Bram Moolenaarb5971142015-03-21 17:32:19 +010010291 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010292 rettv->v_type = VAR_STRING;
10293 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294}
10295
10296/*
10297 * "exists()" function
10298 */
10299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010300f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 typval_T *argvars;
10302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303{
10304 char_u *p;
10305 char_u *name;
10306 int n = FALSE;
10307 int len = 0;
10308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 if (*p == '$') /* environment variable */
10311 {
10312 /* first try "normal" environment variables (fast) */
10313 if (mch_getenv(p + 1) != NULL)
10314 n = TRUE;
10315 else
10316 {
10317 /* try expanding things like $VIM and ${HOME} */
10318 p = expand_env_save(p);
10319 if (p != NULL && *p != '$')
10320 n = TRUE;
10321 vim_free(p);
10322 }
10323 }
10324 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010327 if (*skipwhite(p) != NUL)
10328 n = FALSE; /* trailing garbage */
10329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 else if (*p == '*') /* internal or user defined function */
10331 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010332 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 }
10334 else if (*p == ':')
10335 {
10336 n = cmd_exists(p + 1);
10337 }
10338 else if (*p == '#')
10339 {
10340#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010341 if (p[1] == '#')
10342 n = autocmd_supported(p + 2);
10343 else
10344 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345#endif
10346 }
10347 else /* internal variable */
10348 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010349 char_u *tofree;
10350 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010352 /* get_name_len() takes care of expanding curly braces */
10353 name = p;
10354 len = get_name_len(&p, &tofree, TRUE, FALSE);
10355 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010357 if (tofree != NULL)
10358 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010359 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010360 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010362 /* handle d.key, l[idx], f(expr) */
10363 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10364 if (n)
10365 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 }
10367 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010368 if (*p != NUL)
10369 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010371 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010374 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375}
10376
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010377#ifdef FEAT_FLOAT
10378/*
10379 * "exp()" function
10380 */
10381 static void
10382f_exp(argvars, rettv)
10383 typval_T *argvars;
10384 typval_T *rettv;
10385{
10386 float_T f;
10387
10388 rettv->v_type = VAR_FLOAT;
10389 if (get_float_arg(argvars, &f) == OK)
10390 rettv->vval.v_float = exp(f);
10391 else
10392 rettv->vval.v_float = 0.0;
10393}
10394#endif
10395
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396/*
10397 * "expand()" function
10398 */
10399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010400f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010401 typval_T *argvars;
10402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403{
10404 char_u *s;
10405 int len;
10406 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010407 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010409 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010410 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010413 if (argvars[1].v_type != VAR_UNKNOWN
10414 && argvars[2].v_type != VAR_UNKNOWN
10415 && get_tv_number_chk(&argvars[2], &error)
10416 && !error)
10417 {
10418 rettv->v_type = VAR_LIST;
10419 rettv->vval.v_list = NULL;
10420 }
10421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 if (*s == '%' || *s == '#' || *s == '<')
10424 {
10425 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010426 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010428 if (rettv->v_type == VAR_LIST)
10429 {
10430 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10431 list_append_string(rettv->vval.v_list, result, -1);
10432 else
10433 vim_free(result);
10434 }
10435 else
10436 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 }
10438 else
10439 {
10440 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010441 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 if (argvars[1].v_type != VAR_UNKNOWN
10443 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010444 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 if (!error)
10446 {
10447 ExpandInit(&xpc);
10448 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010449 if (p_wic)
10450 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010451 if (rettv->v_type == VAR_STRING)
10452 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10453 options, WILD_ALL);
10454 else if (rettv_list_alloc(rettv) != FAIL)
10455 {
10456 int i;
10457
10458 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10459 for (i = 0; i < xpc.xp_numfiles; i++)
10460 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10461 ExpandCleanup(&xpc);
10462 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 }
10464 else
10465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 }
10467}
10468
10469/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010470 * Go over all entries in "d2" and add them to "d1".
10471 * When "action" is "error" then a duplicate key is an error.
10472 * When "action" is "force" then a duplicate key is overwritten.
10473 * Otherwise duplicate keys are ignored ("action" is "keep").
10474 */
10475 void
10476dict_extend(d1, d2, action)
10477 dict_T *d1;
10478 dict_T *d2;
10479 char_u *action;
10480{
10481 dictitem_T *di1;
10482 hashitem_T *hi2;
10483 int todo;
10484
10485 todo = (int)d2->dv_hashtab.ht_used;
10486 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10487 {
10488 if (!HASHITEM_EMPTY(hi2))
10489 {
10490 --todo;
10491 di1 = dict_find(d1, hi2->hi_key, -1);
10492 if (d1->dv_scope != 0)
10493 {
10494 /* Disallow replacing a builtin function in l: and g:.
10495 * Check the key to be valid when adding to any
10496 * scope. */
10497 if (d1->dv_scope == VAR_DEF_SCOPE
10498 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10499 && var_check_func_name(hi2->hi_key,
10500 di1 == NULL))
10501 break;
10502 if (!valid_varname(hi2->hi_key))
10503 break;
10504 }
10505 if (di1 == NULL)
10506 {
10507 di1 = dictitem_copy(HI2DI(hi2));
10508 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10509 dictitem_free(di1);
10510 }
10511 else if (*action == 'e')
10512 {
10513 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10514 break;
10515 }
10516 else if (*action == 'f' && HI2DI(hi2) != di1)
10517 {
10518 clear_tv(&di1->di_tv);
10519 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10520 }
10521 }
10522 }
10523}
10524
10525/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010526 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010527 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010528 */
10529 static void
10530f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010531 typval_T *argvars;
10532 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010533{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010534 char *arg_errmsg = N_("extend() argument");
10535
Bram Moolenaare9a41262005-01-15 22:18:47 +000010536 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010537 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010538 list_T *l1, *l2;
10539 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010540 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010542
Bram Moolenaare9a41262005-01-15 22:18:47 +000010543 l1 = argvars[0].vval.v_list;
10544 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010545 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010546 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 {
10548 if (argvars[2].v_type != VAR_UNKNOWN)
10549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010550 before = get_tv_number_chk(&argvars[2], &error);
10551 if (error)
10552 return; /* type error; errmsg already given */
10553
Bram Moolenaar758711c2005-02-02 23:11:38 +000010554 if (before == l1->lv_len)
10555 item = NULL;
10556 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010558 item = list_find(l1, before);
10559 if (item == NULL)
10560 {
10561 EMSGN(_(e_listidx), before);
10562 return;
10563 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564 }
10565 }
10566 else
10567 item = NULL;
10568 list_extend(l1, l2, item);
10569
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 copy_tv(&argvars[0], rettv);
10571 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010572 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10574 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010575 dict_T *d1, *d2;
10576 char_u *action;
10577 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578
10579 d1 = argvars[0].vval.v_dict;
10580 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010581 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010582 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 {
10584 /* Check the third argument. */
10585 if (argvars[2].v_type != VAR_UNKNOWN)
10586 {
10587 static char *(av[]) = {"keep", "force", "error"};
10588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 action = get_tv_string_chk(&argvars[2]);
10590 if (action == NULL)
10591 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 for (i = 0; i < 3; ++i)
10593 if (STRCMP(action, av[i]) == 0)
10594 break;
10595 if (i == 3)
10596 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010597 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010598 return;
10599 }
10600 }
10601 else
10602 action = (char_u *)"force";
10603
Bram Moolenaara9922d62013-05-30 13:01:18 +020010604 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 copy_tv(&argvars[0], rettv);
10607 }
10608 }
10609 else
10610 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010611}
10612
10613/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010614 * "feedkeys()" function
10615 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010616 static void
10617f_feedkeys(argvars, rettv)
10618 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010619 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010620{
10621 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010622 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010623 char_u *keys, *flags;
10624 char_u nbuf[NUMBUFLEN];
10625 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010626 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010627
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010628 /* This is not allowed in the sandbox. If the commands would still be
10629 * executed in the sandbox it would be OK, but it probably happens later,
10630 * when "sandbox" is no longer set. */
10631 if (check_secure())
10632 return;
10633
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010634 keys = get_tv_string(&argvars[0]);
10635 if (*keys != NUL)
10636 {
10637 if (argvars[1].v_type != VAR_UNKNOWN)
10638 {
10639 flags = get_tv_string_buf(&argvars[1], nbuf);
10640 for ( ; *flags != NUL; ++flags)
10641 {
10642 switch (*flags)
10643 {
10644 case 'n': remap = FALSE; break;
10645 case 'm': remap = TRUE; break;
10646 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010647 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010648 }
10649 }
10650 }
10651
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010652 /* Need to escape K_SPECIAL and CSI before putting the string in the
10653 * typeahead buffer. */
10654 keys_esc = vim_strsave_escape_csi(keys);
10655 if (keys_esc != NULL)
10656 {
10657 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010658 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010659 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010660 if (vgetc_busy)
10661 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010662 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010663 }
10664}
10665
10666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 * "filereadable()" function
10668 */
10669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *argvars;
10672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010674 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 char_u *p;
10676 int n;
10677
Bram Moolenaarc236c162008-07-13 17:41:49 +000010678#ifndef O_NONBLOCK
10679# define O_NONBLOCK 0
10680#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010682 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10683 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 {
10685 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010686 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 }
10688 else
10689 n = FALSE;
10690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692}
10693
10694/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010695 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 * rights to write into.
10697 */
10698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010700 typval_T *argvars;
10701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010703 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704}
10705
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010706static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010707
10708 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010709findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010710 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010711 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010712 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010713{
10714#ifdef FEAT_SEARCHPATH
10715 char_u *fname;
10716 char_u *fresult = NULL;
10717 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10718 char_u *p;
10719 char_u pathbuf[NUMBUFLEN];
10720 int count = 1;
10721 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010722 int error = FALSE;
10723#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010724
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010725 rettv->vval.v_string = NULL;
10726 rettv->v_type = VAR_STRING;
10727
10728#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010730
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010731 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010733 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10734 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010735 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 else
10737 {
10738 if (*p != NUL)
10739 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010742 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010743 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010744 }
10745
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010746 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10747 error = TRUE;
10748
10749 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010751 do
10752 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010753 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010754 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755 fresult = find_file_in_path_option(first ? fname : NULL,
10756 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010757 0, first, path,
10758 find_what,
10759 curbuf->b_ffname,
10760 find_what == FINDFILE_DIR
10761 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010762 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010763
10764 if (fresult != NULL && rettv->v_type == VAR_LIST)
10765 list_append_string(rettv->vval.v_list, fresult, -1);
10766
10767 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010768 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010769
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010770 if (rettv->v_type == VAR_STRING)
10771 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010772#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010773}
10774
Bram Moolenaar33570922005-01-25 22:26:29 +000010775static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10776static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010777
10778/*
10779 * Implementation of map() and filter().
10780 */
10781 static void
10782filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 typval_T *argvars;
10784 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010785 int map;
10786{
10787 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010788 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 listitem_T *li, *nli;
10790 list_T *l = NULL;
10791 dictitem_T *di;
10792 hashtab_T *ht;
10793 hashitem_T *hi;
10794 dict_T *d = NULL;
10795 typval_T save_val;
10796 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010798 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010799 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10800 char *arg_errmsg = (map ? N_("map() argument")
10801 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010802 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010803 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010804
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010806 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010807 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010808 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809 return;
10810 }
10811 else if (argvars[0].v_type == VAR_DICT)
10812 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010813 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010814 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815 return;
10816 }
10817 else
10818 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010819 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 return;
10821 }
10822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823 expr = get_tv_string_buf_chk(&argvars[1], buf);
10824 /* On type errors, the preceding call has already displayed an error
10825 * message. Avoid a misleading error message for an empty string that
10826 * was not passed as argument. */
10827 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010829 prepare_vimvar(VV_VAL, &save_val);
10830 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010831
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010832 /* We reset "did_emsg" to be able to detect whether an error
10833 * occurred during evaluation of the expression. */
10834 save_did_emsg = did_emsg;
10835 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010836
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010837 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 vimvars[VV_KEY].vv_type = VAR_STRING;
10841
10842 ht = &d->dv_hashtab;
10843 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010844 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 if (!HASHITEM_EMPTY(hi))
10848 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010849 int r;
10850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 --todo;
10852 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010853 if (tv_check_lock(di->di_tv.v_lock,
10854 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010855 break;
10856 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010857 r = filter_map_one(&di->di_tv, expr, map, &rem);
10858 clear_tv(&vimvars[VV_KEY].vv_tv);
10859 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010860 break;
10861 if (!map && rem)
10862 dictitem_remove(d, di);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 }
10864 }
10865 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010866 }
10867 else
10868 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010869 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 for (li = l->lv_first; li != NULL; li = nli)
10872 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010873 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010874 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010876 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010877 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010878 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010879 break;
10880 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010882 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010883 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010884 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010885
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010886 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010888
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010889 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891
10892 copy_tv(&argvars[0], rettv);
10893}
10894
10895 static int
10896filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010897 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010898 char_u *expr;
10899 int map;
10900 int *remp;
10901{
Bram Moolenaar33570922005-01-25 22:26:29 +000010902 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010903 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010904 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010905
Bram Moolenaar33570922005-01-25 22:26:29 +000010906 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 s = expr;
10908 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010909 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010910 if (*s != NUL) /* check for trailing chars after expr */
10911 {
10912 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010913 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010914 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010915 }
10916 if (map)
10917 {
10918 /* map(): replace the list item value */
10919 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010920 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010921 *tv = rettv;
10922 }
10923 else
10924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010925 int error = FALSE;
10926
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010928 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010929 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 /* On type error, nothing has been removed; return FAIL to stop the
10931 * loop. The error message was given by get_tv_number_chk(). */
10932 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010933 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010934 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010935 retval = OK;
10936theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010937 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010938 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010939}
10940
10941/*
10942 * "filter()" function
10943 */
10944 static void
10945f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010946 typval_T *argvars;
10947 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010948{
10949 filter_map(argvars, rettv, FALSE);
10950}
10951
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010953 * "finddir({fname}[, {path}[, {count}]])" function
10954 */
10955 static void
10956f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 typval_T *argvars;
10958 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010959{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010960 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010961}
10962
10963/*
10964 * "findfile({fname}[, {path}[, {count}]])" function
10965 */
10966 static void
10967f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010968 typval_T *argvars;
10969 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010970{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010971 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010972}
10973
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010974#ifdef FEAT_FLOAT
10975/*
10976 * "float2nr({float})" function
10977 */
10978 static void
10979f_float2nr(argvars, rettv)
10980 typval_T *argvars;
10981 typval_T *rettv;
10982{
10983 float_T f;
10984
10985 if (get_float_arg(argvars, &f) == OK)
10986 {
10987 if (f < -0x7fffffff)
10988 rettv->vval.v_number = -0x7fffffff;
10989 else if (f > 0x7fffffff)
10990 rettv->vval.v_number = 0x7fffffff;
10991 else
10992 rettv->vval.v_number = (varnumber_T)f;
10993 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010994}
10995
10996/*
10997 * "floor({float})" function
10998 */
10999 static void
11000f_floor(argvars, rettv)
11001 typval_T *argvars;
11002 typval_T *rettv;
11003{
11004 float_T f;
11005
11006 rettv->v_type = VAR_FLOAT;
11007 if (get_float_arg(argvars, &f) == OK)
11008 rettv->vval.v_float = floor(f);
11009 else
11010 rettv->vval.v_float = 0.0;
11011}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011012
11013/*
11014 * "fmod()" function
11015 */
11016 static void
11017f_fmod(argvars, rettv)
11018 typval_T *argvars;
11019 typval_T *rettv;
11020{
11021 float_T fx, fy;
11022
11023 rettv->v_type = VAR_FLOAT;
11024 if (get_float_arg(argvars, &fx) == OK
11025 && get_float_arg(&argvars[1], &fy) == OK)
11026 rettv->vval.v_float = fmod(fx, fy);
11027 else
11028 rettv->vval.v_float = 0.0;
11029}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011030#endif
11031
Bram Moolenaar0d660222005-01-07 21:51:51 +000011032/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011033 * "fnameescape({string})" function
11034 */
11035 static void
11036f_fnameescape(argvars, rettv)
11037 typval_T *argvars;
11038 typval_T *rettv;
11039{
11040 rettv->vval.v_string = vim_strsave_fnameescape(
11041 get_tv_string(&argvars[0]), FALSE);
11042 rettv->v_type = VAR_STRING;
11043}
11044
11045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 * "fnamemodify({fname}, {mods})" function
11047 */
11048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011050 typval_T *argvars;
11051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052{
11053 char_u *fname;
11054 char_u *mods;
11055 int usedlen = 0;
11056 int len;
11057 char_u *fbuf = NULL;
11058 char_u buf[NUMBUFLEN];
11059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 fname = get_tv_string_chk(&argvars[0]);
11061 mods = get_tv_string_buf_chk(&argvars[1], buf);
11062 if (fname == NULL || mods == NULL)
11063 fname = NULL;
11064 else
11065 {
11066 len = (int)STRLEN(fname);
11067 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011072 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 vim_free(fbuf);
11076}
11077
Bram Moolenaar33570922005-01-25 22:26:29 +000011078static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079
11080/*
11081 * "foldclosed()" function
11082 */
11083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011085 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011086 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011087 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089#ifdef FEAT_FOLDING
11090 linenr_T lnum;
11091 linenr_T first, last;
11092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11095 {
11096 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11097 {
11098 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 return;
11103 }
11104 }
11105#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107}
11108
11109/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011110 * "foldclosed()" function
11111 */
11112 static void
11113f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011114 typval_T *argvars;
11115 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011116{
11117 foldclosed_both(argvars, rettv, FALSE);
11118}
11119
11120/*
11121 * "foldclosedend()" function
11122 */
11123 static void
11124f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011125 typval_T *argvars;
11126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011127{
11128 foldclosed_both(argvars, rettv, TRUE);
11129}
11130
11131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 * "foldlevel()" function
11133 */
11134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011136 typval_T *argvars UNUSED;
11137 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138{
11139#ifdef FEAT_FOLDING
11140 linenr_T lnum;
11141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146}
11147
11148/*
11149 * "foldtext()" function
11150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011153 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155{
11156#ifdef FEAT_FOLDING
11157 linenr_T lnum;
11158 char_u *s;
11159 char_u *r;
11160 int len;
11161 char *txt;
11162#endif
11163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->v_type = VAR_STRING;
11165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011167 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11168 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11169 <= curbuf->b_ml.ml_line_count
11170 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 {
11172 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11174 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 {
11176 if (!linewhite(lnum))
11177 break;
11178 ++lnum;
11179 }
11180
11181 /* Find interesting text in this line. */
11182 s = skipwhite(ml_get(lnum));
11183 /* skip C comment-start */
11184 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011187 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011188 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011189 {
11190 s = skipwhite(ml_get(lnum + 1));
11191 if (*s == '*')
11192 s = skipwhite(s + 1);
11193 }
11194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 txt = _("+-%s%3ld lines: ");
11196 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011197 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 + 20 /* for %3ld */
11199 + STRLEN(s))); /* concatenated */
11200 if (r != NULL)
11201 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011202 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11203 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11204 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 len = (int)STRLEN(r);
11206 STRCAT(r, s);
11207 /* remove 'foldmarker' and 'commentstring' */
11208 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 }
11211 }
11212#endif
11213}
11214
11215/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011216 * "foldtextresult(lnum)" function
11217 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011220 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011222{
11223#ifdef FEAT_FOLDING
11224 linenr_T lnum;
11225 char_u *text;
11226 char_u buf[51];
11227 foldinfo_T foldinfo;
11228 int fold_count;
11229#endif
11230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 rettv->v_type = VAR_STRING;
11232 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011233#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011234 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 /* treat illegal types and illegal string values for {lnum} the same */
11236 if (lnum < 0)
11237 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011238 fold_count = foldedCount(curwin, lnum, &foldinfo);
11239 if (fold_count > 0)
11240 {
11241 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11242 &foldinfo, buf);
11243 if (text == buf)
11244 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011246 }
11247#endif
11248}
11249
11250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 * "foreground()" function
11252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011255 typval_T *argvars UNUSED;
11256 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258#ifdef FEAT_GUI
11259 if (gui.in_use)
11260 gui_mch_set_foreground();
11261#else
11262# ifdef WIN32
11263 win32_set_foreground();
11264# endif
11265#endif
11266}
11267
11268/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011269 * "function()" function
11270 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 typval_T *argvars;
11274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011275{
11276 char_u *s;
11277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011279 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011280 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011281 /* Don't check an autoload name for existence here. */
11282 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011283 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011284 else
11285 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011286 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011287 {
11288 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011289 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011290
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011291 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11292 * also be called from another script. Using trans_function_name()
11293 * would also work, but some plugins depend on the name being
11294 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011295 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011296 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011297 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011298 if (rettv->vval.v_string != NULL)
11299 {
11300 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011301 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011302 }
11303 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011304 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011305 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011307 }
11308}
11309
11310/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011311 * "garbagecollect()" function
11312 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011313 static void
11314f_garbagecollect(argvars, rettv)
11315 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011316 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011317{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011318 /* This is postponed until we are back at the toplevel, because we may be
11319 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11320 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011321
11322 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11323 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011324}
11325
11326/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011327 * "get()" function
11328 */
11329 static void
11330f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011331 typval_T *argvars;
11332 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011333{
Bram Moolenaar33570922005-01-25 22:26:29 +000011334 listitem_T *li;
11335 list_T *l;
11336 dictitem_T *di;
11337 dict_T *d;
11338 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011339
Bram Moolenaare9a41262005-01-15 22:18:47 +000011340 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011341 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011342 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011344 int error = FALSE;
11345
11346 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11347 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011348 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011350 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011351 else if (argvars[0].v_type == VAR_DICT)
11352 {
11353 if ((d = argvars[0].vval.v_dict) != NULL)
11354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011355 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011356 if (di != NULL)
11357 tv = &di->di_tv;
11358 }
11359 }
11360 else
11361 EMSG2(_(e_listdictarg), "get()");
11362
11363 if (tv == NULL)
11364 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011365 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011366 copy_tv(&argvars[2], rettv);
11367 }
11368 else
11369 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011370}
11371
Bram Moolenaar342337a2005-07-21 21:11:17 +000011372static 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 +000011373
11374/*
11375 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011376 * Return a range (from start to end) of lines in rettv from the specified
11377 * buffer.
11378 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011379 */
11380 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011381get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011382 buf_T *buf;
11383 linenr_T start;
11384 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011385 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011386 typval_T *rettv;
11387{
11388 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011389
Bram Moolenaar959a1432013-12-14 12:17:38 +010011390 rettv->v_type = VAR_STRING;
11391 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011392 if (retlist && rettv_list_alloc(rettv) == FAIL)
11393 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011394
11395 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11396 return;
11397
11398 if (!retlist)
11399 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011400 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11401 p = ml_get_buf(buf, start, FALSE);
11402 else
11403 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011404 rettv->vval.v_string = vim_strsave(p);
11405 }
11406 else
11407 {
11408 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011409 return;
11410
11411 if (start < 1)
11412 start = 1;
11413 if (end > buf->b_ml.ml_line_count)
11414 end = buf->b_ml.ml_line_count;
11415 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011416 if (list_append_string(rettv->vval.v_list,
11417 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011418 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011419 }
11420}
11421
11422/*
11423 * "getbufline()" function
11424 */
11425 static void
11426f_getbufline(argvars, rettv)
11427 typval_T *argvars;
11428 typval_T *rettv;
11429{
11430 linenr_T lnum;
11431 linenr_T end;
11432 buf_T *buf;
11433
11434 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11435 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011436 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011437 --emsg_off;
11438
Bram Moolenaar661b1822005-07-28 22:36:45 +000011439 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011440 if (argvars[2].v_type == VAR_UNKNOWN)
11441 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011442 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011443 end = get_tv_lnum_buf(&argvars[2], buf);
11444
Bram Moolenaar342337a2005-07-21 21:11:17 +000011445 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011446}
11447
Bram Moolenaar0d660222005-01-07 21:51:51 +000011448/*
11449 * "getbufvar()" function
11450 */
11451 static void
11452f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011453 typval_T *argvars;
11454 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011455{
11456 buf_T *buf;
11457 buf_T *save_curbuf;
11458 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011459 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011460 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11463 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011464 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011465 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011466
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011467 rettv->v_type = VAR_STRING;
11468 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011469
11470 if (buf != NULL && varname != NULL)
11471 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011472 /* set curbuf to be our buf, temporarily */
11473 save_curbuf = curbuf;
11474 curbuf = buf;
11475
Bram Moolenaar0d660222005-01-07 21:51:51 +000011476 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011477 {
11478 if (get_option_tv(&varname, rettv, TRUE) == OK)
11479 done = TRUE;
11480 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011481 else if (STRCMP(varname, "changedtick") == 0)
11482 {
11483 rettv->v_type = VAR_NUMBER;
11484 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011485 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011486 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011487 else
11488 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011489 /* Look up the variable. */
11490 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11491 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11492 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011493 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011494 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011496 done = TRUE;
11497 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011498 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011499
11500 /* restore previous notion of curbuf */
11501 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011502 }
11503
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011504 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11505 /* use the default value */
11506 copy_tv(&argvars[2], rettv);
11507
Bram Moolenaar0d660222005-01-07 21:51:51 +000011508 --emsg_off;
11509}
11510
11511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 * "getchar()" function
11513 */
11514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011516 typval_T *argvars;
11517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518{
11519 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011520 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011522 /* Position the cursor. Needed after a message that ends in a space. */
11523 windgoto(msg_row, msg_col);
11524
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 ++no_mapping;
11526 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011527 for (;;)
11528 {
11529 if (argvars[0].v_type == VAR_UNKNOWN)
11530 /* getchar(): blocking wait. */
11531 n = safe_vgetc();
11532 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11533 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011534 n = vpeekc_any();
11535 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011536 /* illegal argument or getchar(0) and no char avail: return zero */
11537 n = 0;
11538 else
11539 /* getchar(0) and char avail: return char */
11540 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011541
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011542 if (n == K_IGNORE)
11543 continue;
11544 break;
11545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 --no_mapping;
11547 --allow_keys;
11548
Bram Moolenaar219b8702006-11-01 14:32:36 +000011549 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11550 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11551 vimvars[VV_MOUSE_COL].vv_nr = 0;
11552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 if (IS_SPECIAL(n) || mod_mask != 0)
11555 {
11556 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11557 int i = 0;
11558
11559 /* Turn a special key into three bytes, plus modifier. */
11560 if (mod_mask != 0)
11561 {
11562 temp[i++] = K_SPECIAL;
11563 temp[i++] = KS_MODIFIER;
11564 temp[i++] = mod_mask;
11565 }
11566 if (IS_SPECIAL(n))
11567 {
11568 temp[i++] = K_SPECIAL;
11569 temp[i++] = K_SECOND(n);
11570 temp[i++] = K_THIRD(n);
11571 }
11572#ifdef FEAT_MBYTE
11573 else if (has_mbyte)
11574 i += (*mb_char2bytes)(n, temp + i);
11575#endif
11576 else
11577 temp[i++] = n;
11578 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 rettv->v_type = VAR_STRING;
11580 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011581
11582#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011583 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011584 {
11585 int row = mouse_row;
11586 int col = mouse_col;
11587 win_T *win;
11588 linenr_T lnum;
11589# ifdef FEAT_WINDOWS
11590 win_T *wp;
11591# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011592 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011593
11594 if (row >= 0 && col >= 0)
11595 {
11596 /* Find the window at the mouse coordinates and compute the
11597 * text position. */
11598 win = mouse_find_win(&row, &col);
11599 (void)mouse_comp_pos(win, &row, &col, &lnum);
11600# ifdef FEAT_WINDOWS
11601 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011602 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011603# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011604 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011605 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11606 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11607 }
11608 }
11609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 }
11611}
11612
11613/*
11614 * "getcharmod()" function
11615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011618 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622}
11623
11624/*
11625 * "getcmdline()" function
11626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011629 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->v_type = VAR_STRING;
11633 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634}
11635
11636/*
11637 * "getcmdpos()" function
11638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011640f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645}
11646
11647/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011648 * "getcmdtype()" function
11649 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011650 static void
11651f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011652 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011653 typval_T *rettv;
11654{
11655 rettv->v_type = VAR_STRING;
11656 rettv->vval.v_string = alloc(2);
11657 if (rettv->vval.v_string != NULL)
11658 {
11659 rettv->vval.v_string[0] = get_cmdline_type();
11660 rettv->vval.v_string[1] = NUL;
11661 }
11662}
11663
11664/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011665 * "getcmdwintype()" function
11666 */
11667 static void
11668f_getcmdwintype(argvars, rettv)
11669 typval_T *argvars UNUSED;
11670 typval_T *rettv;
11671{
11672 rettv->v_type = VAR_STRING;
11673 rettv->vval.v_string = NULL;
11674#ifdef FEAT_CMDWIN
11675 rettv->vval.v_string = alloc(2);
11676 if (rettv->vval.v_string != NULL)
11677 {
11678 rettv->vval.v_string[0] = cmdwin_type;
11679 rettv->vval.v_string[1] = NUL;
11680 }
11681#endif
11682}
11683
11684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 * "getcwd()" function
11686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011692 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011695 rettv->vval.v_string = NULL;
11696 cwd = alloc(MAXPATHL);
11697 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011699 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11700 {
11701 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011703 if (rettv->vval.v_string != NULL)
11704 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011706 }
11707 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708 }
11709}
11710
11711/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011712 * "getfontname()" function
11713 */
11714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011716 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011717 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011718{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719 rettv->v_type = VAR_STRING;
11720 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011721#ifdef FEAT_GUI
11722 if (gui.in_use)
11723 {
11724 GuiFont font;
11725 char_u *name = NULL;
11726
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011727 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011728 {
11729 /* Get the "Normal" font. Either the name saved by
11730 * hl_set_font_name() or from the font ID. */
11731 font = gui.norm_font;
11732 name = hl_get_font_name();
11733 }
11734 else
11735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011737 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11738 return;
11739 font = gui_mch_get_font(name, FALSE);
11740 if (font == NOFONT)
11741 return; /* Invalid font name, return empty string. */
11742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011744 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011745 gui_mch_free_font(font);
11746 }
11747#endif
11748}
11749
11750/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011751 * "getfperm({fname})" function
11752 */
11753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011755 typval_T *argvars;
11756 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011757{
11758 char_u *fname;
11759 struct stat st;
11760 char_u *perm = NULL;
11761 char_u flags[] = "rwx";
11762 int i;
11763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011767 if (mch_stat((char *)fname, &st) >= 0)
11768 {
11769 perm = vim_strsave((char_u *)"---------");
11770 if (perm != NULL)
11771 {
11772 for (i = 0; i < 9; i++)
11773 {
11774 if (st.st_mode & (1 << (8 - i)))
11775 perm[i] = flags[i % 3];
11776 }
11777 }
11778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011780}
11781
11782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 * "getfsize({fname})" function
11784 */
11785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011787 typval_T *argvars;
11788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789{
11790 char_u *fname;
11791 struct stat st;
11792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796
11797 if (mch_stat((char *)fname, &st) >= 0)
11798 {
11799 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011804
11805 /* non-perfect check for overflow */
11806 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11807 rettv->vval.v_number = -2;
11808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 }
11810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812}
11813
11814/*
11815 * "getftime({fname})" function
11816 */
11817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011819 typval_T *argvars;
11820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821{
11822 char_u *fname;
11823 struct stat st;
11824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826
11827 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831}
11832
11833/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011834 * "getftype({fname})" function
11835 */
11836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 typval_T *argvars;
11839 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011840{
11841 char_u *fname;
11842 struct stat st;
11843 char_u *type = NULL;
11844 char *t;
11845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011849 if (mch_lstat((char *)fname, &st) >= 0)
11850 {
11851#ifdef S_ISREG
11852 if (S_ISREG(st.st_mode))
11853 t = "file";
11854 else if (S_ISDIR(st.st_mode))
11855 t = "dir";
11856# ifdef S_ISLNK
11857 else if (S_ISLNK(st.st_mode))
11858 t = "link";
11859# endif
11860# ifdef S_ISBLK
11861 else if (S_ISBLK(st.st_mode))
11862 t = "bdev";
11863# endif
11864# ifdef S_ISCHR
11865 else if (S_ISCHR(st.st_mode))
11866 t = "cdev";
11867# endif
11868# ifdef S_ISFIFO
11869 else if (S_ISFIFO(st.st_mode))
11870 t = "fifo";
11871# endif
11872# ifdef S_ISSOCK
11873 else if (S_ISSOCK(st.st_mode))
11874 t = "fifo";
11875# endif
11876 else
11877 t = "other";
11878#else
11879# ifdef S_IFMT
11880 switch (st.st_mode & S_IFMT)
11881 {
11882 case S_IFREG: t = "file"; break;
11883 case S_IFDIR: t = "dir"; break;
11884# ifdef S_IFLNK
11885 case S_IFLNK: t = "link"; break;
11886# endif
11887# ifdef S_IFBLK
11888 case S_IFBLK: t = "bdev"; break;
11889# endif
11890# ifdef S_IFCHR
11891 case S_IFCHR: t = "cdev"; break;
11892# endif
11893# ifdef S_IFIFO
11894 case S_IFIFO: t = "fifo"; break;
11895# endif
11896# ifdef S_IFSOCK
11897 case S_IFSOCK: t = "socket"; break;
11898# endif
11899 default: t = "other";
11900 }
11901# else
11902 if (mch_isdir(fname))
11903 t = "dir";
11904 else
11905 t = "file";
11906# endif
11907#endif
11908 type = vim_strsave((char_u *)t);
11909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011910 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011911}
11912
11913/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011914 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011915 */
11916 static void
11917f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *argvars;
11919 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011920{
11921 linenr_T lnum;
11922 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011923 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011924
11925 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011926 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011927 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011928 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011929 retlist = FALSE;
11930 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011931 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011932 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011933 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011934 retlist = TRUE;
11935 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011936
Bram Moolenaar342337a2005-07-21 21:11:17 +000011937 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011938}
11939
11940/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011941 * "getmatches()" function
11942 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011943 static void
11944f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011945 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011946 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011947{
11948#ifdef FEAT_SEARCH_EXTRA
11949 dict_T *dict;
11950 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011951 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011952
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011953 if (rettv_list_alloc(rettv) == OK)
11954 {
11955 while (cur != NULL)
11956 {
11957 dict = dict_alloc();
11958 if (dict == NULL)
11959 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011960 if (cur->match.regprog == NULL)
11961 {
11962 /* match added with matchaddpos() */
11963 for (i = 0; i < MAXPOSMATCH; ++i)
11964 {
11965 llpos_T *llpos;
11966 char buf[6];
11967 list_T *l;
11968
11969 llpos = &cur->pos.pos[i];
11970 if (llpos->lnum == 0)
11971 break;
11972 l = list_alloc();
11973 if (l == NULL)
11974 break;
11975 list_append_number(l, (varnumber_T)llpos->lnum);
11976 if (llpos->col > 0)
11977 {
11978 list_append_number(l, (varnumber_T)llpos->col);
11979 list_append_number(l, (varnumber_T)llpos->len);
11980 }
11981 sprintf(buf, "pos%d", i + 1);
11982 dict_add_list(dict, buf, l);
11983 }
11984 }
11985 else
11986 {
11987 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11988 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011989 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011990 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11991 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11992 list_append_dict(rettv->vval.v_list, dict);
11993 cur = cur->next;
11994 }
11995 }
11996#endif
11997}
11998
11999/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012000 * "getpid()" function
12001 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012002 static void
12003f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012004 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012005 typval_T *rettv;
12006{
12007 rettv->vval.v_number = mch_get_pid();
12008}
12009
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012010static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12011
12012/*
12013 * "getcurpos()" function
12014 */
12015 static void
12016f_getcurpos(argvars, rettv)
12017 typval_T *argvars;
12018 typval_T *rettv;
12019{
12020 getpos_both(argvars, rettv, TRUE);
12021}
12022
Bram Moolenaar18081e32008-02-20 19:11:07 +000012023/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012024 * "getpos(string)" function
12025 */
12026 static void
12027f_getpos(argvars, rettv)
12028 typval_T *argvars;
12029 typval_T *rettv;
12030{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012031 getpos_both(argvars, rettv, FALSE);
12032}
12033
12034 static void
12035getpos_both(argvars, rettv, getcurpos)
12036 typval_T *argvars;
12037 typval_T *rettv;
12038 int getcurpos;
12039{
Bram Moolenaara5525202006-03-02 22:52:09 +000012040 pos_T *fp;
12041 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012042 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012043
12044 if (rettv_list_alloc(rettv) == OK)
12045 {
12046 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012047 if (getcurpos)
12048 fp = &curwin->w_cursor;
12049 else
12050 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012051 if (fnum != -1)
12052 list_append_number(l, (varnumber_T)fnum);
12053 else
12054 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012055 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12056 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012057 list_append_number(l, (fp != NULL)
12058 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012059 : (varnumber_T)0);
12060 list_append_number(l,
12061#ifdef FEAT_VIRTUALEDIT
12062 (fp != NULL) ? (varnumber_T)fp->coladd :
12063#endif
12064 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012065 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012066 list_append_number(l, curwin->w_curswant == MAXCOL ?
12067 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012068 }
12069 else
12070 rettv->vval.v_number = FALSE;
12071}
12072
12073/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012074 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012075 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012076 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012077f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012078 typval_T *argvars UNUSED;
12079 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012080{
12081#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012082 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012083#endif
12084
Bram Moolenaar2641f772005-03-25 21:58:17 +000012085#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012086 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012087 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012088 wp = NULL;
12089 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12090 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012091 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012092 if (wp == NULL)
12093 return;
12094 }
12095
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012096 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012097 }
12098#endif
12099}
12100
12101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 * "getreg()" function
12103 */
12104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T *argvars;
12107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108{
12109 char_u *strregname;
12110 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012111 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012112 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012113 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012115 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012117 strregname = get_tv_string_chk(&argvars[0]);
12118 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012119 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012121 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012122 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12123 return_list = get_tv_number_chk(&argvars[2], &error);
12124 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012127 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012128
12129 if (error)
12130 return;
12131
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 regname = (strregname == NULL ? '"' : *strregname);
12133 if (regname == 0)
12134 regname = '"';
12135
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012136 if (return_list)
12137 {
12138 rettv->v_type = VAR_LIST;
12139 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12140 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012141 if (rettv->vval.v_list != NULL)
12142 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012143 }
12144 else
12145 {
12146 rettv->v_type = VAR_STRING;
12147 rettv->vval.v_string = get_reg_contents(regname,
12148 arg2 ? GREG_EXPR_SRC : 0);
12149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150}
12151
12152/*
12153 * "getregtype()" function
12154 */
12155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012157 typval_T *argvars;
12158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159{
12160 char_u *strregname;
12161 int regname;
12162 char_u buf[NUMBUFLEN + 2];
12163 long reglen = 0;
12164
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012165 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012166 {
12167 strregname = get_tv_string_chk(&argvars[0]);
12168 if (strregname == NULL) /* type error; errmsg already given */
12169 {
12170 rettv->v_type = VAR_STRING;
12171 rettv->vval.v_string = NULL;
12172 return;
12173 }
12174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 else
12176 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012177 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178
12179 regname = (strregname == NULL ? '"' : *strregname);
12180 if (regname == 0)
12181 regname = '"';
12182
12183 buf[0] = NUL;
12184 buf[1] = NUL;
12185 switch (get_reg_type(regname, &reglen))
12186 {
12187 case MLINE: buf[0] = 'V'; break;
12188 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 case MBLOCK:
12190 buf[0] = Ctrl_V;
12191 sprintf((char *)buf + 1, "%ld", reglen + 1);
12192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->v_type = VAR_STRING;
12195 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196}
12197
12198/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012199 * "gettabvar()" function
12200 */
12201 static void
12202f_gettabvar(argvars, rettv)
12203 typval_T *argvars;
12204 typval_T *rettv;
12205{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012206 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012207 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012208 dictitem_T *v;
12209 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012210 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012211
12212 rettv->v_type = VAR_STRING;
12213 rettv->vval.v_string = NULL;
12214
12215 varname = get_tv_string_chk(&argvars[1]);
12216 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12217 if (tp != NULL && varname != NULL)
12218 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012219 /* Set tp to be our tabpage, temporarily. Also set the window to the
12220 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012221 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12222 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012223 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012224 /* look up the variable */
12225 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12226 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12227 if (v != NULL)
12228 {
12229 copy_tv(&v->di_tv, rettv);
12230 done = TRUE;
12231 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012232 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012233
12234 /* restore previous notion of curwin */
12235 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012236 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012237
12238 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012239 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012240}
12241
12242/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012243 * "gettabwinvar()" function
12244 */
12245 static void
12246f_gettabwinvar(argvars, rettv)
12247 typval_T *argvars;
12248 typval_T *rettv;
12249{
12250 getwinvar(argvars, rettv, 1);
12251}
12252
12253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254 * "getwinposx()" function
12255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012258 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262#ifdef FEAT_GUI
12263 if (gui.in_use)
12264 {
12265 int x, y;
12266
12267 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 }
12270#endif
12271}
12272
12273/*
12274 * "getwinposy()" function
12275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012277f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012278 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282#ifdef FEAT_GUI
12283 if (gui.in_use)
12284 {
12285 int x, y;
12286
12287 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 }
12290#endif
12291}
12292
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012293/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012294 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012295 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012296 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012297find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012298 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012299 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012300{
12301#ifdef FEAT_WINDOWS
12302 win_T *wp;
12303#endif
12304 int nr;
12305
12306 nr = get_tv_number_chk(vp, NULL);
12307
12308#ifdef FEAT_WINDOWS
12309 if (nr < 0)
12310 return NULL;
12311 if (nr == 0)
12312 return curwin;
12313
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012314 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12315 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012316 if (--nr <= 0)
12317 break;
12318 return wp;
12319#else
12320 if (nr == 0 || nr == 1)
12321 return curwin;
12322 return NULL;
12323#endif
12324}
12325
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326/*
12327 * "getwinvar()" function
12328 */
12329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012331 typval_T *argvars;
12332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012334 getwinvar(argvars, rettv, 0);
12335}
12336
12337/*
12338 * getwinvar() and gettabwinvar()
12339 */
12340 static void
12341getwinvar(argvars, rettv, off)
12342 typval_T *argvars;
12343 typval_T *rettv;
12344 int off; /* 1 for gettabwinvar() */
12345{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 win_T *win, *oldcurwin;
12347 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012348 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012349 tabpage_T *tp = NULL;
12350 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012351 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012353#ifdef FEAT_WINDOWS
12354 if (off == 1)
12355 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12356 else
12357 tp = curtab;
12358#endif
12359 win = find_win_by_nr(&argvars[off], tp);
12360 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012361 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012363 rettv->v_type = VAR_STRING;
12364 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365
12366 if (win != NULL && varname != NULL)
12367 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012368 /* Set curwin to be our win, temporarily. Also set the tabpage,
12369 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012370 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012371 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012372 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012373 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012374 if (get_option_tv(&varname, rettv, 1) == OK)
12375 done = TRUE;
12376 }
12377 else
12378 {
12379 /* Look up the variable. */
12380 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12381 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12382 varname, FALSE);
12383 if (v != NULL)
12384 {
12385 copy_tv(&v->di_tv, rettv);
12386 done = TRUE;
12387 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012390
12391 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012392 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393 }
12394
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012395 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12396 /* use the default return value */
12397 copy_tv(&argvars[off + 2], rettv);
12398
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399 --emsg_off;
12400}
12401
12402/*
12403 * "glob()" function
12404 */
12405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012407 typval_T *argvars;
12408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012410 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012412 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012414 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012415 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012416 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012417 if (argvars[1].v_type != VAR_UNKNOWN)
12418 {
12419 if (get_tv_number_chk(&argvars[1], &error))
12420 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012421 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012422 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012423 if (get_tv_number_chk(&argvars[2], &error))
12424 {
12425 rettv->v_type = VAR_LIST;
12426 rettv->vval.v_list = NULL;
12427 }
12428 if (argvars[3].v_type != VAR_UNKNOWN
12429 && get_tv_number_chk(&argvars[3], &error))
12430 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012431 }
12432 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012433 if (!error)
12434 {
12435 ExpandInit(&xpc);
12436 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012437 if (p_wic)
12438 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012439 if (rettv->v_type == VAR_STRING)
12440 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012441 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012442 else if (rettv_list_alloc(rettv) != FAIL)
12443 {
12444 int i;
12445
12446 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12447 NULL, options, WILD_ALL_KEEP);
12448 for (i = 0; i < xpc.xp_numfiles; i++)
12449 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12450
12451 ExpandCleanup(&xpc);
12452 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012453 }
12454 else
12455 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456}
12457
12458/*
12459 * "globpath()" function
12460 */
12461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012462f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012463 typval_T *argvars;
12464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012466 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012469 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012470 garray_T ga;
12471 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012473 /* When the optional second argument is non-zero, don't remove matches
12474 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012476 if (argvars[2].v_type != VAR_UNKNOWN)
12477 {
12478 if (get_tv_number_chk(&argvars[2], &error))
12479 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012480 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012481 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012482 if (get_tv_number_chk(&argvars[3], &error))
12483 {
12484 rettv->v_type = VAR_LIST;
12485 rettv->vval.v_list = NULL;
12486 }
12487 if (argvars[4].v_type != VAR_UNKNOWN
12488 && get_tv_number_chk(&argvars[4], &error))
12489 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012490 }
12491 }
12492 if (file != NULL && !error)
12493 {
12494 ga_init2(&ga, (int)sizeof(char_u *), 10);
12495 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12496 if (rettv->v_type == VAR_STRING)
12497 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12498 else if (rettv_list_alloc(rettv) != FAIL)
12499 for (i = 0; i < ga.ga_len; ++i)
12500 list_append_string(rettv->vval.v_list,
12501 ((char_u **)(ga.ga_data))[i], -1);
12502 ga_clear_strings(&ga);
12503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012505 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506}
12507
12508/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012509 * "glob2regpat()" function
12510 */
12511 static void
12512f_glob2regpat(argvars, rettv)
12513 typval_T *argvars;
12514 typval_T *rettv;
12515{
12516 char_u *pat = get_tv_string_chk(&argvars[0]);
12517
12518 rettv->v_type = VAR_STRING;
12519 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12520}
12521
12522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 * "has()" function
12524 */
12525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012527 typval_T *argvars;
12528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529{
12530 int i;
12531 char_u *name;
12532 int n = FALSE;
12533 static char *(has_list[]) =
12534 {
12535#ifdef AMIGA
12536 "amiga",
12537# ifdef FEAT_ARP
12538 "arp",
12539# endif
12540#endif
12541#ifdef __BEOS__
12542 "beos",
12543#endif
12544#ifdef MSDOS
12545# ifdef DJGPP
12546 "dos32",
12547# else
12548 "dos16",
12549# endif
12550#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012551#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 "mac",
12553#endif
12554#if defined(MACOS_X_UNIX)
12555 "macunix",
12556#endif
12557#ifdef OS2
12558 "os2",
12559#endif
12560#ifdef __QNX__
12561 "qnx",
12562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563#ifdef UNIX
12564 "unix",
12565#endif
12566#ifdef VMS
12567 "vms",
12568#endif
12569#ifdef WIN16
12570 "win16",
12571#endif
12572#ifdef WIN32
12573 "win32",
12574#endif
12575#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12576 "win32unix",
12577#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012578#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579 "win64",
12580#endif
12581#ifdef EBCDIC
12582 "ebcdic",
12583#endif
12584#ifndef CASE_INSENSITIVE_FILENAME
12585 "fname_case",
12586#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012587#ifdef HAVE_ACL
12588 "acl",
12589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590#ifdef FEAT_ARABIC
12591 "arabic",
12592#endif
12593#ifdef FEAT_AUTOCMD
12594 "autocmd",
12595#endif
12596#ifdef FEAT_BEVAL
12597 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012598# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12599 "balloon_multiline",
12600# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601#endif
12602#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12603 "builtin_terms",
12604# ifdef ALL_BUILTIN_TCAPS
12605 "all_builtin_terms",
12606# endif
12607#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012608#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12609 || defined(FEAT_GUI_W32) \
12610 || defined(FEAT_GUI_MOTIF))
12611 "browsefilter",
12612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613#ifdef FEAT_BYTEOFF
12614 "byte_offset",
12615#endif
12616#ifdef FEAT_CINDENT
12617 "cindent",
12618#endif
12619#ifdef FEAT_CLIENTSERVER
12620 "clientserver",
12621#endif
12622#ifdef FEAT_CLIPBOARD
12623 "clipboard",
12624#endif
12625#ifdef FEAT_CMDL_COMPL
12626 "cmdline_compl",
12627#endif
12628#ifdef FEAT_CMDHIST
12629 "cmdline_hist",
12630#endif
12631#ifdef FEAT_COMMENTS
12632 "comments",
12633#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012634#ifdef FEAT_CONCEAL
12635 "conceal",
12636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637#ifdef FEAT_CRYPT
12638 "cryptv",
12639#endif
12640#ifdef FEAT_CSCOPE
12641 "cscope",
12642#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012643#ifdef FEAT_CURSORBIND
12644 "cursorbind",
12645#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012646#ifdef CURSOR_SHAPE
12647 "cursorshape",
12648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649#ifdef DEBUG
12650 "debug",
12651#endif
12652#ifdef FEAT_CON_DIALOG
12653 "dialog_con",
12654#endif
12655#ifdef FEAT_GUI_DIALOG
12656 "dialog_gui",
12657#endif
12658#ifdef FEAT_DIFF
12659 "diff",
12660#endif
12661#ifdef FEAT_DIGRAPHS
12662 "digraphs",
12663#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012664#ifdef FEAT_DIRECTX
12665 "directx",
12666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667#ifdef FEAT_DND
12668 "dnd",
12669#endif
12670#ifdef FEAT_EMACS_TAGS
12671 "emacs_tags",
12672#endif
12673 "eval", /* always present, of course! */
12674#ifdef FEAT_EX_EXTRA
12675 "ex_extra",
12676#endif
12677#ifdef FEAT_SEARCH_EXTRA
12678 "extra_search",
12679#endif
12680#ifdef FEAT_FKMAP
12681 "farsi",
12682#endif
12683#ifdef FEAT_SEARCHPATH
12684 "file_in_path",
12685#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012686#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012687 "filterpipe",
12688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689#ifdef FEAT_FIND_ID
12690 "find_in_path",
12691#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012692#ifdef FEAT_FLOAT
12693 "float",
12694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695#ifdef FEAT_FOLDING
12696 "folding",
12697#endif
12698#ifdef FEAT_FOOTER
12699 "footer",
12700#endif
12701#if !defined(USE_SYSTEM) && defined(UNIX)
12702 "fork",
12703#endif
12704#ifdef FEAT_GETTEXT
12705 "gettext",
12706#endif
12707#ifdef FEAT_GUI
12708 "gui",
12709#endif
12710#ifdef FEAT_GUI_ATHENA
12711# ifdef FEAT_GUI_NEXTAW
12712 "gui_neXtaw",
12713# else
12714 "gui_athena",
12715# endif
12716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717#ifdef FEAT_GUI_GTK
12718 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012721#ifdef FEAT_GUI_GNOME
12722 "gui_gnome",
12723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724#ifdef FEAT_GUI_MAC
12725 "gui_mac",
12726#endif
12727#ifdef FEAT_GUI_MOTIF
12728 "gui_motif",
12729#endif
12730#ifdef FEAT_GUI_PHOTON
12731 "gui_photon",
12732#endif
12733#ifdef FEAT_GUI_W16
12734 "gui_win16",
12735#endif
12736#ifdef FEAT_GUI_W32
12737 "gui_win32",
12738#endif
12739#ifdef FEAT_HANGULIN
12740 "hangul_input",
12741#endif
12742#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12743 "iconv",
12744#endif
12745#ifdef FEAT_INS_EXPAND
12746 "insert_expand",
12747#endif
12748#ifdef FEAT_JUMPLIST
12749 "jumplist",
12750#endif
12751#ifdef FEAT_KEYMAP
12752 "keymap",
12753#endif
12754#ifdef FEAT_LANGMAP
12755 "langmap",
12756#endif
12757#ifdef FEAT_LIBCALL
12758 "libcall",
12759#endif
12760#ifdef FEAT_LINEBREAK
12761 "linebreak",
12762#endif
12763#ifdef FEAT_LISP
12764 "lispindent",
12765#endif
12766#ifdef FEAT_LISTCMDS
12767 "listcmds",
12768#endif
12769#ifdef FEAT_LOCALMAP
12770 "localmap",
12771#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012772#ifdef FEAT_LUA
12773# ifndef DYNAMIC_LUA
12774 "lua",
12775# endif
12776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777#ifdef FEAT_MENU
12778 "menu",
12779#endif
12780#ifdef FEAT_SESSION
12781 "mksession",
12782#endif
12783#ifdef FEAT_MODIFY_FNAME
12784 "modify_fname",
12785#endif
12786#ifdef FEAT_MOUSE
12787 "mouse",
12788#endif
12789#ifdef FEAT_MOUSESHAPE
12790 "mouseshape",
12791#endif
12792#if defined(UNIX) || defined(VMS)
12793# ifdef FEAT_MOUSE_DEC
12794 "mouse_dec",
12795# endif
12796# ifdef FEAT_MOUSE_GPM
12797 "mouse_gpm",
12798# endif
12799# ifdef FEAT_MOUSE_JSB
12800 "mouse_jsbterm",
12801# endif
12802# ifdef FEAT_MOUSE_NET
12803 "mouse_netterm",
12804# endif
12805# ifdef FEAT_MOUSE_PTERM
12806 "mouse_pterm",
12807# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012808# ifdef FEAT_MOUSE_SGR
12809 "mouse_sgr",
12810# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012811# ifdef FEAT_SYSMOUSE
12812 "mouse_sysmouse",
12813# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012814# ifdef FEAT_MOUSE_URXVT
12815 "mouse_urxvt",
12816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817# ifdef FEAT_MOUSE_XTERM
12818 "mouse_xterm",
12819# endif
12820#endif
12821#ifdef FEAT_MBYTE
12822 "multi_byte",
12823#endif
12824#ifdef FEAT_MBYTE_IME
12825 "multi_byte_ime",
12826#endif
12827#ifdef FEAT_MULTI_LANG
12828 "multi_lang",
12829#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012830#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012831#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012832 "mzscheme",
12833#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835#ifdef FEAT_OLE
12836 "ole",
12837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838#ifdef FEAT_PATH_EXTRA
12839 "path_extra",
12840#endif
12841#ifdef FEAT_PERL
12842#ifndef DYNAMIC_PERL
12843 "perl",
12844#endif
12845#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012846#ifdef FEAT_PERSISTENT_UNDO
12847 "persistent_undo",
12848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849#ifdef FEAT_PYTHON
12850#ifndef DYNAMIC_PYTHON
12851 "python",
12852#endif
12853#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012854#ifdef FEAT_PYTHON3
12855#ifndef DYNAMIC_PYTHON3
12856 "python3",
12857#endif
12858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859#ifdef FEAT_POSTSCRIPT
12860 "postscript",
12861#endif
12862#ifdef FEAT_PRINTER
12863 "printer",
12864#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012865#ifdef FEAT_PROFILE
12866 "profile",
12867#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012868#ifdef FEAT_RELTIME
12869 "reltime",
12870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871#ifdef FEAT_QUICKFIX
12872 "quickfix",
12873#endif
12874#ifdef FEAT_RIGHTLEFT
12875 "rightleft",
12876#endif
12877#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12878 "ruby",
12879#endif
12880#ifdef FEAT_SCROLLBIND
12881 "scrollbind",
12882#endif
12883#ifdef FEAT_CMDL_INFO
12884 "showcmd",
12885 "cmdline_info",
12886#endif
12887#ifdef FEAT_SIGNS
12888 "signs",
12889#endif
12890#ifdef FEAT_SMARTINDENT
12891 "smartindent",
12892#endif
12893#ifdef FEAT_SNIFF
12894 "sniff",
12895#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012896#ifdef STARTUPTIME
12897 "startuptime",
12898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899#ifdef FEAT_STL_OPT
12900 "statusline",
12901#endif
12902#ifdef FEAT_SUN_WORKSHOP
12903 "sun_workshop",
12904#endif
12905#ifdef FEAT_NETBEANS_INTG
12906 "netbeans_intg",
12907#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012908#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012909 "spell",
12910#endif
12911#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 "syntax",
12913#endif
12914#if defined(USE_SYSTEM) || !defined(UNIX)
12915 "system",
12916#endif
12917#ifdef FEAT_TAG_BINS
12918 "tag_binary",
12919#endif
12920#ifdef FEAT_TAG_OLDSTATIC
12921 "tag_old_static",
12922#endif
12923#ifdef FEAT_TAG_ANYWHITE
12924 "tag_any_white",
12925#endif
12926#ifdef FEAT_TCL
12927# ifndef DYNAMIC_TCL
12928 "tcl",
12929# endif
12930#endif
12931#ifdef TERMINFO
12932 "terminfo",
12933#endif
12934#ifdef FEAT_TERMRESPONSE
12935 "termresponse",
12936#endif
12937#ifdef FEAT_TEXTOBJ
12938 "textobjects",
12939#endif
12940#ifdef HAVE_TGETENT
12941 "tgetent",
12942#endif
12943#ifdef FEAT_TITLE
12944 "title",
12945#endif
12946#ifdef FEAT_TOOLBAR
12947 "toolbar",
12948#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012949#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12950 "unnamedplus",
12951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952#ifdef FEAT_USR_CMDS
12953 "user-commands", /* was accidentally included in 5.4 */
12954 "user_commands",
12955#endif
12956#ifdef FEAT_VIMINFO
12957 "viminfo",
12958#endif
12959#ifdef FEAT_VERTSPLIT
12960 "vertsplit",
12961#endif
12962#ifdef FEAT_VIRTUALEDIT
12963 "virtualedit",
12964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966#ifdef FEAT_VISUALEXTRA
12967 "visualextra",
12968#endif
12969#ifdef FEAT_VREPLACE
12970 "vreplace",
12971#endif
12972#ifdef FEAT_WILDIGN
12973 "wildignore",
12974#endif
12975#ifdef FEAT_WILDMENU
12976 "wildmenu",
12977#endif
12978#ifdef FEAT_WINDOWS
12979 "windows",
12980#endif
12981#ifdef FEAT_WAK
12982 "winaltkeys",
12983#endif
12984#ifdef FEAT_WRITEBACKUP
12985 "writebackup",
12986#endif
12987#ifdef FEAT_XIM
12988 "xim",
12989#endif
12990#ifdef FEAT_XFONTSET
12991 "xfontset",
12992#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012993#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012994 "xpm",
12995 "xpm_w32", /* for backward compatibility */
12996#else
12997# if defined(HAVE_XPM)
12998 "xpm",
12999# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001#ifdef USE_XSMP
13002 "xsmp",
13003#endif
13004#ifdef USE_XSMP_INTERACT
13005 "xsmp_interact",
13006#endif
13007#ifdef FEAT_XCLIPBOARD
13008 "xterm_clipboard",
13009#endif
13010#ifdef FEAT_XTERM_SAVE
13011 "xterm_save",
13012#endif
13013#if defined(UNIX) && defined(FEAT_X11)
13014 "X11",
13015#endif
13016 NULL
13017 };
13018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 for (i = 0; has_list[i] != NULL; ++i)
13021 if (STRICMP(name, has_list[i]) == 0)
13022 {
13023 n = TRUE;
13024 break;
13025 }
13026
13027 if (n == FALSE)
13028 {
13029 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013030 {
13031 if (name[5] == '-'
13032 && STRLEN(name) > 11
13033 && vim_isdigit(name[6])
13034 && vim_isdigit(name[8])
13035 && vim_isdigit(name[10]))
13036 {
13037 int major = atoi((char *)name + 6);
13038 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013039
13040 /* Expect "patch-9.9.01234". */
13041 n = (major < VIM_VERSION_MAJOR
13042 || (major == VIM_VERSION_MAJOR
13043 && (minor < VIM_VERSION_MINOR
13044 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013045 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013046 }
13047 else
13048 n = has_patch(atoi((char *)name + 5));
13049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 else if (STRICMP(name, "vim_starting") == 0)
13051 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013052#ifdef FEAT_MBYTE
13053 else if (STRICMP(name, "multi_byte_encoding") == 0)
13054 n = has_mbyte;
13055#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013056#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13057 else if (STRICMP(name, "balloon_multiline") == 0)
13058 n = multiline_balloon_available();
13059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060#ifdef DYNAMIC_TCL
13061 else if (STRICMP(name, "tcl") == 0)
13062 n = tcl_enabled(FALSE);
13063#endif
13064#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13065 else if (STRICMP(name, "iconv") == 0)
13066 n = iconv_enabled(FALSE);
13067#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013068#ifdef DYNAMIC_LUA
13069 else if (STRICMP(name, "lua") == 0)
13070 n = lua_enabled(FALSE);
13071#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013072#ifdef DYNAMIC_MZSCHEME
13073 else if (STRICMP(name, "mzscheme") == 0)
13074 n = mzscheme_enabled(FALSE);
13075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076#ifdef DYNAMIC_RUBY
13077 else if (STRICMP(name, "ruby") == 0)
13078 n = ruby_enabled(FALSE);
13079#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013080#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081#ifdef DYNAMIC_PYTHON
13082 else if (STRICMP(name, "python") == 0)
13083 n = python_enabled(FALSE);
13084#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013085#endif
13086#ifdef FEAT_PYTHON3
13087#ifdef DYNAMIC_PYTHON3
13088 else if (STRICMP(name, "python3") == 0)
13089 n = python3_enabled(FALSE);
13090#endif
13091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092#ifdef DYNAMIC_PERL
13093 else if (STRICMP(name, "perl") == 0)
13094 n = perl_enabled(FALSE);
13095#endif
13096#ifdef FEAT_GUI
13097 else if (STRICMP(name, "gui_running") == 0)
13098 n = (gui.in_use || gui.starting);
13099# ifdef FEAT_GUI_W32
13100 else if (STRICMP(name, "gui_win32s") == 0)
13101 n = gui_is_win32s();
13102# endif
13103# ifdef FEAT_BROWSE
13104 else if (STRICMP(name, "browse") == 0)
13105 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13106# endif
13107#endif
13108#ifdef FEAT_SYN_HL
13109 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013110 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111#endif
13112#if defined(WIN3264)
13113 else if (STRICMP(name, "win95") == 0)
13114 n = mch_windows95();
13115#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013116#ifdef FEAT_NETBEANS_INTG
13117 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013118 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 }
13121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013122 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123}
13124
13125/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013126 * "has_key()" function
13127 */
13128 static void
13129f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013130 typval_T *argvars;
13131 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013132{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013133 if (argvars[0].v_type != VAR_DICT)
13134 {
13135 EMSG(_(e_dictreq));
13136 return;
13137 }
13138 if (argvars[0].vval.v_dict == NULL)
13139 return;
13140
13141 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013142 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013143}
13144
13145/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013146 * "haslocaldir()" function
13147 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013148 static void
13149f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013150 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013151 typval_T *rettv;
13152{
13153 rettv->vval.v_number = (curwin->w_localdir != NULL);
13154}
13155
13156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 * "hasmapto()" function
13158 */
13159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013161 typval_T *argvars;
13162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163{
13164 char_u *name;
13165 char_u *mode;
13166 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013167 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013170 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 mode = (char_u *)"nvo";
13172 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013175 if (argvars[2].v_type != VAR_UNKNOWN)
13176 abbr = get_tv_number(&argvars[2]);
13177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178
Bram Moolenaar2c932302006-03-18 21:42:09 +000013179 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183}
13184
13185/*
13186 * "histadd()" function
13187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013190 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192{
13193#ifdef FEAT_CMDHIST
13194 int histype;
13195 char_u *str;
13196 char_u buf[NUMBUFLEN];
13197#endif
13198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200 if (check_restricted() || check_secure())
13201 return;
13202#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013203 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13204 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 if (histype >= 0)
13206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 if (*str != NUL)
13209 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013210 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213 return;
13214 }
13215 }
13216#endif
13217}
13218
13219/*
13220 * "histdel()" function
13221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013223f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013224 typval_T *argvars UNUSED;
13225 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226{
13227#ifdef FEAT_CMDHIST
13228 int n;
13229 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013230 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13233 if (str == NULL)
13234 n = 0;
13235 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013237 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013238 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013240 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 else
13243 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013244 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 get_tv_string_buf(&argvars[1], buf));
13246 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247#endif
13248}
13249
13250/*
13251 * "histget()" function
13252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013255 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257{
13258#ifdef FEAT_CMDHIST
13259 int type;
13260 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13264 if (str == NULL)
13265 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013267 {
13268 type = get_histtype(str);
13269 if (argvars[1].v_type == VAR_UNKNOWN)
13270 idx = get_history_idx(type);
13271 else
13272 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13273 /* -1 on type error */
13274 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280}
13281
13282/*
13283 * "histnr()" function
13284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013286f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013287 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289{
13290 int i;
13291
13292#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013293 char_u *history = get_tv_string_chk(&argvars[0]);
13294
13295 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 if (i >= HIST_CMD && i < HIST_COUNT)
13297 i = get_history_idx(i);
13298 else
13299#endif
13300 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302}
13303
13304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 * "highlightID(name)" function
13306 */
13307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013309 typval_T *argvars;
13310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313}
13314
13315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013316 * "highlight_exists()" function
13317 */
13318 static void
13319f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013320 typval_T *argvars;
13321 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322{
13323 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13324}
13325
13326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 * "hostname()" function
13328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013331 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333{
13334 char_u hostname[256];
13335
13336 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337 rettv->v_type = VAR_STRING;
13338 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339}
13340
13341/*
13342 * iconv() function
13343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348{
13349#ifdef FEAT_MBYTE
13350 char_u buf1[NUMBUFLEN];
13351 char_u buf2[NUMBUFLEN];
13352 char_u *from, *to, *str;
13353 vimconv_T vimconv;
13354#endif
13355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013356 rettv->v_type = VAR_STRING;
13357 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358
13359#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360 str = get_tv_string(&argvars[0]);
13361 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13362 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 vimconv.vc_type = CONV_NONE;
13364 convert_setup(&vimconv, from, to);
13365
13366 /* If the encodings are equal, no conversion needed. */
13367 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371
13372 convert_setup(&vimconv, NULL, NULL);
13373 vim_free(from);
13374 vim_free(to);
13375#endif
13376}
13377
13378/*
13379 * "indent()" function
13380 */
13381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013382f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013383 typval_T *argvars;
13384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385{
13386 linenr_T lnum;
13387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013388 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013392 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393}
13394
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013395/*
13396 * "index()" function
13397 */
13398 static void
13399f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013400 typval_T *argvars;
13401 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013402{
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 list_T *l;
13404 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013405 long idx = 0;
13406 int ic = FALSE;
13407
13408 rettv->vval.v_number = -1;
13409 if (argvars[0].v_type != VAR_LIST)
13410 {
13411 EMSG(_(e_listreq));
13412 return;
13413 }
13414 l = argvars[0].vval.v_list;
13415 if (l != NULL)
13416 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013417 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013418 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013420 int error = FALSE;
13421
Bram Moolenaar758711c2005-02-02 23:11:38 +000013422 /* Start at specified item. Use the cached index that list_find()
13423 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013424 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013425 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013426 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013427 ic = get_tv_number_chk(&argvars[3], &error);
13428 if (error)
13429 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013430 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013431
Bram Moolenaar758711c2005-02-02 23:11:38 +000013432 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013433 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013434 {
13435 rettv->vval.v_number = idx;
13436 break;
13437 }
13438 }
13439}
13440
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441static int inputsecret_flag = 0;
13442
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013443static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13444
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013446 * This function is used by f_input() and f_inputdialog() functions. The third
13447 * argument to f_input() specifies the type of completion to use at the
13448 * prompt. The third argument to f_inputdialog() specifies the value to return
13449 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 */
13451 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013452get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 typval_T *argvars;
13454 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013455 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013457 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 char_u *p = NULL;
13459 int c;
13460 char_u buf[NUMBUFLEN];
13461 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013462 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013463 int xp_type = EXPAND_NOTHING;
13464 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013467 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468
13469#ifdef NO_CONSOLE_INPUT
13470 /* While starting up, there is no place to enter text. */
13471 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473#endif
13474
13475 cmd_silent = FALSE; /* Want to see the prompt. */
13476 if (prompt != NULL)
13477 {
13478 /* Only the part of the message after the last NL is considered as
13479 * prompt for the command line */
13480 p = vim_strrchr(prompt, '\n');
13481 if (p == NULL)
13482 p = prompt;
13483 else
13484 {
13485 ++p;
13486 c = *p;
13487 *p = NUL;
13488 msg_start();
13489 msg_clr_eos();
13490 msg_puts_attr(prompt, echo_attr);
13491 msg_didout = FALSE;
13492 msg_starthere();
13493 *p = c;
13494 }
13495 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 if (argvars[1].v_type != VAR_UNKNOWN)
13498 {
13499 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13500 if (defstr != NULL)
13501 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013503 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013504 {
13505 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013506 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013507 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013508
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013509 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013510 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013511
Bram Moolenaar4463f292005-09-25 22:20:24 +000013512 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13513 if (xp_name == NULL)
13514 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013515
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013516 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013517
Bram Moolenaar4463f292005-09-25 22:20:24 +000013518 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13519 &xp_arg) == FAIL)
13520 return;
13521 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013522 }
13523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013525 {
13526# ifdef FEAT_EX_EXTRA
13527 int save_ex_normal_busy = ex_normal_busy;
13528 ex_normal_busy = 0;
13529# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013531 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13532 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013533# ifdef FEAT_EX_EXTRA
13534 ex_normal_busy = save_ex_normal_busy;
13535# endif
13536 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013537 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013538 && argvars[1].v_type != VAR_UNKNOWN
13539 && argvars[2].v_type != VAR_UNKNOWN)
13540 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13541 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013542
13543 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 /* since the user typed this, no need to wait for return */
13546 need_wait_return = FALSE;
13547 msg_didout = FALSE;
13548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 cmd_silent = cmd_silent_save;
13550}
13551
13552/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013553 * "input()" function
13554 * Also handles inputsecret() when inputsecret is set.
13555 */
13556 static void
13557f_input(argvars, rettv)
13558 typval_T *argvars;
13559 typval_T *rettv;
13560{
13561 get_user_input(argvars, rettv, FALSE);
13562}
13563
13564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 * "inputdialog()" function
13566 */
13567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571{
13572#if defined(FEAT_GUI_TEXTDIALOG)
13573 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13574 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13575 {
13576 char_u *message;
13577 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013580 message = get_tv_string_chk(&argvars[0]);
13581 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013582 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013583 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 else
13585 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 if (message != NULL && defstr != NULL
13587 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013588 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 else
13591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013592 if (message != NULL && defstr != NULL
13593 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013594 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 rettv->vval.v_string = vim_strsave(
13596 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013598 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013600 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 }
13602 else
13603#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013604 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605}
13606
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013607/*
13608 * "inputlist()" function
13609 */
13610 static void
13611f_inputlist(argvars, rettv)
13612 typval_T *argvars;
13613 typval_T *rettv;
13614{
13615 listitem_T *li;
13616 int selected;
13617 int mouse_used;
13618
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013619#ifdef NO_CONSOLE_INPUT
13620 /* While starting up, there is no place to enter text. */
13621 if (no_console_input())
13622 return;
13623#endif
13624 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13625 {
13626 EMSG2(_(e_listarg), "inputlist()");
13627 return;
13628 }
13629
13630 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013631 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013632 lines_left = Rows; /* avoid more prompt */
13633 msg_scroll = TRUE;
13634 msg_clr_eos();
13635
13636 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13637 {
13638 msg_puts(get_tv_string(&li->li_tv));
13639 msg_putchar('\n');
13640 }
13641
13642 /* Ask for choice. */
13643 selected = prompt_for_number(&mouse_used);
13644 if (mouse_used)
13645 selected -= lines_left;
13646
13647 rettv->vval.v_number = selected;
13648}
13649
13650
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13652
13653/*
13654 * "inputrestore()" function
13655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013658 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660{
13661 if (ga_userinput.ga_len > 0)
13662 {
13663 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13665 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013666 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 }
13668 else if (p_verbose > 1)
13669 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013670 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 }
13673}
13674
13675/*
13676 * "inputsave()" function
13677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013680 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013683 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684 if (ga_grow(&ga_userinput, 1) == OK)
13685 {
13686 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13687 + ga_userinput.ga_len);
13688 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013689 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 }
13691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693}
13694
13695/*
13696 * "inputsecret()" function
13697 */
13698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013699f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 typval_T *argvars;
13701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702{
13703 ++cmdline_star;
13704 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 --cmdline_star;
13707 --inputsecret_flag;
13708}
13709
13710/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013711 * "insert()" function
13712 */
13713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013715 typval_T *argvars;
13716 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013717{
13718 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013719 listitem_T *item;
13720 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013721 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013722
13723 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013724 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013725 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013726 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013727 {
13728 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 before = get_tv_number_chk(&argvars[2], &error);
13730 if (error)
13731 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013732
Bram Moolenaar758711c2005-02-02 23:11:38 +000013733 if (before == l->lv_len)
13734 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013735 else
13736 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013737 item = list_find(l, before);
13738 if (item == NULL)
13739 {
13740 EMSGN(_(e_listidx), before);
13741 l = NULL;
13742 }
13743 }
13744 if (l != NULL)
13745 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013746 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013747 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013748 }
13749 }
13750}
13751
13752/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013753 * "invert(expr)" function
13754 */
13755 static void
13756f_invert(argvars, rettv)
13757 typval_T *argvars;
13758 typval_T *rettv;
13759{
13760 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13761}
13762
13763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 * "isdirectory()" function
13765 */
13766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013767f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013768 typval_T *argvars;
13769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013771 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772}
13773
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013774/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013775 * "islocked()" function
13776 */
13777 static void
13778f_islocked(argvars, rettv)
13779 typval_T *argvars;
13780 typval_T *rettv;
13781{
13782 lval_T lv;
13783 char_u *end;
13784 dictitem_T *di;
13785
13786 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013787 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13788 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013789 if (end != NULL && lv.ll_name != NULL)
13790 {
13791 if (*end != NUL)
13792 EMSG(_(e_trailing));
13793 else
13794 {
13795 if (lv.ll_tv == NULL)
13796 {
13797 if (check_changedtick(lv.ll_name))
13798 rettv->vval.v_number = 1; /* always locked */
13799 else
13800 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013801 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013802 if (di != NULL)
13803 {
13804 /* Consider a variable locked when:
13805 * 1. the variable itself is locked
13806 * 2. the value of the variable is locked.
13807 * 3. the List or Dict value is locked.
13808 */
13809 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13810 || tv_islocked(&di->di_tv));
13811 }
13812 }
13813 }
13814 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013815 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013816 else if (lv.ll_newkey != NULL)
13817 EMSG2(_(e_dictkey), lv.ll_newkey);
13818 else if (lv.ll_list != NULL)
13819 /* List item. */
13820 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13821 else
13822 /* Dictionary item. */
13823 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13824 }
13825 }
13826
13827 clear_lval(&lv);
13828}
13829
Bram Moolenaar33570922005-01-25 22:26:29 +000013830static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013831
13832/*
13833 * Turn a dict into a list:
13834 * "what" == 0: list of keys
13835 * "what" == 1: list of values
13836 * "what" == 2: list of items
13837 */
13838 static void
13839dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013840 typval_T *argvars;
13841 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013842 int what;
13843{
Bram Moolenaar33570922005-01-25 22:26:29 +000013844 list_T *l2;
13845 dictitem_T *di;
13846 hashitem_T *hi;
13847 listitem_T *li;
13848 listitem_T *li2;
13849 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013850 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013851
Bram Moolenaar8c711452005-01-14 21:53:12 +000013852 if (argvars[0].v_type != VAR_DICT)
13853 {
13854 EMSG(_(e_dictreq));
13855 return;
13856 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013857 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013858 return;
13859
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013860 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013861 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013862
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013863 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013864 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013865 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013866 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013868 --todo;
13869 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013870
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013871 li = listitem_alloc();
13872 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013873 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013874 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013875
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013876 if (what == 0)
13877 {
13878 /* keys() */
13879 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013880 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013881 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13882 }
13883 else if (what == 1)
13884 {
13885 /* values() */
13886 copy_tv(&di->di_tv, &li->li_tv);
13887 }
13888 else
13889 {
13890 /* items() */
13891 l2 = list_alloc();
13892 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013893 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013894 li->li_tv.vval.v_list = l2;
13895 if (l2 == NULL)
13896 break;
13897 ++l2->lv_refcount;
13898
13899 li2 = listitem_alloc();
13900 if (li2 == NULL)
13901 break;
13902 list_append(l2, li2);
13903 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013904 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013905 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13906
13907 li2 = listitem_alloc();
13908 if (li2 == NULL)
13909 break;
13910 list_append(l2, li2);
13911 copy_tv(&di->di_tv, &li2->li_tv);
13912 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013913 }
13914 }
13915}
13916
13917/*
13918 * "items(dict)" function
13919 */
13920 static void
13921f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013922 typval_T *argvars;
13923 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013924{
13925 dict_list(argvars, rettv, 2);
13926}
13927
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013929 * "join()" function
13930 */
13931 static void
13932f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013933 typval_T *argvars;
13934 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013935{
13936 garray_T ga;
13937 char_u *sep;
13938
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013939 if (argvars[0].v_type != VAR_LIST)
13940 {
13941 EMSG(_(e_listreq));
13942 return;
13943 }
13944 if (argvars[0].vval.v_list == NULL)
13945 return;
13946 if (argvars[1].v_type == VAR_UNKNOWN)
13947 sep = (char_u *)" ";
13948 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013950
13951 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952
13953 if (sep != NULL)
13954 {
13955 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013956 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013957 ga_append(&ga, NUL);
13958 rettv->vval.v_string = (char_u *)ga.ga_data;
13959 }
13960 else
13961 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013962}
13963
13964/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013965 * "keys()" function
13966 */
13967 static void
13968f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013971{
13972 dict_list(argvars, rettv, 0);
13973}
13974
13975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 * "last_buffer_nr()" function.
13977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013980 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982{
13983 int n = 0;
13984 buf_T *buf;
13985
13986 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13987 if (n < buf->b_fnum)
13988 n = buf->b_fnum;
13989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013990 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013991}
13992
13993/*
13994 * "len()" function
13995 */
13996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013997f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013998 typval_T *argvars;
13999 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014000{
14001 switch (argvars[0].v_type)
14002 {
14003 case VAR_STRING:
14004 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014005 rettv->vval.v_number = (varnumber_T)STRLEN(
14006 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014007 break;
14008 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014010 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014011 case VAR_DICT:
14012 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14013 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014014 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014015 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014016 break;
14017 }
14018}
14019
Bram Moolenaar33570922005-01-25 22:26:29 +000014020static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014021
14022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014024 typval_T *argvars;
14025 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014026 int type;
14027{
14028#ifdef FEAT_LIBCALL
14029 char_u *string_in;
14030 char_u **string_result;
14031 int nr_result;
14032#endif
14033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014034 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014035 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014037
14038 if (check_restricted() || check_secure())
14039 return;
14040
14041#ifdef FEAT_LIBCALL
14042 /* The first two args must be strings, otherwise its meaningless */
14043 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14044 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014045 string_in = NULL;
14046 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014047 string_in = argvars[2].vval.v_string;
14048 if (type == VAR_NUMBER)
14049 string_result = NULL;
14050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014051 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014052 if (mch_libcall(argvars[0].vval.v_string,
14053 argvars[1].vval.v_string,
14054 string_in,
14055 argvars[2].vval.v_number,
14056 string_result,
14057 &nr_result) == OK
14058 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014059 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014060 }
14061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062}
14063
14064/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014065 * "libcall()" function
14066 */
14067 static void
14068f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014069 typval_T *argvars;
14070 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071{
14072 libcall_common(argvars, rettv, VAR_STRING);
14073}
14074
14075/*
14076 * "libcallnr()" function
14077 */
14078 static void
14079f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014080 typval_T *argvars;
14081 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082{
14083 libcall_common(argvars, rettv, VAR_NUMBER);
14084}
14085
14086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 * "line(string)" function
14088 */
14089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014091 typval_T *argvars;
14092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093{
14094 linenr_T lnum = 0;
14095 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014096 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014098 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 if (fp != NULL)
14100 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014101 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102}
14103
14104/*
14105 * "line2byte(lnum)" function
14106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014108f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111{
14112#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114#else
14115 linenr_T lnum;
14116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014117 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014119 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014121 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14122 if (rettv->vval.v_number >= 0)
14123 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124#endif
14125}
14126
14127/*
14128 * "lispindent(lnum)" function
14129 */
14130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014132 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134{
14135#ifdef FEAT_LISP
14136 pos_T pos;
14137 linenr_T lnum;
14138
14139 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14142 {
14143 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014144 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 curwin->w_cursor = pos;
14146 }
14147 else
14148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150}
14151
14152/*
14153 * "localtime()" function
14154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014156f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014157 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161}
14162
Bram Moolenaar33570922005-01-25 22:26:29 +000014163static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164
14165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014166get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014167 typval_T *argvars;
14168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 int exact;
14170{
14171 char_u *keys;
14172 char_u *which;
14173 char_u buf[NUMBUFLEN];
14174 char_u *keys_buf = NULL;
14175 char_u *rhs;
14176 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014177 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014178 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014179 mapblock_T *mp;
14180 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181
14182 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183 rettv->v_type = VAR_STRING;
14184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014186 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 if (*keys == NUL)
14188 return;
14189
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014190 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014193 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014194 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014195 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014196 if (argvars[3].v_type != VAR_UNKNOWN)
14197 get_dict = get_tv_number(&argvars[3]);
14198 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 else
14201 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202 if (which == NULL)
14203 return;
14204
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 mode = get_map_mode(&which, 0);
14206
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014207 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014208 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014210
14211 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014213 /* Return a string. */
14214 if (rhs != NULL)
14215 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216
Bram Moolenaarbd743252010-10-20 21:23:33 +020014217 }
14218 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14219 {
14220 /* Return a dictionary. */
14221 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14222 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14223 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224
Bram Moolenaarbd743252010-10-20 21:23:33 +020014225 dict_add_nr_str(dict, "lhs", 0L, lhs);
14226 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14227 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14228 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14229 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14230 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14231 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014232 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014233 dict_add_nr_str(dict, "mode", 0L, mapmode);
14234
14235 vim_free(lhs);
14236 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237 }
14238}
14239
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014240#ifdef FEAT_FLOAT
14241/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014242 * "log()" function
14243 */
14244 static void
14245f_log(argvars, rettv)
14246 typval_T *argvars;
14247 typval_T *rettv;
14248{
14249 float_T f;
14250
14251 rettv->v_type = VAR_FLOAT;
14252 if (get_float_arg(argvars, &f) == OK)
14253 rettv->vval.v_float = log(f);
14254 else
14255 rettv->vval.v_float = 0.0;
14256}
14257
14258/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014259 * "log10()" function
14260 */
14261 static void
14262f_log10(argvars, rettv)
14263 typval_T *argvars;
14264 typval_T *rettv;
14265{
14266 float_T f;
14267
14268 rettv->v_type = VAR_FLOAT;
14269 if (get_float_arg(argvars, &f) == OK)
14270 rettv->vval.v_float = log10(f);
14271 else
14272 rettv->vval.v_float = 0.0;
14273}
14274#endif
14275
Bram Moolenaar1dced572012-04-05 16:54:08 +020014276#ifdef FEAT_LUA
14277/*
14278 * "luaeval()" function
14279 */
14280 static void
14281f_luaeval(argvars, rettv)
14282 typval_T *argvars;
14283 typval_T *rettv;
14284{
14285 char_u *str;
14286 char_u buf[NUMBUFLEN];
14287
14288 str = get_tv_string_buf(&argvars[0], buf);
14289 do_luaeval(str, argvars + 1, rettv);
14290}
14291#endif
14292
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014294 * "map()" function
14295 */
14296 static void
14297f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014298 typval_T *argvars;
14299 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014300{
14301 filter_map(argvars, rettv, TRUE);
14302}
14303
14304/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014305 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 */
14307 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014308f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014309 typval_T *argvars;
14310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014312 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313}
14314
14315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 */
14318 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014319f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014320 typval_T *argvars;
14321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324}
14325
Bram Moolenaar33570922005-01-25 22:26:29 +000014326static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327
14328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014329find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014330 typval_T *argvars;
14331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 int type;
14333{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014334 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014335 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014336 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 char_u *pat;
14338 regmatch_T regmatch;
14339 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014340 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 char_u *save_cpo;
14342 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014343 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014344 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014345 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014346 list_T *l = NULL;
14347 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014348 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014349 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350
14351 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14352 save_cpo = p_cpo;
14353 p_cpo = (char_u *)"";
14354
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014355 rettv->vval.v_number = -1;
14356 if (type == 3)
14357 {
14358 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014359 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014360 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014361 }
14362 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 rettv->v_type = VAR_STRING;
14365 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014368 if (argvars[0].v_type == VAR_LIST)
14369 {
14370 if ((l = argvars[0].vval.v_list) == NULL)
14371 goto theend;
14372 li = l->lv_first;
14373 }
14374 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014375 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014376 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014377 len = (long)STRLEN(str);
14378 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14381 if (pat == NULL)
14382 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014384 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 int error = FALSE;
14387
14388 start = get_tv_number_chk(&argvars[2], &error);
14389 if (error)
14390 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014391 if (l != NULL)
14392 {
14393 li = list_find(l, start);
14394 if (li == NULL)
14395 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014396 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014397 }
14398 else
14399 {
14400 if (start < 0)
14401 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014402 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014403 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014404 /* When "count" argument is there ignore matches before "start",
14405 * otherwise skip part of the string. Differs when pattern is "^"
14406 * or "\<". */
14407 if (argvars[3].v_type != VAR_UNKNOWN)
14408 startcol = start;
14409 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014410 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014411 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014412 len -= start;
14413 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014414 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014415
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014416 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 nth = get_tv_number_chk(&argvars[3], &error);
14418 if (error)
14419 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 }
14421
14422 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14423 if (regmatch.regprog != NULL)
14424 {
14425 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014426
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014427 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014428 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014429 if (l != NULL)
14430 {
14431 if (li == NULL)
14432 {
14433 match = FALSE;
14434 break;
14435 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014436 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014437 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014438 if (str == NULL)
14439 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014440 }
14441
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014442 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014443
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014444 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014445 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014446 if (l == NULL && !match)
14447 break;
14448
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014449 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014450 if (l != NULL)
14451 {
14452 li = li->li_next;
14453 ++idx;
14454 }
14455 else
14456 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014457#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014458 startcol = (colnr_T)(regmatch.startp[0]
14459 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014460#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014461 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014462#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014463 if (startcol > (colnr_T)len
14464 || str + startcol <= regmatch.startp[0])
14465 {
14466 match = FALSE;
14467 break;
14468 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014469 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014470 }
14471
14472 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014474 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014475 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014476 int i;
14477
14478 /* return list with matched string and submatches */
14479 for (i = 0; i < NSUBEXP; ++i)
14480 {
14481 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014482 {
14483 if (list_append_string(rettv->vval.v_list,
14484 (char_u *)"", 0) == FAIL)
14485 break;
14486 }
14487 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014488 regmatch.startp[i],
14489 (int)(regmatch.endp[i] - regmatch.startp[i]))
14490 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014491 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014492 }
14493 }
14494 else if (type == 2)
14495 {
14496 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014497 if (l != NULL)
14498 copy_tv(&li->li_tv, rettv);
14499 else
14500 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014502 }
14503 else if (l != NULL)
14504 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 else
14506 {
14507 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 (varnumber_T)(regmatch.startp[0] - str);
14510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014513 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 }
14515 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014516 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 }
14518
14519theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014520 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 p_cpo = save_cpo;
14522}
14523
14524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014525 * "match()" function
14526 */
14527 static void
14528f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 typval_T *argvars;
14530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014531{
14532 find_some_match(argvars, rettv, 1);
14533}
14534
14535/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014536 * "matchadd()" function
14537 */
14538 static void
14539f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014540 typval_T *argvars UNUSED;
14541 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014542{
14543#ifdef FEAT_SEARCH_EXTRA
14544 char_u buf[NUMBUFLEN];
14545 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14546 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14547 int prio = 10; /* default priority */
14548 int id = -1;
14549 int error = FALSE;
14550
14551 rettv->vval.v_number = -1;
14552
14553 if (grp == NULL || pat == NULL)
14554 return;
14555 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014556 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014557 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014558 if (argvars[3].v_type != VAR_UNKNOWN)
14559 id = get_tv_number_chk(&argvars[3], &error);
14560 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014561 if (error == TRUE)
14562 return;
14563 if (id >= 1 && id <= 3)
14564 {
14565 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14566 return;
14567 }
14568
Bram Moolenaarb3414592014-06-17 17:48:32 +020014569 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14570#endif
14571}
14572
14573/*
14574 * "matchaddpos()" function
14575 */
14576 static void
14577f_matchaddpos(argvars, rettv)
14578 typval_T *argvars UNUSED;
14579 typval_T *rettv UNUSED;
14580{
14581#ifdef FEAT_SEARCH_EXTRA
14582 char_u buf[NUMBUFLEN];
14583 char_u *group;
14584 int prio = 10;
14585 int id = -1;
14586 int error = FALSE;
14587 list_T *l;
14588
14589 rettv->vval.v_number = -1;
14590
14591 group = get_tv_string_buf_chk(&argvars[0], buf);
14592 if (group == NULL)
14593 return;
14594
14595 if (argvars[1].v_type != VAR_LIST)
14596 {
14597 EMSG2(_(e_listarg), "matchaddpos()");
14598 return;
14599 }
14600 l = argvars[1].vval.v_list;
14601 if (l == NULL)
14602 return;
14603
14604 if (argvars[2].v_type != VAR_UNKNOWN)
14605 {
14606 prio = get_tv_number_chk(&argvars[2], &error);
14607 if (argvars[3].v_type != VAR_UNKNOWN)
14608 id = get_tv_number_chk(&argvars[3], &error);
14609 }
14610 if (error == TRUE)
14611 return;
14612
14613 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14614 if (id == 1 || id == 2)
14615 {
14616 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14617 return;
14618 }
14619
14620 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014621#endif
14622}
14623
14624/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014625 * "matcharg()" function
14626 */
14627 static void
14628f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014629 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014630 typval_T *rettv;
14631{
14632 if (rettv_list_alloc(rettv) == OK)
14633 {
14634#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014635 int id = get_tv_number(&argvars[0]);
14636 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014637
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014638 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014639 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014640 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14641 {
14642 list_append_string(rettv->vval.v_list,
14643 syn_id2name(m->hlg_id), -1);
14644 list_append_string(rettv->vval.v_list, m->pattern, -1);
14645 }
14646 else
14647 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014648 list_append_string(rettv->vval.v_list, NULL, -1);
14649 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014650 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014651 }
14652#endif
14653 }
14654}
14655
14656/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014657 * "matchdelete()" function
14658 */
14659 static void
14660f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014661 typval_T *argvars UNUSED;
14662 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014663{
14664#ifdef FEAT_SEARCH_EXTRA
14665 rettv->vval.v_number = match_delete(curwin,
14666 (int)get_tv_number(&argvars[0]), TRUE);
14667#endif
14668}
14669
14670/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671 * "matchend()" function
14672 */
14673 static void
14674f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014675 typval_T *argvars;
14676 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014677{
14678 find_some_match(argvars, rettv, 0);
14679}
14680
14681/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014682 * "matchlist()" function
14683 */
14684 static void
14685f_matchlist(argvars, rettv)
14686 typval_T *argvars;
14687 typval_T *rettv;
14688{
14689 find_some_match(argvars, rettv, 3);
14690}
14691
14692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014693 * "matchstr()" function
14694 */
14695 static void
14696f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014697 typval_T *argvars;
14698 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699{
14700 find_some_match(argvars, rettv, 2);
14701}
14702
Bram Moolenaar33570922005-01-25 22:26:29 +000014703static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014704
14705 static void
14706max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014707 typval_T *argvars;
14708 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014709 int domax;
14710{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014711 long n = 0;
14712 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014713 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014714
14715 if (argvars[0].v_type == VAR_LIST)
14716 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014717 list_T *l;
14718 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014719
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014720 l = argvars[0].vval.v_list;
14721 if (l != NULL)
14722 {
14723 li = l->lv_first;
14724 if (li != NULL)
14725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014726 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014727 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014728 {
14729 li = li->li_next;
14730 if (li == NULL)
14731 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014732 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014733 if (domax ? i > n : i < n)
14734 n = i;
14735 }
14736 }
14737 }
14738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014739 else if (argvars[0].v_type == VAR_DICT)
14740 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014742 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014744 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014745
14746 d = argvars[0].vval.v_dict;
14747 if (d != NULL)
14748 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014749 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014751 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014752 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014753 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014754 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014755 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014756 if (first)
14757 {
14758 n = i;
14759 first = FALSE;
14760 }
14761 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014762 n = i;
14763 }
14764 }
14765 }
14766 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014767 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014768 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014769 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014770}
14771
14772/*
14773 * "max()" function
14774 */
14775 static void
14776f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 typval_T *argvars;
14778 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014779{
14780 max_min(argvars, rettv, TRUE);
14781}
14782
14783/*
14784 * "min()" function
14785 */
14786 static void
14787f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014788 typval_T *argvars;
14789 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014790{
14791 max_min(argvars, rettv, FALSE);
14792}
14793
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014794static int mkdir_recurse __ARGS((char_u *dir, int prot));
14795
14796/*
14797 * Create the directory in which "dir" is located, and higher levels when
14798 * needed.
14799 */
14800 static int
14801mkdir_recurse(dir, prot)
14802 char_u *dir;
14803 int prot;
14804{
14805 char_u *p;
14806 char_u *updir;
14807 int r = FAIL;
14808
14809 /* Get end of directory name in "dir".
14810 * We're done when it's "/" or "c:/". */
14811 p = gettail_sep(dir);
14812 if (p <= get_past_head(dir))
14813 return OK;
14814
14815 /* If the directory exists we're done. Otherwise: create it.*/
14816 updir = vim_strnsave(dir, (int)(p - dir));
14817 if (updir == NULL)
14818 return FAIL;
14819 if (mch_isdir(updir))
14820 r = OK;
14821 else if (mkdir_recurse(updir, prot) == OK)
14822 r = vim_mkdir_emsg(updir, prot);
14823 vim_free(updir);
14824 return r;
14825}
14826
14827#ifdef vim_mkdir
14828/*
14829 * "mkdir()" function
14830 */
14831 static void
14832f_mkdir(argvars, rettv)
14833 typval_T *argvars;
14834 typval_T *rettv;
14835{
14836 char_u *dir;
14837 char_u buf[NUMBUFLEN];
14838 int prot = 0755;
14839
14840 rettv->vval.v_number = FAIL;
14841 if (check_restricted() || check_secure())
14842 return;
14843
14844 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014845 if (*dir == NUL)
14846 rettv->vval.v_number = FAIL;
14847 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014848 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014849 if (*gettail(dir) == NUL)
14850 /* remove trailing slashes */
14851 *gettail_sep(dir) = NUL;
14852
14853 if (argvars[1].v_type != VAR_UNKNOWN)
14854 {
14855 if (argvars[2].v_type != VAR_UNKNOWN)
14856 prot = get_tv_number_chk(&argvars[2], NULL);
14857 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14858 mkdir_recurse(dir, prot);
14859 }
14860 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014861 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014862}
14863#endif
14864
Bram Moolenaar0d660222005-01-07 21:51:51 +000014865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 * "mode()" function
14867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014869f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014870 typval_T *argvars;
14871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014873 char_u buf[3];
14874
14875 buf[1] = NUL;
14876 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 if (VIsual_active)
14879 {
14880 if (VIsual_select)
14881 buf[0] = VIsual_mode + 's' - 'v';
14882 else
14883 buf[0] = VIsual_mode;
14884 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014885 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014886 || State == CONFIRM)
14887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014889 if (State == ASKMORE)
14890 buf[1] = 'm';
14891 else if (State == CONFIRM)
14892 buf[1] = '?';
14893 }
14894 else if (State == EXTERNCMD)
14895 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 else if (State & INSERT)
14897 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014898#ifdef FEAT_VREPLACE
14899 if (State & VREPLACE_FLAG)
14900 {
14901 buf[0] = 'R';
14902 buf[1] = 'v';
14903 }
14904 else
14905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906 if (State & REPLACE_FLAG)
14907 buf[0] = 'R';
14908 else
14909 buf[0] = 'i';
14910 }
14911 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014914 if (exmode_active)
14915 buf[1] = 'v';
14916 }
14917 else if (exmode_active)
14918 {
14919 buf[0] = 'c';
14920 buf[1] = 'e';
14921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014925 if (finish_op)
14926 buf[1] = 'o';
14927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014929 /* Clear out the minor mode when the argument is not a non-zero number or
14930 * non-empty string. */
14931 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014932 buf[1] = NUL;
14933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014934 rettv->vval.v_string = vim_strsave(buf);
14935 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936}
14937
Bram Moolenaar429fa852013-04-15 12:27:36 +020014938#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014939/*
14940 * "mzeval()" function
14941 */
14942 static void
14943f_mzeval(argvars, rettv)
14944 typval_T *argvars;
14945 typval_T *rettv;
14946{
14947 char_u *str;
14948 char_u buf[NUMBUFLEN];
14949
14950 str = get_tv_string_buf(&argvars[0], buf);
14951 do_mzeval(str, rettv);
14952}
Bram Moolenaar75676462013-01-30 14:55:42 +010014953
14954 void
14955mzscheme_call_vim(name, args, rettv)
14956 char_u *name;
14957 typval_T *args;
14958 typval_T *rettv;
14959{
14960 typval_T argvars[3];
14961
14962 argvars[0].v_type = VAR_STRING;
14963 argvars[0].vval.v_string = name;
14964 copy_tv(args, &argvars[1]);
14965 argvars[2].v_type = VAR_UNKNOWN;
14966 f_call(argvars, rettv);
14967 clear_tv(&argvars[1]);
14968}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014969#endif
14970
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972 * "nextnonblank()" function
14973 */
14974 static void
14975f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *argvars;
14977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978{
14979 linenr_T lnum;
14980
14981 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014983 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 {
14985 lnum = 0;
14986 break;
14987 }
14988 if (*skipwhite(ml_get(lnum)) != NUL)
14989 break;
14990 }
14991 rettv->vval.v_number = lnum;
14992}
14993
14994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995 * "nr2char()" function
14996 */
14997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014998f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014999 typval_T *argvars;
15000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001{
15002 char_u buf[NUMBUFLEN];
15003
15004#ifdef FEAT_MBYTE
15005 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015006 {
15007 int utf8 = 0;
15008
15009 if (argvars[1].v_type != VAR_UNKNOWN)
15010 utf8 = get_tv_number_chk(&argvars[1], NULL);
15011 if (utf8)
15012 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15013 else
15014 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 else
15017#endif
15018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 buf[1] = NUL;
15021 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015022 rettv->v_type = VAR_STRING;
15023 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015024}
15025
15026/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015027 * "or(expr, expr)" function
15028 */
15029 static void
15030f_or(argvars, rettv)
15031 typval_T *argvars;
15032 typval_T *rettv;
15033{
15034 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15035 | get_tv_number_chk(&argvars[1], NULL);
15036}
15037
15038/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015039 * "pathshorten()" function
15040 */
15041 static void
15042f_pathshorten(argvars, rettv)
15043 typval_T *argvars;
15044 typval_T *rettv;
15045{
15046 char_u *p;
15047
15048 rettv->v_type = VAR_STRING;
15049 p = get_tv_string_chk(&argvars[0]);
15050 if (p == NULL)
15051 rettv->vval.v_string = NULL;
15052 else
15053 {
15054 p = vim_strsave(p);
15055 rettv->vval.v_string = p;
15056 if (p != NULL)
15057 shorten_dir(p);
15058 }
15059}
15060
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015061#ifdef FEAT_FLOAT
15062/*
15063 * "pow()" function
15064 */
15065 static void
15066f_pow(argvars, rettv)
15067 typval_T *argvars;
15068 typval_T *rettv;
15069{
15070 float_T fx, fy;
15071
15072 rettv->v_type = VAR_FLOAT;
15073 if (get_float_arg(argvars, &fx) == OK
15074 && get_float_arg(&argvars[1], &fy) == OK)
15075 rettv->vval.v_float = pow(fx, fy);
15076 else
15077 rettv->vval.v_float = 0.0;
15078}
15079#endif
15080
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 * "prevnonblank()" function
15083 */
15084 static void
15085f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015086 typval_T *argvars;
15087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088{
15089 linenr_T lnum;
15090
15091 lnum = get_tv_lnum(argvars);
15092 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15093 lnum = 0;
15094 else
15095 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15096 --lnum;
15097 rettv->vval.v_number = lnum;
15098}
15099
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015100#ifdef HAVE_STDARG_H
15101/* This dummy va_list is here because:
15102 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15103 * - locally in the function results in a "used before set" warning
15104 * - using va_start() to initialize it gives "function with fixed args" error */
15105static va_list ap;
15106#endif
15107
Bram Moolenaar8c711452005-01-14 21:53:12 +000015108/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015109 * "printf()" function
15110 */
15111 static void
15112f_printf(argvars, rettv)
15113 typval_T *argvars;
15114 typval_T *rettv;
15115{
15116 rettv->v_type = VAR_STRING;
15117 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015118#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015119 {
15120 char_u buf[NUMBUFLEN];
15121 int len;
15122 char_u *s;
15123 int saved_did_emsg = did_emsg;
15124 char *fmt;
15125
15126 /* Get the required length, allocate the buffer and do it for real. */
15127 did_emsg = FALSE;
15128 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015129 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015130 if (!did_emsg)
15131 {
15132 s = alloc(len + 1);
15133 if (s != NULL)
15134 {
15135 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015136 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015137 }
15138 }
15139 did_emsg |= saved_did_emsg;
15140 }
15141#endif
15142}
15143
15144/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015145 * "pumvisible()" function
15146 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015147 static void
15148f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015149 typval_T *argvars UNUSED;
15150 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015151{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015152#ifdef FEAT_INS_EXPAND
15153 if (pum_visible())
15154 rettv->vval.v_number = 1;
15155#endif
15156}
15157
Bram Moolenaardb913952012-06-29 12:54:53 +020015158#ifdef FEAT_PYTHON3
15159/*
15160 * "py3eval()" function
15161 */
15162 static void
15163f_py3eval(argvars, rettv)
15164 typval_T *argvars;
15165 typval_T *rettv;
15166{
15167 char_u *str;
15168 char_u buf[NUMBUFLEN];
15169
15170 str = get_tv_string_buf(&argvars[0], buf);
15171 do_py3eval(str, rettv);
15172}
15173#endif
15174
15175#ifdef FEAT_PYTHON
15176/*
15177 * "pyeval()" function
15178 */
15179 static void
15180f_pyeval(argvars, rettv)
15181 typval_T *argvars;
15182 typval_T *rettv;
15183{
15184 char_u *str;
15185 char_u buf[NUMBUFLEN];
15186
15187 str = get_tv_string_buf(&argvars[0], buf);
15188 do_pyeval(str, rettv);
15189}
15190#endif
15191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015192/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015193 * "range()" function
15194 */
15195 static void
15196f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015197 typval_T *argvars;
15198 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015199{
15200 long start;
15201 long end;
15202 long stride = 1;
15203 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015204 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015206 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015207 if (argvars[1].v_type == VAR_UNKNOWN)
15208 {
15209 end = start - 1;
15210 start = 0;
15211 }
15212 else
15213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015214 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015215 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015217 }
15218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219 if (error)
15220 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015221 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015222 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015223 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015224 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015225 else
15226 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015227 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015228 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015229 if (list_append_number(rettv->vval.v_list,
15230 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015231 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015232 }
15233}
15234
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015235/*
15236 * "readfile()" function
15237 */
15238 static void
15239f_readfile(argvars, rettv)
15240 typval_T *argvars;
15241 typval_T *rettv;
15242{
15243 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015244 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015245 char_u *fname;
15246 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015247 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15248 int io_size = sizeof(buf);
15249 int readlen; /* size of last fread() */
15250 char_u *prev = NULL; /* previously read bytes, if any */
15251 long prevlen = 0; /* length of data in prev */
15252 long prevsize = 0; /* size of prev buffer */
15253 long maxline = MAXLNUM;
15254 long cnt = 0;
15255 char_u *p; /* position in buf */
15256 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015257
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015258 if (argvars[1].v_type != VAR_UNKNOWN)
15259 {
15260 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15261 binary = TRUE;
15262 if (argvars[2].v_type != VAR_UNKNOWN)
15263 maxline = get_tv_number(&argvars[2]);
15264 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015265
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015266 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015267 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015268
15269 /* Always open the file in binary mode, library functions have a mind of
15270 * their own about CR-LF conversion. */
15271 fname = get_tv_string(&argvars[0]);
15272 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15273 {
15274 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15275 return;
15276 }
15277
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015278 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015279 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015280 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015281
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015282 /* This for loop processes what was read, but is also entered at end
15283 * of file so that either:
15284 * - an incomplete line gets written
15285 * - a "binary" file gets an empty line at the end if it ends in a
15286 * newline. */
15287 for (p = buf, start = buf;
15288 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15289 ++p)
15290 {
15291 if (*p == '\n' || readlen <= 0)
15292 {
15293 listitem_T *li;
15294 char_u *s = NULL;
15295 long_u len = p - start;
15296
15297 /* Finished a line. Remove CRs before NL. */
15298 if (readlen > 0 && !binary)
15299 {
15300 while (len > 0 && start[len - 1] == '\r')
15301 --len;
15302 /* removal may cross back to the "prev" string */
15303 if (len == 0)
15304 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15305 --prevlen;
15306 }
15307 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015308 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015309 else
15310 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015311 /* Change "prev" buffer to be the right size. This way
15312 * the bytes are only copied once, and very long lines are
15313 * allocated only once. */
15314 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015315 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015316 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015317 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015318 prev = NULL; /* the list will own the string */
15319 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015320 }
15321 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015322 if (s == NULL)
15323 {
15324 do_outofmem_msg((long_u) prevlen + len + 1);
15325 failed = TRUE;
15326 break;
15327 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015328
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015329 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015330 {
15331 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015332 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015333 break;
15334 }
15335 li->li_tv.v_type = VAR_STRING;
15336 li->li_tv.v_lock = 0;
15337 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015338 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015339
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015340 start = p + 1; /* step over newline */
15341 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015342 break;
15343 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015344 else if (*p == NUL)
15345 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015346#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015347 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15348 * when finding the BF and check the previous two bytes. */
15349 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015350 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015351 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15352 * + 1, these may be in the "prev" string. */
15353 char_u back1 = p >= buf + 1 ? p[-1]
15354 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15355 char_u back2 = p >= buf + 2 ? p[-2]
15356 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15357 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015358
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015359 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015360 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015361 char_u *dest = p - 2;
15362
15363 /* Usually a BOM is at the beginning of a file, and so at
15364 * the beginning of a line; then we can just step over it.
15365 */
15366 if (start == dest)
15367 start = p + 1;
15368 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015369 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015370 /* have to shuffle buf to close gap */
15371 int adjust_prevlen = 0;
15372
15373 if (dest < buf)
15374 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015375 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015376 dest = buf;
15377 }
15378 if (readlen > p - buf + 1)
15379 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15380 readlen -= 3 - adjust_prevlen;
15381 prevlen -= adjust_prevlen;
15382 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015383 }
15384 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015385 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015386#endif
15387 } /* for */
15388
15389 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15390 break;
15391 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015392 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015393 /* There's part of a line in buf, store it in "prev". */
15394 if (p - start + prevlen >= prevsize)
15395 {
15396 /* need bigger "prev" buffer */
15397 char_u *newprev;
15398
15399 /* A common use case is ordinary text files and "prev" gets a
15400 * fragment of a line, so the first allocation is made
15401 * small, to avoid repeatedly 'allocing' large and
15402 * 'reallocing' small. */
15403 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015404 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015405 else
15406 {
15407 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015408 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015409 prevsize = grow50pc > growmin ? grow50pc : growmin;
15410 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015411 newprev = prev == NULL ? alloc(prevsize)
15412 : vim_realloc(prev, prevsize);
15413 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015414 {
15415 do_outofmem_msg((long_u)prevsize);
15416 failed = TRUE;
15417 break;
15418 }
15419 prev = newprev;
15420 }
15421 /* Add the line part to end of "prev". */
15422 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015423 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015424 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015425 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015426
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015427 /*
15428 * For a negative line count use only the lines at the end of the file,
15429 * free the rest.
15430 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015431 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015432 while (cnt > -maxline)
15433 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015434 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015435 --cnt;
15436 }
15437
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015438 if (failed)
15439 {
15440 list_free(rettv->vval.v_list, TRUE);
15441 /* readfile doc says an empty list is returned on error */
15442 rettv->vval.v_list = list_alloc();
15443 }
15444
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015445 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015446 fclose(fd);
15447}
15448
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015449#if defined(FEAT_RELTIME)
15450static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15451
15452/*
15453 * Convert a List to proftime_T.
15454 * Return FAIL when there is something wrong.
15455 */
15456 static int
15457list2proftime(arg, tm)
15458 typval_T *arg;
15459 proftime_T *tm;
15460{
15461 long n1, n2;
15462 int error = FALSE;
15463
15464 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15465 || arg->vval.v_list->lv_len != 2)
15466 return FAIL;
15467 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15468 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15469# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015470 tm->HighPart = n1;
15471 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015472# else
15473 tm->tv_sec = n1;
15474 tm->tv_usec = n2;
15475# endif
15476 return error ? FAIL : OK;
15477}
15478#endif /* FEAT_RELTIME */
15479
15480/*
15481 * "reltime()" function
15482 */
15483 static void
15484f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015485 typval_T *argvars UNUSED;
15486 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015487{
15488#ifdef FEAT_RELTIME
15489 proftime_T res;
15490 proftime_T start;
15491
15492 if (argvars[0].v_type == VAR_UNKNOWN)
15493 {
15494 /* No arguments: get current time. */
15495 profile_start(&res);
15496 }
15497 else if (argvars[1].v_type == VAR_UNKNOWN)
15498 {
15499 if (list2proftime(&argvars[0], &res) == FAIL)
15500 return;
15501 profile_end(&res);
15502 }
15503 else
15504 {
15505 /* Two arguments: compute the difference. */
15506 if (list2proftime(&argvars[0], &start) == FAIL
15507 || list2proftime(&argvars[1], &res) == FAIL)
15508 return;
15509 profile_sub(&res, &start);
15510 }
15511
15512 if (rettv_list_alloc(rettv) == OK)
15513 {
15514 long n1, n2;
15515
15516# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015517 n1 = res.HighPart;
15518 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015519# else
15520 n1 = res.tv_sec;
15521 n2 = res.tv_usec;
15522# endif
15523 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15524 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15525 }
15526#endif
15527}
15528
15529/*
15530 * "reltimestr()" function
15531 */
15532 static void
15533f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015534 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015535 typval_T *rettv;
15536{
15537#ifdef FEAT_RELTIME
15538 proftime_T tm;
15539#endif
15540
15541 rettv->v_type = VAR_STRING;
15542 rettv->vval.v_string = NULL;
15543#ifdef FEAT_RELTIME
15544 if (list2proftime(&argvars[0], &tm) == OK)
15545 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15546#endif
15547}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548
Bram Moolenaar0d660222005-01-07 21:51:51 +000015549#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15550static void make_connection __ARGS((void));
15551static int check_connection __ARGS((void));
15552
15553 static void
15554make_connection()
15555{
15556 if (X_DISPLAY == NULL
15557# ifdef FEAT_GUI
15558 && !gui.in_use
15559# endif
15560 )
15561 {
15562 x_force_connect = TRUE;
15563 setup_term_clip();
15564 x_force_connect = FALSE;
15565 }
15566}
15567
15568 static int
15569check_connection()
15570{
15571 make_connection();
15572 if (X_DISPLAY == NULL)
15573 {
15574 EMSG(_("E240: No connection to Vim server"));
15575 return FAIL;
15576 }
15577 return OK;
15578}
15579#endif
15580
15581#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015582static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015583
15584 static void
15585remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015586 typval_T *argvars;
15587 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588 int expr;
15589{
15590 char_u *server_name;
15591 char_u *keys;
15592 char_u *r = NULL;
15593 char_u buf[NUMBUFLEN];
15594# ifdef WIN32
15595 HWND w;
15596# else
15597 Window w;
15598# endif
15599
15600 if (check_restricted() || check_secure())
15601 return;
15602
15603# ifdef FEAT_X11
15604 if (check_connection() == FAIL)
15605 return;
15606# endif
15607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015608 server_name = get_tv_string_chk(&argvars[0]);
15609 if (server_name == NULL)
15610 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611 keys = get_tv_string_buf(&argvars[1], buf);
15612# ifdef WIN32
15613 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15614# else
15615 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15616 < 0)
15617# endif
15618 {
15619 if (r != NULL)
15620 EMSG(r); /* sending worked but evaluation failed */
15621 else
15622 EMSG2(_("E241: Unable to send to %s"), server_name);
15623 return;
15624 }
15625
15626 rettv->vval.v_string = r;
15627
15628 if (argvars[2].v_type != VAR_UNKNOWN)
15629 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015630 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015631 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015632 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015634 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015635 v.di_tv.v_type = VAR_STRING;
15636 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 idvar = get_tv_string_chk(&argvars[2]);
15638 if (idvar != NULL)
15639 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015640 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015641 }
15642}
15643#endif
15644
15645/*
15646 * "remote_expr()" function
15647 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015648 static void
15649f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015650 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015652{
15653 rettv->v_type = VAR_STRING;
15654 rettv->vval.v_string = NULL;
15655#ifdef FEAT_CLIENTSERVER
15656 remote_common(argvars, rettv, TRUE);
15657#endif
15658}
15659
15660/*
15661 * "remote_foreground()" function
15662 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663 static void
15664f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015665 typval_T *argvars UNUSED;
15666 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015668#ifdef FEAT_CLIENTSERVER
15669# ifdef WIN32
15670 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015671 {
15672 char_u *server_name = get_tv_string_chk(&argvars[0]);
15673
15674 if (server_name != NULL)
15675 serverForeground(server_name);
15676 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677# else
15678 /* Send a foreground() expression to the server. */
15679 argvars[1].v_type = VAR_STRING;
15680 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15681 argvars[2].v_type = VAR_UNKNOWN;
15682 remote_common(argvars, rettv, TRUE);
15683 vim_free(argvars[1].vval.v_string);
15684# endif
15685#endif
15686}
15687
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688 static void
15689f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015690 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015692{
15693#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015694 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015695 char_u *s = NULL;
15696# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015697 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015698# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015699 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015700
15701 if (check_restricted() || check_secure())
15702 {
15703 rettv->vval.v_number = -1;
15704 return;
15705 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015706 serverid = get_tv_string_chk(&argvars[0]);
15707 if (serverid == NULL)
15708 {
15709 rettv->vval.v_number = -1;
15710 return; /* type error; errmsg already given */
15711 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015712# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015713 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015714 if (n == 0)
15715 rettv->vval.v_number = -1;
15716 else
15717 {
15718 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15719 rettv->vval.v_number = (s != NULL);
15720 }
15721# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722 if (check_connection() == FAIL)
15723 return;
15724
15725 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015726 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015727# endif
15728
15729 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015731 char_u *retvar;
15732
Bram Moolenaar33570922005-01-25 22:26:29 +000015733 v.di_tv.v_type = VAR_STRING;
15734 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 retvar = get_tv_string_chk(&argvars[1]);
15736 if (retvar != NULL)
15737 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015738 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 }
15740#else
15741 rettv->vval.v_number = -1;
15742#endif
15743}
15744
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745 static void
15746f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015748 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749{
15750 char_u *r = NULL;
15751
15752#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015753 char_u *serverid = get_tv_string_chk(&argvars[0]);
15754
15755 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756 {
15757# ifdef WIN32
15758 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015759 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015761 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762 if (n != 0)
15763 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15764 if (r == NULL)
15765# else
15766 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015767 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015768# endif
15769 EMSG(_("E277: Unable to read a server reply"));
15770 }
15771#endif
15772 rettv->v_type = VAR_STRING;
15773 rettv->vval.v_string = r;
15774}
15775
15776/*
15777 * "remote_send()" function
15778 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015779 static void
15780f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015783{
15784 rettv->v_type = VAR_STRING;
15785 rettv->vval.v_string = NULL;
15786#ifdef FEAT_CLIENTSERVER
15787 remote_common(argvars, rettv, FALSE);
15788#endif
15789}
15790
15791/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015792 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015793 */
15794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015795f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015796 typval_T *argvars;
15797 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015798{
Bram Moolenaar33570922005-01-25 22:26:29 +000015799 list_T *l;
15800 listitem_T *item, *item2;
15801 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015802 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015803 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015804 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015805 dict_T *d;
15806 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015807 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015808
Bram Moolenaar8c711452005-01-14 21:53:12 +000015809 if (argvars[0].v_type == VAR_DICT)
15810 {
15811 if (argvars[2].v_type != VAR_UNKNOWN)
15812 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015813 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015814 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015816 key = get_tv_string_chk(&argvars[1]);
15817 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015819 di = dict_find(d, key, -1);
15820 if (di == NULL)
15821 EMSG2(_(e_dictkey), key);
15822 else
15823 {
15824 *rettv = di->di_tv;
15825 init_tv(&di->di_tv);
15826 dictitem_remove(d, di);
15827 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015829 }
15830 }
15831 else if (argvars[0].v_type != VAR_LIST)
15832 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015833 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015834 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015836 int error = FALSE;
15837
15838 idx = get_tv_number_chk(&argvars[1], &error);
15839 if (error)
15840 ; /* type error: do nothing, errmsg already given */
15841 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015842 EMSGN(_(e_listidx), idx);
15843 else
15844 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015845 if (argvars[2].v_type == VAR_UNKNOWN)
15846 {
15847 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015848 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015849 *rettv = item->li_tv;
15850 vim_free(item);
15851 }
15852 else
15853 {
15854 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015855 end = get_tv_number_chk(&argvars[2], &error);
15856 if (error)
15857 ; /* type error: do nothing */
15858 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015859 EMSGN(_(e_listidx), end);
15860 else
15861 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015862 int cnt = 0;
15863
15864 for (li = item; li != NULL; li = li->li_next)
15865 {
15866 ++cnt;
15867 if (li == item2)
15868 break;
15869 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015870 if (li == NULL) /* didn't find "item2" after "item" */
15871 EMSG(_(e_invrange));
15872 else
15873 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015874 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015875 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015876 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015877 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015878 l->lv_first = item;
15879 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015880 item->li_prev = NULL;
15881 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015882 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015883 }
15884 }
15885 }
15886 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015887 }
15888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889}
15890
15891/*
15892 * "rename({from}, {to})" function
15893 */
15894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015895f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015896 typval_T *argvars;
15897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898{
15899 char_u buf[NUMBUFLEN];
15900
15901 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015902 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015904 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15905 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906}
15907
15908/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015909 * "repeat()" function
15910 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015911 static void
15912f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015913 typval_T *argvars;
15914 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015915{
15916 char_u *p;
15917 int n;
15918 int slen;
15919 int len;
15920 char_u *r;
15921 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015922
15923 n = get_tv_number(&argvars[1]);
15924 if (argvars[0].v_type == VAR_LIST)
15925 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015926 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015927 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015928 if (list_extend(rettv->vval.v_list,
15929 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015930 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015931 }
15932 else
15933 {
15934 p = get_tv_string(&argvars[0]);
15935 rettv->v_type = VAR_STRING;
15936 rettv->vval.v_string = NULL;
15937
15938 slen = (int)STRLEN(p);
15939 len = slen * n;
15940 if (len <= 0)
15941 return;
15942
15943 r = alloc(len + 1);
15944 if (r != NULL)
15945 {
15946 for (i = 0; i < n; i++)
15947 mch_memmove(r + i * slen, p, (size_t)slen);
15948 r[len] = NUL;
15949 }
15950
15951 rettv->vval.v_string = r;
15952 }
15953}
15954
15955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 * "resolve()" function
15957 */
15958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015959f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015960 typval_T *argvars;
15961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962{
15963 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015964#ifdef HAVE_READLINK
15965 char_u *buf = NULL;
15966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015968 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969#ifdef FEAT_SHORTCUT
15970 {
15971 char_u *v = NULL;
15972
15973 v = mch_resolve_shortcut(p);
15974 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015975 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015977 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978 }
15979#else
15980# ifdef HAVE_READLINK
15981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 char_u *cpy;
15983 int len;
15984 char_u *remain = NULL;
15985 char_u *q;
15986 int is_relative_to_current = FALSE;
15987 int has_trailing_pathsep = FALSE;
15988 int limit = 100;
15989
15990 p = vim_strsave(p);
15991
15992 if (p[0] == '.' && (vim_ispathsep(p[1])
15993 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15994 is_relative_to_current = TRUE;
15995
15996 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015997 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015998 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016000 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002
16003 q = getnextcomp(p);
16004 if (*q != NUL)
16005 {
16006 /* Separate the first path component in "p", and keep the
16007 * remainder (beginning with the path separator). */
16008 remain = vim_strsave(q - 1);
16009 q[-1] = NUL;
16010 }
16011
Bram Moolenaard9462e32011-04-11 21:35:11 +020016012 buf = alloc(MAXPATHL + 1);
16013 if (buf == NULL)
16014 goto fail;
16015
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 for (;;)
16017 {
16018 for (;;)
16019 {
16020 len = readlink((char *)p, (char *)buf, MAXPATHL);
16021 if (len <= 0)
16022 break;
16023 buf[len] = NUL;
16024
16025 if (limit-- == 0)
16026 {
16027 vim_free(p);
16028 vim_free(remain);
16029 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 goto fail;
16032 }
16033
16034 /* Ensure that the result will have a trailing path separator
16035 * if the argument has one. */
16036 if (remain == NULL && has_trailing_pathsep)
16037 add_pathsep(buf);
16038
16039 /* Separate the first path component in the link value and
16040 * concatenate the remainders. */
16041 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16042 if (*q != NUL)
16043 {
16044 if (remain == NULL)
16045 remain = vim_strsave(q - 1);
16046 else
16047 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016048 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 if (cpy != NULL)
16050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 vim_free(remain);
16052 remain = cpy;
16053 }
16054 }
16055 q[-1] = NUL;
16056 }
16057
16058 q = gettail(p);
16059 if (q > p && *q == NUL)
16060 {
16061 /* Ignore trailing path separator. */
16062 q[-1] = NUL;
16063 q = gettail(p);
16064 }
16065 if (q > p && !mch_isFullName(buf))
16066 {
16067 /* symlink is relative to directory of argument */
16068 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16069 if (cpy != NULL)
16070 {
16071 STRCPY(cpy, p);
16072 STRCPY(gettail(cpy), buf);
16073 vim_free(p);
16074 p = cpy;
16075 }
16076 }
16077 else
16078 {
16079 vim_free(p);
16080 p = vim_strsave(buf);
16081 }
16082 }
16083
16084 if (remain == NULL)
16085 break;
16086
16087 /* Append the first path component of "remain" to "p". */
16088 q = getnextcomp(remain + 1);
16089 len = q - remain - (*q != NUL);
16090 cpy = vim_strnsave(p, STRLEN(p) + len);
16091 if (cpy != NULL)
16092 {
16093 STRNCAT(cpy, remain, len);
16094 vim_free(p);
16095 p = cpy;
16096 }
16097 /* Shorten "remain". */
16098 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016099 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 else
16101 {
16102 vim_free(remain);
16103 remain = NULL;
16104 }
16105 }
16106
16107 /* If the result is a relative path name, make it explicitly relative to
16108 * the current directory if and only if the argument had this form. */
16109 if (!vim_ispathsep(*p))
16110 {
16111 if (is_relative_to_current
16112 && *p != NUL
16113 && !(p[0] == '.'
16114 && (p[1] == NUL
16115 || vim_ispathsep(p[1])
16116 || (p[1] == '.'
16117 && (p[2] == NUL
16118 || vim_ispathsep(p[2]))))))
16119 {
16120 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016121 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 if (cpy != NULL)
16123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 vim_free(p);
16125 p = cpy;
16126 }
16127 }
16128 else if (!is_relative_to_current)
16129 {
16130 /* Strip leading "./". */
16131 q = p;
16132 while (q[0] == '.' && vim_ispathsep(q[1]))
16133 q += 2;
16134 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016135 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 }
16137 }
16138
16139 /* Ensure that the result will have no trailing path separator
16140 * if the argument had none. But keep "/" or "//". */
16141 if (!has_trailing_pathsep)
16142 {
16143 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016144 if (after_pathsep(p, q))
16145 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 }
16147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016148 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 }
16150# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016151 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152# endif
16153#endif
16154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016155 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156
16157#ifdef HAVE_READLINK
16158fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016159 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016161 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162}
16163
16164/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 */
16167 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 typval_T *argvars;
16170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171{
Bram Moolenaar33570922005-01-25 22:26:29 +000016172 list_T *l;
16173 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175 if (argvars[0].v_type != VAR_LIST)
16176 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016177 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016178 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016179 {
16180 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016181 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016182 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016183 while (li != NULL)
16184 {
16185 ni = li->li_prev;
16186 list_append(l, li);
16187 li = ni;
16188 }
16189 rettv->vval.v_list = l;
16190 rettv->v_type = VAR_LIST;
16191 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016192 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194}
16195
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016196#define SP_NOMOVE 0x01 /* don't move cursor */
16197#define SP_REPEAT 0x02 /* repeat to find outer pair */
16198#define SP_RETCOUNT 0x04 /* return matchcount */
16199#define SP_SETPCMARK 0x08 /* set previous context mark */
16200#define SP_START 0x10 /* accept match at start position */
16201#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16202#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016203
Bram Moolenaar33570922005-01-25 22:26:29 +000016204static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205
16206/*
16207 * Get flags for a search function.
16208 * Possibly sets "p_ws".
16209 * Returns BACKWARD, FORWARD or zero (for an error).
16210 */
16211 static int
16212get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016214 int *flagsp;
16215{
16216 int dir = FORWARD;
16217 char_u *flags;
16218 char_u nbuf[NUMBUFLEN];
16219 int mask;
16220
16221 if (varp->v_type != VAR_UNKNOWN)
16222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016223 flags = get_tv_string_buf_chk(varp, nbuf);
16224 if (flags == NULL)
16225 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226 while (*flags != NUL)
16227 {
16228 switch (*flags)
16229 {
16230 case 'b': dir = BACKWARD; break;
16231 case 'w': p_ws = TRUE; break;
16232 case 'W': p_ws = FALSE; break;
16233 default: mask = 0;
16234 if (flagsp != NULL)
16235 switch (*flags)
16236 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016237 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016238 case 'e': mask = SP_END; break;
16239 case 'm': mask = SP_RETCOUNT; break;
16240 case 'n': mask = SP_NOMOVE; break;
16241 case 'p': mask = SP_SUBPAT; break;
16242 case 'r': mask = SP_REPEAT; break;
16243 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244 }
16245 if (mask == 0)
16246 {
16247 EMSG2(_(e_invarg2), flags);
16248 dir = 0;
16249 }
16250 else
16251 *flagsp |= mask;
16252 }
16253 if (dir == 0)
16254 break;
16255 ++flags;
16256 }
16257 }
16258 return dir;
16259}
16260
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016262 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016264 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016265search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016266 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016267 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016268 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016270 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 char_u *pat;
16272 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016273 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 int save_p_ws = p_ws;
16275 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016276 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016277 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016278 proftime_T tm;
16279#ifdef FEAT_RELTIME
16280 long time_limit = 0;
16281#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016282 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016283 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016285 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016286 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016287 if (dir == 0)
16288 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016289 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016290 if (flags & SP_START)
16291 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016292 if (flags & SP_END)
16293 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016294
Bram Moolenaar76929292008-01-06 19:07:36 +000016295 /* Optional arguments: line number to stop searching and timeout. */
16296 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016297 {
16298 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16299 if (lnum_stop < 0)
16300 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016301#ifdef FEAT_RELTIME
16302 if (argvars[3].v_type != VAR_UNKNOWN)
16303 {
16304 time_limit = get_tv_number_chk(&argvars[3], NULL);
16305 if (time_limit < 0)
16306 goto theend;
16307 }
16308#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016309 }
16310
Bram Moolenaar76929292008-01-06 19:07:36 +000016311#ifdef FEAT_RELTIME
16312 /* Set the time limit, if there is one. */
16313 profile_setlimit(time_limit, &tm);
16314#endif
16315
Bram Moolenaar231334e2005-07-25 20:46:57 +000016316 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016317 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016318 * Check to make sure only those flags are set.
16319 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16320 * flags cannot be set. Check for that condition also.
16321 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016322 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016323 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016325 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016326 goto theend;
16327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016329 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016330 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016331 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016332 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016334 if (flags & SP_SUBPAT)
16335 retval = subpatnum;
16336 else
16337 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016338 if (flags & SP_SETPCMARK)
16339 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016341 if (match_pos != NULL)
16342 {
16343 /* Store the match cursor position */
16344 match_pos->lnum = pos.lnum;
16345 match_pos->col = pos.col + 1;
16346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 /* "/$" will put the cursor after the end of the line, may need to
16348 * correct that here */
16349 check_cursor();
16350 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016351
16352 /* If 'n' flag is used: restore cursor position. */
16353 if (flags & SP_NOMOVE)
16354 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016355 else
16356 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016357theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016359
16360 return retval;
16361}
16362
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016363#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016364
16365/*
16366 * round() is not in C90, use ceil() or floor() instead.
16367 */
16368 float_T
16369vim_round(f)
16370 float_T f;
16371{
16372 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16373}
16374
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016375/*
16376 * "round({float})" function
16377 */
16378 static void
16379f_round(argvars, rettv)
16380 typval_T *argvars;
16381 typval_T *rettv;
16382{
16383 float_T f;
16384
16385 rettv->v_type = VAR_FLOAT;
16386 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016387 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016388 else
16389 rettv->vval.v_float = 0.0;
16390}
16391#endif
16392
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016393/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016394 * "screenattr()" function
16395 */
16396 static void
16397f_screenattr(argvars, rettv)
16398 typval_T *argvars UNUSED;
16399 typval_T *rettv;
16400{
16401 int row;
16402 int col;
16403 int c;
16404
16405 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16406 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16407 if (row < 0 || row >= screen_Rows
16408 || col < 0 || col >= screen_Columns)
16409 c = -1;
16410 else
16411 c = ScreenAttrs[LineOffset[row] + col];
16412 rettv->vval.v_number = c;
16413}
16414
16415/*
16416 * "screenchar()" function
16417 */
16418 static void
16419f_screenchar(argvars, rettv)
16420 typval_T *argvars UNUSED;
16421 typval_T *rettv;
16422{
16423 int row;
16424 int col;
16425 int off;
16426 int c;
16427
16428 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16429 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16430 if (row < 0 || row >= screen_Rows
16431 || col < 0 || col >= screen_Columns)
16432 c = -1;
16433 else
16434 {
16435 off = LineOffset[row] + col;
16436#ifdef FEAT_MBYTE
16437 if (enc_utf8 && ScreenLinesUC[off] != 0)
16438 c = ScreenLinesUC[off];
16439 else
16440#endif
16441 c = ScreenLines[off];
16442 }
16443 rettv->vval.v_number = c;
16444}
16445
16446/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016447 * "screencol()" function
16448 *
16449 * First column is 1 to be consistent with virtcol().
16450 */
16451 static void
16452f_screencol(argvars, rettv)
16453 typval_T *argvars UNUSED;
16454 typval_T *rettv;
16455{
16456 rettv->vval.v_number = screen_screencol() + 1;
16457}
16458
16459/*
16460 * "screenrow()" function
16461 */
16462 static void
16463f_screenrow(argvars, rettv)
16464 typval_T *argvars UNUSED;
16465 typval_T *rettv;
16466{
16467 rettv->vval.v_number = screen_screenrow() + 1;
16468}
16469
16470/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016471 * "search()" function
16472 */
16473 static void
16474f_search(argvars, rettv)
16475 typval_T *argvars;
16476 typval_T *rettv;
16477{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016478 int flags = 0;
16479
16480 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481}
16482
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016484 * "searchdecl()" function
16485 */
16486 static void
16487f_searchdecl(argvars, rettv)
16488 typval_T *argvars;
16489 typval_T *rettv;
16490{
16491 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016492 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016493 int error = FALSE;
16494 char_u *name;
16495
16496 rettv->vval.v_number = 1; /* default: FAIL */
16497
16498 name = get_tv_string_chk(&argvars[0]);
16499 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016500 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016501 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016502 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16503 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16504 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016505 if (!error && name != NULL)
16506 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016507 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016508}
16509
16510/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016511 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016513 static int
16514searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016516 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517{
16518 char_u *spat, *mpat, *epat;
16519 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 int dir;
16522 int flags = 0;
16523 char_u nbuf1[NUMBUFLEN];
16524 char_u nbuf2[NUMBUFLEN];
16525 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016526 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016527 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016528 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 spat = get_tv_string_chk(&argvars[0]);
16532 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16533 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16534 if (spat == NULL || mpat == NULL || epat == NULL)
16535 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 /* Handle the optional fourth argument: flags */
16538 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016539 if (dir == 0)
16540 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016541
16542 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016543 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16544 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016545 if ((flags & (SP_END | SP_SUBPAT)) != 0
16546 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016547 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016548 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016549 goto theend;
16550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016552 /* Using 'r' implies 'W', otherwise it doesn't work. */
16553 if (flags & SP_REPEAT)
16554 p_ws = FALSE;
16555
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016556 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016557 if (argvars[3].v_type == VAR_UNKNOWN
16558 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 skip = (char_u *)"";
16560 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016562 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016563 if (argvars[5].v_type != VAR_UNKNOWN)
16564 {
16565 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16566 if (lnum_stop < 0)
16567 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016568#ifdef FEAT_RELTIME
16569 if (argvars[6].v_type != VAR_UNKNOWN)
16570 {
16571 time_limit = get_tv_number_chk(&argvars[6], NULL);
16572 if (time_limit < 0)
16573 goto theend;
16574 }
16575#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016576 }
16577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016578 if (skip == NULL)
16579 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016581 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016582 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016583
16584theend:
16585 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016586
16587 return retval;
16588}
16589
16590/*
16591 * "searchpair()" function
16592 */
16593 static void
16594f_searchpair(argvars, rettv)
16595 typval_T *argvars;
16596 typval_T *rettv;
16597{
16598 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16599}
16600
16601/*
16602 * "searchpairpos()" function
16603 */
16604 static void
16605f_searchpairpos(argvars, rettv)
16606 typval_T *argvars;
16607 typval_T *rettv;
16608{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016609 pos_T match_pos;
16610 int lnum = 0;
16611 int col = 0;
16612
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016613 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016614 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016615
16616 if (searchpair_cmn(argvars, &match_pos) > 0)
16617 {
16618 lnum = match_pos.lnum;
16619 col = match_pos.col;
16620 }
16621
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016622 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16623 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016624}
16625
16626/*
16627 * Search for a start/middle/end thing.
16628 * Used by searchpair(), see its documentation for the details.
16629 * Returns 0 or -1 for no match,
16630 */
16631 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016632do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16633 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016634 char_u *spat; /* start pattern */
16635 char_u *mpat; /* middle pattern */
16636 char_u *epat; /* end pattern */
16637 int dir; /* BACKWARD or FORWARD */
16638 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016639 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016640 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016641 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016642 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016643{
16644 char_u *save_cpo;
16645 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16646 long retval = 0;
16647 pos_T pos;
16648 pos_T firstpos;
16649 pos_T foundpos;
16650 pos_T save_cursor;
16651 pos_T save_pos;
16652 int n;
16653 int r;
16654 int nest = 1;
16655 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016656 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016657 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016658
16659 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16660 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016661 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016662
Bram Moolenaar76929292008-01-06 19:07:36 +000016663#ifdef FEAT_RELTIME
16664 /* Set the time limit, if there is one. */
16665 profile_setlimit(time_limit, &tm);
16666#endif
16667
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016668 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16669 * start/middle/end (pat3, for the top pair). */
16670 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16671 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16672 if (pat2 == NULL || pat3 == NULL)
16673 goto theend;
16674 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16675 if (*mpat == NUL)
16676 STRCPY(pat3, pat2);
16677 else
16678 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16679 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016680 if (flags & SP_START)
16681 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016682
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683 save_cursor = curwin->w_cursor;
16684 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016685 clearpos(&firstpos);
16686 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 pat = pat3;
16688 for (;;)
16689 {
16690 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016691 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16693 /* didn't find it or found the first match again: FAIL */
16694 break;
16695
16696 if (firstpos.lnum == 0)
16697 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016698 if (equalpos(pos, foundpos))
16699 {
16700 /* Found the same position again. Can happen with a pattern that
16701 * has "\zs" at the end and searching backwards. Advance one
16702 * character and try again. */
16703 if (dir == BACKWARD)
16704 decl(&pos);
16705 else
16706 incl(&pos);
16707 }
16708 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016710 /* clear the start flag to avoid getting stuck here */
16711 options &= ~SEARCH_START;
16712
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 /* If the skip pattern matches, ignore this match. */
16714 if (*skip != NUL)
16715 {
16716 save_pos = curwin->w_cursor;
16717 curwin->w_cursor = pos;
16718 r = eval_to_bool(skip, &err, NULL, FALSE);
16719 curwin->w_cursor = save_pos;
16720 if (err)
16721 {
16722 /* Evaluating {skip} caused an error, break here. */
16723 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016724 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 break;
16726 }
16727 if (r)
16728 continue;
16729 }
16730
16731 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16732 {
16733 /* Found end when searching backwards or start when searching
16734 * forward: nested pair. */
16735 ++nest;
16736 pat = pat2; /* nested, don't search for middle */
16737 }
16738 else
16739 {
16740 /* Found end when searching forward or start when searching
16741 * backward: end of (nested) pair; or found middle in outer pair. */
16742 if (--nest == 1)
16743 pat = pat3; /* outer level, search for middle */
16744 }
16745
16746 if (nest == 0)
16747 {
16748 /* Found the match: return matchcount or line number. */
16749 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016750 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016752 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016753 if (flags & SP_SETPCMARK)
16754 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 curwin->w_cursor = pos;
16756 if (!(flags & SP_REPEAT))
16757 break;
16758 nest = 1; /* search for next unmatched */
16759 }
16760 }
16761
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016762 if (match_pos != NULL)
16763 {
16764 /* Store the match cursor position */
16765 match_pos->lnum = curwin->w_cursor.lnum;
16766 match_pos->col = curwin->w_cursor.col + 1;
16767 }
16768
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016770 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 curwin->w_cursor = save_cursor;
16772
16773theend:
16774 vim_free(pat2);
16775 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016776 if (p_cpo == empty_option)
16777 p_cpo = save_cpo;
16778 else
16779 /* Darn, evaluating the {skip} expression changed the value. */
16780 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016781
16782 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783}
16784
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016785/*
16786 * "searchpos()" function
16787 */
16788 static void
16789f_searchpos(argvars, rettv)
16790 typval_T *argvars;
16791 typval_T *rettv;
16792{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016793 pos_T match_pos;
16794 int lnum = 0;
16795 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016796 int n;
16797 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016798
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016799 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016800 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016801
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016802 n = search_cmn(argvars, &match_pos, &flags);
16803 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016804 {
16805 lnum = match_pos.lnum;
16806 col = match_pos.col;
16807 }
16808
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016809 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16810 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016811 if (flags & SP_SUBPAT)
16812 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016813}
16814
16815
Bram Moolenaar0d660222005-01-07 21:51:51 +000016816 static void
16817f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016818 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016821#ifdef FEAT_CLIENTSERVER
16822 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016823 char_u *server = get_tv_string_chk(&argvars[0]);
16824 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825
Bram Moolenaar0d660222005-01-07 21:51:51 +000016826 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016827 if (server == NULL || reply == NULL)
16828 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016829 if (check_restricted() || check_secure())
16830 return;
16831# ifdef FEAT_X11
16832 if (check_connection() == FAIL)
16833 return;
16834# endif
16835
16836 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016838 EMSG(_("E258: Unable to send to client"));
16839 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841 rettv->vval.v_number = 0;
16842#else
16843 rettv->vval.v_number = -1;
16844#endif
16845}
16846
Bram Moolenaar0d660222005-01-07 21:51:51 +000016847 static void
16848f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016849 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016850 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016851{
16852 char_u *r = NULL;
16853
16854#ifdef FEAT_CLIENTSERVER
16855# ifdef WIN32
16856 r = serverGetVimNames();
16857# else
16858 make_connection();
16859 if (X_DISPLAY != NULL)
16860 r = serverGetVimNames(X_DISPLAY);
16861# endif
16862#endif
16863 rettv->v_type = VAR_STRING;
16864 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865}
16866
16867/*
16868 * "setbufvar()" function
16869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016871f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016872 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016873 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874{
16875 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016878 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 char_u nbuf[NUMBUFLEN];
16880
16881 if (check_restricted() || check_secure())
16882 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016883 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16884 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016885 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 varp = &argvars[2];
16887
16888 if (buf != NULL && varname != NULL && varp != NULL)
16889 {
16890 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892
16893 if (*varname == '&')
16894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016895 long numval;
16896 char_u *strval;
16897 int error = FALSE;
16898
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016900 numval = get_tv_number_chk(varp, &error);
16901 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016902 if (!error && strval != NULL)
16903 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904 }
16905 else
16906 {
16907 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16908 if (bufvarname != NULL)
16909 {
16910 STRCPY(bufvarname, "b:");
16911 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016912 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 vim_free(bufvarname);
16914 }
16915 }
16916
16917 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920}
16921
16922/*
16923 * "setcmdpos()" function
16924 */
16925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016926f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 typval_T *argvars;
16928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016930 int pos = (int)get_tv_number(&argvars[0]) - 1;
16931
16932 if (pos >= 0)
16933 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934}
16935
16936/*
16937 * "setline()" function
16938 */
16939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016940f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016941 typval_T *argvars;
16942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943{
16944 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016945 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016946 list_T *l = NULL;
16947 listitem_T *li = NULL;
16948 long added = 0;
16949 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016951 lnum = get_tv_lnum(&argvars[0]);
16952 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016954 l = argvars[1].vval.v_list;
16955 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016957 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016958 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016959
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016960 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016961 for (;;)
16962 {
16963 if (l != NULL)
16964 {
16965 /* list argument, get next string */
16966 if (li == NULL)
16967 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016968 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016969 li = li->li_next;
16970 }
16971
16972 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016973 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016974 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016975
16976 /* When coming here from Insert mode, sync undo, so that this can be
16977 * undone separately from what was previously inserted. */
16978 if (u_sync_once == 2)
16979 {
16980 u_sync_once = 1; /* notify that u_sync() was called */
16981 u_sync(TRUE);
16982 }
16983
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016984 if (lnum <= curbuf->b_ml.ml_line_count)
16985 {
16986 /* existing line, replace it */
16987 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16988 {
16989 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016990 if (lnum == curwin->w_cursor.lnum)
16991 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016992 rettv->vval.v_number = 0; /* OK */
16993 }
16994 }
16995 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16996 {
16997 /* lnum is one past the last line, append the line */
16998 ++added;
16999 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17000 rettv->vval.v_number = 0; /* OK */
17001 }
17002
17003 if (l == NULL) /* only one string argument */
17004 break;
17005 ++lnum;
17006 }
17007
17008 if (added > 0)
17009 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010}
17011
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017012static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17013
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017015 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017016 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017017 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017018set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017019 win_T *wp UNUSED;
17020 typval_T *list_arg UNUSED;
17021 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017022 typval_T *rettv;
17023{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017024#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017025 char_u *act;
17026 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017027#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017028
Bram Moolenaar2641f772005-03-25 21:58:17 +000017029 rettv->vval.v_number = -1;
17030
17031#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017032 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017033 EMSG(_(e_listreq));
17034 else
17035 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017036 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017037
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017038 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017039 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017040 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017041 if (act == NULL)
17042 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017043 if (*act == 'a' || *act == 'r')
17044 action = *act;
17045 }
17046
Bram Moolenaar81484f42012-12-05 15:16:47 +010017047 if (l != NULL && set_errorlist(wp, l, action,
17048 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017049 rettv->vval.v_number = 0;
17050 }
17051#endif
17052}
17053
17054/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017055 * "setloclist()" function
17056 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017057 static void
17058f_setloclist(argvars, rettv)
17059 typval_T *argvars;
17060 typval_T *rettv;
17061{
17062 win_T *win;
17063
17064 rettv->vval.v_number = -1;
17065
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017066 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017067 if (win != NULL)
17068 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17069}
17070
17071/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017072 * "setmatches()" function
17073 */
17074 static void
17075f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017076 typval_T *argvars UNUSED;
17077 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017078{
17079#ifdef FEAT_SEARCH_EXTRA
17080 list_T *l;
17081 listitem_T *li;
17082 dict_T *d;
17083
17084 rettv->vval.v_number = -1;
17085 if (argvars[0].v_type != VAR_LIST)
17086 {
17087 EMSG(_(e_listreq));
17088 return;
17089 }
17090 if ((l = argvars[0].vval.v_list) != NULL)
17091 {
17092
17093 /* To some extent make sure that we are dealing with a list from
17094 * "getmatches()". */
17095 li = l->lv_first;
17096 while (li != NULL)
17097 {
17098 if (li->li_tv.v_type != VAR_DICT
17099 || (d = li->li_tv.vval.v_dict) == NULL)
17100 {
17101 EMSG(_(e_invarg));
17102 return;
17103 }
17104 if (!(dict_find(d, (char_u *)"group", -1) != NULL
17105 && dict_find(d, (char_u *)"pattern", -1) != NULL
17106 && dict_find(d, (char_u *)"priority", -1) != NULL
17107 && dict_find(d, (char_u *)"id", -1) != NULL))
17108 {
17109 EMSG(_(e_invarg));
17110 return;
17111 }
17112 li = li->li_next;
17113 }
17114
17115 clear_matches(curwin);
17116 li = l->lv_first;
17117 while (li != NULL)
17118 {
17119 d = li->li_tv.vval.v_dict;
17120 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17121 get_dict_string(d, (char_u *)"pattern", FALSE),
17122 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017123 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017124 li = li->li_next;
17125 }
17126 rettv->vval.v_number = 0;
17127 }
17128#endif
17129}
17130
17131/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017132 * "setpos()" function
17133 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017134 static void
17135f_setpos(argvars, rettv)
17136 typval_T *argvars;
17137 typval_T *rettv;
17138{
17139 pos_T pos;
17140 int fnum;
17141 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017142 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017143
Bram Moolenaar08250432008-02-13 11:42:46 +000017144 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017145 name = get_tv_string_chk(argvars);
17146 if (name != NULL)
17147 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017148 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017149 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017150 if (--pos.col < 0)
17151 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017152 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017153 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017154 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017155 if (fnum == curbuf->b_fnum)
17156 {
17157 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017158 if (curswant >= 0)
17159 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017160 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017161 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017162 }
17163 else
17164 EMSG(_(e_invarg));
17165 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017166 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17167 {
17168 /* set mark */
17169 if (setmark_pos(name[1], &pos, fnum) == OK)
17170 rettv->vval.v_number = 0;
17171 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017172 else
17173 EMSG(_(e_invarg));
17174 }
17175 }
17176}
17177
17178/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017179 * "setqflist()" function
17180 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017181 static void
17182f_setqflist(argvars, rettv)
17183 typval_T *argvars;
17184 typval_T *rettv;
17185{
17186 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17187}
17188
17189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 * "setreg()" function
17191 */
17192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017193f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017194 typval_T *argvars;
17195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196{
17197 int regname;
17198 char_u *strregname;
17199 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017200 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201 int append;
17202 char_u yank_type;
17203 long block_len;
17204
17205 block_len = -1;
17206 yank_type = MAUTO;
17207 append = FALSE;
17208
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017209 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017212 if (strregname == NULL)
17213 return; /* type error; errmsg already given */
17214 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 if (regname == 0 || regname == '@')
17216 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017218 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017220 stropt = get_tv_string_chk(&argvars[2]);
17221 if (stropt == NULL)
17222 return; /* type error */
17223 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 switch (*stropt)
17225 {
17226 case 'a': case 'A': /* append */
17227 append = TRUE;
17228 break;
17229 case 'v': case 'c': /* character-wise selection */
17230 yank_type = MCHAR;
17231 break;
17232 case 'V': case 'l': /* line-wise selection */
17233 yank_type = MLINE;
17234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 case 'b': case Ctrl_V: /* block-wise selection */
17236 yank_type = MBLOCK;
17237 if (VIM_ISDIGIT(stropt[1]))
17238 {
17239 ++stropt;
17240 block_len = getdigits(&stropt) - 1;
17241 --stropt;
17242 }
17243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 }
17245 }
17246
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017247 if (argvars[1].v_type == VAR_LIST)
17248 {
17249 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017250 char_u **allocval;
17251 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017252 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017253 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017254 int len = argvars[1].vval.v_list->lv_len;
17255 listitem_T *li;
17256
Bram Moolenaar7d647822014-04-05 21:28:56 +020017257 /* First half: use for pointers to result lines; second half: use for
17258 * pointers to allocated copies. */
17259 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017260 if (lstval == NULL)
17261 return;
17262 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017263 allocval = lstval + len + 2;
17264 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017265
17266 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17267 li = li->li_next)
17268 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017269 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017270 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017271 goto free_lstval;
17272 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017273 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017274 /* Need to make a copy, next get_tv_string_buf_chk() will
17275 * overwrite the string. */
17276 strval = vim_strsave(buf);
17277 if (strval == NULL)
17278 goto free_lstval;
17279 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017280 }
17281 *curval++ = strval;
17282 }
17283 *curval++ = NULL;
17284
17285 write_reg_contents_lst(regname, lstval, -1,
17286 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017287free_lstval:
17288 while (curallocval > allocval)
17289 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017290 vim_free(lstval);
17291 }
17292 else
17293 {
17294 strval = get_tv_string_chk(&argvars[1]);
17295 if (strval == NULL)
17296 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017297 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301}
17302
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017303/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017304 * "settabvar()" function
17305 */
17306 static void
17307f_settabvar(argvars, rettv)
17308 typval_T *argvars;
17309 typval_T *rettv;
17310{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017311#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017312 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017313 tabpage_T *tp;
17314#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017315 char_u *varname, *tabvarname;
17316 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017317
17318 rettv->vval.v_number = 0;
17319
17320 if (check_restricted() || check_secure())
17321 return;
17322
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017323#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017324 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017325#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017326 varname = get_tv_string_chk(&argvars[1]);
17327 varp = &argvars[2];
17328
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017329 if (varname != NULL && varp != NULL
17330#ifdef FEAT_WINDOWS
17331 && tp != NULL
17332#endif
17333 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017334 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017335#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017336 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017337 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017338#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017339
17340 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17341 if (tabvarname != NULL)
17342 {
17343 STRCPY(tabvarname, "t:");
17344 STRCPY(tabvarname + 2, varname);
17345 set_var(tabvarname, varp, TRUE);
17346 vim_free(tabvarname);
17347 }
17348
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017349#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017350 /* Restore current tabpage */
17351 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017352 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017353#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017354 }
17355}
17356
17357/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017358 * "settabwinvar()" function
17359 */
17360 static void
17361f_settabwinvar(argvars, rettv)
17362 typval_T *argvars;
17363 typval_T *rettv;
17364{
17365 setwinvar(argvars, rettv, 1);
17366}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367
17368/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017369 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 typval_T *argvars;
17374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017376 setwinvar(argvars, rettv, 0);
17377}
17378
17379/*
17380 * "setwinvar()" and "settabwinvar()" functions
17381 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017382
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017383 static void
17384setwinvar(argvars, rettv, off)
17385 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017386 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017387 int off;
17388{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 win_T *win;
17390#ifdef FEAT_WINDOWS
17391 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017392 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393#endif
17394 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017395 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017397 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398
17399 if (check_restricted() || check_secure())
17400 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017401
17402#ifdef FEAT_WINDOWS
17403 if (off == 1)
17404 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17405 else
17406 tp = curtab;
17407#endif
17408 win = find_win_by_nr(&argvars[off], tp);
17409 varname = get_tv_string_chk(&argvars[off + 1]);
17410 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411
17412 if (win != NULL && varname != NULL && varp != NULL)
17413 {
17414#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017415 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017418 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017420 long numval;
17421 char_u *strval;
17422 int error = FALSE;
17423
17424 ++varname;
17425 numval = get_tv_number_chk(varp, &error);
17426 strval = get_tv_string_buf_chk(varp, nbuf);
17427 if (!error && strval != NULL)
17428 set_option_value(varname, numval, strval, OPT_LOCAL);
17429 }
17430 else
17431 {
17432 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17433 if (winvarname != NULL)
17434 {
17435 STRCPY(winvarname, "w:");
17436 STRCPY(winvarname + 2, varname);
17437 set_var(winvarname, varp, TRUE);
17438 vim_free(winvarname);
17439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 }
17441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017443 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444#endif
17445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446}
17447
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017448#ifdef FEAT_CRYPT
17449/*
17450 * "sha256({string})" function
17451 */
17452 static void
17453f_sha256(argvars, rettv)
17454 typval_T *argvars;
17455 typval_T *rettv;
17456{
17457 char_u *p;
17458
17459 p = get_tv_string(&argvars[0]);
17460 rettv->vval.v_string = vim_strsave(
17461 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17462 rettv->v_type = VAR_STRING;
17463}
17464#endif /* FEAT_CRYPT */
17465
Bram Moolenaar071d4272004-06-13 20:20:40 +000017466/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017467 * "shellescape({string})" function
17468 */
17469 static void
17470f_shellescape(argvars, rettv)
17471 typval_T *argvars;
17472 typval_T *rettv;
17473{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017474 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017475 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017476 rettv->v_type = VAR_STRING;
17477}
17478
17479/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017480 * shiftwidth() function
17481 */
17482 static void
17483f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017484 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017485 typval_T *rettv;
17486{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017487 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017488}
17489
17490/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017491 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 */
17493 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017494f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017495 typval_T *argvars;
17496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017498 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499
Bram Moolenaar0d660222005-01-07 21:51:51 +000017500 p = get_tv_string(&argvars[0]);
17501 rettv->vval.v_string = vim_strsave(p);
17502 simplify_filename(rettv->vval.v_string); /* simplify in place */
17503 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504}
17505
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017506#ifdef FEAT_FLOAT
17507/*
17508 * "sin()" function
17509 */
17510 static void
17511f_sin(argvars, rettv)
17512 typval_T *argvars;
17513 typval_T *rettv;
17514{
17515 float_T f;
17516
17517 rettv->v_type = VAR_FLOAT;
17518 if (get_float_arg(argvars, &f) == OK)
17519 rettv->vval.v_float = sin(f);
17520 else
17521 rettv->vval.v_float = 0.0;
17522}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017523
17524/*
17525 * "sinh()" function
17526 */
17527 static void
17528f_sinh(argvars, rettv)
17529 typval_T *argvars;
17530 typval_T *rettv;
17531{
17532 float_T f;
17533
17534 rettv->v_type = VAR_FLOAT;
17535 if (get_float_arg(argvars, &f) == OK)
17536 rettv->vval.v_float = sinh(f);
17537 else
17538 rettv->vval.v_float = 0.0;
17539}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017540#endif
17541
Bram Moolenaar0d660222005-01-07 21:51:51 +000017542static int
17543#ifdef __BORLANDC__
17544 _RTLENTRYF
17545#endif
17546 item_compare __ARGS((const void *s1, const void *s2));
17547static int
17548#ifdef __BORLANDC__
17549 _RTLENTRYF
17550#endif
17551 item_compare2 __ARGS((const void *s1, const void *s2));
17552
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017553/* struct used in the array that's given to qsort() */
17554typedef struct
17555{
17556 listitem_T *item;
17557 int idx;
17558} sortItem_T;
17559
Bram Moolenaar0d660222005-01-07 21:51:51 +000017560static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017561static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017562static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017563static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017564static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017565static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017566static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567#define ITEM_COMPARE_FAIL 999
17568
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017570 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017572 static int
17573#ifdef __BORLANDC__
17574_RTLENTRYF
17575#endif
17576item_compare(s1, s2)
17577 const void *s1;
17578 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017580 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017581 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017582 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017583 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017584 int res;
17585 char_u numbuf1[NUMBUFLEN];
17586 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017588 si1 = (sortItem_T *)s1;
17589 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017590 tv1 = &si1->item->li_tv;
17591 tv2 = &si2->item->li_tv;
17592 /* tv2string() puts quotes around a string and allocates memory. Don't do
17593 * that for string variables. Use a single quote when comparing with a
17594 * non-string to do what the docs promise. */
17595 if (tv1->v_type == VAR_STRING)
17596 {
17597 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17598 p1 = (char_u *)"'";
17599 else
17600 p1 = tv1->vval.v_string;
17601 }
17602 else
17603 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17604 if (tv2->v_type == VAR_STRING)
17605 {
17606 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17607 p2 = (char_u *)"'";
17608 else
17609 p2 = tv2->vval.v_string;
17610 }
17611 else
17612 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017613 if (p1 == NULL)
17614 p1 = (char_u *)"";
17615 if (p2 == NULL)
17616 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017617 if (!item_compare_numeric)
17618 {
17619 if (item_compare_ic)
17620 res = STRICMP(p1, p2);
17621 else
17622 res = STRCMP(p1, p2);
17623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017625 {
17626 double n1, n2;
17627 n1 = strtod((char *)p1, (char **)&p1);
17628 n2 = strtod((char *)p2, (char **)&p2);
17629 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17630 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017631
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017632 /* When the result would be zero, compare the item indexes. Makes the
17633 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017634 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017635 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017636
Bram Moolenaar0d660222005-01-07 21:51:51 +000017637 vim_free(tofree1);
17638 vim_free(tofree2);
17639 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640}
17641
17642 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017643#ifdef __BORLANDC__
17644_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017646item_compare2(s1, s2)
17647 const void *s1;
17648 const void *s2;
17649{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017650 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017651 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017652 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017653 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017654 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017656 /* shortcut after failure in previous call; compare all items equal */
17657 if (item_compare_func_err)
17658 return 0;
17659
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017660 si1 = (sortItem_T *)s1;
17661 si2 = (sortItem_T *)s2;
17662
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017663 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017664 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017665 copy_tv(&si1->item->li_tv, &argv[0]);
17666 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017667
17668 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017669 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017670 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17671 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017672 clear_tv(&argv[0]);
17673 clear_tv(&argv[1]);
17674
17675 if (res == FAIL)
17676 res = ITEM_COMPARE_FAIL;
17677 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017678 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17679 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017680 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017681 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017682
17683 /* When the result would be zero, compare the pointers themselves. Makes
17684 * the sort stable. */
17685 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017686 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017687
Bram Moolenaar0d660222005-01-07 21:51:51 +000017688 return res;
17689}
17690
17691/*
17692 * "sort({list})" function
17693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017695do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017696 typval_T *argvars;
17697 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017698 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699{
Bram Moolenaar33570922005-01-25 22:26:29 +000017700 list_T *l;
17701 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017702 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703 long len;
17704 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705
Bram Moolenaar0d660222005-01-07 21:51:51 +000017706 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017707 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 else
17709 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017710 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017711 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017712 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017713 return;
17714 rettv->vval.v_list = l;
17715 rettv->v_type = VAR_LIST;
17716 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717
Bram Moolenaar0d660222005-01-07 21:51:51 +000017718 len = list_len(l);
17719 if (len <= 1)
17720 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721
Bram Moolenaar0d660222005-01-07 21:51:51 +000017722 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017723 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017724 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017725 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017726 if (argvars[1].v_type != VAR_UNKNOWN)
17727 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017728 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017729 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017730 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017731 else
17732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017733 int error = FALSE;
17734
17735 i = get_tv_number_chk(&argvars[1], &error);
17736 if (error)
17737 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738 if (i == 1)
17739 item_compare_ic = TRUE;
17740 else
17741 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017742 if (item_compare_func != NULL)
17743 {
17744 if (STRCMP(item_compare_func, "n") == 0)
17745 {
17746 item_compare_func = NULL;
17747 item_compare_numeric = TRUE;
17748 }
17749 else if (STRCMP(item_compare_func, "i") == 0)
17750 {
17751 item_compare_func = NULL;
17752 item_compare_ic = TRUE;
17753 }
17754 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017755 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017756
17757 if (argvars[2].v_type != VAR_UNKNOWN)
17758 {
17759 /* optional third argument: {dict} */
17760 if (argvars[2].v_type != VAR_DICT)
17761 {
17762 EMSG(_(e_dictreq));
17763 return;
17764 }
17765 item_compare_selfdict = argvars[2].vval.v_dict;
17766 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768
Bram Moolenaar0d660222005-01-07 21:51:51 +000017769 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017770 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017771 if (ptrs == NULL)
17772 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773
Bram Moolenaar327aa022014-03-25 18:24:23 +010017774 i = 0;
17775 if (sort)
17776 {
17777 /* sort(): ptrs will be the list to sort */
17778 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017779 {
17780 ptrs[i].item = li;
17781 ptrs[i].idx = i;
17782 ++i;
17783 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017784
17785 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017786 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017787 /* test the compare function */
17788 if (item_compare_func != NULL
17789 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017791 EMSG(_("E702: Sort compare function failed"));
17792 else
17793 {
17794 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017795 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017796 item_compare_func == NULL ? item_compare : item_compare2);
17797
17798 if (!item_compare_func_err)
17799 {
17800 /* Clear the List and append the items in sorted order. */
17801 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17802 l->lv_len = 0;
17803 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017804 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017805 }
17806 }
17807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017809 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017810 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17811
17812 /* f_uniq(): ptrs will be a stack of items to remove */
17813 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017814 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017815 item_compare_func_ptr = item_compare_func
17816 ? item_compare2 : item_compare;
17817
17818 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17819 li = li->li_next)
17820 {
17821 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17822 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017823 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017824 if (item_compare_func_err)
17825 {
17826 EMSG(_("E882: Uniq compare function failed"));
17827 break;
17828 }
17829 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017831 if (!item_compare_func_err)
17832 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017833 while (--i >= 0)
17834 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017835 li = ptrs[i].item->li_next;
17836 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017837 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017838 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017839 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017840 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017841 list_fix_watch(l, li);
17842 listitem_free(li);
17843 l->lv_len--;
17844 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017845 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017846 }
17847
17848 vim_free(ptrs);
17849 }
17850}
17851
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017852/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017853 * "sort({list})" function
17854 */
17855 static void
17856f_sort(argvars, rettv)
17857 typval_T *argvars;
17858 typval_T *rettv;
17859{
17860 do_sort_uniq(argvars, rettv, TRUE);
17861}
17862
17863/*
17864 * "uniq({list})" function
17865 */
17866 static void
17867f_uniq(argvars, rettv)
17868 typval_T *argvars;
17869 typval_T *rettv;
17870{
17871 do_sort_uniq(argvars, rettv, FALSE);
17872}
17873
17874/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017875 * "soundfold({word})" function
17876 */
17877 static void
17878f_soundfold(argvars, rettv)
17879 typval_T *argvars;
17880 typval_T *rettv;
17881{
17882 char_u *s;
17883
17884 rettv->v_type = VAR_STRING;
17885 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017886#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017887 rettv->vval.v_string = eval_soundfold(s);
17888#else
17889 rettv->vval.v_string = vim_strsave(s);
17890#endif
17891}
17892
17893/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017894 * "spellbadword()" function
17895 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017896 static void
17897f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017898 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017899 typval_T *rettv;
17900{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017901 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017902 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017903 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017904
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017905 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017906 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017907
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017908#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017909 if (argvars[0].v_type == VAR_UNKNOWN)
17910 {
17911 /* Find the start and length of the badly spelled word. */
17912 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17913 if (len != 0)
17914 word = ml_get_cursor();
17915 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017916 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017917 {
17918 char_u *str = get_tv_string_chk(&argvars[0]);
17919 int capcol = -1;
17920
17921 if (str != NULL)
17922 {
17923 /* Check the argument for spelling. */
17924 while (*str != NUL)
17925 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017926 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017927 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017928 {
17929 word = str;
17930 break;
17931 }
17932 str += len;
17933 }
17934 }
17935 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017936#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017937
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017938 list_append_string(rettv->vval.v_list, word, len);
17939 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017940 attr == HLF_SPB ? "bad" :
17941 attr == HLF_SPR ? "rare" :
17942 attr == HLF_SPL ? "local" :
17943 attr == HLF_SPC ? "caps" :
17944 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017945}
17946
17947/*
17948 * "spellsuggest()" function
17949 */
17950 static void
17951f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017952 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017953 typval_T *rettv;
17954{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017955#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017956 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017957 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017958 int maxcount;
17959 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017960 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017961 listitem_T *li;
17962 int need_capital = FALSE;
17963#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017964
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017965 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017966 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017967
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017968#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017969 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017970 {
17971 str = get_tv_string(&argvars[0]);
17972 if (argvars[1].v_type != VAR_UNKNOWN)
17973 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017974 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017975 if (maxcount <= 0)
17976 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017977 if (argvars[2].v_type != VAR_UNKNOWN)
17978 {
17979 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17980 if (typeerr)
17981 return;
17982 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017983 }
17984 else
17985 maxcount = 25;
17986
Bram Moolenaar4770d092006-01-12 23:22:24 +000017987 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017988
17989 for (i = 0; i < ga.ga_len; ++i)
17990 {
17991 str = ((char_u **)ga.ga_data)[i];
17992
17993 li = listitem_alloc();
17994 if (li == NULL)
17995 vim_free(str);
17996 else
17997 {
17998 li->li_tv.v_type = VAR_STRING;
17999 li->li_tv.v_lock = 0;
18000 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018001 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018002 }
18003 }
18004 ga_clear(&ga);
18005 }
18006#endif
18007}
18008
Bram Moolenaar0d660222005-01-07 21:51:51 +000018009 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018010f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018011 typval_T *argvars;
18012 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018013{
18014 char_u *str;
18015 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018016 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017 regmatch_T regmatch;
18018 char_u patbuf[NUMBUFLEN];
18019 char_u *save_cpo;
18020 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018021 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018022 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018023 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018024
18025 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18026 save_cpo = p_cpo;
18027 p_cpo = (char_u *)"";
18028
18029 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018030 if (argvars[1].v_type != VAR_UNKNOWN)
18031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018032 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18033 if (pat == NULL)
18034 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018035 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018036 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018037 }
18038 if (pat == NULL || *pat == NUL)
18039 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018040
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018041 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018043 if (typeerr)
18044 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045
Bram Moolenaar0d660222005-01-07 21:51:51 +000018046 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18047 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018049 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018050 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018051 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018052 if (*str == NUL)
18053 match = FALSE; /* empty item at the end */
18054 else
18055 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018056 if (match)
18057 end = regmatch.startp[0];
18058 else
18059 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018060 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18061 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018062 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018063 if (list_append_string(rettv->vval.v_list, str,
18064 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018065 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018066 }
18067 if (!match)
18068 break;
18069 /* Advance to just after the match. */
18070 if (regmatch.endp[0] > str)
18071 col = 0;
18072 else
18073 {
18074 /* Don't get stuck at the same match. */
18075#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018076 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018077#else
18078 col = 1;
18079#endif
18080 }
18081 str = regmatch.endp[0];
18082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083
Bram Moolenaar473de612013-06-08 18:19:48 +020018084 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086
Bram Moolenaar0d660222005-01-07 21:51:51 +000018087 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088}
18089
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018090#ifdef FEAT_FLOAT
18091/*
18092 * "sqrt()" function
18093 */
18094 static void
18095f_sqrt(argvars, rettv)
18096 typval_T *argvars;
18097 typval_T *rettv;
18098{
18099 float_T f;
18100
18101 rettv->v_type = VAR_FLOAT;
18102 if (get_float_arg(argvars, &f) == OK)
18103 rettv->vval.v_float = sqrt(f);
18104 else
18105 rettv->vval.v_float = 0.0;
18106}
18107
18108/*
18109 * "str2float()" function
18110 */
18111 static void
18112f_str2float(argvars, rettv)
18113 typval_T *argvars;
18114 typval_T *rettv;
18115{
18116 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18117
18118 if (*p == '+')
18119 p = skipwhite(p + 1);
18120 (void)string2float(p, &rettv->vval.v_float);
18121 rettv->v_type = VAR_FLOAT;
18122}
18123#endif
18124
Bram Moolenaar2c932302006-03-18 21:42:09 +000018125/*
18126 * "str2nr()" function
18127 */
18128 static void
18129f_str2nr(argvars, rettv)
18130 typval_T *argvars;
18131 typval_T *rettv;
18132{
18133 int base = 10;
18134 char_u *p;
18135 long n;
18136
18137 if (argvars[1].v_type != VAR_UNKNOWN)
18138 {
18139 base = get_tv_number(&argvars[1]);
18140 if (base != 8 && base != 10 && base != 16)
18141 {
18142 EMSG(_(e_invarg));
18143 return;
18144 }
18145 }
18146
18147 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018148 if (*p == '+')
18149 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018150 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18151 rettv->vval.v_number = n;
18152}
18153
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154#ifdef HAVE_STRFTIME
18155/*
18156 * "strftime({format}[, {time}])" function
18157 */
18158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018159f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018160 typval_T *argvars;
18161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162{
18163 char_u result_buf[256];
18164 struct tm *curtime;
18165 time_t seconds;
18166 char_u *p;
18167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018168 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018171 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 seconds = time(NULL);
18173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018174 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 curtime = localtime(&seconds);
18176 /* MSVC returns NULL for an invalid value of seconds. */
18177 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018178 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 else
18180 {
18181# ifdef FEAT_MBYTE
18182 vimconv_T conv;
18183 char_u *enc;
18184
18185 conv.vc_type = CONV_NONE;
18186 enc = enc_locale();
18187 convert_setup(&conv, p_enc, enc);
18188 if (conv.vc_type != CONV_NONE)
18189 p = string_convert(&conv, p, NULL);
18190# endif
18191 if (p != NULL)
18192 (void)strftime((char *)result_buf, sizeof(result_buf),
18193 (char *)p, curtime);
18194 else
18195 result_buf[0] = NUL;
18196
18197# ifdef FEAT_MBYTE
18198 if (conv.vc_type != CONV_NONE)
18199 vim_free(p);
18200 convert_setup(&conv, enc, p_enc);
18201 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 else
18204# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018205 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206
18207# ifdef FEAT_MBYTE
18208 /* Release conversion descriptors */
18209 convert_setup(&conv, NULL, NULL);
18210 vim_free(enc);
18211# endif
18212 }
18213}
18214#endif
18215
18216/*
18217 * "stridx()" function
18218 */
18219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018221 typval_T *argvars;
18222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223{
18224 char_u buf[NUMBUFLEN];
18225 char_u *needle;
18226 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018227 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018229 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018231 needle = get_tv_string_chk(&argvars[1]);
18232 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018233 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018234 if (needle == NULL || haystack == NULL)
18235 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236
Bram Moolenaar33570922005-01-25 22:26:29 +000018237 if (argvars[2].v_type != VAR_UNKNOWN)
18238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018239 int error = FALSE;
18240
18241 start_idx = get_tv_number_chk(&argvars[2], &error);
18242 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018243 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018244 if (start_idx >= 0)
18245 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018246 }
18247
18248 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18249 if (pos != NULL)
18250 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251}
18252
18253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018254 * "string()" function
18255 */
18256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018258 typval_T *argvars;
18259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018260{
18261 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018262 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018264 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018265 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018266 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018267 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018268 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269}
18270
18271/*
18272 * "strlen()" function
18273 */
18274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018275f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018276 typval_T *argvars;
18277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018279 rettv->vval.v_number = (varnumber_T)(STRLEN(
18280 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281}
18282
18283/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018284 * "strchars()" function
18285 */
18286 static void
18287f_strchars(argvars, rettv)
18288 typval_T *argvars;
18289 typval_T *rettv;
18290{
18291 char_u *s = get_tv_string(&argvars[0]);
18292#ifdef FEAT_MBYTE
18293 varnumber_T len = 0;
18294
18295 while (*s != NUL)
18296 {
18297 mb_cptr2char_adv(&s);
18298 ++len;
18299 }
18300 rettv->vval.v_number = len;
18301#else
18302 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18303#endif
18304}
18305
18306/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018307 * "strdisplaywidth()" function
18308 */
18309 static void
18310f_strdisplaywidth(argvars, rettv)
18311 typval_T *argvars;
18312 typval_T *rettv;
18313{
18314 char_u *s = get_tv_string(&argvars[0]);
18315 int col = 0;
18316
18317 if (argvars[1].v_type != VAR_UNKNOWN)
18318 col = get_tv_number(&argvars[1]);
18319
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018320 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018321}
18322
18323/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018324 * "strwidth()" function
18325 */
18326 static void
18327f_strwidth(argvars, rettv)
18328 typval_T *argvars;
18329 typval_T *rettv;
18330{
18331 char_u *s = get_tv_string(&argvars[0]);
18332
18333 rettv->vval.v_number = (varnumber_T)(
18334#ifdef FEAT_MBYTE
18335 mb_string2cells(s, -1)
18336#else
18337 STRLEN(s)
18338#endif
18339 );
18340}
18341
18342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 * "strpart()" function
18344 */
18345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018346f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018347 typval_T *argvars;
18348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349{
18350 char_u *p;
18351 int n;
18352 int len;
18353 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018354 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018356 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 slen = (int)STRLEN(p);
18358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018359 n = get_tv_number_chk(&argvars[1], &error);
18360 if (error)
18361 len = 0;
18362 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018363 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 else
18365 len = slen - n; /* default len: all bytes that are available. */
18366
18367 /*
18368 * Only return the overlap between the specified part and the actual
18369 * string.
18370 */
18371 if (n < 0)
18372 {
18373 len += n;
18374 n = 0;
18375 }
18376 else if (n > slen)
18377 n = slen;
18378 if (len < 0)
18379 len = 0;
18380 else if (n + len > slen)
18381 len = slen - n;
18382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383 rettv->v_type = VAR_STRING;
18384 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385}
18386
18387/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018388 * "strridx()" function
18389 */
18390 static void
18391f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018392 typval_T *argvars;
18393 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018394{
18395 char_u buf[NUMBUFLEN];
18396 char_u *needle;
18397 char_u *haystack;
18398 char_u *rest;
18399 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018400 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018402 needle = get_tv_string_chk(&argvars[1]);
18403 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018404
18405 rettv->vval.v_number = -1;
18406 if (needle == NULL || haystack == NULL)
18407 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018408
18409 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018410 if (argvars[2].v_type != VAR_UNKNOWN)
18411 {
18412 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018413 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018414 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018415 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018416 }
18417 else
18418 end_idx = haystack_len;
18419
Bram Moolenaar0d660222005-01-07 21:51:51 +000018420 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018421 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018422 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018423 lastmatch = haystack + end_idx;
18424 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018425 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018426 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018427 for (rest = haystack; *rest != '\0'; ++rest)
18428 {
18429 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018430 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018431 break;
18432 lastmatch = rest;
18433 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018434 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018435
18436 if (lastmatch == NULL)
18437 rettv->vval.v_number = -1;
18438 else
18439 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18440}
18441
18442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443 * "strtrans()" function
18444 */
18445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018446f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018447 typval_T *argvars;
18448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018450 rettv->v_type = VAR_STRING;
18451 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452}
18453
18454/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018455 * "submatch()" function
18456 */
18457 static void
18458f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018459 typval_T *argvars;
18460 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018461{
Bram Moolenaar41571762014-04-02 19:00:58 +020018462 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018463 int no;
18464 int retList = 0;
18465
18466 no = (int)get_tv_number_chk(&argvars[0], &error);
18467 if (error)
18468 return;
18469 error = FALSE;
18470 if (argvars[1].v_type != VAR_UNKNOWN)
18471 retList = get_tv_number_chk(&argvars[1], &error);
18472 if (error)
18473 return;
18474
18475 if (retList == 0)
18476 {
18477 rettv->v_type = VAR_STRING;
18478 rettv->vval.v_string = reg_submatch(no);
18479 }
18480 else
18481 {
18482 rettv->v_type = VAR_LIST;
18483 rettv->vval.v_list = reg_submatch_list(no);
18484 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018485}
18486
18487/*
18488 * "substitute()" function
18489 */
18490 static void
18491f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 typval_T *argvars;
18493 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018494{
18495 char_u patbuf[NUMBUFLEN];
18496 char_u subbuf[NUMBUFLEN];
18497 char_u flagsbuf[NUMBUFLEN];
18498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018499 char_u *str = get_tv_string_chk(&argvars[0]);
18500 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18501 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18502 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18503
Bram Moolenaar0d660222005-01-07 21:51:51 +000018504 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018505 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18506 rettv->vval.v_string = NULL;
18507 else
18508 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018509}
18510
18511/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018512 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018515f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018516 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518{
18519 int id = 0;
18520#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018521 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 long col;
18523 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018524 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018526 lnum = get_tv_lnum(argvars); /* -1 on type error */
18527 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18528 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018530 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018531 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018532 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533#endif
18534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018535 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536}
18537
18538/*
18539 * "synIDattr(id, what [, mode])" function
18540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018542f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018543 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545{
18546 char_u *p = NULL;
18547#ifdef FEAT_SYN_HL
18548 int id;
18549 char_u *what;
18550 char_u *mode;
18551 char_u modebuf[NUMBUFLEN];
18552 int modec;
18553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554 id = get_tv_number(&argvars[0]);
18555 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018560 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 modec = 0; /* replace invalid with current */
18562 }
18563 else
18564 {
18565#ifdef FEAT_GUI
18566 if (gui.in_use)
18567 modec = 'g';
18568 else
18569#endif
18570 if (t_colors > 1)
18571 modec = 'c';
18572 else
18573 modec = 't';
18574 }
18575
18576
18577 switch (TOLOWER_ASC(what[0]))
18578 {
18579 case 'b':
18580 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18581 p = highlight_color(id, what, modec);
18582 else /* bold */
18583 p = highlight_has_attr(id, HL_BOLD, modec);
18584 break;
18585
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018586 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 p = highlight_color(id, what, modec);
18588 break;
18589
18590 case 'i':
18591 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18592 p = highlight_has_attr(id, HL_INVERSE, modec);
18593 else /* italic */
18594 p = highlight_has_attr(id, HL_ITALIC, modec);
18595 break;
18596
18597 case 'n': /* name */
18598 p = get_highlight_name(NULL, id - 1);
18599 break;
18600
18601 case 'r': /* reverse */
18602 p = highlight_has_attr(id, HL_INVERSE, modec);
18603 break;
18604
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018605 case 's':
18606 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18607 p = highlight_color(id, what, modec);
18608 else /* standout */
18609 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610 break;
18611
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018612 case 'u':
18613 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18614 /* underline */
18615 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18616 else
18617 /* undercurl */
18618 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 break;
18620 }
18621
18622 if (p != NULL)
18623 p = vim_strsave(p);
18624#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625 rettv->v_type = VAR_STRING;
18626 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627}
18628
18629/*
18630 * "synIDtrans(id)" function
18631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636{
18637 int id;
18638
18639#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641
18642 if (id > 0)
18643 id = syn_get_final_id(id);
18644 else
18645#endif
18646 id = 0;
18647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018648 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649}
18650
18651/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018652 * "synconcealed(lnum, col)" function
18653 */
18654 static void
18655f_synconcealed(argvars, rettv)
18656 typval_T *argvars UNUSED;
18657 typval_T *rettv;
18658{
18659#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18660 long lnum;
18661 long col;
18662 int syntax_flags = 0;
18663 int cchar;
18664 int matchid = 0;
18665 char_u str[NUMBUFLEN];
18666#endif
18667
18668 rettv->v_type = VAR_LIST;
18669 rettv->vval.v_list = NULL;
18670
18671#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18672 lnum = get_tv_lnum(argvars); /* -1 on type error */
18673 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18674
18675 vim_memset(str, NUL, sizeof(str));
18676
18677 if (rettv_list_alloc(rettv) != FAIL)
18678 {
18679 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18680 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18681 && curwin->w_p_cole > 0)
18682 {
18683 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18684 syntax_flags = get_syntax_info(&matchid);
18685
18686 /* get the conceal character */
18687 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18688 {
18689 cchar = syn_get_sub_char();
18690 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18691 cchar = lcs_conceal;
18692 if (cchar != NUL)
18693 {
18694# ifdef FEAT_MBYTE
18695 if (has_mbyte)
18696 (*mb_char2bytes)(cchar, str);
18697 else
18698# endif
18699 str[0] = cchar;
18700 }
18701 }
18702 }
18703
18704 list_append_number(rettv->vval.v_list,
18705 (syntax_flags & HL_CONCEAL) != 0);
18706 /* -1 to auto-determine strlen */
18707 list_append_string(rettv->vval.v_list, str, -1);
18708 list_append_number(rettv->vval.v_list, matchid);
18709 }
18710#endif
18711}
18712
18713/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018714 * "synstack(lnum, col)" function
18715 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018716 static void
18717f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018718 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018719 typval_T *rettv;
18720{
18721#ifdef FEAT_SYN_HL
18722 long lnum;
18723 long col;
18724 int i;
18725 int id;
18726#endif
18727
18728 rettv->v_type = VAR_LIST;
18729 rettv->vval.v_list = NULL;
18730
18731#ifdef FEAT_SYN_HL
18732 lnum = get_tv_lnum(argvars); /* -1 on type error */
18733 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18734
18735 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018736 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018737 && rettv_list_alloc(rettv) != FAIL)
18738 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018739 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018740 for (i = 0; ; ++i)
18741 {
18742 id = syn_get_stack_item(i);
18743 if (id < 0)
18744 break;
18745 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18746 break;
18747 }
18748 }
18749#endif
18750}
18751
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018753get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018754 typval_T *argvars;
18755 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018756 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018758 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018760 char_u *infile = NULL;
18761 char_u buf[NUMBUFLEN];
18762 int err = FALSE;
18763 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018764 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018765 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018767 rettv->v_type = VAR_STRING;
18768 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018769 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018770 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018771
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018772 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018773 {
18774 /*
18775 * Write the string to a temp file, to be used for input of the shell
18776 * command.
18777 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018778 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018779 {
18780 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018781 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018782 }
18783
18784 fd = mch_fopen((char *)infile, WRITEBIN);
18785 if (fd == NULL)
18786 {
18787 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018788 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018789 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018790 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018791 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018792 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18793 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018794 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018795 else
18796 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018797 size_t len;
18798
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018799 p = get_tv_string_buf_chk(&argvars[1], buf);
18800 if (p == NULL)
18801 {
18802 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018803 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018804 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018805 len = STRLEN(p);
18806 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018807 err = TRUE;
18808 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018809 if (fclose(fd) != 0)
18810 err = TRUE;
18811 if (err)
18812 {
18813 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018814 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018815 }
18816 }
18817
Bram Moolenaar52a72462014-08-29 15:53:52 +020018818 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18819 * echoes typeahead, that messes up the display. */
18820 if (!msg_silent)
18821 flags += SHELL_COOKED;
18822
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018823 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018825 int len;
18826 listitem_T *li;
18827 char_u *s = NULL;
18828 char_u *start;
18829 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018830 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831
Bram Moolenaar52a72462014-08-29 15:53:52 +020018832 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018833 if (res == NULL)
18834 goto errret;
18835
18836 list = list_alloc();
18837 if (list == NULL)
18838 goto errret;
18839
18840 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018842 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018843 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018844 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018845 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018846
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018847 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018848 if (s == NULL)
18849 goto errret;
18850
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018851 for (p = s; start < end; ++p, ++start)
18852 *p = *start == NUL ? NL : *start;
18853 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018854
18855 li = listitem_alloc();
18856 if (li == NULL)
18857 {
18858 vim_free(s);
18859 goto errret;
18860 }
18861 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018862 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018863 li->li_tv.vval.v_string = s;
18864 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018866
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018867 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018868 rettv->v_type = VAR_LIST;
18869 rettv->vval.v_list = list;
18870 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018872 else
18873 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018874 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018875#ifdef USE_CR
18876 /* translate <CR> into <NL> */
18877 if (res != NULL)
18878 {
18879 char_u *s;
18880
18881 for (s = res; *s; ++s)
18882 {
18883 if (*s == CAR)
18884 *s = NL;
18885 }
18886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887#else
18888# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018889 /* translate <CR><NL> into <NL> */
18890 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018892 char_u *s, *d;
18893
18894 d = res;
18895 for (s = res; *s; ++s)
18896 {
18897 if (s[0] == CAR && s[1] == NL)
18898 ++s;
18899 *d++ = *s;
18900 }
18901 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903# endif
18904#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018905 rettv->vval.v_string = res;
18906 res = NULL;
18907 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018908
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018909errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018910 if (infile != NULL)
18911 {
18912 mch_remove(infile);
18913 vim_free(infile);
18914 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018915 if (res != NULL)
18916 vim_free(res);
18917 if (list != NULL)
18918 list_free(list, TRUE);
18919}
18920
18921/*
18922 * "system()" function
18923 */
18924 static void
18925f_system(argvars, rettv)
18926 typval_T *argvars;
18927 typval_T *rettv;
18928{
18929 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18930}
18931
18932/*
18933 * "systemlist()" function
18934 */
18935 static void
18936f_systemlist(argvars, rettv)
18937 typval_T *argvars;
18938 typval_T *rettv;
18939{
18940 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941}
18942
18943/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018944 * "tabpagebuflist()" function
18945 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018946 static void
18947f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018948 typval_T *argvars UNUSED;
18949 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018950{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018951#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018952 tabpage_T *tp;
18953 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018954
18955 if (argvars[0].v_type == VAR_UNKNOWN)
18956 wp = firstwin;
18957 else
18958 {
18959 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18960 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018961 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018962 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018963 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018964 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018965 for (; wp != NULL; wp = wp->w_next)
18966 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018967 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018968 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018969 }
18970#endif
18971}
18972
18973
18974/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018975 * "tabpagenr()" function
18976 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018977 static void
18978f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018979 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018980 typval_T *rettv;
18981{
18982 int nr = 1;
18983#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018984 char_u *arg;
18985
18986 if (argvars[0].v_type != VAR_UNKNOWN)
18987 {
18988 arg = get_tv_string_chk(&argvars[0]);
18989 nr = 0;
18990 if (arg != NULL)
18991 {
18992 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018993 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018994 else
18995 EMSG2(_(e_invexpr2), arg);
18996 }
18997 }
18998 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018999 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019000#endif
19001 rettv->vval.v_number = nr;
19002}
19003
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019004
19005#ifdef FEAT_WINDOWS
19006static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19007
19008/*
19009 * Common code for tabpagewinnr() and winnr().
19010 */
19011 static int
19012get_winnr(tp, argvar)
19013 tabpage_T *tp;
19014 typval_T *argvar;
19015{
19016 win_T *twin;
19017 int nr = 1;
19018 win_T *wp;
19019 char_u *arg;
19020
19021 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19022 if (argvar->v_type != VAR_UNKNOWN)
19023 {
19024 arg = get_tv_string_chk(argvar);
19025 if (arg == NULL)
19026 nr = 0; /* type error; errmsg already given */
19027 else if (STRCMP(arg, "$") == 0)
19028 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19029 else if (STRCMP(arg, "#") == 0)
19030 {
19031 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19032 if (twin == NULL)
19033 nr = 0;
19034 }
19035 else
19036 {
19037 EMSG2(_(e_invexpr2), arg);
19038 nr = 0;
19039 }
19040 }
19041
19042 if (nr > 0)
19043 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19044 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019045 {
19046 if (wp == NULL)
19047 {
19048 /* didn't find it in this tabpage */
19049 nr = 0;
19050 break;
19051 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019052 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019053 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019054 return nr;
19055}
19056#endif
19057
19058/*
19059 * "tabpagewinnr()" function
19060 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019061 static void
19062f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019063 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019064 typval_T *rettv;
19065{
19066 int nr = 1;
19067#ifdef FEAT_WINDOWS
19068 tabpage_T *tp;
19069
19070 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19071 if (tp == NULL)
19072 nr = 0;
19073 else
19074 nr = get_winnr(tp, &argvars[1]);
19075#endif
19076 rettv->vval.v_number = nr;
19077}
19078
19079
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019080/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019081 * "tagfiles()" function
19082 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019083 static void
19084f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019085 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019086 typval_T *rettv;
19087{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019088 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019089 tagname_T tn;
19090 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019091
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019092 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019093 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019094 fname = alloc(MAXPATHL);
19095 if (fname == NULL)
19096 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019097
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019098 for (first = TRUE; ; first = FALSE)
19099 if (get_tagfname(&tn, first, fname) == FAIL
19100 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019101 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019102 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019103 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019104}
19105
19106/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019107 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019108 */
19109 static void
19110f_taglist(argvars, rettv)
19111 typval_T *argvars;
19112 typval_T *rettv;
19113{
19114 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019115
19116 tag_pattern = get_tv_string(&argvars[0]);
19117
19118 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019119 if (*tag_pattern == NUL)
19120 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019121
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019122 if (rettv_list_alloc(rettv) == OK)
19123 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019124}
19125
19126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 * "tempname()" function
19128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133{
19134 static int x = 'A';
19135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019137 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138
19139 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19140 * names. Skip 'I' and 'O', they are used for shell redirection. */
19141 do
19142 {
19143 if (x == 'Z')
19144 x = '0';
19145 else if (x == '9')
19146 x = 'A';
19147 else
19148 {
19149#ifdef EBCDIC
19150 if (x == 'I')
19151 x = 'J';
19152 else if (x == 'R')
19153 x = 'S';
19154 else
19155#endif
19156 ++x;
19157 }
19158 } while (x == 'I' || x == 'O');
19159}
19160
19161/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019162 * "test(list)" function: Just checking the walls...
19163 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019164 static void
19165f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019166 typval_T *argvars UNUSED;
19167 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019168{
19169 /* Used for unit testing. Change the code below to your liking. */
19170#if 0
19171 listitem_T *li;
19172 list_T *l;
19173 char_u *bad, *good;
19174
19175 if (argvars[0].v_type != VAR_LIST)
19176 return;
19177 l = argvars[0].vval.v_list;
19178 if (l == NULL)
19179 return;
19180 li = l->lv_first;
19181 if (li == NULL)
19182 return;
19183 bad = get_tv_string(&li->li_tv);
19184 li = li->li_next;
19185 if (li == NULL)
19186 return;
19187 good = get_tv_string(&li->li_tv);
19188 rettv->vval.v_number = test_edit_score(bad, good);
19189#endif
19190}
19191
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019192#ifdef FEAT_FLOAT
19193/*
19194 * "tan()" function
19195 */
19196 static void
19197f_tan(argvars, rettv)
19198 typval_T *argvars;
19199 typval_T *rettv;
19200{
19201 float_T f;
19202
19203 rettv->v_type = VAR_FLOAT;
19204 if (get_float_arg(argvars, &f) == OK)
19205 rettv->vval.v_float = tan(f);
19206 else
19207 rettv->vval.v_float = 0.0;
19208}
19209
19210/*
19211 * "tanh()" function
19212 */
19213 static void
19214f_tanh(argvars, rettv)
19215 typval_T *argvars;
19216 typval_T *rettv;
19217{
19218 float_T f;
19219
19220 rettv->v_type = VAR_FLOAT;
19221 if (get_float_arg(argvars, &f) == OK)
19222 rettv->vval.v_float = tanh(f);
19223 else
19224 rettv->vval.v_float = 0.0;
19225}
19226#endif
19227
Bram Moolenaard52d9742005-08-21 22:20:28 +000019228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 * "tolower(string)" function
19230 */
19231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019233 typval_T *argvars;
19234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235{
19236 char_u *p;
19237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019238 p = vim_strsave(get_tv_string(&argvars[0]));
19239 rettv->v_type = VAR_STRING;
19240 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241
19242 if (p != NULL)
19243 while (*p != NUL)
19244 {
19245#ifdef FEAT_MBYTE
19246 int l;
19247
19248 if (enc_utf8)
19249 {
19250 int c, lc;
19251
19252 c = utf_ptr2char(p);
19253 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019254 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 /* TODO: reallocate string when byte count changes. */
19256 if (utf_char2len(lc) == l)
19257 utf_char2bytes(lc, p);
19258 p += l;
19259 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019260 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 p += l; /* skip multi-byte character */
19262 else
19263#endif
19264 {
19265 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19266 ++p;
19267 }
19268 }
19269}
19270
19271/*
19272 * "toupper(string)" function
19273 */
19274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019276 typval_T *argvars;
19277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019279 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019280 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281}
19282
19283/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019284 * "tr(string, fromstr, tostr)" function
19285 */
19286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019287f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019288 typval_T *argvars;
19289 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019290{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019291 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019292 char_u *fromstr;
19293 char_u *tostr;
19294 char_u *p;
19295#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019296 int inlen;
19297 int fromlen;
19298 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019299 int idx;
19300 char_u *cpstr;
19301 int cplen;
19302 int first = TRUE;
19303#endif
19304 char_u buf[NUMBUFLEN];
19305 char_u buf2[NUMBUFLEN];
19306 garray_T ga;
19307
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019308 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019309 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19310 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019311
19312 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019313 rettv->v_type = VAR_STRING;
19314 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019315 if (fromstr == NULL || tostr == NULL)
19316 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019317 ga_init2(&ga, (int)sizeof(char), 80);
19318
19319#ifdef FEAT_MBYTE
19320 if (!has_mbyte)
19321#endif
19322 /* not multi-byte: fromstr and tostr must be the same length */
19323 if (STRLEN(fromstr) != STRLEN(tostr))
19324 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019325#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019326error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019327#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019328 EMSG2(_(e_invarg2), fromstr);
19329 ga_clear(&ga);
19330 return;
19331 }
19332
19333 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019334 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019335 {
19336#ifdef FEAT_MBYTE
19337 if (has_mbyte)
19338 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019339 inlen = (*mb_ptr2len)(in_str);
19340 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019341 cplen = inlen;
19342 idx = 0;
19343 for (p = fromstr; *p != NUL; p += fromlen)
19344 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019345 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019346 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019347 {
19348 for (p = tostr; *p != NUL; p += tolen)
19349 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019350 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019351 if (idx-- == 0)
19352 {
19353 cplen = tolen;
19354 cpstr = p;
19355 break;
19356 }
19357 }
19358 if (*p == NUL) /* tostr is shorter than fromstr */
19359 goto error;
19360 break;
19361 }
19362 ++idx;
19363 }
19364
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019365 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019366 {
19367 /* Check that fromstr and tostr have the same number of
19368 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019369 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019370 first = FALSE;
19371 for (p = tostr; *p != NUL; p += tolen)
19372 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019373 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019374 --idx;
19375 }
19376 if (idx != 0)
19377 goto error;
19378 }
19379
19380 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019381 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019382 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019383
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019384 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019385 }
19386 else
19387#endif
19388 {
19389 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019390 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019391 if (p != NULL)
19392 ga_append(&ga, tostr[p - fromstr]);
19393 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019394 ga_append(&ga, *in_str);
19395 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019396 }
19397 }
19398
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019399 /* add a terminating NUL */
19400 ga_grow(&ga, 1);
19401 ga_append(&ga, NUL);
19402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019403 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019404}
19405
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019406#ifdef FEAT_FLOAT
19407/*
19408 * "trunc({float})" function
19409 */
19410 static void
19411f_trunc(argvars, rettv)
19412 typval_T *argvars;
19413 typval_T *rettv;
19414{
19415 float_T f;
19416
19417 rettv->v_type = VAR_FLOAT;
19418 if (get_float_arg(argvars, &f) == OK)
19419 /* trunc() is not in C90, use floor() or ceil() instead. */
19420 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19421 else
19422 rettv->vval.v_float = 0.0;
19423}
19424#endif
19425
Bram Moolenaar8299df92004-07-10 09:47:34 +000019426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 * "type(expr)" function
19428 */
19429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019430f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019431 typval_T *argvars;
19432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019434 int n;
19435
19436 switch (argvars[0].v_type)
19437 {
19438 case VAR_NUMBER: n = 0; break;
19439 case VAR_STRING: n = 1; break;
19440 case VAR_FUNC: n = 2; break;
19441 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019442 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019443#ifdef FEAT_FLOAT
19444 case VAR_FLOAT: n = 5; break;
19445#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019446 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19447 }
19448 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449}
19450
19451/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019452 * "undofile(name)" function
19453 */
19454 static void
19455f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019456 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019457 typval_T *rettv;
19458{
19459 rettv->v_type = VAR_STRING;
19460#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019461 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019462 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019463
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019464 if (*fname == NUL)
19465 {
19466 /* If there is no file name there will be no undo file. */
19467 rettv->vval.v_string = NULL;
19468 }
19469 else
19470 {
19471 char_u *ffname = FullName_save(fname, FALSE);
19472
19473 if (ffname != NULL)
19474 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19475 vim_free(ffname);
19476 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019477 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019478#else
19479 rettv->vval.v_string = NULL;
19480#endif
19481}
19482
19483/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019484 * "undotree()" function
19485 */
19486 static void
19487f_undotree(argvars, rettv)
19488 typval_T *argvars UNUSED;
19489 typval_T *rettv;
19490{
19491 if (rettv_dict_alloc(rettv) == OK)
19492 {
19493 dict_T *dict = rettv->vval.v_dict;
19494 list_T *list;
19495
Bram Moolenaar730cde92010-06-27 05:18:54 +020019496 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019497 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019498 dict_add_nr_str(dict, "save_last",
19499 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019500 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19501 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019502 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019503
19504 list = list_alloc();
19505 if (list != NULL)
19506 {
19507 u_eval_tree(curbuf->b_u_oldhead, list);
19508 dict_add_list(dict, "entries", list);
19509 }
19510 }
19511}
19512
19513/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019514 * "values(dict)" function
19515 */
19516 static void
19517f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 typval_T *argvars;
19519 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019520{
19521 dict_list(argvars, rettv, 1);
19522}
19523
19524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 * "virtcol(string)" function
19526 */
19527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019528f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019529 typval_T *argvars;
19530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531{
19532 colnr_T vcol = 0;
19533 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019534 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019536 fp = var2fpos(&argvars[0], FALSE, &fnum);
19537 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19538 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 {
19540 getvvcol(curwin, fp, NULL, NULL, &vcol);
19541 ++vcol;
19542 }
19543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019544 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545}
19546
19547/*
19548 * "visualmode()" function
19549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019551f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019552 typval_T *argvars UNUSED;
19553 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 char_u str[2];
19556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 str[0] = curbuf->b_visual_mode_eval;
19559 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561
19562 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019563 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565}
19566
19567/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019568 * "wildmenumode()" function
19569 */
19570 static void
19571f_wildmenumode(argvars, rettv)
19572 typval_T *argvars UNUSED;
19573 typval_T *rettv UNUSED;
19574{
19575#ifdef FEAT_WILDMENU
19576 if (wild_menu_showing)
19577 rettv->vval.v_number = 1;
19578#endif
19579}
19580
19581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 * "winbufnr(nr)" function
19583 */
19584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019585f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019586 typval_T *argvars;
19587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588{
19589 win_T *wp;
19590
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019591 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019593 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019595 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596}
19597
19598/*
19599 * "wincol()" function
19600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019602f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019603 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605{
19606 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019607 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608}
19609
19610/*
19611 * "winheight(nr)" function
19612 */
19613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019614f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 typval_T *argvars;
19616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617{
19618 win_T *wp;
19619
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019620 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625}
19626
19627/*
19628 * "winline()" function
19629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019631f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019632 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634{
19635 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019636 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637}
19638
19639/*
19640 * "winnr()" function
19641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019644 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646{
19647 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019648
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019650 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019652 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653}
19654
19655/*
19656 * "winrestcmd()" function
19657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019659f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019660 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662{
19663#ifdef FEAT_WINDOWS
19664 win_T *wp;
19665 int winnr = 1;
19666 garray_T ga;
19667 char_u buf[50];
19668
19669 ga_init2(&ga, (int)sizeof(char), 70);
19670 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19671 {
19672 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19673 ga_concat(&ga, buf);
19674# ifdef FEAT_VERTSPLIT
19675 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19676 ga_concat(&ga, buf);
19677# endif
19678 ++winnr;
19679 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019680 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019682 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019684 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019686 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687}
19688
19689/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019690 * "winrestview()" function
19691 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019692 static void
19693f_winrestview(argvars, rettv)
19694 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019695 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019696{
19697 dict_T *dict;
19698
19699 if (argvars[0].v_type != VAR_DICT
19700 || (dict = argvars[0].vval.v_dict) == NULL)
19701 EMSG(_(e_invarg));
19702 else
19703 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019704 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19705 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19706 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19707 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019708#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019709 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19710 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019711#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019712 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19713 {
19714 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19715 curwin->w_set_curswant = FALSE;
19716 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019717
Bram Moolenaar82c25852014-05-28 16:47:16 +020019718 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19719 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019720#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019721 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19722 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019723#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019724 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19725 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19726 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19727 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019728
19729 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019730 win_new_height(curwin, curwin->w_height);
19731# ifdef FEAT_VERTSPLIT
19732 win_new_width(curwin, W_WIDTH(curwin));
19733# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019734 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019735
Bram Moolenaarb851a962014-10-31 15:45:52 +010019736 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019737 curwin->w_topline = 1;
19738 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19739 curwin->w_topline = curbuf->b_ml.ml_line_count;
19740#ifdef FEAT_DIFF
19741 check_topfill(curwin, TRUE);
19742#endif
19743 }
19744}
19745
19746/*
19747 * "winsaveview()" function
19748 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019749 static void
19750f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019751 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019752 typval_T *rettv;
19753{
19754 dict_T *dict;
19755
Bram Moolenaara800b422010-06-27 01:15:55 +020019756 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019757 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019758 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019759
19760 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19761 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19762#ifdef FEAT_VIRTUALEDIT
19763 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19764#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019765 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019766 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19767
19768 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19769#ifdef FEAT_DIFF
19770 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19771#endif
19772 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19773 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19774}
19775
19776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 * "winwidth(nr)" function
19778 */
19779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019780f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019781 typval_T *argvars;
19782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783{
19784 win_T *wp;
19785
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019786 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 else
19790#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019791 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794#endif
19795}
19796
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019798 * Write list of strings to file
19799 */
19800 static int
19801write_list(fd, list, binary)
19802 FILE *fd;
19803 list_T *list;
19804 int binary;
19805{
19806 listitem_T *li;
19807 int c;
19808 int ret = OK;
19809 char_u *s;
19810
19811 for (li = list->lv_first; li != NULL; li = li->li_next)
19812 {
19813 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19814 {
19815 if (*s == '\n')
19816 c = putc(NUL, fd);
19817 else
19818 c = putc(*s, fd);
19819 if (c == EOF)
19820 {
19821 ret = FAIL;
19822 break;
19823 }
19824 }
19825 if (!binary || li->li_next != NULL)
19826 if (putc('\n', fd) == EOF)
19827 {
19828 ret = FAIL;
19829 break;
19830 }
19831 if (ret == FAIL)
19832 {
19833 EMSG(_(e_write));
19834 break;
19835 }
19836 }
19837 return ret;
19838}
19839
19840/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019841 * "writefile()" function
19842 */
19843 static void
19844f_writefile(argvars, rettv)
19845 typval_T *argvars;
19846 typval_T *rettv;
19847{
19848 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019849 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019850 char_u *fname;
19851 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019852 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019853
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019854 if (check_restricted() || check_secure())
19855 return;
19856
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019857 if (argvars[0].v_type != VAR_LIST)
19858 {
19859 EMSG2(_(e_listarg), "writefile()");
19860 return;
19861 }
19862 if (argvars[0].vval.v_list == NULL)
19863 return;
19864
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019865 if (argvars[2].v_type != VAR_UNKNOWN)
19866 {
19867 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19868 binary = TRUE;
19869 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19870 append = TRUE;
19871 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019872
19873 /* Always open the file in binary mode, library functions have a mind of
19874 * their own about CR-LF conversion. */
19875 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019876 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19877 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019878 {
19879 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19880 ret = -1;
19881 }
19882 else
19883 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019884 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19885 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019886 fclose(fd);
19887 }
19888
19889 rettv->vval.v_number = ret;
19890}
19891
19892/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019893 * "xor(expr, expr)" function
19894 */
19895 static void
19896f_xor(argvars, rettv)
19897 typval_T *argvars;
19898 typval_T *rettv;
19899{
19900 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19901 ^ get_tv_number_chk(&argvars[1], NULL);
19902}
19903
19904
19905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019907 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 */
19909 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019910var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019911 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019912 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019913 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019915 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019917 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918
Bram Moolenaara5525202006-03-02 22:52:09 +000019919 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019920 if (varp->v_type == VAR_LIST)
19921 {
19922 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019923 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019924 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019925 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019926
19927 l = varp->vval.v_list;
19928 if (l == NULL)
19929 return NULL;
19930
19931 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019932 pos.lnum = list_find_nr(l, 0L, &error);
19933 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019934 return NULL; /* invalid line number */
19935
19936 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019937 pos.col = list_find_nr(l, 1L, &error);
19938 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019939 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019940 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019941
19942 /* We accept "$" for the column number: last column. */
19943 li = list_find(l, 1L);
19944 if (li != NULL && li->li_tv.v_type == VAR_STRING
19945 && li->li_tv.vval.v_string != NULL
19946 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19947 pos.col = len + 1;
19948
Bram Moolenaara5525202006-03-02 22:52:09 +000019949 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019950 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019951 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019952 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019953
Bram Moolenaara5525202006-03-02 22:52:09 +000019954#ifdef FEAT_VIRTUALEDIT
19955 /* Get the virtual offset. Defaults to zero. */
19956 pos.coladd = list_find_nr(l, 2L, &error);
19957 if (error)
19958 pos.coladd = 0;
19959#endif
19960
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019961 return &pos;
19962 }
19963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019964 name = get_tv_string_chk(varp);
19965 if (name == NULL)
19966 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019967 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019969 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19970 {
19971 if (VIsual_active)
19972 return &VIsual;
19973 return &curwin->w_cursor;
19974 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019975 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019977 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19979 return NULL;
19980 return pp;
19981 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019982
19983#ifdef FEAT_VIRTUALEDIT
19984 pos.coladd = 0;
19985#endif
19986
Bram Moolenaar477933c2007-07-17 14:32:23 +000019987 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019988 {
19989 pos.col = 0;
19990 if (name[1] == '0') /* "w0": first visible line */
19991 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019992 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019993 pos.lnum = curwin->w_topline;
19994 return &pos;
19995 }
19996 else if (name[1] == '$') /* "w$": last visible line */
19997 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019998 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019999 pos.lnum = curwin->w_botline - 1;
20000 return &pos;
20001 }
20002 }
20003 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020005 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 {
20007 pos.lnum = curbuf->b_ml.ml_line_count;
20008 pos.col = 0;
20009 }
20010 else
20011 {
20012 pos.lnum = curwin->w_cursor.lnum;
20013 pos.col = (colnr_T)STRLEN(ml_get_curline());
20014 }
20015 return &pos;
20016 }
20017 return NULL;
20018}
20019
20020/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020021 * Convert list in "arg" into a position and optional file number.
20022 * When "fnump" is NULL there is no file number, only 3 items.
20023 * Note that the column is passed on as-is, the caller may want to decrement
20024 * it to use 1 for the first column.
20025 * Return FAIL when conversion is not possible, doesn't check the position for
20026 * validity.
20027 */
20028 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020029list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020030 typval_T *arg;
20031 pos_T *posp;
20032 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020033 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020034{
20035 list_T *l = arg->vval.v_list;
20036 long i = 0;
20037 long n;
20038
Bram Moolenaar493c1782014-05-28 14:34:46 +020020039 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20040 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020041 if (arg->v_type != VAR_LIST
20042 || l == NULL
20043 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020044 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020045 return FAIL;
20046
20047 if (fnump != NULL)
20048 {
20049 n = list_find_nr(l, i++, NULL); /* fnum */
20050 if (n < 0)
20051 return FAIL;
20052 if (n == 0)
20053 n = curbuf->b_fnum; /* current buffer */
20054 *fnump = n;
20055 }
20056
20057 n = list_find_nr(l, i++, NULL); /* lnum */
20058 if (n < 0)
20059 return FAIL;
20060 posp->lnum = n;
20061
20062 n = list_find_nr(l, i++, NULL); /* col */
20063 if (n < 0)
20064 return FAIL;
20065 posp->col = n;
20066
20067#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020068 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020069 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020070 posp->coladd = 0;
20071 else
20072 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020073#endif
20074
Bram Moolenaar493c1782014-05-28 14:34:46 +020020075 if (curswantp != NULL)
20076 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20077
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020078 return OK;
20079}
20080
20081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 * Get the length of an environment variable name.
20083 * Advance "arg" to the first character after the name.
20084 * Return 0 for error.
20085 */
20086 static int
20087get_env_len(arg)
20088 char_u **arg;
20089{
20090 char_u *p;
20091 int len;
20092
20093 for (p = *arg; vim_isIDc(*p); ++p)
20094 ;
20095 if (p == *arg) /* no name found */
20096 return 0;
20097
20098 len = (int)(p - *arg);
20099 *arg = p;
20100 return len;
20101}
20102
20103/*
20104 * Get the length of the name of a function or internal variable.
20105 * "arg" is advanced to the first non-white character after the name.
20106 * Return 0 if something is wrong.
20107 */
20108 static int
20109get_id_len(arg)
20110 char_u **arg;
20111{
20112 char_u *p;
20113 int len;
20114
20115 /* Find the end of the name. */
20116 for (p = *arg; eval_isnamec(*p); ++p)
20117 ;
20118 if (p == *arg) /* no name found */
20119 return 0;
20120
20121 len = (int)(p - *arg);
20122 *arg = skipwhite(p);
20123
20124 return len;
20125}
20126
20127/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020128 * Get the length of the name of a variable or function.
20129 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020131 * Return -1 if curly braces expansion failed.
20132 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 * If the name contains 'magic' {}'s, expand them and return the
20134 * expanded name in an allocated string via 'alias' - caller must free.
20135 */
20136 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020137get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 char_u **arg;
20139 char_u **alias;
20140 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020141 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142{
20143 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 char_u *p;
20145 char_u *expr_start;
20146 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147
20148 *alias = NULL; /* default to no alias */
20149
20150 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20151 && (*arg)[2] == (int)KE_SNR)
20152 {
20153 /* hard coded <SNR>, already translated */
20154 *arg += 3;
20155 return get_id_len(arg) + 3;
20156 }
20157 len = eval_fname_script(*arg);
20158 if (len > 0)
20159 {
20160 /* literal "<SID>", "s:" or "<SNR>" */
20161 *arg += len;
20162 }
20163
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020165 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020167 p = find_name_end(*arg, &expr_start, &expr_end,
20168 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 if (expr_start != NULL)
20170 {
20171 char_u *temp_string;
20172
20173 if (!evaluate)
20174 {
20175 len += (int)(p - *arg);
20176 *arg = skipwhite(p);
20177 return len;
20178 }
20179
20180 /*
20181 * Include any <SID> etc in the expanded string:
20182 * Thus the -len here.
20183 */
20184 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20185 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020186 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 *alias = temp_string;
20188 *arg = skipwhite(p);
20189 return (int)STRLEN(temp_string);
20190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191
20192 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020193 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 EMSG2(_(e_invexpr2), *arg);
20195
20196 return len;
20197}
20198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020199/*
20200 * Find the end of a variable or function name, taking care of magic braces.
20201 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20202 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020203 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020204 * Return a pointer to just after the name. Equal to "arg" if there is no
20205 * valid name.
20206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020208find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 char_u *arg;
20210 char_u **expr_start;
20211 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020212 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020214 int mb_nest = 0;
20215 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 char_u *p;
20217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020220 *expr_start = NULL;
20221 *expr_end = NULL;
20222 }
20223
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020224 /* Quick check for valid starting character. */
20225 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20226 return arg;
20227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020228 for (p = arg; *p != NUL
20229 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020230 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020231 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020232 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020233 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020235 if (*p == '\'')
20236 {
20237 /* skip over 'string' to avoid counting [ and ] inside it. */
20238 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20239 ;
20240 if (*p == NUL)
20241 break;
20242 }
20243 else if (*p == '"')
20244 {
20245 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20246 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20247 if (*p == '\\' && p[1] != NUL)
20248 ++p;
20249 if (*p == NUL)
20250 break;
20251 }
20252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 if (*p == '[')
20256 ++br_nest;
20257 else if (*p == ']')
20258 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020261 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263 if (*p == '{')
20264 {
20265 mb_nest++;
20266 if (expr_start != NULL && *expr_start == NULL)
20267 *expr_start = p;
20268 }
20269 else if (*p == '}')
20270 {
20271 mb_nest--;
20272 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20273 *expr_end = p;
20274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 }
20277
20278 return p;
20279}
20280
20281/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020282 * Expands out the 'magic' {}'s in a variable/function name.
20283 * Note that this can call itself recursively, to deal with
20284 * constructs like foo{bar}{baz}{bam}
20285 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20286 * "in_start" ^
20287 * "expr_start" ^
20288 * "expr_end" ^
20289 * "in_end" ^
20290 *
20291 * Returns a new allocated string, which the caller must free.
20292 * Returns NULL for failure.
20293 */
20294 static char_u *
20295make_expanded_name(in_start, expr_start, expr_end, in_end)
20296 char_u *in_start;
20297 char_u *expr_start;
20298 char_u *expr_end;
20299 char_u *in_end;
20300{
20301 char_u c1;
20302 char_u *retval = NULL;
20303 char_u *temp_result;
20304 char_u *nextcmd = NULL;
20305
20306 if (expr_end == NULL || in_end == NULL)
20307 return NULL;
20308 *expr_start = NUL;
20309 *expr_end = NUL;
20310 c1 = *in_end;
20311 *in_end = NUL;
20312
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020313 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020314 if (temp_result != NULL && nextcmd == NULL)
20315 {
20316 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20317 + (in_end - expr_end) + 1));
20318 if (retval != NULL)
20319 {
20320 STRCPY(retval, in_start);
20321 STRCAT(retval, temp_result);
20322 STRCAT(retval, expr_end + 1);
20323 }
20324 }
20325 vim_free(temp_result);
20326
20327 *in_end = c1; /* put char back for error messages */
20328 *expr_start = '{';
20329 *expr_end = '}';
20330
20331 if (retval != NULL)
20332 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020333 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020334 if (expr_start != NULL)
20335 {
20336 /* Further expansion! */
20337 temp_result = make_expanded_name(retval, expr_start,
20338 expr_end, temp_result);
20339 vim_free(retval);
20340 retval = temp_result;
20341 }
20342 }
20343
20344 return retval;
20345}
20346
20347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020349 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 */
20351 static int
20352eval_isnamec(c)
20353 int c;
20354{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020355 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20356}
20357
20358/*
20359 * Return TRUE if character "c" can be used as the first character in a
20360 * variable or function name (excluding '{' and '}').
20361 */
20362 static int
20363eval_isnamec1(c)
20364 int c;
20365{
20366 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367}
20368
20369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 * Set number v: variable to "val".
20371 */
20372 void
20373set_vim_var_nr(idx, val)
20374 int idx;
20375 long val;
20376{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020377 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378}
20379
20380/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020381 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 */
20383 long
20384get_vim_var_nr(idx)
20385 int idx;
20386{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020387 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388}
20389
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020390/*
20391 * Get string v: variable value. Uses a static buffer, can only be used once.
20392 */
20393 char_u *
20394get_vim_var_str(idx)
20395 int idx;
20396{
20397 return get_tv_string(&vimvars[idx].vv_tv);
20398}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020399
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020401 * Get List v: variable value. Caller must take care of reference count when
20402 * needed.
20403 */
20404 list_T *
20405get_vim_var_list(idx)
20406 int idx;
20407{
20408 return vimvars[idx].vv_list;
20409}
20410
20411/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020412 * Set v:char to character "c".
20413 */
20414 void
20415set_vim_var_char(c)
20416 int c;
20417{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020418 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020419
20420#ifdef FEAT_MBYTE
20421 if (has_mbyte)
20422 buf[(*mb_char2bytes)(c, buf)] = NUL;
20423 else
20424#endif
20425 {
20426 buf[0] = c;
20427 buf[1] = NUL;
20428 }
20429 set_vim_var_string(VV_CHAR, buf, -1);
20430}
20431
20432/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020433 * Set v:count to "count" and v:count1 to "count1".
20434 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 */
20436 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020437set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 long count;
20439 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020440 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020442 if (set_prevcount)
20443 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020444 vimvars[VV_COUNT].vv_nr = count;
20445 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446}
20447
20448/*
20449 * Set string v: variable to a copy of "val".
20450 */
20451 void
20452set_vim_var_string(idx, val, len)
20453 int idx;
20454 char_u *val;
20455 int len; /* length of "val" to use or -1 (whole string) */
20456{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020457 /* Need to do this (at least) once, since we can't initialize a union.
20458 * Will always be invoked when "v:progname" is set. */
20459 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20460
Bram Moolenaare9a41262005-01-15 22:18:47 +000020461 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020463 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020465 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020467 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468}
20469
20470/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020471 * Set List v: variable to "val".
20472 */
20473 void
20474set_vim_var_list(idx, val)
20475 int idx;
20476 list_T *val;
20477{
20478 list_unref(vimvars[idx].vv_list);
20479 vimvars[idx].vv_list = val;
20480 if (val != NULL)
20481 ++val->lv_refcount;
20482}
20483
20484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 * Set v:register if needed.
20486 */
20487 void
20488set_reg_var(c)
20489 int c;
20490{
20491 char_u regname;
20492
20493 if (c == 0 || c == ' ')
20494 regname = '"';
20495 else
20496 regname = c;
20497 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020498 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 set_vim_var_string(VV_REG, &regname, 1);
20500}
20501
20502/*
20503 * Get or set v:exception. If "oldval" == NULL, return the current value.
20504 * Otherwise, restore the value to "oldval" and return NULL.
20505 * Must always be called in pairs to save and restore v:exception! Does not
20506 * take care of memory allocations.
20507 */
20508 char_u *
20509v_exception(oldval)
20510 char_u *oldval;
20511{
20512 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020513 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514
Bram Moolenaare9a41262005-01-15 22:18:47 +000020515 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 return NULL;
20517}
20518
20519/*
20520 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20521 * Otherwise, restore the value to "oldval" and return NULL.
20522 * Must always be called in pairs to save and restore v:throwpoint! Does not
20523 * take care of memory allocations.
20524 */
20525 char_u *
20526v_throwpoint(oldval)
20527 char_u *oldval;
20528{
20529 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020530 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531
Bram Moolenaare9a41262005-01-15 22:18:47 +000020532 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 return NULL;
20534}
20535
20536#if defined(FEAT_AUTOCMD) || defined(PROTO)
20537/*
20538 * Set v:cmdarg.
20539 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20540 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20541 * Must always be called in pairs!
20542 */
20543 char_u *
20544set_cmdarg(eap, oldarg)
20545 exarg_T *eap;
20546 char_u *oldarg;
20547{
20548 char_u *oldval;
20549 char_u *newval;
20550 unsigned len;
20551
Bram Moolenaare9a41262005-01-15 22:18:47 +000020552 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020553 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020555 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020556 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020557 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558 }
20559
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020560 if (eap->force_bin == FORCE_BIN)
20561 len = 6;
20562 else if (eap->force_bin == FORCE_NOBIN)
20563 len = 8;
20564 else
20565 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020566
20567 if (eap->read_edit)
20568 len += 7;
20569
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020570 if (eap->force_ff != 0)
20571 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20572# ifdef FEAT_MBYTE
20573 if (eap->force_enc != 0)
20574 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020575 if (eap->bad_char != 0)
20576 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020577# endif
20578
20579 newval = alloc(len + 1);
20580 if (newval == NULL)
20581 return NULL;
20582
20583 if (eap->force_bin == FORCE_BIN)
20584 sprintf((char *)newval, " ++bin");
20585 else if (eap->force_bin == FORCE_NOBIN)
20586 sprintf((char *)newval, " ++nobin");
20587 else
20588 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020589
20590 if (eap->read_edit)
20591 STRCAT(newval, " ++edit");
20592
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020593 if (eap->force_ff != 0)
20594 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20595 eap->cmd + eap->force_ff);
20596# ifdef FEAT_MBYTE
20597 if (eap->force_enc != 0)
20598 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20599 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020600 if (eap->bad_char == BAD_KEEP)
20601 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20602 else if (eap->bad_char == BAD_DROP)
20603 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20604 else if (eap->bad_char != 0)
20605 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020606# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020607 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020608 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609}
20610#endif
20611
20612/*
20613 * Get the value of internal variable "name".
20614 * Return OK or FAIL.
20615 */
20616 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020617get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 char_u *name;
20619 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020620 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020621 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020622 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623{
20624 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020625 typval_T *tv = NULL;
20626 typval_T atv;
20627 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629
20630 /* truncate the name, so that we can use strcmp() */
20631 cc = name[len];
20632 name[len] = NUL;
20633
20634 /*
20635 * Check for "b:changedtick".
20636 */
20637 if (STRCMP(name, "b:changedtick") == 0)
20638 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020639 atv.v_type = VAR_NUMBER;
20640 atv.vval.v_number = curbuf->b_changedtick;
20641 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 }
20643
20644 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 * Check for user-defined variables.
20646 */
20647 else
20648 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020649 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020651 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 }
20653
Bram Moolenaare9a41262005-01-15 22:18:47 +000020654 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020656 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 ret = FAIL;
20659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020660 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020661 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662
20663 name[len] = cc;
20664
20665 return ret;
20666}
20667
20668/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020669 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20670 * Also handle function call with Funcref variable: func(expr)
20671 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20672 */
20673 static int
20674handle_subscript(arg, rettv, evaluate, verbose)
20675 char_u **arg;
20676 typval_T *rettv;
20677 int evaluate; /* do more than finding the end */
20678 int verbose; /* give error messages */
20679{
20680 int ret = OK;
20681 dict_T *selfdict = NULL;
20682 char_u *s;
20683 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020684 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020685
20686 while (ret == OK
20687 && (**arg == '['
20688 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020689 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020690 && !vim_iswhite(*(*arg - 1)))
20691 {
20692 if (**arg == '(')
20693 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020694 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020695 if (evaluate)
20696 {
20697 functv = *rettv;
20698 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020699
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020700 /* Invoke the function. Recursive! */
20701 s = functv.vval.v_string;
20702 }
20703 else
20704 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020705 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020706 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20707 &len, evaluate, selfdict);
20708
20709 /* Clear the funcref afterwards, so that deleting it while
20710 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020711 if (evaluate)
20712 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020713
20714 /* Stop the expression evaluation when immediately aborting on
20715 * error, or when an interrupt occurred or an exception was thrown
20716 * but not caught. */
20717 if (aborting())
20718 {
20719 if (ret == OK)
20720 clear_tv(rettv);
20721 ret = FAIL;
20722 }
20723 dict_unref(selfdict);
20724 selfdict = NULL;
20725 }
20726 else /* **arg == '[' || **arg == '.' */
20727 {
20728 dict_unref(selfdict);
20729 if (rettv->v_type == VAR_DICT)
20730 {
20731 selfdict = rettv->vval.v_dict;
20732 if (selfdict != NULL)
20733 ++selfdict->dv_refcount;
20734 }
20735 else
20736 selfdict = NULL;
20737 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20738 {
20739 clear_tv(rettv);
20740 ret = FAIL;
20741 }
20742 }
20743 }
20744 dict_unref(selfdict);
20745 return ret;
20746}
20747
20748/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020749 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020750 * value).
20751 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020752 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020753alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020754{
Bram Moolenaar33570922005-01-25 22:26:29 +000020755 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020756}
20757
20758/*
20759 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 * The string "s" must have been allocated, it is consumed.
20761 * Return NULL for out of memory, the variable otherwise.
20762 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020763 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020764alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 char_u *s;
20766{
Bram Moolenaar33570922005-01-25 22:26:29 +000020767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020769 rettv = alloc_tv();
20770 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020772 rettv->v_type = VAR_STRING;
20773 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 }
20775 else
20776 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020777 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778}
20779
20780/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020781 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020783 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020784free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020785 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786{
20787 if (varp != NULL)
20788 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020789 switch (varp->v_type)
20790 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020791 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020792 func_unref(varp->vval.v_string);
20793 /*FALLTHROUGH*/
20794 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020795 vim_free(varp->vval.v_string);
20796 break;
20797 case VAR_LIST:
20798 list_unref(varp->vval.v_list);
20799 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020800 case VAR_DICT:
20801 dict_unref(varp->vval.v_dict);
20802 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020803 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020804#ifdef FEAT_FLOAT
20805 case VAR_FLOAT:
20806#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020807 case VAR_UNKNOWN:
20808 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020809 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020810 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020811 break;
20812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 vim_free(varp);
20814 }
20815}
20816
20817/*
20818 * Free the memory for a variable value and set the value to NULL or 0.
20819 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020820 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020821clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020822 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823{
20824 if (varp != NULL)
20825 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020826 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020828 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020829 func_unref(varp->vval.v_string);
20830 /*FALLTHROUGH*/
20831 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020832 vim_free(varp->vval.v_string);
20833 varp->vval.v_string = NULL;
20834 break;
20835 case VAR_LIST:
20836 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020837 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020838 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020839 case VAR_DICT:
20840 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020841 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020842 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020844 varp->vval.v_number = 0;
20845 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020846#ifdef FEAT_FLOAT
20847 case VAR_FLOAT:
20848 varp->vval.v_float = 0.0;
20849 break;
20850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020851 case VAR_UNKNOWN:
20852 break;
20853 default:
20854 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020856 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 }
20858}
20859
20860/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020861 * Set the value of a variable to NULL without freeing items.
20862 */
20863 static void
20864init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020865 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020866{
20867 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020869}
20870
20871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 * Get the number value of a variable.
20873 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020874 * For incompatible types, return 0.
20875 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20876 * caller of incompatible types: it sets *denote to TRUE if "denote"
20877 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 */
20879 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020880get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020881 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020883 int error = FALSE;
20884
20885 return get_tv_number_chk(varp, &error); /* return 0L on error */
20886}
20887
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020888 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020889get_tv_number_chk(varp, denote)
20890 typval_T *varp;
20891 int *denote;
20892{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020893 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020895 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020897 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020898 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020899#ifdef FEAT_FLOAT
20900 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020901 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020902 break;
20903#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020904 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020905 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020906 break;
20907 case VAR_STRING:
20908 if (varp->vval.v_string != NULL)
20909 vim_str2nr(varp->vval.v_string, NULL, NULL,
20910 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020911 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020912 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020913 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020914 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020915 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020916 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020917 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020918 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020919 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020922 if (denote == NULL) /* useful for values that must be unsigned */
20923 n = -1;
20924 else
20925 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020926 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927}
20928
20929/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020930 * Get the lnum from the first argument.
20931 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020932 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 */
20934 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020935get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020936 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937{
Bram Moolenaar33570922005-01-25 22:26:29 +000020938 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 linenr_T lnum;
20940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020941 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 if (lnum == 0) /* no valid number, try using line() */
20943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 rettv.v_type = VAR_NUMBER;
20945 f_line(argvars, &rettv);
20946 lnum = rettv.vval.v_number;
20947 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 }
20949 return lnum;
20950}
20951
20952/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020953 * Get the lnum from the first argument.
20954 * Also accepts "$", then "buf" is used.
20955 * Returns 0 on error.
20956 */
20957 static linenr_T
20958get_tv_lnum_buf(argvars, buf)
20959 typval_T *argvars;
20960 buf_T *buf;
20961{
20962 if (argvars[0].v_type == VAR_STRING
20963 && argvars[0].vval.v_string != NULL
20964 && argvars[0].vval.v_string[0] == '$'
20965 && buf != NULL)
20966 return buf->b_ml.ml_line_count;
20967 return get_tv_number_chk(&argvars[0], NULL);
20968}
20969
20970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 * Get the string value of a variable.
20972 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020973 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20974 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 * If the String variable has never been set, return an empty string.
20976 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020977 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20978 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 */
20980 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020981get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020982 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983{
20984 static char_u mybuf[NUMBUFLEN];
20985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020986 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987}
20988
20989 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020990get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020991 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020992 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020994 char_u *res = get_tv_string_buf_chk(varp, buf);
20995
20996 return res != NULL ? res : (char_u *)"";
20997}
20998
Bram Moolenaar7d647822014-04-05 21:28:56 +020020999/*
21000 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21001 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021002 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021003get_tv_string_chk(varp)
21004 typval_T *varp;
21005{
21006 static char_u mybuf[NUMBUFLEN];
21007
21008 return get_tv_string_buf_chk(varp, mybuf);
21009}
21010
21011 static char_u *
21012get_tv_string_buf_chk(varp, buf)
21013 typval_T *varp;
21014 char_u *buf;
21015{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021016 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021018 case VAR_NUMBER:
21019 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21020 return buf;
21021 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021022 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021023 break;
21024 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021025 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021026 break;
21027 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021028 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021029 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021030#ifdef FEAT_FLOAT
21031 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021032 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021033 break;
21034#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021035 case VAR_STRING:
21036 if (varp->vval.v_string != NULL)
21037 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021038 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021039 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021040 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021041 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021043 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044}
21045
21046/*
21047 * Find variable "name" in the list of variables.
21048 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021049 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021050 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021051 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021054find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021056 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021057 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021060 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061
Bram Moolenaara7043832005-01-21 11:56:39 +000021062 ht = find_var_ht(name, &varname);
21063 if (htp != NULL)
21064 *htp = ht;
21065 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021067 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068}
21069
21070/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021071 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021072 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021074 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021075find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021076 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021077 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021078 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021079 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021080{
Bram Moolenaar33570922005-01-25 22:26:29 +000021081 hashitem_T *hi;
21082
21083 if (*varname == NUL)
21084 {
21085 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021086 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021087 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021088 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021089 case 'g': return &globvars_var;
21090 case 'v': return &vimvars_var;
21091 case 'b': return &curbuf->b_bufvar;
21092 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021093#ifdef FEAT_WINDOWS
21094 case 't': return &curtab->tp_winvar;
21095#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021096 case 'l': return current_funccal == NULL
21097 ? NULL : &current_funccal->l_vars_var;
21098 case 'a': return current_funccal == NULL
21099 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021100 }
21101 return NULL;
21102 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021103
21104 hi = hash_find(ht, varname);
21105 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021106 {
21107 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021108 * worked find the variable again. Don't auto-load a script if it was
21109 * loaded already, otherwise it would be loaded every time when
21110 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021111 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021112 {
21113 /* Note: script_autoload() may make "hi" invalid. It must either
21114 * be obtained again or not used. */
21115 if (!script_autoload(varname, FALSE) || aborting())
21116 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021117 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021118 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021119 if (HASHITEM_EMPTY(hi))
21120 return NULL;
21121 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021122 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021123}
21124
21125/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021126 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021127 * Set "varname" to the start of name without ':'.
21128 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021129 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021130find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 char_u *name;
21132 char_u **varname;
21133{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021134 hashitem_T *hi;
21135
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 if (name[1] != ':')
21137 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021138 /* The name must not start with a colon or #. */
21139 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 return NULL;
21141 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021142
21143 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021144 hi = hash_find(&compat_hashtab, name);
21145 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021146 return &compat_hashtab;
21147
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021149 return &globvarht; /* global variable */
21150 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 }
21152 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021153 if (*name == 'g') /* global variable */
21154 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021155 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21156 */
21157 if (vim_strchr(name + 2, ':') != NULL
21158 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021159 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021161 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021163 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021164#ifdef FEAT_WINDOWS
21165 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021166 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021167#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021168 if (*name == 'v') /* v: variable */
21169 return &vimvarht;
21170 if (*name == 'a' && current_funccal != NULL) /* function argument */
21171 return &current_funccal->l_avars.dv_hashtab;
21172 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21173 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 if (*name == 's' /* script variable */
21175 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21176 return &SCRIPT_VARS(current_SID);
21177 return NULL;
21178}
21179
21180/*
21181 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021182 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 * Returns NULL when it doesn't exist.
21184 */
21185 char_u *
21186get_var_value(name)
21187 char_u *name;
21188{
Bram Moolenaar33570922005-01-25 22:26:29 +000021189 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021191 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 if (v == NULL)
21193 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021194 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195}
21196
21197/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021198 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 * sourcing this script and when executing functions defined in the script.
21200 */
21201 void
21202new_script_vars(id)
21203 scid_T id;
21204{
Bram Moolenaara7043832005-01-21 11:56:39 +000021205 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021206 hashtab_T *ht;
21207 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021208
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21210 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021211 /* Re-allocating ga_data means that an ht_array pointing to
21212 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021213 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021214 for (i = 1; i <= ga_scripts.ga_len; ++i)
21215 {
21216 ht = &SCRIPT_VARS(i);
21217 if (ht->ht_mask == HT_INIT_SIZE - 1)
21218 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021219 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021220 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021221 }
21222
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 while (ga_scripts.ga_len < id)
21224 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021225 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021226 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021227 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 }
21230 }
21231}
21232
21233/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021234 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21235 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 */
21237 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021238init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021239 dict_T *dict;
21240 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021241 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242{
Bram Moolenaar33570922005-01-25 22:26:29 +000021243 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021244 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021245 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021246 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021247 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021248 dict_var->di_tv.vval.v_dict = dict;
21249 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021250 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021251 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21252 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253}
21254
21255/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021256 * Unreference a dictionary initialized by init_var_dict().
21257 */
21258 void
21259unref_var_dict(dict)
21260 dict_T *dict;
21261{
21262 /* Now the dict needs to be freed if no one else is using it, go back to
21263 * normal reference counting. */
21264 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21265 dict_unref(dict);
21266}
21267
21268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021270 * Frees all allocated variables and the value they contain.
21271 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 */
21273 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021274vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021275 hashtab_T *ht;
21276{
21277 vars_clear_ext(ht, TRUE);
21278}
21279
21280/*
21281 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21282 */
21283 static void
21284vars_clear_ext(ht, free_val)
21285 hashtab_T *ht;
21286 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287{
Bram Moolenaara7043832005-01-21 11:56:39 +000021288 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021289 hashitem_T *hi;
21290 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291
Bram Moolenaar33570922005-01-25 22:26:29 +000021292 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021293 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021294 for (hi = ht->ht_array; todo > 0; ++hi)
21295 {
21296 if (!HASHITEM_EMPTY(hi))
21297 {
21298 --todo;
21299
Bram Moolenaar33570922005-01-25 22:26:29 +000021300 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021301 * ht_array might change then. hash_clear() takes care of it
21302 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021303 v = HI2DI(hi);
21304 if (free_val)
21305 clear_tv(&v->di_tv);
21306 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21307 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021308 }
21309 }
21310 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021311 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312}
21313
Bram Moolenaara7043832005-01-21 11:56:39 +000021314/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021315 * Delete a variable from hashtab "ht" at item "hi".
21316 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021319delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021320 hashtab_T *ht;
21321 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322{
Bram Moolenaar33570922005-01-25 22:26:29 +000021323 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021324
21325 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021326 clear_tv(&di->di_tv);
21327 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328}
21329
21330/*
21331 * List the value of one internal variable.
21332 */
21333 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021334list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021335 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021337 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021339 char_u *tofree;
21340 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021341 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021342
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021343 current_copyID += COPYID_INC;
21344 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021345 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021346 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021347 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348}
21349
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021351list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 char_u *prefix;
21353 char_u *name;
21354 int type;
21355 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021356 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357{
Bram Moolenaar31859182007-08-14 20:41:13 +000021358 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21359 msg_start();
21360 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 if (name != NULL) /* "a:" vars don't have a name stored */
21362 msg_puts(name);
21363 msg_putchar(' ');
21364 msg_advance(22);
21365 if (type == VAR_NUMBER)
21366 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021367 else if (type == VAR_FUNC)
21368 msg_putchar('*');
21369 else if (type == VAR_LIST)
21370 {
21371 msg_putchar('[');
21372 if (*string == '[')
21373 ++string;
21374 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021375 else if (type == VAR_DICT)
21376 {
21377 msg_putchar('{');
21378 if (*string == '{')
21379 ++string;
21380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 else
21382 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021383
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021385
21386 if (type == VAR_FUNC)
21387 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021388 if (*first)
21389 {
21390 msg_clr_eos();
21391 *first = FALSE;
21392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393}
21394
21395/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021396 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 * If the variable already exists, the value is updated.
21398 * Otherwise the variable is created.
21399 */
21400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021401set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021403 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405{
Bram Moolenaar33570922005-01-25 22:26:29 +000021406 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021408 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021410 ht = find_var_ht(name, &varname);
21411 if (ht == NULL || *varname == NUL)
21412 {
21413 EMSG2(_(e_illvar), name);
21414 return;
21415 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021416 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021417
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021418 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21419 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021420
Bram Moolenaar33570922005-01-25 22:26:29 +000021421 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021423 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021424 if (var_check_ro(v->di_flags, name)
21425 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021426 return;
21427 if (v->di_tv.v_type != tv->v_type
21428 && !((v->di_tv.v_type == VAR_STRING
21429 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021430 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021431 || tv->v_type == VAR_NUMBER))
21432#ifdef FEAT_FLOAT
21433 && !((v->di_tv.v_type == VAR_NUMBER
21434 || v->di_tv.v_type == VAR_FLOAT)
21435 && (tv->v_type == VAR_NUMBER
21436 || tv->v_type == VAR_FLOAT))
21437#endif
21438 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021439 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021440 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021441 return;
21442 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021443
21444 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021445 * Handle setting internal v: variables separately: we don't change
21446 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021447 */
21448 if (ht == &vimvarht)
21449 {
21450 if (v->di_tv.v_type == VAR_STRING)
21451 {
21452 vim_free(v->di_tv.vval.v_string);
21453 if (copy || tv->v_type != VAR_STRING)
21454 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21455 else
21456 {
21457 /* Take over the string to avoid an extra alloc/free. */
21458 v->di_tv.vval.v_string = tv->vval.v_string;
21459 tv->vval.v_string = NULL;
21460 }
21461 }
21462 else if (v->di_tv.v_type != VAR_NUMBER)
21463 EMSG2(_(e_intern2), "set_var()");
21464 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021465 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021466 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021467 if (STRCMP(varname, "searchforward") == 0)
21468 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021469#ifdef FEAT_SEARCH_EXTRA
21470 else if (STRCMP(varname, "hlsearch") == 0)
21471 {
21472 no_hlsearch = !v->di_tv.vval.v_number;
21473 redraw_all_later(SOME_VALID);
21474 }
21475#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021476 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021477 return;
21478 }
21479
21480 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 }
21482 else /* add a new variable */
21483 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021484 /* Can't add "v:" variable. */
21485 if (ht == &vimvarht)
21486 {
21487 EMSG2(_(e_illvar), name);
21488 return;
21489 }
21490
Bram Moolenaar92124a32005-06-17 22:03:40 +000021491 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021492 if (!valid_varname(varname))
21493 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021494
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021495 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21496 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021497 if (v == NULL)
21498 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021499 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021500 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021502 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 return;
21504 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021505 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021507
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021508 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021509 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021510 else
21511 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021512 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021513 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021514 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516}
21517
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021518/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021519 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021520 * Also give an error message.
21521 */
21522 static int
21523var_check_ro(flags, name)
21524 int flags;
21525 char_u *name;
21526{
21527 if (flags & DI_FLAGS_RO)
21528 {
21529 EMSG2(_(e_readonlyvar), name);
21530 return TRUE;
21531 }
21532 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21533 {
21534 EMSG2(_(e_readonlysbx), name);
21535 return TRUE;
21536 }
21537 return FALSE;
21538}
21539
21540/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021541 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21542 * Also give an error message.
21543 */
21544 static int
21545var_check_fixed(flags, name)
21546 int flags;
21547 char_u *name;
21548{
21549 if (flags & DI_FLAGS_FIX)
21550 {
21551 EMSG2(_("E795: Cannot delete variable %s"), name);
21552 return TRUE;
21553 }
21554 return FALSE;
21555}
21556
21557/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021558 * Check if a funcref is assigned to a valid variable name.
21559 * Return TRUE and give an error if not.
21560 */
21561 static int
21562var_check_func_name(name, new_var)
21563 char_u *name; /* points to start of variable name */
21564 int new_var; /* TRUE when creating the variable */
21565{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021566 /* Allow for w: b: s: and t:. */
21567 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021568 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21569 ? name[2] : name[0]))
21570 {
21571 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21572 name);
21573 return TRUE;
21574 }
21575 /* Don't allow hiding a function. When "v" is not NULL we might be
21576 * assigning another function to the same var, the type is checked
21577 * below. */
21578 if (new_var && function_exists(name))
21579 {
21580 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21581 name);
21582 return TRUE;
21583 }
21584 return FALSE;
21585}
21586
21587/*
21588 * Check if a variable name is valid.
21589 * Return FALSE and give an error if not.
21590 */
21591 static int
21592valid_varname(varname)
21593 char_u *varname;
21594{
21595 char_u *p;
21596
21597 for (p = varname; *p != NUL; ++p)
21598 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21599 && *p != AUTOLOAD_CHAR)
21600 {
21601 EMSG2(_(e_illvar), varname);
21602 return FALSE;
21603 }
21604 return TRUE;
21605}
21606
21607/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021608 * Return TRUE if typeval "tv" is set to be locked (immutable).
21609 * Also give an error message, using "name".
21610 */
21611 static int
21612tv_check_lock(lock, name)
21613 int lock;
21614 char_u *name;
21615{
21616 if (lock & VAR_LOCKED)
21617 {
21618 EMSG2(_("E741: Value is locked: %s"),
21619 name == NULL ? (char_u *)_("Unknown") : name);
21620 return TRUE;
21621 }
21622 if (lock & VAR_FIXED)
21623 {
21624 EMSG2(_("E742: Cannot change value of %s"),
21625 name == NULL ? (char_u *)_("Unknown") : name);
21626 return TRUE;
21627 }
21628 return FALSE;
21629}
21630
21631/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021632 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021633 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021634 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021635 * It is OK for "from" and "to" to point to the same item. This is used to
21636 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021637 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021638 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021639copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021640 typval_T *from;
21641 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021643 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021644 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021645 switch (from->v_type)
21646 {
21647 case VAR_NUMBER:
21648 to->vval.v_number = from->vval.v_number;
21649 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021650#ifdef FEAT_FLOAT
21651 case VAR_FLOAT:
21652 to->vval.v_float = from->vval.v_float;
21653 break;
21654#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021655 case VAR_STRING:
21656 case VAR_FUNC:
21657 if (from->vval.v_string == NULL)
21658 to->vval.v_string = NULL;
21659 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021661 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021662 if (from->v_type == VAR_FUNC)
21663 func_ref(to->vval.v_string);
21664 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021665 break;
21666 case VAR_LIST:
21667 if (from->vval.v_list == NULL)
21668 to->vval.v_list = NULL;
21669 else
21670 {
21671 to->vval.v_list = from->vval.v_list;
21672 ++to->vval.v_list->lv_refcount;
21673 }
21674 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021675 case VAR_DICT:
21676 if (from->vval.v_dict == NULL)
21677 to->vval.v_dict = NULL;
21678 else
21679 {
21680 to->vval.v_dict = from->vval.v_dict;
21681 ++to->vval.v_dict->dv_refcount;
21682 }
21683 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021684 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021685 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021686 break;
21687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688}
21689
21690/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021691 * Make a copy of an item.
21692 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021693 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21694 * reference to an already copied list/dict can be used.
21695 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021696 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021697 static int
21698item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021699 typval_T *from;
21700 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021701 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021702 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021703{
21704 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021705 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021706
Bram Moolenaar33570922005-01-25 22:26:29 +000021707 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021708 {
21709 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021710 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021711 }
21712 ++recurse;
21713
21714 switch (from->v_type)
21715 {
21716 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021717#ifdef FEAT_FLOAT
21718 case VAR_FLOAT:
21719#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021720 case VAR_STRING:
21721 case VAR_FUNC:
21722 copy_tv(from, to);
21723 break;
21724 case VAR_LIST:
21725 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021726 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021727 if (from->vval.v_list == NULL)
21728 to->vval.v_list = NULL;
21729 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21730 {
21731 /* use the copy made earlier */
21732 to->vval.v_list = from->vval.v_list->lv_copylist;
21733 ++to->vval.v_list->lv_refcount;
21734 }
21735 else
21736 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21737 if (to->vval.v_list == NULL)
21738 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021739 break;
21740 case VAR_DICT:
21741 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021742 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021743 if (from->vval.v_dict == NULL)
21744 to->vval.v_dict = NULL;
21745 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21746 {
21747 /* use the copy made earlier */
21748 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21749 ++to->vval.v_dict->dv_refcount;
21750 }
21751 else
21752 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21753 if (to->vval.v_dict == NULL)
21754 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021755 break;
21756 default:
21757 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021758 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021759 }
21760 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021761 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021762}
21763
21764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 * ":echo expr1 ..." print each argument separated with a space, add a
21766 * newline at the end.
21767 * ":echon expr1 ..." print each argument plain.
21768 */
21769 void
21770ex_echo(eap)
21771 exarg_T *eap;
21772{
21773 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021775 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 char_u *p;
21777 int needclr = TRUE;
21778 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021779 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780
21781 if (eap->skip)
21782 ++emsg_skip;
21783 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21784 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021785 /* If eval1() causes an error message the text from the command may
21786 * still need to be cleared. E.g., "echo 22,44". */
21787 need_clr_eos = needclr;
21788
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021790 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 {
21792 /*
21793 * Report the invalid expression unless the expression evaluation
21794 * has been cancelled due to an aborting error, an interrupt, or an
21795 * exception.
21796 */
21797 if (!aborting())
21798 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021799 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 break;
21801 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021802 need_clr_eos = FALSE;
21803
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 if (!eap->skip)
21805 {
21806 if (atstart)
21807 {
21808 atstart = FALSE;
21809 /* Call msg_start() after eval1(), evaluating the expression
21810 * may cause a message to appear. */
21811 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021812 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021813 /* Mark the saved text as finishing the line, so that what
21814 * follows is displayed on a new line when scrolling back
21815 * at the more prompt. */
21816 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 }
21820 else if (eap->cmdidx == CMD_echo)
21821 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021822 current_copyID += COPYID_INC;
21823 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021824 if (p != NULL)
21825 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021827 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021829 if (*p != TAB && needclr)
21830 {
21831 /* remove any text still there from the command */
21832 msg_clr_eos();
21833 needclr = FALSE;
21834 }
21835 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 }
21837 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021838 {
21839#ifdef FEAT_MBYTE
21840 if (has_mbyte)
21841 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021842 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021843
21844 (void)msg_outtrans_len_attr(p, i, echo_attr);
21845 p += i - 1;
21846 }
21847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021849 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021852 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021854 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 arg = skipwhite(arg);
21856 }
21857 eap->nextcmd = check_nextcmd(arg);
21858
21859 if (eap->skip)
21860 --emsg_skip;
21861 else
21862 {
21863 /* remove text that may still be there from the command */
21864 if (needclr)
21865 msg_clr_eos();
21866 if (eap->cmdidx == CMD_echo)
21867 msg_end();
21868 }
21869}
21870
21871/*
21872 * ":echohl {name}".
21873 */
21874 void
21875ex_echohl(eap)
21876 exarg_T *eap;
21877{
21878 int id;
21879
21880 id = syn_name2id(eap->arg);
21881 if (id == 0)
21882 echo_attr = 0;
21883 else
21884 echo_attr = syn_id2attr(id);
21885}
21886
21887/*
21888 * ":execute expr1 ..." execute the result of an expression.
21889 * ":echomsg expr1 ..." Print a message
21890 * ":echoerr expr1 ..." Print an error
21891 * Each gets spaces around each argument and a newline at the end for
21892 * echo commands
21893 */
21894 void
21895ex_execute(eap)
21896 exarg_T *eap;
21897{
21898 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021899 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 int ret = OK;
21901 char_u *p;
21902 garray_T ga;
21903 int len;
21904 int save_did_emsg;
21905
21906 ga_init2(&ga, 1, 80);
21907
21908 if (eap->skip)
21909 ++emsg_skip;
21910 while (*arg != NUL && *arg != '|' && *arg != '\n')
21911 {
21912 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021913 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914 {
21915 /*
21916 * Report the invalid expression unless the expression evaluation
21917 * has been cancelled due to an aborting error, an interrupt, or an
21918 * exception.
21919 */
21920 if (!aborting())
21921 EMSG2(_(e_invexpr2), p);
21922 ret = FAIL;
21923 break;
21924 }
21925
21926 if (!eap->skip)
21927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021928 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 len = (int)STRLEN(p);
21930 if (ga_grow(&ga, len + 2) == FAIL)
21931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021932 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 ret = FAIL;
21934 break;
21935 }
21936 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 ga.ga_len += len;
21940 }
21941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021942 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 arg = skipwhite(arg);
21944 }
21945
21946 if (ret != FAIL && ga.ga_data != NULL)
21947 {
21948 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021951 out_flush();
21952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 else if (eap->cmdidx == CMD_echoerr)
21954 {
21955 /* We don't want to abort following commands, restore did_emsg. */
21956 save_did_emsg = did_emsg;
21957 EMSG((char_u *)ga.ga_data);
21958 if (!force_abort)
21959 did_emsg = save_did_emsg;
21960 }
21961 else if (eap->cmdidx == CMD_execute)
21962 do_cmdline((char_u *)ga.ga_data,
21963 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21964 }
21965
21966 ga_clear(&ga);
21967
21968 if (eap->skip)
21969 --emsg_skip;
21970
21971 eap->nextcmd = check_nextcmd(arg);
21972}
21973
21974/*
21975 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21976 * "arg" points to the "&" or '+' when called, to "option" when returning.
21977 * Returns NULL when no option name found. Otherwise pointer to the char
21978 * after the option name.
21979 */
21980 static char_u *
21981find_option_end(arg, opt_flags)
21982 char_u **arg;
21983 int *opt_flags;
21984{
21985 char_u *p = *arg;
21986
21987 ++p;
21988 if (*p == 'g' && p[1] == ':')
21989 {
21990 *opt_flags = OPT_GLOBAL;
21991 p += 2;
21992 }
21993 else if (*p == 'l' && p[1] == ':')
21994 {
21995 *opt_flags = OPT_LOCAL;
21996 p += 2;
21997 }
21998 else
21999 *opt_flags = 0;
22000
22001 if (!ASCII_ISALPHA(*p))
22002 return NULL;
22003 *arg = p;
22004
22005 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22006 p += 4; /* termcap option */
22007 else
22008 while (ASCII_ISALPHA(*p))
22009 ++p;
22010 return p;
22011}
22012
22013/*
22014 * ":function"
22015 */
22016 void
22017ex_function(eap)
22018 exarg_T *eap;
22019{
22020 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022021 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 int j;
22023 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022025 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 char_u *name = NULL;
22027 char_u *p;
22028 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022029 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 garray_T newargs;
22031 garray_T newlines;
22032 int varargs = FALSE;
22033 int mustend = FALSE;
22034 int flags = 0;
22035 ufunc_T *fp;
22036 int indent;
22037 int nesting;
22038 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022039 dictitem_T *v;
22040 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022041 static int func_nr = 0; /* number for nameless function */
22042 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022043 hashtab_T *ht;
22044 int todo;
22045 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022046 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047
22048 /*
22049 * ":function" without argument: list functions.
22050 */
22051 if (ends_excmd(*eap->arg))
22052 {
22053 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022054 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022055 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022056 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022057 {
22058 if (!HASHITEM_EMPTY(hi))
22059 {
22060 --todo;
22061 fp = HI2UF(hi);
22062 if (!isdigit(*fp->uf_name))
22063 list_func_head(fp, FALSE);
22064 }
22065 }
22066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 eap->nextcmd = check_nextcmd(eap->arg);
22068 return;
22069 }
22070
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022071 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022072 * ":function /pat": list functions matching pattern.
22073 */
22074 if (*eap->arg == '/')
22075 {
22076 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22077 if (!eap->skip)
22078 {
22079 regmatch_T regmatch;
22080
22081 c = *p;
22082 *p = NUL;
22083 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22084 *p = c;
22085 if (regmatch.regprog != NULL)
22086 {
22087 regmatch.rm_ic = p_ic;
22088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022089 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022090 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22091 {
22092 if (!HASHITEM_EMPTY(hi))
22093 {
22094 --todo;
22095 fp = HI2UF(hi);
22096 if (!isdigit(*fp->uf_name)
22097 && vim_regexec(&regmatch, fp->uf_name, 0))
22098 list_func_head(fp, FALSE);
22099 }
22100 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022101 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022102 }
22103 }
22104 if (*p == '/')
22105 ++p;
22106 eap->nextcmd = check_nextcmd(p);
22107 return;
22108 }
22109
22110 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022111 * Get the function name. There are these situations:
22112 * func normal function name
22113 * "name" == func, "fudi.fd_dict" == NULL
22114 * dict.func new dictionary entry
22115 * "name" == NULL, "fudi.fd_dict" set,
22116 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22117 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022118 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022119 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22120 * dict.func existing dict entry that's not a Funcref
22121 * "name" == NULL, "fudi.fd_dict" set,
22122 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022123 * s:func script-local function name
22124 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022127 name = trans_function_name(&p, eap->skip, 0, &fudi);
22128 paren = (vim_strchr(p, '(') != NULL);
22129 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130 {
22131 /*
22132 * Return on an invalid expression in braces, unless the expression
22133 * evaluation has been cancelled due to an aborting error, an
22134 * interrupt, or an exception.
22135 */
22136 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022138 if (!eap->skip && fudi.fd_newkey != NULL)
22139 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022140 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 else
22144 eap->skip = TRUE;
22145 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022146
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 /* An error in a function call during evaluation of an expression in magic
22148 * braces should not cause the function not to be defined. */
22149 saved_did_emsg = did_emsg;
22150 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151
22152 /*
22153 * ":function func" with only function name: list function.
22154 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022155 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 {
22157 if (!ends_excmd(*skipwhite(p)))
22158 {
22159 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022160 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 }
22162 eap->nextcmd = check_nextcmd(p);
22163 if (eap->nextcmd != NULL)
22164 *p = NUL;
22165 if (!eap->skip && !got_int)
22166 {
22167 fp = find_func(name);
22168 if (fp != NULL)
22169 {
22170 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022171 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022173 if (FUNCLINE(fp, j) == NULL)
22174 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 msg_putchar('\n');
22176 msg_outnum((long)(j + 1));
22177 if (j < 9)
22178 msg_putchar(' ');
22179 if (j < 99)
22180 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022181 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 out_flush(); /* show a line at a time */
22183 ui_breakcheck();
22184 }
22185 if (!got_int)
22186 {
22187 msg_putchar('\n');
22188 msg_puts((char_u *)" endfunction");
22189 }
22190 }
22191 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022192 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022194 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 }
22196
22197 /*
22198 * ":function name(arg1, arg2)" Define function.
22199 */
22200 p = skipwhite(p);
22201 if (*p != '(')
22202 {
22203 if (!eap->skip)
22204 {
22205 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022206 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 }
22208 /* attempt to continue by skipping some text */
22209 if (vim_strchr(p, '(') != NULL)
22210 p = vim_strchr(p, '(');
22211 }
22212 p = skipwhite(p + 1);
22213
22214 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22215 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22216
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022217 if (!eap->skip)
22218 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022219 /* Check the name of the function. Unless it's a dictionary function
22220 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022221 if (name != NULL)
22222 arg = name;
22223 else
22224 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022225 if (arg != NULL && (fudi.fd_di == NULL
22226 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022227 {
22228 if (*arg == K_SPECIAL)
22229 j = 3;
22230 else
22231 j = 0;
22232 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22233 : eval_isnamec(arg[j])))
22234 ++j;
22235 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022236 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022237 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022238 /* Disallow using the g: dict. */
22239 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22240 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022241 }
22242
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243 /*
22244 * Isolate the arguments: "arg1, arg2, ...)"
22245 */
22246 while (*p != ')')
22247 {
22248 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22249 {
22250 varargs = TRUE;
22251 p += 3;
22252 mustend = TRUE;
22253 }
22254 else
22255 {
22256 arg = p;
22257 while (ASCII_ISALNUM(*p) || *p == '_')
22258 ++p;
22259 if (arg == p || isdigit(*arg)
22260 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22261 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22262 {
22263 if (!eap->skip)
22264 EMSG2(_("E125: Illegal argument: %s"), arg);
22265 break;
22266 }
22267 if (ga_grow(&newargs, 1) == FAIL)
22268 goto erret;
22269 c = *p;
22270 *p = NUL;
22271 arg = vim_strsave(arg);
22272 if (arg == NULL)
22273 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022274
22275 /* Check for duplicate argument name. */
22276 for (i = 0; i < newargs.ga_len; ++i)
22277 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22278 {
22279 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022280 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022281 goto erret;
22282 }
22283
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22285 *p = c;
22286 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 if (*p == ',')
22288 ++p;
22289 else
22290 mustend = TRUE;
22291 }
22292 p = skipwhite(p);
22293 if (mustend && *p != ')')
22294 {
22295 if (!eap->skip)
22296 EMSG2(_(e_invarg2), eap->arg);
22297 break;
22298 }
22299 }
22300 ++p; /* skip the ')' */
22301
Bram Moolenaare9a41262005-01-15 22:18:47 +000022302 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 for (;;)
22304 {
22305 p = skipwhite(p);
22306 if (STRNCMP(p, "range", 5) == 0)
22307 {
22308 flags |= FC_RANGE;
22309 p += 5;
22310 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022311 else if (STRNCMP(p, "dict", 4) == 0)
22312 {
22313 flags |= FC_DICT;
22314 p += 4;
22315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 else if (STRNCMP(p, "abort", 5) == 0)
22317 {
22318 flags |= FC_ABORT;
22319 p += 5;
22320 }
22321 else
22322 break;
22323 }
22324
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022325 /* When there is a line break use what follows for the function body.
22326 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22327 if (*p == '\n')
22328 line_arg = p + 1;
22329 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 EMSG(_(e_trailing));
22331
22332 /*
22333 * Read the body of the function, until ":endfunction" is found.
22334 */
22335 if (KeyTyped)
22336 {
22337 /* Check if the function already exists, don't let the user type the
22338 * whole function before telling him it doesn't work! For a script we
22339 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340 if (!eap->skip && !eap->forceit)
22341 {
22342 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22343 EMSG(_(e_funcdict));
22344 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022345 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022348 if (!eap->skip && did_emsg)
22349 goto erret;
22350
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 msg_putchar('\n'); /* don't overwrite the function name */
22352 cmdline_row = msg_row;
22353 }
22354
22355 indent = 2;
22356 nesting = 0;
22357 for (;;)
22358 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022359 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022360 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022361 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022362 saved_wait_return = FALSE;
22363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022365 sourcing_lnum_off = sourcing_lnum;
22366
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022367 if (line_arg != NULL)
22368 {
22369 /* Use eap->arg, split up in parts by line breaks. */
22370 theline = line_arg;
22371 p = vim_strchr(theline, '\n');
22372 if (p == NULL)
22373 line_arg += STRLEN(line_arg);
22374 else
22375 {
22376 *p = NUL;
22377 line_arg = p + 1;
22378 }
22379 }
22380 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 theline = getcmdline(':', 0L, indent);
22382 else
22383 theline = eap->getline(':', eap->cookie, indent);
22384 if (KeyTyped)
22385 lines_left = Rows - 1;
22386 if (theline == NULL)
22387 {
22388 EMSG(_("E126: Missing :endfunction"));
22389 goto erret;
22390 }
22391
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022392 /* Detect line continuation: sourcing_lnum increased more than one. */
22393 if (sourcing_lnum > sourcing_lnum_off + 1)
22394 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22395 else
22396 sourcing_lnum_off = 0;
22397
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 if (skip_until != NULL)
22399 {
22400 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22401 * don't check for ":endfunc". */
22402 if (STRCMP(theline, skip_until) == 0)
22403 {
22404 vim_free(skip_until);
22405 skip_until = NULL;
22406 }
22407 }
22408 else
22409 {
22410 /* skip ':' and blanks*/
22411 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22412 ;
22413
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022414 /* Check for "endfunction". */
22415 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022417 if (line_arg == NULL)
22418 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 break;
22420 }
22421
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022422 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 * at "end". */
22424 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22425 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022426 else if (STRNCMP(p, "if", 2) == 0
22427 || STRNCMP(p, "wh", 2) == 0
22428 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 || STRNCMP(p, "try", 3) == 0)
22430 indent += 2;
22431
22432 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022433 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022435 if (*p == '!')
22436 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022438 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22439 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022441 ++nesting;
22442 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 }
22444 }
22445
22446 /* Check for ":append" or ":insert". */
22447 p = skip_range(p, NULL);
22448 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22449 || (p[0] == 'i'
22450 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22451 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22452 skip_until = vim_strsave((char_u *)".");
22453
22454 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22455 arg = skipwhite(skiptowhite(p));
22456 if (arg[0] == '<' && arg[1] =='<'
22457 && ((p[0] == 'p' && p[1] == 'y'
22458 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22459 || (p[0] == 'p' && p[1] == 'e'
22460 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22461 || (p[0] == 't' && p[1] == 'c'
22462 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022463 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22464 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22466 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022467 || (p[0] == 'm' && p[1] == 'z'
22468 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 ))
22470 {
22471 /* ":python <<" continues until a dot, like ":append" */
22472 p = skipwhite(arg + 2);
22473 if (*p == NUL)
22474 skip_until = vim_strsave((char_u *)".");
22475 else
22476 skip_until = vim_strsave(p);
22477 }
22478 }
22479
22480 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022481 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022482 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022483 if (line_arg == NULL)
22484 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022486 }
22487
22488 /* Copy the line to newly allocated memory. get_one_sourceline()
22489 * allocates 250 bytes per line, this saves 80% on average. The cost
22490 * is an extra alloc/free. */
22491 p = vim_strsave(theline);
22492 if (p != NULL)
22493 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022494 if (line_arg == NULL)
22495 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022496 theline = p;
22497 }
22498
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022499 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22500
22501 /* Add NULL lines for continuation lines, so that the line count is
22502 * equal to the index in the growarray. */
22503 while (sourcing_lnum_off-- > 0)
22504 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022505
22506 /* Check for end of eap->arg. */
22507 if (line_arg != NULL && *line_arg == NUL)
22508 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 }
22510
22511 /* Don't define the function when skipping commands or when an error was
22512 * detected. */
22513 if (eap->skip || did_emsg)
22514 goto erret;
22515
22516 /*
22517 * If there are no errors, add the function
22518 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022519 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022520 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022521 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022522 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022523 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022524 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022525 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022526 goto erret;
22527 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022529 fp = find_func(name);
22530 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022532 if (!eap->forceit)
22533 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022534 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022535 goto erret;
22536 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022537 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022538 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022539 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022540 name);
22541 goto erret;
22542 }
22543 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022544 ga_clear_strings(&(fp->uf_args));
22545 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022546 vim_free(name);
22547 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 }
22550 else
22551 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022552 char numbuf[20];
22553
22554 fp = NULL;
22555 if (fudi.fd_newkey == NULL && !eap->forceit)
22556 {
22557 EMSG(_(e_funcdict));
22558 goto erret;
22559 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022560 if (fudi.fd_di == NULL)
22561 {
22562 /* Can't add a function to a locked dictionary */
22563 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22564 goto erret;
22565 }
22566 /* Can't change an existing function if it is locked */
22567 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22568 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022569
22570 /* Give the function a sequential number. Can only be used with a
22571 * Funcref! */
22572 vim_free(name);
22573 sprintf(numbuf, "%d", ++func_nr);
22574 name = vim_strsave((char_u *)numbuf);
22575 if (name == NULL)
22576 goto erret;
22577 }
22578
22579 if (fp == NULL)
22580 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022581 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022582 {
22583 int slen, plen;
22584 char_u *scriptname;
22585
22586 /* Check that the autoload name matches the script name. */
22587 j = FAIL;
22588 if (sourcing_name != NULL)
22589 {
22590 scriptname = autoload_name(name);
22591 if (scriptname != NULL)
22592 {
22593 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022594 plen = (int)STRLEN(p);
22595 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022596 if (slen > plen && fnamecmp(p,
22597 sourcing_name + slen - plen) == 0)
22598 j = OK;
22599 vim_free(scriptname);
22600 }
22601 }
22602 if (j == FAIL)
22603 {
22604 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22605 goto erret;
22606 }
22607 }
22608
22609 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 if (fp == NULL)
22611 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022612
22613 if (fudi.fd_dict != NULL)
22614 {
22615 if (fudi.fd_di == NULL)
22616 {
22617 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022618 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022619 if (fudi.fd_di == NULL)
22620 {
22621 vim_free(fp);
22622 goto erret;
22623 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022624 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22625 {
22626 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022627 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022628 goto erret;
22629 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022630 }
22631 else
22632 /* overwrite existing dict entry */
22633 clear_tv(&fudi.fd_di->di_tv);
22634 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022635 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022636 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022637 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022638
22639 /* behave like "dict" was used */
22640 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022641 }
22642
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022644 STRCPY(fp->uf_name, name);
22645 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022647 fp->uf_args = newargs;
22648 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022649#ifdef FEAT_PROFILE
22650 fp->uf_tml_count = NULL;
22651 fp->uf_tml_total = NULL;
22652 fp->uf_tml_self = NULL;
22653 fp->uf_profiling = FALSE;
22654 if (prof_def_func())
22655 func_do_profile(fp);
22656#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022657 fp->uf_varargs = varargs;
22658 fp->uf_flags = flags;
22659 fp->uf_calls = 0;
22660 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022661 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662
22663erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 ga_clear_strings(&newargs);
22665 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022666ret_free:
22667 vim_free(skip_until);
22668 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022671 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672}
22673
22674/*
22675 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022676 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022678 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022679 * TFN_INT: internal function name OK
22680 * TFN_QUIET: be quiet
22681 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 * Advances "pp" to just after the function name (if no error).
22683 */
22684 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022685trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 char_u **pp;
22687 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022688 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022689 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022691 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 char_u *start;
22693 char_u *end;
22694 int lead;
22695 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022697 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022698
22699 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022700 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022702
22703 /* Check for hard coded <SNR>: already translated function ID (from a user
22704 * command). */
22705 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22706 && (*pp)[2] == (int)KE_SNR)
22707 {
22708 *pp += 3;
22709 len = get_id_len(pp) + 3;
22710 return vim_strnsave(start, len);
22711 }
22712
22713 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22714 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022716 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022718
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022719 /* Note that TFN_ flags use the same values as GLV_ flags. */
22720 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022721 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022722 if (end == start)
22723 {
22724 if (!skip)
22725 EMSG(_("E129: Function name required"));
22726 goto theend;
22727 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022728 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022729 {
22730 /*
22731 * Report an invalid expression in braces, unless the expression
22732 * evaluation has been cancelled due to an aborting error, an
22733 * interrupt, or an exception.
22734 */
22735 if (!aborting())
22736 {
22737 if (end != NULL)
22738 EMSG2(_(e_invarg2), start);
22739 }
22740 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022741 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022742 goto theend;
22743 }
22744
22745 if (lv.ll_tv != NULL)
22746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022747 if (fdp != NULL)
22748 {
22749 fdp->fd_dict = lv.ll_dict;
22750 fdp->fd_newkey = lv.ll_newkey;
22751 lv.ll_newkey = NULL;
22752 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022753 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022754 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22755 {
22756 name = vim_strsave(lv.ll_tv->vval.v_string);
22757 *pp = end;
22758 }
22759 else
22760 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022761 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22762 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022763 EMSG(_(e_funcref));
22764 else
22765 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022766 name = NULL;
22767 }
22768 goto theend;
22769 }
22770
22771 if (lv.ll_name == NULL)
22772 {
22773 /* Error found, but continue after the function name. */
22774 *pp = end;
22775 goto theend;
22776 }
22777
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022778 /* Check if the name is a Funcref. If so, use the value. */
22779 if (lv.ll_exp_name != NULL)
22780 {
22781 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022782 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022783 if (name == lv.ll_exp_name)
22784 name = NULL;
22785 }
22786 else
22787 {
22788 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022789 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022790 if (name == *pp)
22791 name = NULL;
22792 }
22793 if (name != NULL)
22794 {
22795 name = vim_strsave(name);
22796 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022797 if (STRNCMP(name, "<SNR>", 5) == 0)
22798 {
22799 /* Change "<SNR>" to the byte sequence. */
22800 name[0] = K_SPECIAL;
22801 name[1] = KS_EXTRA;
22802 name[2] = (int)KE_SNR;
22803 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22804 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022805 goto theend;
22806 }
22807
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022808 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022809 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022810 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022811 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22812 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22813 {
22814 /* When there was "s:" already or the name expanded to get a
22815 * leading "s:" then remove it. */
22816 lv.ll_name += 2;
22817 len -= 2;
22818 lead = 2;
22819 }
22820 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022821 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022822 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022823 /* skip over "s:" and "g:" */
22824 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022825 lv.ll_name += 2;
22826 len = (int)(end - lv.ll_name);
22827 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022828
22829 /*
22830 * Copy the function name to allocated memory.
22831 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22832 * Accept <SNR>123_name() outside a script.
22833 */
22834 if (skip)
22835 lead = 0; /* do nothing */
22836 else if (lead > 0)
22837 {
22838 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022839 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22840 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022841 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022842 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022843 if (current_SID <= 0)
22844 {
22845 EMSG(_(e_usingsid));
22846 goto theend;
22847 }
22848 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22849 lead += (int)STRLEN(sid_buf);
22850 }
22851 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022852 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022853 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022854 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022855 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022856 goto theend;
22857 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022858 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022859 {
22860 char_u *cp = vim_strchr(lv.ll_name, ':');
22861
22862 if (cp != NULL && cp < end)
22863 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022864 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022865 goto theend;
22866 }
22867 }
22868
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022869 name = alloc((unsigned)(len + lead + 1));
22870 if (name != NULL)
22871 {
22872 if (lead > 0)
22873 {
22874 name[0] = K_SPECIAL;
22875 name[1] = KS_EXTRA;
22876 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022877 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022878 STRCPY(name + 3, sid_buf);
22879 }
22880 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022881 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022882 }
22883 *pp = end;
22884
22885theend:
22886 clear_lval(&lv);
22887 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888}
22889
22890/*
22891 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22892 * Return 2 if "p" starts with "s:".
22893 * Return 0 otherwise.
22894 */
22895 static int
22896eval_fname_script(p)
22897 char_u *p;
22898{
22899 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22900 || STRNICMP(p + 1, "SNR>", 4) == 0))
22901 return 5;
22902 if (p[0] == 's' && p[1] == ':')
22903 return 2;
22904 return 0;
22905}
22906
22907/*
22908 * Return TRUE if "p" starts with "<SID>" or "s:".
22909 * Only works if eval_fname_script() returned non-zero for "p"!
22910 */
22911 static int
22912eval_fname_sid(p)
22913 char_u *p;
22914{
22915 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22916}
22917
22918/*
22919 * List the head of the function: "name(arg1, arg2)".
22920 */
22921 static void
22922list_func_head(fp, indent)
22923 ufunc_T *fp;
22924 int indent;
22925{
22926 int j;
22927
22928 msg_start();
22929 if (indent)
22930 MSG_PUTS(" ");
22931 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022932 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 {
22934 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022935 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 }
22937 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022938 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022940 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 {
22942 if (j)
22943 MSG_PUTS(", ");
22944 msg_puts(FUNCARG(fp, j));
22945 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022946 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 {
22948 if (j)
22949 MSG_PUTS(", ");
22950 MSG_PUTS("...");
22951 }
22952 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022953 if (fp->uf_flags & FC_ABORT)
22954 MSG_PUTS(" abort");
22955 if (fp->uf_flags & FC_RANGE)
22956 MSG_PUTS(" range");
22957 if (fp->uf_flags & FC_DICT)
22958 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022959 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022960 if (p_verbose > 0)
22961 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962}
22963
22964/*
22965 * Find a function by name, return pointer to it in ufuncs.
22966 * Return NULL for unknown function.
22967 */
22968 static ufunc_T *
22969find_func(name)
22970 char_u *name;
22971{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022972 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022974 hi = hash_find(&func_hashtab, name);
22975 if (!HASHITEM_EMPTY(hi))
22976 return HI2UF(hi);
22977 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978}
22979
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022980#if defined(EXITFREE) || defined(PROTO)
22981 void
22982free_all_functions()
22983{
22984 hashitem_T *hi;
22985
22986 /* Need to start all over every time, because func_free() may change the
22987 * hash table. */
22988 while (func_hashtab.ht_used > 0)
22989 for (hi = func_hashtab.ht_array; ; ++hi)
22990 if (!HASHITEM_EMPTY(hi))
22991 {
22992 func_free(HI2UF(hi));
22993 break;
22994 }
22995}
22996#endif
22997
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022998 int
22999translated_function_exists(name)
23000 char_u *name;
23001{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023002 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023003 return find_internal_func(name) >= 0;
23004 return find_func(name) != NULL;
23005}
23006
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023007/*
23008 * Return TRUE if a function "name" exists.
23009 */
23010 static int
23011function_exists(name)
23012 char_u *name;
23013{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023014 char_u *nm = name;
23015 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023016 int n = FALSE;
23017
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023018 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23019 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023020 nm = skipwhite(nm);
23021
23022 /* Only accept "funcname", "funcname ", "funcname (..." and
23023 * "funcname(...", not "funcname!...". */
23024 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023025 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023026 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023027 return n;
23028}
23029
Bram Moolenaara1544c02013-05-30 12:35:52 +020023030 char_u *
23031get_expanded_name(name, check)
23032 char_u *name;
23033 int check;
23034{
23035 char_u *nm = name;
23036 char_u *p;
23037
23038 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23039
23040 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023041 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023042 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023043
Bram Moolenaara1544c02013-05-30 12:35:52 +020023044 vim_free(p);
23045 return NULL;
23046}
23047
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023048/*
23049 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023050 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23051 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023052 */
23053 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023054builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023055 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023056 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023057{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023058 char_u *p;
23059
23060 if (!ASCII_ISLOWER(name[0]))
23061 return FALSE;
23062 p = vim_strchr(name, AUTOLOAD_CHAR);
23063 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023064}
23065
Bram Moolenaar05159a02005-02-26 23:04:13 +000023066#if defined(FEAT_PROFILE) || defined(PROTO)
23067/*
23068 * Start profiling function "fp".
23069 */
23070 static void
23071func_do_profile(fp)
23072 ufunc_T *fp;
23073{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023074 int len = fp->uf_lines.ga_len;
23075
23076 if (len == 0)
23077 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023078 fp->uf_tm_count = 0;
23079 profile_zero(&fp->uf_tm_self);
23080 profile_zero(&fp->uf_tm_total);
23081 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023082 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023083 if (fp->uf_tml_total == NULL)
23084 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023085 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023086 if (fp->uf_tml_self == NULL)
23087 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023088 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023089 fp->uf_tml_idx = -1;
23090 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23091 || fp->uf_tml_self == NULL)
23092 return; /* out of memory */
23093
23094 fp->uf_profiling = TRUE;
23095}
23096
23097/*
23098 * Dump the profiling results for all functions in file "fd".
23099 */
23100 void
23101func_dump_profile(fd)
23102 FILE *fd;
23103{
23104 hashitem_T *hi;
23105 int todo;
23106 ufunc_T *fp;
23107 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023108 ufunc_T **sorttab;
23109 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023111 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023112 if (todo == 0)
23113 return; /* nothing to dump */
23114
Bram Moolenaar73830342005-02-28 22:48:19 +000023115 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
23116
Bram Moolenaar05159a02005-02-26 23:04:13 +000023117 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23118 {
23119 if (!HASHITEM_EMPTY(hi))
23120 {
23121 --todo;
23122 fp = HI2UF(hi);
23123 if (fp->uf_profiling)
23124 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023125 if (sorttab != NULL)
23126 sorttab[st_len++] = fp;
23127
Bram Moolenaar05159a02005-02-26 23:04:13 +000023128 if (fp->uf_name[0] == K_SPECIAL)
23129 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23130 else
23131 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23132 if (fp->uf_tm_count == 1)
23133 fprintf(fd, "Called 1 time\n");
23134 else
23135 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23136 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23137 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23138 fprintf(fd, "\n");
23139 fprintf(fd, "count total (s) self (s)\n");
23140
23141 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23142 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023143 if (FUNCLINE(fp, i) == NULL)
23144 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023145 prof_func_line(fd, fp->uf_tml_count[i],
23146 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023147 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23148 }
23149 fprintf(fd, "\n");
23150 }
23151 }
23152 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023153
23154 if (sorttab != NULL && st_len > 0)
23155 {
23156 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23157 prof_total_cmp);
23158 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23159 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23160 prof_self_cmp);
23161 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23162 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023163
23164 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023165}
Bram Moolenaar73830342005-02-28 22:48:19 +000023166
23167 static void
23168prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23169 FILE *fd;
23170 ufunc_T **sorttab;
23171 int st_len;
23172 char *title;
23173 int prefer_self; /* when equal print only self time */
23174{
23175 int i;
23176 ufunc_T *fp;
23177
23178 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23179 fprintf(fd, "count total (s) self (s) function\n");
23180 for (i = 0; i < 20 && i < st_len; ++i)
23181 {
23182 fp = sorttab[i];
23183 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23184 prefer_self);
23185 if (fp->uf_name[0] == K_SPECIAL)
23186 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23187 else
23188 fprintf(fd, " %s()\n", fp->uf_name);
23189 }
23190 fprintf(fd, "\n");
23191}
23192
23193/*
23194 * Print the count and times for one function or function line.
23195 */
23196 static void
23197prof_func_line(fd, count, total, self, prefer_self)
23198 FILE *fd;
23199 int count;
23200 proftime_T *total;
23201 proftime_T *self;
23202 int prefer_self; /* when equal print only self time */
23203{
23204 if (count > 0)
23205 {
23206 fprintf(fd, "%5d ", count);
23207 if (prefer_self && profile_equal(total, self))
23208 fprintf(fd, " ");
23209 else
23210 fprintf(fd, "%s ", profile_msg(total));
23211 if (!prefer_self && profile_equal(total, self))
23212 fprintf(fd, " ");
23213 else
23214 fprintf(fd, "%s ", profile_msg(self));
23215 }
23216 else
23217 fprintf(fd, " ");
23218}
23219
23220/*
23221 * Compare function for total time sorting.
23222 */
23223 static int
23224#ifdef __BORLANDC__
23225_RTLENTRYF
23226#endif
23227prof_total_cmp(s1, s2)
23228 const void *s1;
23229 const void *s2;
23230{
23231 ufunc_T *p1, *p2;
23232
23233 p1 = *(ufunc_T **)s1;
23234 p2 = *(ufunc_T **)s2;
23235 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23236}
23237
23238/*
23239 * Compare function for self time sorting.
23240 */
23241 static int
23242#ifdef __BORLANDC__
23243_RTLENTRYF
23244#endif
23245prof_self_cmp(s1, s2)
23246 const void *s1;
23247 const void *s2;
23248{
23249 ufunc_T *p1, *p2;
23250
23251 p1 = *(ufunc_T **)s1;
23252 p2 = *(ufunc_T **)s2;
23253 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23254}
23255
Bram Moolenaar05159a02005-02-26 23:04:13 +000023256#endif
23257
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023258/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023259 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023260 * Return TRUE if a package was loaded.
23261 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023262 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023263script_autoload(name, reload)
23264 char_u *name;
23265 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023266{
23267 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023268 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023269 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023270 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023271
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023272 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023273 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023274 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023275 return FALSE;
23276
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023277 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023278
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023279 /* Find the name in the list of previously loaded package names. Skip
23280 * "autoload/", it's always the same. */
23281 for (i = 0; i < ga_loaded.ga_len; ++i)
23282 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23283 break;
23284 if (!reload && i < ga_loaded.ga_len)
23285 ret = FALSE; /* was loaded already */
23286 else
23287 {
23288 /* Remember the name if it wasn't loaded already. */
23289 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23290 {
23291 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23292 tofree = NULL;
23293 }
23294
23295 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023296 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023297 ret = TRUE;
23298 }
23299
23300 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023301 return ret;
23302}
23303
23304/*
23305 * Return the autoload script name for a function or variable name.
23306 * Returns NULL when out of memory.
23307 */
23308 static char_u *
23309autoload_name(name)
23310 char_u *name;
23311{
23312 char_u *p;
23313 char_u *scriptname;
23314
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023315 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023316 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23317 if (scriptname == NULL)
23318 return FALSE;
23319 STRCPY(scriptname, "autoload/");
23320 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023321 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023322 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023323 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023324 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023325 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023326}
23327
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23329
23330/*
23331 * Function given to ExpandGeneric() to obtain the list of user defined
23332 * function names.
23333 */
23334 char_u *
23335get_user_func_name(xp, idx)
23336 expand_T *xp;
23337 int idx;
23338{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023339 static long_u done;
23340 static hashitem_T *hi;
23341 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342
23343 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023345 done = 0;
23346 hi = func_hashtab.ht_array;
23347 }
23348 if (done < func_hashtab.ht_used)
23349 {
23350 if (done++ > 0)
23351 ++hi;
23352 while (HASHITEM_EMPTY(hi))
23353 ++hi;
23354 fp = HI2UF(hi);
23355
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023356 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023357 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023358
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023359 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23360 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361
23362 cat_func_name(IObuff, fp);
23363 if (xp->xp_context != EXPAND_USER_FUNC)
23364 {
23365 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023366 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 STRCAT(IObuff, ")");
23368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 return IObuff;
23370 }
23371 return NULL;
23372}
23373
23374#endif /* FEAT_CMDL_COMPL */
23375
23376/*
23377 * Copy the function name of "fp" to buffer "buf".
23378 * "buf" must be able to hold the function name plus three bytes.
23379 * Takes care of script-local function names.
23380 */
23381 static void
23382cat_func_name(buf, fp)
23383 char_u *buf;
23384 ufunc_T *fp;
23385{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023386 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 {
23388 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023389 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 }
23391 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023392 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393}
23394
23395/*
23396 * ":delfunction {name}"
23397 */
23398 void
23399ex_delfunction(eap)
23400 exarg_T *eap;
23401{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023402 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403 char_u *p;
23404 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023405 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406
23407 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023408 name = trans_function_name(&p, eap->skip, 0, &fudi);
23409 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023411 {
23412 if (fudi.fd_dict != NULL && !eap->skip)
23413 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416 if (!ends_excmd(*skipwhite(p)))
23417 {
23418 vim_free(name);
23419 EMSG(_(e_trailing));
23420 return;
23421 }
23422 eap->nextcmd = check_nextcmd(p);
23423 if (eap->nextcmd != NULL)
23424 *p = NUL;
23425
23426 if (!eap->skip)
23427 fp = find_func(name);
23428 vim_free(name);
23429
23430 if (!eap->skip)
23431 {
23432 if (fp == NULL)
23433 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023434 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 return;
23436 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023437 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 {
23439 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23440 return;
23441 }
23442
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023443 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023445 /* Delete the dict item that refers to the function, it will
23446 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023447 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023449 else
23450 func_free(fp);
23451 }
23452}
23453
23454/*
23455 * Free a function and remove it from the list of functions.
23456 */
23457 static void
23458func_free(fp)
23459 ufunc_T *fp;
23460{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023461 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023462
23463 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023464 ga_clear_strings(&(fp->uf_args));
23465 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023466#ifdef FEAT_PROFILE
23467 vim_free(fp->uf_tml_count);
23468 vim_free(fp->uf_tml_total);
23469 vim_free(fp->uf_tml_self);
23470#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023472 /* remove the function from the function hashtable */
23473 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23474 if (HASHITEM_EMPTY(hi))
23475 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023476 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023477 hash_remove(&func_hashtab, hi);
23478
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 vim_free(fp);
23480}
23481
23482/*
23483 * Unreference a Function: decrement the reference count and free it when it
23484 * becomes zero. Only for numbered functions.
23485 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023486 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023487func_unref(name)
23488 char_u *name;
23489{
23490 ufunc_T *fp;
23491
23492 if (name != NULL && isdigit(*name))
23493 {
23494 fp = find_func(name);
23495 if (fp == NULL)
23496 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023497 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023498 {
23499 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023500 * when "uf_calls" becomes zero. */
23501 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023502 func_free(fp);
23503 }
23504 }
23505}
23506
23507/*
23508 * Count a reference to a Function.
23509 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023510 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023511func_ref(name)
23512 char_u *name;
23513{
23514 ufunc_T *fp;
23515
23516 if (name != NULL && isdigit(*name))
23517 {
23518 fp = find_func(name);
23519 if (fp == NULL)
23520 EMSG2(_(e_intern2), "func_ref()");
23521 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023522 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 }
23524}
23525
23526/*
23527 * Call a user function.
23528 */
23529 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023530call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531 ufunc_T *fp; /* pointer to function */
23532 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023533 typval_T *argvars; /* arguments */
23534 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 linenr_T firstline; /* first line of range */
23536 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023537 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538{
Bram Moolenaar33570922005-01-25 22:26:29 +000023539 char_u *save_sourcing_name;
23540 linenr_T save_sourcing_lnum;
23541 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023542 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023543 int save_did_emsg;
23544 static int depth = 0;
23545 dictitem_T *v;
23546 int fixvar_idx = 0; /* index in fixvar[] */
23547 int i;
23548 int ai;
23549 char_u numbuf[NUMBUFLEN];
23550 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023551#ifdef FEAT_PROFILE
23552 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023553 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555
23556 /* If depth of calling is getting too high, don't execute the function */
23557 if (depth >= p_mfd)
23558 {
23559 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023560 rettv->v_type = VAR_NUMBER;
23561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 return;
23563 }
23564 ++depth;
23565
23566 line_breakcheck(); /* check for CTRL-C hit */
23567
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023568 fc = (funccall_T *)alloc(sizeof(funccall_T));
23569 fc->caller = current_funccal;
23570 current_funccal = fc;
23571 fc->func = fp;
23572 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023573 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023574 fc->linenr = 0;
23575 fc->returned = FALSE;
23576 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023578 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23579 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580
Bram Moolenaar33570922005-01-25 22:26:29 +000023581 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023582 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023583 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23584 * each argument variable and saves a lot of time.
23585 */
23586 /*
23587 * Init l: variables.
23588 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023589 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023590 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023591 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023592 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23593 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023594 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023595 name = v->di_key;
23596 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023597 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023598 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023599 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023600 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023601 v->di_tv.vval.v_dict = selfdict;
23602 ++selfdict->dv_refcount;
23603 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023604
Bram Moolenaar33570922005-01-25 22:26:29 +000023605 /*
23606 * Init a: variables.
23607 * Set a:0 to "argcount".
23608 * Set a:000 to a list with room for the "..." arguments.
23609 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023610 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023611 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023612 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023613 /* Use "name" to avoid a warning from some compiler that checks the
23614 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023615 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023616 name = v->di_key;
23617 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023618 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023619 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023620 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023621 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023622 v->di_tv.vval.v_list = &fc->l_varlist;
23623 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23624 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23625 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023626
23627 /*
23628 * Set a:firstline to "firstline" and a:lastline to "lastline".
23629 * Set a:name to named arguments.
23630 * Set a:N to the "..." arguments.
23631 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023632 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023633 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023634 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023635 (varnumber_T)lastline);
23636 for (i = 0; i < argcount; ++i)
23637 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023638 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023639 if (ai < 0)
23640 /* named argument a:name */
23641 name = FUNCARG(fp, i);
23642 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023643 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023644 /* "..." argument a:1, a:2, etc. */
23645 sprintf((char *)numbuf, "%d", ai + 1);
23646 name = numbuf;
23647 }
23648 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23649 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023650 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023651 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23652 }
23653 else
23654 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023655 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23656 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023657 if (v == NULL)
23658 break;
23659 v->di_flags = DI_FLAGS_RO;
23660 }
23661 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023662 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023663
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023664 /* Note: the values are copied directly to avoid alloc/free.
23665 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023666 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023667 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023668
23669 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23670 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023671 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23672 fc->l_listitems[ai].li_tv = argvars[i];
23673 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023674 }
23675 }
23676
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 /* Don't redraw while executing the function. */
23678 ++RedrawingDisabled;
23679 save_sourcing_name = sourcing_name;
23680 save_sourcing_lnum = sourcing_lnum;
23681 sourcing_lnum = 1;
23682 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023683 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 if (sourcing_name != NULL)
23685 {
23686 if (save_sourcing_name != NULL
23687 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23688 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23689 else
23690 STRCPY(sourcing_name, "function ");
23691 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23692
23693 if (p_verbose >= 12)
23694 {
23695 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023696 verbose_enter_scroll();
23697
Bram Moolenaar555b2802005-05-19 21:08:39 +000023698 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 if (p_verbose >= 14)
23700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023702 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023703 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023704 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705
23706 msg_puts((char_u *)"(");
23707 for (i = 0; i < argcount; ++i)
23708 {
23709 if (i > 0)
23710 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023711 if (argvars[i].v_type == VAR_NUMBER)
23712 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713 else
23714 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023715 /* Do not want errors such as E724 here. */
23716 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023717 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023718 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023719 if (s != NULL)
23720 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023721 if (vim_strsize(s) > MSG_BUF_CLEN)
23722 {
23723 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23724 s = buf;
23725 }
23726 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023727 vim_free(tofree);
23728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 }
23730 }
23731 msg_puts((char_u *)")");
23732 }
23733 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023734
23735 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 --no_wait_return;
23737 }
23738 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023739#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023740 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023741 {
23742 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23743 func_do_profile(fp);
23744 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023745 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023746 {
23747 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023748 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023749 profile_zero(&fp->uf_tm_children);
23750 }
23751 script_prof_save(&wait_start);
23752 }
23753#endif
23754
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023756 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 save_did_emsg = did_emsg;
23758 did_emsg = FALSE;
23759
23760 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023761 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23763
23764 --RedrawingDisabled;
23765
23766 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023767 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023769 clear_tv(rettv);
23770 rettv->v_type = VAR_NUMBER;
23771 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023772 }
23773
Bram Moolenaar05159a02005-02-26 23:04:13 +000023774#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023775 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023776 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023777 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023778 profile_end(&call_start);
23779 profile_sub_wait(&wait_start, &call_start);
23780 profile_add(&fp->uf_tm_total, &call_start);
23781 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023782 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023783 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023784 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23785 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023786 }
23787 }
23788#endif
23789
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790 /* when being verbose, mention the return value */
23791 if (p_verbose >= 12)
23792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023794 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023797 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023798 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023799 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023800 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023801 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023803 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023804 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023805 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023806 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023807
Bram Moolenaar555b2802005-05-19 21:08:39 +000023808 /* The value may be very long. Skip the middle part, so that we
23809 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023810 * truncate it at the end. Don't want errors such as E724 here. */
23811 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023812 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023813 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023814 if (s != NULL)
23815 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023816 if (vim_strsize(s) > MSG_BUF_CLEN)
23817 {
23818 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23819 s = buf;
23820 }
23821 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023822 vim_free(tofree);
23823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 }
23825 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023826
23827 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 --no_wait_return;
23829 }
23830
23831 vim_free(sourcing_name);
23832 sourcing_name = save_sourcing_name;
23833 sourcing_lnum = save_sourcing_lnum;
23834 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023835#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023836 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023837 script_prof_restore(&wait_start);
23838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839
23840 if (p_verbose >= 12 && sourcing_name != NULL)
23841 {
23842 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023843 verbose_enter_scroll();
23844
Bram Moolenaar555b2802005-05-19 21:08:39 +000023845 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023847
23848 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 --no_wait_return;
23850 }
23851
23852 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023853 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023855
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023856 /* If the a:000 list and the l: and a: dicts are not referenced we can
23857 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023858 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23859 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23860 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23861 {
23862 free_funccal(fc, FALSE);
23863 }
23864 else
23865 {
23866 hashitem_T *hi;
23867 listitem_T *li;
23868 int todo;
23869
23870 /* "fc" is still in use. This can happen when returning "a:000" or
23871 * assigning "l:" to a global variable.
23872 * Link "fc" in the list for garbage collection later. */
23873 fc->caller = previous_funccal;
23874 previous_funccal = fc;
23875
23876 /* Make a copy of the a: variables, since we didn't do that above. */
23877 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23878 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23879 {
23880 if (!HASHITEM_EMPTY(hi))
23881 {
23882 --todo;
23883 v = HI2DI(hi);
23884 copy_tv(&v->di_tv, &v->di_tv);
23885 }
23886 }
23887
23888 /* Make a copy of the a:000 items, since we didn't do that above. */
23889 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23890 copy_tv(&li->li_tv, &li->li_tv);
23891 }
23892}
23893
23894/*
23895 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023896 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023897 */
23898 static int
23899can_free_funccal(fc, copyID)
23900 funccall_T *fc;
23901 int copyID;
23902{
23903 return (fc->l_varlist.lv_copyID != copyID
23904 && fc->l_vars.dv_copyID != copyID
23905 && fc->l_avars.dv_copyID != copyID);
23906}
23907
23908/*
23909 * Free "fc" and what it contains.
23910 */
23911 static void
23912free_funccal(fc, free_val)
23913 funccall_T *fc;
23914 int free_val; /* a: vars were allocated */
23915{
23916 listitem_T *li;
23917
23918 /* The a: variables typevals may not have been allocated, only free the
23919 * allocated variables. */
23920 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23921
23922 /* free all l: variables */
23923 vars_clear(&fc->l_vars.dv_hashtab);
23924
23925 /* Free the a:000 variables if they were allocated. */
23926 if (free_val)
23927 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23928 clear_tv(&li->li_tv);
23929
23930 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931}
23932
23933/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023934 * Add a number variable "name" to dict "dp" with value "nr".
23935 */
23936 static void
23937add_nr_var(dp, v, name, nr)
23938 dict_T *dp;
23939 dictitem_T *v;
23940 char *name;
23941 varnumber_T nr;
23942{
23943 STRCPY(v->di_key, name);
23944 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23945 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23946 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023947 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023948 v->di_tv.vval.v_number = nr;
23949}
23950
23951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 * ":return [expr]"
23953 */
23954 void
23955ex_return(eap)
23956 exarg_T *eap;
23957{
23958 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023959 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 int returning = FALSE;
23961
23962 if (current_funccal == NULL)
23963 {
23964 EMSG(_("E133: :return not inside a function"));
23965 return;
23966 }
23967
23968 if (eap->skip)
23969 ++emsg_skip;
23970
23971 eap->nextcmd = NULL;
23972 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023973 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 {
23975 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023976 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023978 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 }
23980 /* It's safer to return also on error. */
23981 else if (!eap->skip)
23982 {
23983 /*
23984 * Return unless the expression evaluation has been cancelled due to an
23985 * aborting error, an interrupt, or an exception.
23986 */
23987 if (!aborting())
23988 returning = do_return(eap, FALSE, TRUE, NULL);
23989 }
23990
23991 /* When skipping or the return gets pending, advance to the next command
23992 * in this line (!returning). Otherwise, ignore the rest of the line.
23993 * Following lines will be ignored by get_func_line(). */
23994 if (returning)
23995 eap->nextcmd = NULL;
23996 else if (eap->nextcmd == NULL) /* no argument */
23997 eap->nextcmd = check_nextcmd(arg);
23998
23999 if (eap->skip)
24000 --emsg_skip;
24001}
24002
24003/*
24004 * Return from a function. Possibly makes the return pending. Also called
24005 * for a pending return at the ":endtry" or after returning from an extra
24006 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024007 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024008 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 * FALSE when the return gets pending.
24010 */
24011 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024012do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 exarg_T *eap;
24014 int reanimate;
24015 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024016 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017{
24018 int idx;
24019 struct condstack *cstack = eap->cstack;
24020
24021 if (reanimate)
24022 /* Undo the return. */
24023 current_funccal->returned = FALSE;
24024
24025 /*
24026 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24027 * not in its finally clause (which then is to be executed next) is found.
24028 * In this case, make the ":return" pending for execution at the ":endtry".
24029 * Otherwise, return normally.
24030 */
24031 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24032 if (idx >= 0)
24033 {
24034 cstack->cs_pending[idx] = CSTP_RETURN;
24035
24036 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024037 /* A pending return again gets pending. "rettv" points to an
24038 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024040 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 else
24042 {
24043 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024044 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024046 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024048 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 {
24050 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024051 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024052 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 else
24054 EMSG(_(e_outofmem));
24055 }
24056 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024057 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058
24059 if (reanimate)
24060 {
24061 /* The pending return value could be overwritten by a ":return"
24062 * without argument in a finally clause; reset the default
24063 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024064 current_funccal->rettv->v_type = VAR_NUMBER;
24065 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024066 }
24067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024068 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 }
24070 else
24071 {
24072 current_funccal->returned = TRUE;
24073
24074 /* If the return is carried out now, store the return value. For
24075 * a return immediately after reanimation, the value is already
24076 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024077 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024079 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024080 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024082 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083 }
24084 }
24085
24086 return idx < 0;
24087}
24088
24089/*
24090 * Free the variable with a pending return value.
24091 */
24092 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024093discard_pending_return(rettv)
24094 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095{
Bram Moolenaar33570922005-01-25 22:26:29 +000024096 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097}
24098
24099/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024100 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 * is an allocated string. Used by report_pending() for verbose messages.
24102 */
24103 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024104get_return_cmd(rettv)
24105 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024107 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024108 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024109 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024111 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024112 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024113 if (s == NULL)
24114 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024115
24116 STRCPY(IObuff, ":return ");
24117 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24118 if (STRLEN(s) + 8 >= IOSIZE)
24119 STRCPY(IObuff + IOSIZE - 4, "...");
24120 vim_free(tofree);
24121 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122}
24123
24124/*
24125 * Get next function line.
24126 * Called by do_cmdline() to get the next line.
24127 * Returns allocated string, or NULL for end of function.
24128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129 char_u *
24130get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024131 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024133 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134{
Bram Moolenaar33570922005-01-25 22:26:29 +000024135 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024136 ufunc_T *fp = fcp->func;
24137 char_u *retval;
24138 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139
24140 /* If breakpoints have been added/deleted need to check for it. */
24141 if (fcp->dbg_tick != debug_tick)
24142 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024143 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 sourcing_lnum);
24145 fcp->dbg_tick = debug_tick;
24146 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024147#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024148 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024149 func_line_end(cookie);
24150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151
Bram Moolenaar05159a02005-02-26 23:04:13 +000024152 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024153 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24154 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155 retval = NULL;
24156 else
24157 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024158 /* Skip NULL lines (continuation lines). */
24159 while (fcp->linenr < gap->ga_len
24160 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24161 ++fcp->linenr;
24162 if (fcp->linenr >= gap->ga_len)
24163 retval = NULL;
24164 else
24165 {
24166 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24167 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024168#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024169 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024170 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024171#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 }
24174
24175 /* Did we encounter a breakpoint? */
24176 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24177 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024178 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024180 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 sourcing_lnum);
24182 fcp->dbg_tick = debug_tick;
24183 }
24184
24185 return retval;
24186}
24187
Bram Moolenaar05159a02005-02-26 23:04:13 +000024188#if defined(FEAT_PROFILE) || defined(PROTO)
24189/*
24190 * Called when starting to read a function line.
24191 * "sourcing_lnum" must be correct!
24192 * When skipping lines it may not actually be executed, but we won't find out
24193 * until later and we need to store the time now.
24194 */
24195 void
24196func_line_start(cookie)
24197 void *cookie;
24198{
24199 funccall_T *fcp = (funccall_T *)cookie;
24200 ufunc_T *fp = fcp->func;
24201
24202 if (fp->uf_profiling && sourcing_lnum >= 1
24203 && sourcing_lnum <= fp->uf_lines.ga_len)
24204 {
24205 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024206 /* Skip continuation lines. */
24207 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24208 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024209 fp->uf_tml_execed = FALSE;
24210 profile_start(&fp->uf_tml_start);
24211 profile_zero(&fp->uf_tml_children);
24212 profile_get_wait(&fp->uf_tml_wait);
24213 }
24214}
24215
24216/*
24217 * Called when actually executing a function line.
24218 */
24219 void
24220func_line_exec(cookie)
24221 void *cookie;
24222{
24223 funccall_T *fcp = (funccall_T *)cookie;
24224 ufunc_T *fp = fcp->func;
24225
24226 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24227 fp->uf_tml_execed = TRUE;
24228}
24229
24230/*
24231 * Called when done with a function line.
24232 */
24233 void
24234func_line_end(cookie)
24235 void *cookie;
24236{
24237 funccall_T *fcp = (funccall_T *)cookie;
24238 ufunc_T *fp = fcp->func;
24239
24240 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24241 {
24242 if (fp->uf_tml_execed)
24243 {
24244 ++fp->uf_tml_count[fp->uf_tml_idx];
24245 profile_end(&fp->uf_tml_start);
24246 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024247 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024248 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24249 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024250 }
24251 fp->uf_tml_idx = -1;
24252 }
24253}
24254#endif
24255
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256/*
24257 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024258 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 */
24260 int
24261func_has_ended(cookie)
24262 void *cookie;
24263{
Bram Moolenaar33570922005-01-25 22:26:29 +000024264 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265
24266 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24267 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024268 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 || fcp->returned);
24270}
24271
24272/*
24273 * return TRUE if cookie indicates a function which "abort"s on errors.
24274 */
24275 int
24276func_has_abort(cookie)
24277 void *cookie;
24278{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024279 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280}
24281
24282#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24283typedef enum
24284{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024285 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24286 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24287 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288} var_flavour_T;
24289
24290static var_flavour_T var_flavour __ARGS((char_u *varname));
24291
24292 static var_flavour_T
24293var_flavour(varname)
24294 char_u *varname;
24295{
24296 char_u *p = varname;
24297
24298 if (ASCII_ISUPPER(*p))
24299 {
24300 while (*(++p))
24301 if (ASCII_ISLOWER(*p))
24302 return VAR_FLAVOUR_SESSION;
24303 return VAR_FLAVOUR_VIMINFO;
24304 }
24305 else
24306 return VAR_FLAVOUR_DEFAULT;
24307}
24308#endif
24309
24310#if defined(FEAT_VIMINFO) || defined(PROTO)
24311/*
24312 * Restore global vars that start with a capital from the viminfo file
24313 */
24314 int
24315read_viminfo_varlist(virp, writing)
24316 vir_T *virp;
24317 int writing;
24318{
24319 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024320 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024321 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322
24323 if (!writing && (find_viminfo_parameter('!') != NULL))
24324 {
24325 tab = vim_strchr(virp->vir_line + 1, '\t');
24326 if (tab != NULL)
24327 {
24328 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024329 switch (*tab)
24330 {
24331 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024332#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024333 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024334#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024335 case 'D': type = VAR_DICT; break;
24336 case 'L': type = VAR_LIST; break;
24337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338
24339 tab = vim_strchr(tab, '\t');
24340 if (tab != NULL)
24341 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024342 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024343 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024344 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024346#ifdef FEAT_FLOAT
24347 else if (type == VAR_FLOAT)
24348 (void)string2float(tab + 1, &tv.vval.v_float);
24349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024351 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024352 if (type == VAR_DICT || type == VAR_LIST)
24353 {
24354 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24355
24356 if (etv == NULL)
24357 /* Failed to parse back the dict or list, use it as a
24358 * string. */
24359 tv.v_type = VAR_STRING;
24360 else
24361 {
24362 vim_free(tv.vval.v_string);
24363 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024364 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024365 }
24366 }
24367
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024368 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024369
24370 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024371 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024372 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 }
24375 }
24376 }
24377
24378 return viminfo_readline(virp);
24379}
24380
24381/*
24382 * Write global vars that start with a capital to the viminfo file
24383 */
24384 void
24385write_viminfo_varlist(fp)
24386 FILE *fp;
24387{
Bram Moolenaar33570922005-01-25 22:26:29 +000024388 hashitem_T *hi;
24389 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024390 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024391 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024392 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024393 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395
24396 if (find_viminfo_parameter('!') == NULL)
24397 return;
24398
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024399 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024400
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024401 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024402 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024404 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024405 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024406 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024407 this_var = HI2DI(hi);
24408 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024409 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024410 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024411 {
24412 case VAR_STRING: s = "STR"; break;
24413 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024414#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024415 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024416#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024417 case VAR_DICT: s = "DIC"; break;
24418 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024419 default: continue;
24420 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024421 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024422 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024423 if (p != NULL)
24424 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024425 vim_free(tofree);
24426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024427 }
24428 }
24429}
24430#endif
24431
24432#if defined(FEAT_SESSION) || defined(PROTO)
24433 int
24434store_session_globals(fd)
24435 FILE *fd;
24436{
Bram Moolenaar33570922005-01-25 22:26:29 +000024437 hashitem_T *hi;
24438 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024439 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440 char_u *p, *t;
24441
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024442 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024443 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024445 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024447 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024448 this_var = HI2DI(hi);
24449 if ((this_var->di_tv.v_type == VAR_NUMBER
24450 || this_var->di_tv.v_type == VAR_STRING)
24451 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024452 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024453 /* Escape special characters with a backslash. Turn a LF and
24454 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024455 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024456 (char_u *)"\\\"\n\r");
24457 if (p == NULL) /* out of memory */
24458 break;
24459 for (t = p; *t != NUL; ++t)
24460 if (*t == '\n')
24461 *t = 'n';
24462 else if (*t == '\r')
24463 *t = 'r';
24464 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024465 this_var->di_key,
24466 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24467 : ' ',
24468 p,
24469 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24470 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024471 || put_eol(fd) == FAIL)
24472 {
24473 vim_free(p);
24474 return FAIL;
24475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476 vim_free(p);
24477 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024478#ifdef FEAT_FLOAT
24479 else if (this_var->di_tv.v_type == VAR_FLOAT
24480 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24481 {
24482 float_T f = this_var->di_tv.vval.v_float;
24483 int sign = ' ';
24484
24485 if (f < 0)
24486 {
24487 f = -f;
24488 sign = '-';
24489 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024490 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024491 this_var->di_key, sign, f) < 0)
24492 || put_eol(fd) == FAIL)
24493 return FAIL;
24494 }
24495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496 }
24497 }
24498 return OK;
24499}
24500#endif
24501
Bram Moolenaar661b1822005-07-28 22:36:45 +000024502/*
24503 * Display script name where an item was last set.
24504 * Should only be invoked when 'verbose' is non-zero.
24505 */
24506 void
24507last_set_msg(scriptID)
24508 scid_T scriptID;
24509{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024510 char_u *p;
24511
Bram Moolenaar661b1822005-07-28 22:36:45 +000024512 if (scriptID != 0)
24513 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024514 p = home_replace_save(NULL, get_scriptname(scriptID));
24515 if (p != NULL)
24516 {
24517 verbose_enter();
24518 MSG_PUTS(_("\n\tLast set from "));
24519 MSG_PUTS(p);
24520 vim_free(p);
24521 verbose_leave();
24522 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024523 }
24524}
24525
Bram Moolenaard812df62008-11-09 12:46:09 +000024526/*
24527 * List v:oldfiles in a nice way.
24528 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024529 void
24530ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024531 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024532{
24533 list_T *l = vimvars[VV_OLDFILES].vv_list;
24534 listitem_T *li;
24535 int nr = 0;
24536
24537 if (l == NULL)
24538 msg((char_u *)_("No old files"));
24539 else
24540 {
24541 msg_start();
24542 msg_scroll = TRUE;
24543 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24544 {
24545 msg_outnum((long)++nr);
24546 MSG_PUTS(": ");
24547 msg_outtrans(get_tv_string(&li->li_tv));
24548 msg_putchar('\n');
24549 out_flush(); /* output one line at a time */
24550 ui_breakcheck();
24551 }
24552 /* Assume "got_int" was set to truncate the listing. */
24553 got_int = FALSE;
24554
24555#ifdef FEAT_BROWSE_CMD
24556 if (cmdmod.browse)
24557 {
24558 quit_more = FALSE;
24559 nr = prompt_for_number(FALSE);
24560 msg_starthere();
24561 if (nr > 0)
24562 {
24563 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24564 (long)nr);
24565
24566 if (p != NULL)
24567 {
24568 p = expand_env_save(p);
24569 eap->arg = p;
24570 eap->cmdidx = CMD_edit;
24571 cmdmod.browse = FALSE;
24572 do_exedit(eap, NULL);
24573 vim_free(p);
24574 }
24575 }
24576 }
24577#endif
24578 }
24579}
24580
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581#endif /* FEAT_EVAL */
24582
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024584#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585
24586#ifdef WIN3264
24587/*
24588 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24589 */
24590static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24591static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24592static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24593
24594/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024595 * Get the short path (8.3) for the filename in "fnamep".
24596 * Only works for a valid file name.
24597 * When the path gets longer "fnamep" is changed and the allocated buffer
24598 * is put in "bufp".
24599 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24600 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 */
24602 static int
24603get_short_pathname(fnamep, bufp, fnamelen)
24604 char_u **fnamep;
24605 char_u **bufp;
24606 int *fnamelen;
24607{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024608 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 char_u *newbuf;
24610
24611 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 l = GetShortPathName(*fnamep, *fnamep, len);
24613 if (l > len - 1)
24614 {
24615 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024616 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 newbuf = vim_strnsave(*fnamep, l);
24618 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024619 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620
24621 vim_free(*bufp);
24622 *fnamep = *bufp = newbuf;
24623
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024624 /* Really should always succeed, as the buffer is big enough. */
24625 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 }
24627
24628 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024629 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630}
24631
24632/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024633 * Get the short path (8.3) for the filename in "fname". The converted
24634 * path is returned in "bufp".
24635 *
24636 * Some of the directories specified in "fname" may not exist. This function
24637 * will shorten the existing directories at the beginning of the path and then
24638 * append the remaining non-existing path.
24639 *
24640 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024641 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024642 * bufp - Pointer to an allocated buffer for the filename.
24643 * fnamelen - Length of the filename pointed to by fname
24644 *
24645 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 */
24647 static int
24648shortpath_for_invalid_fname(fname, bufp, fnamelen)
24649 char_u **fname;
24650 char_u **bufp;
24651 int *fnamelen;
24652{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024653 char_u *short_fname, *save_fname, *pbuf_unused;
24654 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024656 int old_len, len;
24657 int new_len, sfx_len;
24658 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659
24660 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024661 old_len = *fnamelen;
24662 save_fname = vim_strnsave(*fname, old_len);
24663 pbuf_unused = NULL;
24664 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024666 endp = save_fname + old_len - 1; /* Find the end of the copy */
24667 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024669 /*
24670 * Try shortening the supplied path till it succeeds by removing one
24671 * directory at a time from the tail of the path.
24672 */
24673 len = 0;
24674 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024676 /* go back one path-separator */
24677 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24678 --endp;
24679 if (endp <= save_fname)
24680 break; /* processed the complete path */
24681
24682 /*
24683 * Replace the path separator with a NUL and try to shorten the
24684 * resulting path.
24685 */
24686 ch = *endp;
24687 *endp = 0;
24688 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024689 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024690 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24691 {
24692 retval = FAIL;
24693 goto theend;
24694 }
24695 *endp = ch; /* preserve the string */
24696
24697 if (len > 0)
24698 break; /* successfully shortened the path */
24699
24700 /* failed to shorten the path. Skip the path separator */
24701 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702 }
24703
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024704 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024706 /*
24707 * Succeeded in shortening the path. Now concatenate the shortened
24708 * path with the remaining path at the tail.
24709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024711 /* Compute the length of the new path. */
24712 sfx_len = (int)(save_endp - endp) + 1;
24713 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024715 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024717 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024719 /* There is not enough space in the currently allocated string,
24720 * copy it to a buffer big enough. */
24721 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024722 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024723 {
24724 retval = FAIL;
24725 goto theend;
24726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 }
24728 else
24729 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024730 /* Transfer short_fname to the main buffer (it's big enough),
24731 * unless get_short_pathname() did its work in-place. */
24732 *fname = *bufp = save_fname;
24733 if (short_fname != save_fname)
24734 vim_strncpy(save_fname, short_fname, len);
24735 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024737
24738 /* concat the not-shortened part of the path */
24739 vim_strncpy(*fname + len, endp, sfx_len);
24740 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024742
24743theend:
24744 vim_free(pbuf_unused);
24745 vim_free(save_fname);
24746
24747 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748}
24749
24750/*
24751 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024752 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 */
24754 static int
24755shortpath_for_partial(fnamep, bufp, fnamelen)
24756 char_u **fnamep;
24757 char_u **bufp;
24758 int *fnamelen;
24759{
24760 int sepcount, len, tflen;
24761 char_u *p;
24762 char_u *pbuf, *tfname;
24763 int hasTilde;
24764
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024765 /* Count up the path separators from the RHS.. so we know which part
24766 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024768 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 if (vim_ispathsep(*p))
24770 ++sepcount;
24771
24772 /* Need full path first (use expand_env() to remove a "~/") */
24773 hasTilde = (**fnamep == '~');
24774 if (hasTilde)
24775 pbuf = tfname = expand_env_save(*fnamep);
24776 else
24777 pbuf = tfname = FullName_save(*fnamep, FALSE);
24778
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024779 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024781 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24782 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783
24784 if (len == 0)
24785 {
24786 /* Don't have a valid filename, so shorten the rest of the
24787 * path if we can. This CAN give us invalid 8.3 filenames, but
24788 * there's not a lot of point in guessing what it might be.
24789 */
24790 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024791 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24792 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793 }
24794
24795 /* Count the paths backward to find the beginning of the desired string. */
24796 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024797 {
24798#ifdef FEAT_MBYTE
24799 if (has_mbyte)
24800 p -= mb_head_off(tfname, p);
24801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 if (vim_ispathsep(*p))
24803 {
24804 if (sepcount == 0 || (hasTilde && sepcount == 1))
24805 break;
24806 else
24807 sepcount --;
24808 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810 if (hasTilde)
24811 {
24812 --p;
24813 if (p >= tfname)
24814 *p = '~';
24815 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817 }
24818 else
24819 ++p;
24820
24821 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24822 vim_free(*bufp);
24823 *fnamelen = (int)STRLEN(p);
24824 *bufp = pbuf;
24825 *fnamep = p;
24826
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024827 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828}
24829#endif /* WIN3264 */
24830
24831/*
24832 * Adjust a filename, according to a string of modifiers.
24833 * *fnamep must be NUL terminated when called. When returning, the length is
24834 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024835 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836 * When there is an error, *fnamep is set to NULL.
24837 */
24838 int
24839modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24840 char_u *src; /* string with modifiers */
24841 int *usedlen; /* characters after src that are used */
24842 char_u **fnamep; /* file name so far */
24843 char_u **bufp; /* buffer for allocated file name or NULL */
24844 int *fnamelen; /* length of fnamep */
24845{
24846 int valid = 0;
24847 char_u *tail;
24848 char_u *s, *p, *pbuf;
24849 char_u dirname[MAXPATHL];
24850 int c;
24851 int has_fullname = 0;
24852#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024853 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 int has_shortname = 0;
24855#endif
24856
24857repeat:
24858 /* ":p" - full path/file_name */
24859 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24860 {
24861 has_fullname = 1;
24862
24863 valid |= VALID_PATH;
24864 *usedlen += 2;
24865
24866 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24867 if ((*fnamep)[0] == '~'
24868#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24869 && ((*fnamep)[1] == '/'
24870# ifdef BACKSLASH_IN_FILENAME
24871 || (*fnamep)[1] == '\\'
24872# endif
24873 || (*fnamep)[1] == NUL)
24874
24875#endif
24876 )
24877 {
24878 *fnamep = expand_env_save(*fnamep);
24879 vim_free(*bufp); /* free any allocated file name */
24880 *bufp = *fnamep;
24881 if (*fnamep == NULL)
24882 return -1;
24883 }
24884
24885 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024886 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 {
24888 if (vim_ispathsep(*p)
24889 && p[1] == '.'
24890 && (p[2] == NUL
24891 || vim_ispathsep(p[2])
24892 || (p[2] == '.'
24893 && (p[3] == NUL || vim_ispathsep(p[3])))))
24894 break;
24895 }
24896
24897 /* FullName_save() is slow, don't use it when not needed. */
24898 if (*p != NUL || !vim_isAbsName(*fnamep))
24899 {
24900 *fnamep = FullName_save(*fnamep, *p != NUL);
24901 vim_free(*bufp); /* free any allocated file name */
24902 *bufp = *fnamep;
24903 if (*fnamep == NULL)
24904 return -1;
24905 }
24906
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024907#ifdef WIN3264
24908# if _WIN32_WINNT >= 0x0500
24909 if (vim_strchr(*fnamep, '~') != NULL)
24910 {
24911 /* Expand 8.3 filename to full path. Needed to make sure the same
24912 * file does not have two different names.
24913 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24914 p = alloc(_MAX_PATH + 1);
24915 if (p != NULL)
24916 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024917 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024918 {
24919 vim_free(*bufp);
24920 *bufp = *fnamep = p;
24921 }
24922 else
24923 vim_free(p);
24924 }
24925 }
24926# endif
24927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 /* Append a path separator to a directory. */
24929 if (mch_isdir(*fnamep))
24930 {
24931 /* Make room for one or two extra characters. */
24932 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24933 vim_free(*bufp); /* free any allocated file name */
24934 *bufp = *fnamep;
24935 if (*fnamep == NULL)
24936 return -1;
24937 add_pathsep(*fnamep);
24938 }
24939 }
24940
24941 /* ":." - path relative to the current directory */
24942 /* ":~" - path relative to the home directory */
24943 /* ":8" - shortname path - postponed till after */
24944 while (src[*usedlen] == ':'
24945 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24946 {
24947 *usedlen += 2;
24948 if (c == '8')
24949 {
24950#ifdef WIN3264
24951 has_shortname = 1; /* Postpone this. */
24952#endif
24953 continue;
24954 }
24955 pbuf = NULL;
24956 /* Need full path first (use expand_env() to remove a "~/") */
24957 if (!has_fullname)
24958 {
24959 if (c == '.' && **fnamep == '~')
24960 p = pbuf = expand_env_save(*fnamep);
24961 else
24962 p = pbuf = FullName_save(*fnamep, FALSE);
24963 }
24964 else
24965 p = *fnamep;
24966
24967 has_fullname = 0;
24968
24969 if (p != NULL)
24970 {
24971 if (c == '.')
24972 {
24973 mch_dirname(dirname, MAXPATHL);
24974 s = shorten_fname(p, dirname);
24975 if (s != NULL)
24976 {
24977 *fnamep = s;
24978 if (pbuf != NULL)
24979 {
24980 vim_free(*bufp); /* free any allocated file name */
24981 *bufp = pbuf;
24982 pbuf = NULL;
24983 }
24984 }
24985 }
24986 else
24987 {
24988 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24989 /* Only replace it when it starts with '~' */
24990 if (*dirname == '~')
24991 {
24992 s = vim_strsave(dirname);
24993 if (s != NULL)
24994 {
24995 *fnamep = s;
24996 vim_free(*bufp);
24997 *bufp = s;
24998 }
24999 }
25000 }
25001 vim_free(pbuf);
25002 }
25003 }
25004
25005 tail = gettail(*fnamep);
25006 *fnamelen = (int)STRLEN(*fnamep);
25007
25008 /* ":h" - head, remove "/file_name", can be repeated */
25009 /* Don't remove the first "/" or "c:\" */
25010 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25011 {
25012 valid |= VALID_HEAD;
25013 *usedlen += 2;
25014 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025015 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025016 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 *fnamelen = (int)(tail - *fnamep);
25018#ifdef VMS
25019 if (*fnamelen > 0)
25020 *fnamelen += 1; /* the path separator is part of the path */
25021#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025022 if (*fnamelen == 0)
25023 {
25024 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25025 p = vim_strsave((char_u *)".");
25026 if (p == NULL)
25027 return -1;
25028 vim_free(*bufp);
25029 *bufp = *fnamep = tail = p;
25030 *fnamelen = 1;
25031 }
25032 else
25033 {
25034 while (tail > s && !after_pathsep(s, tail))
25035 mb_ptr_back(*fnamep, tail);
25036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037 }
25038
25039 /* ":8" - shortname */
25040 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25041 {
25042 *usedlen += 2;
25043#ifdef WIN3264
25044 has_shortname = 1;
25045#endif
25046 }
25047
25048#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025049 /*
25050 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051 */
25052 if (has_shortname)
25053 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025054 /* Copy the string if it is shortened by :h and when it wasn't copied
25055 * yet, because we are going to change it in place. Avoids changing
25056 * the buffer name for "%:8". */
25057 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 {
25059 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025060 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061 return -1;
25062 vim_free(*bufp);
25063 *bufp = *fnamep = p;
25064 }
25065
25066 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025067 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 if (!has_fullname && !vim_isAbsName(*fnamep))
25069 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025070 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 return -1;
25072 }
25073 else
25074 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025075 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076
Bram Moolenaardc935552011-08-17 15:23:23 +020025077 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025079 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080 return -1;
25081
25082 if (l == 0)
25083 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025084 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025086 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087 return -1;
25088 }
25089 *fnamelen = l;
25090 }
25091 }
25092#endif /* WIN3264 */
25093
25094 /* ":t" - tail, just the basename */
25095 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25096 {
25097 *usedlen += 2;
25098 *fnamelen -= (int)(tail - *fnamep);
25099 *fnamep = tail;
25100 }
25101
25102 /* ":e" - extension, can be repeated */
25103 /* ":r" - root, without extension, can be repeated */
25104 while (src[*usedlen] == ':'
25105 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25106 {
25107 /* find a '.' in the tail:
25108 * - for second :e: before the current fname
25109 * - otherwise: The last '.'
25110 */
25111 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25112 s = *fnamep - 2;
25113 else
25114 s = *fnamep + *fnamelen - 1;
25115 for ( ; s > tail; --s)
25116 if (s[0] == '.')
25117 break;
25118 if (src[*usedlen + 1] == 'e') /* :e */
25119 {
25120 if (s > tail)
25121 {
25122 *fnamelen += (int)(*fnamep - (s + 1));
25123 *fnamep = s + 1;
25124#ifdef VMS
25125 /* cut version from the extension */
25126 s = *fnamep + *fnamelen - 1;
25127 for ( ; s > *fnamep; --s)
25128 if (s[0] == ';')
25129 break;
25130 if (s > *fnamep)
25131 *fnamelen = s - *fnamep;
25132#endif
25133 }
25134 else if (*fnamep <= tail)
25135 *fnamelen = 0;
25136 }
25137 else /* :r */
25138 {
25139 if (s > tail) /* remove one extension */
25140 *fnamelen = (int)(s - *fnamep);
25141 }
25142 *usedlen += 2;
25143 }
25144
25145 /* ":s?pat?foo?" - substitute */
25146 /* ":gs?pat?foo?" - global substitute */
25147 if (src[*usedlen] == ':'
25148 && (src[*usedlen + 1] == 's'
25149 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25150 {
25151 char_u *str;
25152 char_u *pat;
25153 char_u *sub;
25154 int sep;
25155 char_u *flags;
25156 int didit = FALSE;
25157
25158 flags = (char_u *)"";
25159 s = src + *usedlen + 2;
25160 if (src[*usedlen + 1] == 'g')
25161 {
25162 flags = (char_u *)"g";
25163 ++s;
25164 }
25165
25166 sep = *s++;
25167 if (sep)
25168 {
25169 /* find end of pattern */
25170 p = vim_strchr(s, sep);
25171 if (p != NULL)
25172 {
25173 pat = vim_strnsave(s, (int)(p - s));
25174 if (pat != NULL)
25175 {
25176 s = p + 1;
25177 /* find end of substitution */
25178 p = vim_strchr(s, sep);
25179 if (p != NULL)
25180 {
25181 sub = vim_strnsave(s, (int)(p - s));
25182 str = vim_strnsave(*fnamep, *fnamelen);
25183 if (sub != NULL && str != NULL)
25184 {
25185 *usedlen = (int)(p + 1 - src);
25186 s = do_string_sub(str, pat, sub, flags);
25187 if (s != NULL)
25188 {
25189 *fnamep = s;
25190 *fnamelen = (int)STRLEN(s);
25191 vim_free(*bufp);
25192 *bufp = s;
25193 didit = TRUE;
25194 }
25195 }
25196 vim_free(sub);
25197 vim_free(str);
25198 }
25199 vim_free(pat);
25200 }
25201 }
25202 /* after using ":s", repeat all the modifiers */
25203 if (didit)
25204 goto repeat;
25205 }
25206 }
25207
Bram Moolenaar26df0922014-02-23 23:39:13 +010025208 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25209 {
25210 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25211 if (p == NULL)
25212 return -1;
25213 vim_free(*bufp);
25214 *bufp = *fnamep = p;
25215 *fnamelen = (int)STRLEN(p);
25216 *usedlen += 2;
25217 }
25218
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219 return valid;
25220}
25221
25222/*
25223 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25224 * "flags" can be "g" to do a global substitute.
25225 * Returns an allocated string, NULL for error.
25226 */
25227 char_u *
25228do_string_sub(str, pat, sub, flags)
25229 char_u *str;
25230 char_u *pat;
25231 char_u *sub;
25232 char_u *flags;
25233{
25234 int sublen;
25235 regmatch_T regmatch;
25236 int i;
25237 int do_all;
25238 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025239 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 garray_T ga;
25241 char_u *ret;
25242 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025243 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244
25245 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25246 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025247 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248
25249 ga_init2(&ga, 1, 200);
25250
25251 do_all = (flags[0] == 'g');
25252
25253 regmatch.rm_ic = p_ic;
25254 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25255 if (regmatch.regprog != NULL)
25256 {
25257 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025258 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25260 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025261 /* Skip empty match except for first match. */
25262 if (regmatch.startp[0] == regmatch.endp[0])
25263 {
25264 if (zero_width == regmatch.startp[0])
25265 {
25266 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025267 i = MB_PTR2LEN(tail);
25268 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25269 (size_t)i);
25270 ga.ga_len += i;
25271 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025272 continue;
25273 }
25274 zero_width = regmatch.startp[0];
25275 }
25276
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 /*
25278 * Get some space for a temporary buffer to do the substitution
25279 * into. It will contain:
25280 * - The text up to where the match is.
25281 * - The substituted text.
25282 * - The text after the match.
25283 */
25284 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025285 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25287 {
25288 ga_clear(&ga);
25289 break;
25290 }
25291
25292 /* copy the text up to where the match is */
25293 i = (int)(regmatch.startp[0] - tail);
25294 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25295 /* add the substituted text */
25296 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25297 + ga.ga_len + i, TRUE, TRUE, FALSE);
25298 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025299 tail = regmatch.endp[0];
25300 if (*tail == NUL)
25301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302 if (!do_all)
25303 break;
25304 }
25305
25306 if (ga.ga_data != NULL)
25307 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25308
Bram Moolenaar473de612013-06-08 18:19:48 +020025309 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310 }
25311
25312 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25313 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025314 if (p_cpo == empty_option)
25315 p_cpo = save_cpo;
25316 else
25317 /* Darn, evaluating {sub} expression changed the value. */
25318 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319
25320 return ret;
25321}
25322
25323#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */